rehype-minify-meta-content
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -1,12 +0,1 @@ | ||
/** | ||
* 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(): | ||
| void | ||
| import('unified').Transformer<import('hast').Root, import('hast').Root> | ||
export type Root = import('hast').Root | ||
export { default } from "./lib/index.js"; |
83
index.js
/** | ||
* @fileoverview | ||
* Minify `content` on `meta` elements. | ||
* rehype plugin to minify `content` attributes on `<meta>` elements. | ||
* | ||
* Note: `meta[name=theme-color]` and `meta[name=msapplication-TileColor]` | ||
* are handled by `rehype-minify-meta-color`. | ||
* ## What is this? | ||
* | ||
* This package is a plugin that can minify the value of the `content` attribute | ||
* of `<meta>` elements. | ||
* | ||
* Note that `meta[name=theme-color]` and `meta[name=msapplication-TileColor]` | ||
* are handled by | ||
* [`rehype-minify-meta-color`](https://github.com/rehypejs/rehype-minify/tree/main/packages/rehype-minify-meta-color). | ||
* | ||
* ## When should I use this? | ||
* | ||
* You can use this plugin when you want to improve the size of HTML documents. | ||
* | ||
* ## API | ||
* | ||
* ### `unified().use(rehypeMinifyMetaContent)` | ||
* | ||
* Minify `content` attributes on `meta` elements. | ||
* | ||
* ###### Returns | ||
* | ||
* Transform ([`Transformer`](https://github.com/unifiedjs/unified#transformer)). | ||
* | ||
* @example | ||
* {} | ||
* <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes"> | ||
@@ -12,54 +33,2 @@ * <meta name="keywords" content="foo, bar baz, qux"> | ||
/** | ||
* @typedef {import('hast').Root} Root | ||
*/ | ||
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' | ||
const lists = new Set([ | ||
'viewport', | ||
'keywords', | ||
'robots', | ||
'apple-itunes-app', | ||
'apple-media-service-subscription' | ||
]) | ||
/** | ||
* 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 | ||
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. | ||
} | ||
if (lists.has(name)) { | ||
value = stringify(parse(value), {padLeft: false}) | ||
} | ||
props.content = value | ||
} | ||
}) | ||
} | ||
} | ||
export {default} from './lib/index.js' |
{ | ||
"name": "rehype-minify-meta-content", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "rehype plugin to minify `content` on `meta` elements", | ||
"license": "MIT", | ||
"keywords": [ | ||
"unified", | ||
"rehype", | ||
"rehype-plugin", | ||
"plugin", | ||
"content", | ||
"html", | ||
"minify", | ||
"mangle", | ||
"meta", | ||
"content" | ||
"minify", | ||
"plugin", | ||
"rehype", | ||
"rehype-plugin", | ||
"unified" | ||
], | ||
@@ -29,27 +29,21 @@ "repository": "https://github.com/rehypejs/rehype-minify/tree/main/packages/rehype-minify-meta-content", | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"exports": "./index.js", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
"index.js", | ||
"lib/" | ||
], | ||
"dependencies": { | ||
"@types/hast": "^2.0.0", | ||
"@types/hast": "^3.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" | ||
"unist-util-visit": "^5.0.0" | ||
}, | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"test": "node --conditions development test.js" | ||
}, | ||
"xo": false, | ||
"scripts": {}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
"ignoreCatch": true, | ||
"strict": true | ||
}, | ||
"xo": false | ||
} |
195
readme.md
@@ -1,2 +0,2 @@ | ||
<!--This file is generated by `build-packages.js`--> | ||
<!--This file is generated--> | ||
@@ -9,18 +9,43 @@ # rehype-minify-meta-content | ||
[![Size][size-badge]][size] | ||
[![Sponsors][sponsors-badge]][collective] | ||
[![Backers][backers-badge]][collective] | ||
[![Sponsors][funding-sponsors-badge]][funding] | ||
[![Backers][funding-backers-badge]][funding] | ||
[![Chat][chat-badge]][chat] | ||
Minify `content` on `meta` elements. | ||
**[rehype][]** plugin to minify `content` attributes on `<meta>` elements. | ||
Note: `meta[name=theme-color]` and `meta[name=msapplication-TileColor]` | ||
are handled by `rehype-minify-meta-color`. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`unified().use(rehypeMinifyMetaContent)`](#unifieduserehypeminifymetacontent) | ||
* [Example](#example) | ||
* [Syntax](#syntax) | ||
* [Syntax tree](#syntax-tree) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package is a plugin that can minify the value of the `content` attribute | ||
of `<meta>` elements. | ||
Note that `meta[name=theme-color]` and `meta[name=msapplication-TileColor]` | ||
are handled by | ||
[`rehype-minify-meta-color`](https://github.com/rehypejs/rehype-minify/tree/main/packages/rehype-minify-meta-color). | ||
## When should I use this? | ||
You can use this plugin when you want to improve the size of HTML documents. | ||
## Install | ||
This package is [ESM only][esm]: | ||
Node 12+ is needed to use it and it must be `imported`ed instead of `required`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 16+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -30,5 +55,16 @@ npm install rehype-minify-meta-content | ||
This package exports no identifiers. | ||
The default export is `rehypeMinifyMetaContent` | ||
In Deno with [`esm.sh`][esm-sh]: | ||
```js | ||
import rehypeMinifyMetaContent from 'https://esm.sh/rehype-minify-meta-content@4' | ||
``` | ||
In browsers with [`esm.sh`][esm-sh]: | ||
```html | ||
<script type="module"> | ||
import rehypeMinifyMetaContent from 'https://esm.sh/rehype-minify-meta-content@4?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -38,16 +74,16 @@ | ||
```diff | ||
import {unified} from 'unified' | ||
import rehypeParse from 'rehype-parse' | ||
+import rehypeMinifyMetaContent from 'rehype-minify-meta-content' | ||
import rehypeStringify from 'rehype-stringify' | ||
```js | ||
import rehypeMinifyMetaContent from 'rehype-minify-meta-content' | ||
import rehypeParse from 'rehype-parse' | ||
import rehypeStringify from 'rehype-stringify' | ||
import {read} from 'to-vfile' | ||
import {unified} from 'unified' | ||
unified() | ||
.use(rehypeParse) | ||
+ .use(rehypeMinifyMetaContent) | ||
.use(rehypeStringify) | ||
.process('<span>some html</span>', function (err, file) { | ||
console.error(report(err || file)) | ||
console.log(String(file)) | ||
}) | ||
const file = await unified() | ||
.use(rehypeParse) | ||
.use(rehypeMinifyMetaContent) | ||
.use(rehypeStringify) | ||
.process(await read('index.html')) | ||
console.log(String(file)) | ||
``` | ||
@@ -58,8 +94,35 @@ | ||
```sh | ||
rehype input.html --use minify-meta-content --output output.html | ||
rehype input.html --use rehype-minify-meta-content --output output.html | ||
``` | ||
On the CLI in a config file (here a `package.json`): | ||
```diff | ||
… | ||
"rehype": { | ||
"plugins": [ | ||
… | ||
+ "rehype-minify-meta-content", | ||
… | ||
] | ||
} | ||
… | ||
``` | ||
## API | ||
This package exports no identifiers. | ||
The default export is `rehypeMinifyMetaContent`. | ||
### `unified().use(rehypeMinifyMetaContent)` | ||
Minify `content` attributes on `meta` elements. | ||
###### Returns | ||
Transform ([`Transformer`](https://github.com/unifiedjs/unified#transformer)). | ||
## Example | ||
##### In | ||
###### In | ||
@@ -71,3 +134,3 @@ ```html | ||
##### Out | ||
###### Out | ||
@@ -79,2 +142,32 @@ ```html | ||
## Syntax | ||
HTML is parsed according to WHATWG HTML (the living standard), which is also | ||
followed by all browsers. | ||
## Syntax tree | ||
The syntax tree used is [hast][]. | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with maintained | ||
versions of Node.js. | ||
When we cut a new major release, we drop support for unmaintained versions of | ||
Node. | ||
This means we try to keep the current release line, | ||
`rehype-minify-meta-content@^4`, | ||
compatible with Node.js 16. | ||
## Security | ||
As **rehype** works on HTML and improper use of HTML can open you up to a | ||
[cross-site scripting (XSS)][xss] attack, use of rehype can also be unsafe. | ||
Use [`rehype-sanitize`][rehype-sanitize] to make the tree safe. | ||
## Contribute | ||
@@ -94,42 +187,54 @@ | ||
[build-badge]: https://github.com/rehypejs/rehype-minify/workflows/main/badge.svg | ||
[author]: https://wooorm.com | ||
[build]: https://github.com/rehypejs/rehype-minify/actions | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-minify.svg | ||
[build-badge]: https://github.com/rehypejs/rehype-minify/workflows/main/badge.svg | ||
[chat]: https://github.com/rehypejs/rehype/discussions | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[coc]: https://github.com/rehypejs/.github/blob/main/code-of-conduct.md | ||
[contributing]: https://github.com/rehypejs/.github/blob/main/contributing.md | ||
[coverage]: https://codecov.io/github/rehypejs/rehype-minify | ||
[downloads-badge]: https://img.shields.io/npm/dm/rehype-minify-meta-content.svg | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-minify.svg | ||
[downloads]: https://www.npmjs.com/package/rehype-minify-meta-content | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/rehype-minify-meta-content.svg | ||
[downloads-badge]: https://img.shields.io/npm/dm/rehype-minify-meta-content.svg | ||
[size]: https://bundlephobia.com/result?p=rehype-minify-meta-content | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg | ||
[esm-sh]: https://esm.sh | ||
[backers-badge]: https://opencollective.com/unified/backers/badge.svg | ||
[funding]: https://opencollective.com/unified | ||
[collective]: https://opencollective.com/unified | ||
[funding-backers-badge]: https://opencollective.com/unified/backers/badge.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[funding-sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg | ||
[chat]: https://github.com/rehypejs/rehype/discussions | ||
[hast]: https://github.com/syntax-tree/hast | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[health]: https://github.com/rehypejs/.github | ||
[license]: https://github.com/rehypejs/rehype-minify/blob/main/license | ||
[npm]: https://docs.npmjs.com/cli/install | ||
[health]: https://github.com/rehypejs/.github | ||
[rehype]: https://github.com/rehypejs/rehype | ||
[contributing]: https://github.com/rehypejs/.github/blob/main/contributing.md | ||
[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize | ||
[size]: https://bundlejs.com/?q=rehype-minify-meta-content | ||
[size-badge]: https://img.shields.io/bundlejs/size/rehype-minify-meta-content | ||
[support]: https://github.com/rehypejs/.github/blob/main/support.md | ||
[coc]: https://github.com/rehypejs/.github/blob/main/code-of-conduct.md | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: https://github.com/rehypejs/rehype-minify/blob/main/license | ||
[author]: https://wooorm.com | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting |
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
9411
3
6
89
234
+ Added@types/hast@3.0.4(transitive)
+ Added@types/unist@3.0.3(transitive)
+ Addedunist-util-is@6.0.0(transitive)
+ Addedunist-util-visit@5.0.0(transitive)
+ Addedunist-util-visit-parents@6.0.1(transitive)
- Removedhast-util-has-property@^2.0.0
- Removedhast-util-is-element@^2.0.0
- Removedunified@^10.0.0
- Removed@types/hast@2.3.10(transitive)
- Removed@types/unist@2.0.11(transitive)
- Removedbail@2.0.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedhast-util-has-property@2.0.1(transitive)
- Removedhast-util-is-element@2.1.3(transitive)
- Removedis-buffer@2.0.5(transitive)
- Removedis-plain-obj@4.1.0(transitive)
- Removedtrough@2.2.0(transitive)
- Removedunified@10.1.2(transitive)
- Removedunist-util-is@5.2.1(transitive)
- Removedunist-util-stringify-position@3.0.3(transitive)
- Removedunist-util-visit@4.1.2(transitive)
- Removedunist-util-visit-parents@5.1.3(transitive)
- Removedvfile@5.3.7(transitive)
- Removedvfile-message@3.1.4(transitive)
Updated@types/hast@^3.0.0
Updatedunist-util-visit@^5.0.0