rehype-minify-language
Advanced tools
Comparing version 1.0.0 to 2.0.0
58
index.js
@@ -11,34 +11,40 @@ /** | ||
'use strict' | ||
/** | ||
* @typedef {import('hast').Root} Root | ||
*/ | ||
var normalize = require('bcp-47-normalize') | ||
var visit = require('unist-util-visit') | ||
var has = require('hast-util-has-property') | ||
import {bcp47Normalize} from 'bcp-47-normalize' | ||
import {visit} from 'unist-util-visit' | ||
import {hasProperty} from 'hast-util-has-property' | ||
module.exports = language | ||
const fields = ['hrefLang', 'lang', 'srcLang', 'xmlLang'] | ||
var fields = ['hrefLang', 'lang', 'srcLang', 'xmlLang'] | ||
/** | ||
* Minify language attributes. | ||
* | ||
* @type {import('unified').Plugin<[], Root>} | ||
*/ | ||
export default function rehypeMinifyLanguage() { | ||
return (tree) => { | ||
visit(tree, 'element', (node) => { | ||
const props = node.properties | ||
let index = -1 | ||
function language() { | ||
return transform | ||
} | ||
while (++index < fields.length) { | ||
const prop = fields[index] | ||
function transform(tree) { | ||
visit(tree, 'element', visitor) | ||
} | ||
function visitor(node) { | ||
var props = node.properties | ||
var index = -1 | ||
var prop | ||
while (++index < fields.length) { | ||
prop = fields[index] | ||
if (has(node, prop) && typeof props[prop] === 'string') { | ||
// BCP 47 tags are case-insensitive, but in this project we prefer | ||
// lowercase which *should* help GZIP. | ||
props[prop] = (normalize(props[prop]) || props[prop]).toLowerCase() | ||
} | ||
if ( | ||
props && | ||
hasProperty(node, prop) && | ||
typeof props[prop] === 'string' | ||
) { | ||
// BCP 47 tags are case-insensitive, but in this project we prefer | ||
// lowercase which *should* help GZIP. | ||
props[prop] = String( | ||
bcp47Normalize(String(props[prop])) || props[prop] | ||
).toLowerCase() | ||
} | ||
} | ||
}) | ||
} | ||
} |
{ | ||
"name": "rehype-minify-language", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "rehype plugin to minify language attributes", | ||
@@ -27,13 +27,30 @@ "license": "MIT", | ||
"contributors": [ | ||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)" | ||
"Titus Wormer <tituswormer@gmail.com>" | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"bcp-47-normalize": "^1.1.0", | ||
"hast-util-has-property": "^1.0.0", | ||
"unist-util-visit": "^2.0.0" | ||
"@types/hast": "^2.0.0", | ||
"bcp-47-normalize": "^2.0.0", | ||
"hast-util-has-property": "^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 | ||
} | ||
} |
@@ -17,2 +17,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][]: | ||
@@ -24,2 +27,5 @@ | ||
This package exports no identifiers. | ||
The default export is `rehypeMinifyLanguage` | ||
## Use | ||
@@ -30,6 +36,11 @@ | ||
```diff | ||
import {unified} from 'unified' | ||
import rehypeParse from 'rehype-parse' | ||
+import rehypeMinifyLanguage from 'rehype-minify-language' | ||
import rehypeStringify from 'rehype-stringify' | ||
unified() | ||
.use(require('rehype-parse')) | ||
+ .use(require('rehype-minify-language')) | ||
.use(require('rehype-stringify')) | ||
.use(rehypeParse) | ||
+ .use(rehypeMinifyLanguage) | ||
.use(rehypeStringify) | ||
.process('<span>some html</span>', function (err, file) { | ||
@@ -44,3 +55,3 @@ console.error(report(err || file)) | ||
```sh | ||
rehype input.html --use minify-language > output.html | ||
rehype input.html --use minify-language --output output.html | ||
``` | ||
@@ -82,5 +93,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 | ||
@@ -109,2 +120,4 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-minify.svg | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[npm]: https://docs.npmjs.com/cli/install | ||
@@ -111,0 +124,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
6266
4
52
130
Yes
5
+ Added@types/hast@^2.0.0
+ Addedunified@^10.0.0
+ Added@types/hast@2.3.10(transitive)
+ Addedbail@2.0.2(transitive)
+ Addedbcp-47@2.1.0(transitive)
+ Addedbcp-47-match@2.0.3(transitive)
+ Addedbcp-47-normalize@2.3.0(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedhast-util-has-property@2.0.1(transitive)
+ Addedis-alphabetical@2.0.1(transitive)
+ Addedis-alphanumerical@2.0.1(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedis-decimal@2.0.1(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)
- Removedbcp-47@1.0.8(transitive)
- Removedbcp-47-match@1.0.3(transitive)
- Removedbcp-47-normalize@1.1.1(transitive)
- Removedhast-util-has-property@1.0.4(transitive)
- Removedis-alphabetical@1.0.4(transitive)
- Removedis-alphanumerical@1.0.4(transitive)
- Removedis-decimal@1.0.4(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedunist-util-visit-parents@3.1.1(transitive)
Updatedbcp-47-normalize@^2.0.0
Updatedunist-util-visit@^4.0.0