rehype-remove-comments
Advanced tools
Comparing version 4.0.2 to 5.0.0
54
index.js
@@ -12,25 +12,51 @@ /** | ||
'use strict' | ||
/** | ||
* @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. | ||
*/ | ||
var filter = require('unist-util-filter') | ||
var conditional = require('hast-util-is-conditional-comment') | ||
import {filter} from 'unist-util-filter' | ||
import {isConditionalComment} from 'hast-util-is-conditional-comment' | ||
module.exports = comments | ||
/** | ||
* 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 | ||
function comments(options) { | ||
var force = (options || {}).removeConditional | ||
return transform | ||
function transform(tree) { | ||
return filter(tree, {cascade: false}, force ? hard : soft) | ||
} | ||
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) { | ||
return hard(node) || conditional(node) | ||
const x = /** @type {HastNode} */ (node) | ||
return hard(x) || isConditionalComment(x) | ||
} | ||
/** | ||
* @param {Node} node | ||
* @returns {boolean} | ||
*/ | ||
function hard(node) { | ||
return node.type !== 'comment' | ||
const x = /** @type {HastNode} */ (node) | ||
return x.type !== 'comment' | ||
} |
{ | ||
"name": "rehype-remove-comments", | ||
"version": "4.0.2", | ||
"version": "5.0.0", | ||
"description": "rehype plugin to remove comments (except conditional comments)", | ||
@@ -26,10 +26,27 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"unist-util-filter": "^2.0.0", | ||
"hast-util-is-conditional-comment": "^1.0.0" | ||
"@types/hast": "^2.0.0", | ||
"hast-util-is-conditional-comment": "^2.0.0", | ||
"unified": "^10.0.0", | ||
"unist-util-filter": "^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 `rehypeRemoveComments` | ||
## Use | ||
On the API: | ||
```diff | ||
import {unified} from 'unified' | ||
import rehypeParse from 'rehype-parse' | ||
+import rehypeRemoveComments from 'rehype-remove-comments' | ||
import rehypeStringify from 'rehype-stringify' | ||
unified() | ||
.use(rehypeParse) | ||
+ .use(rehypeRemoveComments) | ||
.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 remove-comments --output output.html | ||
``` | ||
## Example | ||
@@ -57,5 +89,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 | ||
@@ -80,6 +112,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 | ||
@@ -86,0 +120,0 @@ |
6743
4
81
128
Yes
4
+ Added@types/hast@^2.0.0
+ Addedunified@^10.0.0
+ Added@types/hast@2.3.10(transitive)
+ Added@types/unist@2.0.11(transitive)
+ Addedbail@2.0.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedhast-util-is-conditional-comment@2.0.0(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-filter@4.0.1(transitive)
+ Addedunist-util-is@5.2.1(transitive)
+ Addedunist-util-stringify-position@3.0.3(transitive)
+ Addedunist-util-visit-parents@5.1.3(transitive)
+ Addedvfile@5.3.7(transitive)
+ Addedvfile-message@3.1.4(transitive)
- Removedhast-util-is-conditional-comment@1.0.4(transitive)
- Removedunist-util-filter@2.0.3(transitive)
- Removedunist-util-is@4.1.0(transitive)
Updatedunist-util-filter@^4.0.0