rehype-minify-meta-content
Advanced tools
Comparing version 2.0.2 to 3.0.0
88
index.js
@@ -12,52 +12,54 @@ /** | ||
'use strict' | ||
/** | ||
* @typedef {import('hast').Root} Root | ||
*/ | ||
var comma = require('comma-separated-tokens') | ||
var visit = require('unist-util-visit') | ||
var is = require('hast-util-is-element') | ||
var has = require('hast-util-has-property') | ||
import {parse, stringify} from 'comma-separated-tokens' | ||
import {visit} from 'unist-util-visit' | ||
import {isElement} from 'hast-util-is-element' | ||
import {hasProperty} from 'hast-util-has-property' | ||
module.exports = content | ||
const lists = new Set([ | ||
'viewport', | ||
'keywords', | ||
'robots', | ||
'apple-itunes-app', | ||
'apple-media-service-subscription' | ||
]) | ||
var own = {}.hasOwnProperty | ||
/** | ||
* Minify `content` on `meta` elements. | ||
* | ||
* Note: `meta[name=theme-color]` and `meta[name=msapplication-TileColor]` | ||
* are handled by `rehype-minify-meta-color`. | ||
* | ||
* @type {import('unified').Plugin<[], Root>} | ||
*/ | ||
export default function rehypeMinifyMetaContent() { | ||
return (tree) => { | ||
visit(tree, 'element', (node) => { | ||
const props = node.properties || {} | ||
const name = String(props.name || '') | ||
let value = props.content | ||
var handlers = {} | ||
if ( | ||
isElement(node, 'meta') && | ||
hasProperty(node, 'content') && | ||
typeof value === 'string' | ||
) { | ||
if (name === 'viewport') { | ||
value = value | ||
.replace(/(\d+\.\d+)/, (d) => String(Number(d))) | ||
.replace(/user-scalable=\s*yes/, '') | ||
// Fall through. | ||
} | ||
handlers.viewport = viewport | ||
handlers.keywords = collapse | ||
handlers.robots = collapse | ||
handlers['apple-itunes-app'] = collapse | ||
handlers['apple-media-service-subscription'] = collapse | ||
if (lists.has(name)) { | ||
value = stringify(parse(value), {padLeft: false}) | ||
} | ||
function content() { | ||
return transform | ||
} | ||
function transform(tree) { | ||
visit(tree, 'element', visitor) | ||
} | ||
function visitor(node) { | ||
var props = node.properties | ||
var name = props.name | ||
if (is(node, 'meta') && has(node, 'content') && own.call(handlers, name)) { | ||
if (typeof props.content === 'string') { | ||
props.content = handlers[name](props.content) | ||
} | ||
props.content = value | ||
} | ||
}) | ||
} | ||
} | ||
function viewport(value) { | ||
return collapse( | ||
value.replace(/(\d+\.\d+)/, toNumber).replace(/user-scalable=\s*yes/, '') | ||
) | ||
} | ||
function collapse(value) { | ||
return comma.stringify(comma.parse(value), {padLeft: false}) | ||
} | ||
function toNumber(value) { | ||
return Number(value) | ||
} |
{ | ||
"name": "rehype-minify-meta-content", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"description": "rehype plugin to minify `content` on `meta` elements", | ||
@@ -27,12 +27,29 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"comma-separated-tokens": "^1.0.2", | ||
"hast-util-has-property": "^1.0.0", | ||
"hast-util-is-element": "^1.0.0", | ||
"unist-util-visit": "^2.0.0" | ||
"@types/hast": "^2.0.0", | ||
"comma-separated-tokens": "^2.0.0", | ||
"hast-util-has-property": "^2.0.0", | ||
"hast-util-is-element": "^2.0.0", | ||
"unified": "^10.0.0", | ||
"unist-util-visit": "^4.0.0" | ||
}, | ||
"xo": false | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"test": "node --conditions development test.js" | ||
}, | ||
"xo": false, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
} |
@@ -20,2 +20,5 @@ <!--This file is generated by `build-packages.js`--> | ||
This package is [ESM only][esm]: | ||
Node 12+ is needed to use it and it must be `imported`ed instead of `required`d. | ||
[npm][]: | ||
@@ -27,2 +30,31 @@ | ||
This package exports no identifiers. | ||
The default export is `rehypeMinifyMetaContent` | ||
## Use | ||
On the API: | ||
```diff | ||
import {unified} from 'unified' | ||
import rehypeParse from 'rehype-parse' | ||
+import rehypeMinifyMetaContent from 'rehype-minify-meta-content' | ||
import rehypeStringify from 'rehype-stringify' | ||
unified() | ||
.use(rehypeParse) | ||
+ .use(rehypeMinifyMetaContent) | ||
.use(rehypeStringify) | ||
.process('<span>some html</span>', function (err, file) { | ||
console.error(report(err || file)) | ||
console.log(String(file)) | ||
}) | ||
``` | ||
On the CLI: | ||
```sh | ||
rehype input.html --use minify-meta-content --output output.html | ||
``` | ||
## Example | ||
@@ -58,5 +90,5 @@ | ||
[build-badge]: https://img.shields.io/travis/rehypejs/rehype-minify.svg | ||
[build-badge]: https://github.com/rehypejs/rehype-minify/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/rehypejs/rehype-minify | ||
[build]: https://github.com/rehypejs/rehype-minify/actions | ||
@@ -81,6 +113,8 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-minify.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[chat]: https://spectrum.chat/unified/rehype | ||
[chat]: https://github.com/rehypejs/rehype/discussions | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[npm]: https://docs.npmjs.com/cli/install | ||
@@ -87,0 +121,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6797
4
69
129
Yes
6
+ Added@types/hast@^2.0.0
+ Addedunified@^10.0.0
+ Added@types/hast@2.3.10(transitive)
+ Addedbail@2.0.2(transitive)
+ Addedcomma-separated-tokens@2.0.3(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedhast-util-has-property@2.0.1(transitive)
+ Addedhast-util-is-element@2.1.3(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedis-plain-obj@4.1.0(transitive)
+ Addedtrough@2.2.0(transitive)
+ Addedunified@10.1.2(transitive)
+ Addedunist-util-is@5.2.1(transitive)
+ Addedunist-util-stringify-position@3.0.3(transitive)
+ Addedunist-util-visit@4.1.2(transitive)
+ Addedunist-util-visit-parents@5.1.3(transitive)
+ Addedvfile@5.3.7(transitive)
+ Addedvfile-message@3.1.4(transitive)
- Removedcomma-separated-tokens@1.0.8(transitive)
- Removedhast-util-has-property@1.0.4(transitive)
- Removedhast-util-is-element@1.1.0(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedunist-util-visit-parents@3.1.1(transitive)
Updatedhast-util-is-element@^2.0.0
Updatedunist-util-visit@^4.0.0