rehype-remove-comments
Advanced tools
Comparing version 5.0.0 to 6.0.0
@@ -1,26 +0,2 @@ | ||
/** | ||
* Remove comments (except conditional comments). | ||
* | ||
* When configured with `force: true` (default: `false`), conditional comments | ||
* are also removed. | ||
* | ||
* @type {import('unified').Plugin<[Options?] | void[], Root>} | ||
*/ | ||
export default function rehypeRemoveComments( | ||
options?: void | Options | undefined | ||
): | ||
| void | ||
| import('unified').Transformer<import('hast').Root, import('hast').Root> | ||
export type Root = import('hast').Root | ||
export type Node = import('unist').Node | ||
export type HastNode = Root | Root['children'][number] | ||
/** | ||
* Configuration. | ||
*/ | ||
export type Options = { | ||
/** | ||
* Conditional comments are also removed when configured with `force: true` | ||
* The default is to leave them. | ||
*/ | ||
removeConditional?: boolean | undefined | ||
} | ||
export { default } from "./lib/index.js"; | ||
export type Options = import('./lib/index.js').Options; |
90
index.js
/** | ||
* @fileoverview | ||
* Remove comments (except conditional comments). | ||
* rehype plugin to remove comments. | ||
* | ||
* When configured with `force: true` (default: `false`), conditional comments | ||
* are also removed. | ||
* ## What is this? | ||
* | ||
* This package is a plugin that removes comments. | ||
* By default it keeps conditional comments, optionally it removes them too. | ||
* | ||
* ## When should I use this? | ||
* | ||
* You can use this plugin when you want to improve the transfer size of HTML | ||
* documents. | ||
* | ||
* ## API | ||
* | ||
* ### `unified().use(rehypeRemoveComments[, options])` | ||
* | ||
* Remove comments. | ||
* | ||
* ###### Parameters | ||
* | ||
* * `options` (`Options`, optional) | ||
* — configuration | ||
* | ||
* ###### Returns | ||
* | ||
* Transform ([`Transformer`](https://github.com/unifiedjs/unified#transformer)). | ||
* | ||
* ### `Options` | ||
* | ||
* Configuration (TypeScript type). | ||
* | ||
* ##### Fields | ||
* | ||
* * `removeConditional` (`boolean`, default: `false`) | ||
* — remove conditional comments (default: `false`); the default is to | ||
* leave them | ||
* | ||
* @example | ||
* {} | ||
* <!--Hello--> | ||
@@ -13,50 +46,5 @@ * <!--[if IE 6]>OK<![endif]--> | ||
/** | ||
* @typedef {import('hast').Root} Root | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {Root|Root['children'][number]} HastNode | ||
* | ||
* @typedef Options | ||
* Configuration. | ||
* @property {boolean} [removeConditional=false] | ||
* Conditional comments are also removed when configured with `force: true` | ||
* The default is to leave them. | ||
* @typedef {import('./lib/index.js').Options} Options | ||
*/ | ||
import {filter} from 'unist-util-filter' | ||
import {isConditionalComment} from 'hast-util-is-conditional-comment' | ||
/** | ||
* Remove comments (except conditional comments). | ||
* | ||
* When configured with `force: true` (default: `false`), conditional comments | ||
* are also removed. | ||
* | ||
* @type {import('unified').Plugin<[Options?] | void[], Root>} | ||
*/ | ||
export default function rehypeRemoveComments(options = {}) { | ||
const force = options.removeConditional | ||
return (tree) => | ||
// `undefined` is never returned because we don’t remove nodes (but TS | ||
// doesn’t know it.) | ||
/* c8 ignore next */ | ||
filter(tree, {cascade: false}, force ? hard : soft) || undefined | ||
} | ||
/** | ||
* @param {Node} node | ||
* @returns {boolean} | ||
*/ | ||
function soft(node) { | ||
const x = /** @type {HastNode} */ (node) | ||
return hard(x) || isConditionalComment(x) | ||
} | ||
/** | ||
* @param {Node} node | ||
* @returns {boolean} | ||
*/ | ||
function hard(node) { | ||
const x = /** @type {HastNode} */ (node) | ||
return x.type !== 'comment' | ||
} | ||
export {default} from './lib/index.js' |
{ | ||
"name": "rehype-remove-comments", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "rehype plugin to remove comments (except conditional comments)", | ||
"license": "MIT", | ||
"keywords": [ | ||
"unified", | ||
"comment", | ||
"html", | ||
"mangle", | ||
"minify", | ||
"plugin", | ||
"rehype", | ||
"rehype-plugin", | ||
"plugin", | ||
"html", | ||
"minify", | ||
"mangle", | ||
"comment" | ||
"unified" | ||
], | ||
@@ -28,25 +28,21 @@ "repository": "https://github.com/rehypejs/rehype-minify/tree/main/packages/rehype-remove-comments", | ||
"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", | ||
"hast-util-is-conditional-comment": "^2.0.0", | ||
"unified": "^10.0.0", | ||
"unist-util-filter": "^4.0.0" | ||
"@types/hast": "^3.0.0", | ||
"hast-util-is-conditional-comment": "^3.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 | ||
} |
208
readme.md
@@ -1,2 +0,2 @@ | ||
<!--This file is generated by `build-packages.js`--> | ||
<!--This file is generated--> | ||
@@ -9,18 +9,41 @@ # rehype-remove-comments | ||
[![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] | ||
Remove comments (except conditional comments). | ||
**[rehype][]** plugin to remove comments. | ||
When configured with `force: true` (default: `false`), conditional comments | ||
are also removed. | ||
## 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(rehypeRemoveComments[, options])`](#unifieduserehyperemovecomments-options) | ||
* [`Options`](#options) | ||
* [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 removes comments. | ||
By default it keeps conditional comments, optionally it removes them too. | ||
## When should I use this? | ||
You can use this plugin when you want to improve the transfer 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 +53,16 @@ npm install rehype-remove-comments | ||
This package exports no identifiers. | ||
The default export is `rehypeRemoveComments` | ||
In Deno with [`esm.sh`][esm-sh]: | ||
```js | ||
import rehypeRemoveComments from 'https://esm.sh/rehype-remove-comments@6' | ||
``` | ||
In browsers with [`esm.sh`][esm-sh]: | ||
```html | ||
<script type="module"> | ||
import rehypeRemoveComments from 'https://esm.sh/rehype-remove-comments@6?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -38,16 +72,16 @@ | ||
```diff | ||
import {unified} from 'unified' | ||
import rehypeParse from 'rehype-parse' | ||
+import rehypeRemoveComments from 'rehype-remove-comments' | ||
import rehypeStringify from 'rehype-stringify' | ||
```js | ||
import rehypeParse from 'rehype-parse' | ||
import rehypeRemoveComments from 'rehype-remove-comments' | ||
import rehypeStringify from 'rehype-stringify' | ||
import {read} from 'to-vfile' | ||
import {unified} from 'unified' | ||
unified() | ||
.use(rehypeParse) | ||
+ .use(rehypeRemoveComments) | ||
.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(rehypeRemoveComments) | ||
.use(rehypeStringify) | ||
.process(await read('index.html')) | ||
console.log(String(file)) | ||
``` | ||
@@ -58,8 +92,50 @@ | ||
```sh | ||
rehype input.html --use remove-comments --output output.html | ||
rehype input.html --use rehype-remove-comments --output output.html | ||
``` | ||
On the CLI in a config file (here a `package.json`): | ||
```diff | ||
… | ||
"rehype": { | ||
"plugins": [ | ||
… | ||
+ "rehype-remove-comments", | ||
… | ||
] | ||
} | ||
… | ||
``` | ||
## API | ||
This package exports no identifiers. | ||
The default export is `rehypeRemoveComments`. | ||
### `unified().use(rehypeRemoveComments[, options])` | ||
Remove comments. | ||
###### Parameters | ||
* `options` (`Options`, optional) | ||
— configuration | ||
###### Returns | ||
Transform ([`Transformer`](https://github.com/unifiedjs/unified#transformer)). | ||
### `Options` | ||
Configuration (TypeScript type). | ||
##### Fields | ||
* `removeConditional` (`boolean`, default: `false`) | ||
— remove conditional comments (default: `false`); the default is to | ||
leave them | ||
## Example | ||
##### In | ||
###### In | ||
@@ -71,3 +147,3 @@ ```html | ||
##### Out | ||
###### Out | ||
@@ -78,2 +154,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-remove-comments@^6`, | ||
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 | ||
@@ -93,42 +199,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-remove-comments.svg | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-minify.svg | ||
[downloads]: https://www.npmjs.com/package/rehype-remove-comments | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/rehype-remove-comments.svg | ||
[downloads-badge]: https://img.shields.io/npm/dm/rehype-remove-comments.svg | ||
[size]: https://bundlephobia.com/result?p=rehype-remove-comments | ||
[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-remove-comments | ||
[size-badge]: https://img.shields.io/bundlejs/size/rehype-remove-comments | ||
[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 |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9547
3
6
111
246
1
+ Addedunist-util-visit@^5.0.0
+ Added@types/hast@3.0.4(transitive)
+ Added@types/unist@3.0.3(transitive)
+ Addedhast-util-is-conditional-comment@3.0.1(transitive)
+ Addedunist-util-is@6.0.0(transitive)
+ Addedunist-util-visit@5.0.0(transitive)
+ Addedunist-util-visit-parents@6.0.1(transitive)
- Removedunified@^10.0.0
- Removedunist-util-filter@^4.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-is-conditional-comment@2.0.0(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-filter@4.0.1(transitive)
- Removedunist-util-is@5.2.1(transitive)
- Removedunist-util-stringify-position@3.0.3(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