forked from rehypejs/rehype-minify
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (80 loc) · 2.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* rehype plugin to remove comments.
*
* ## 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.
*
* ##### `options`
*
* Configuration (optional).
*
* ###### `options.removeConditional`
*
* Whether to remove conditional comments too (`boolean`, default: `false`).
* The default behavior is to keep conditional comments.
* Conditional comments are a legacy feature that was specific to Internet
* Explorer.
* They were no longer used in IE 10.
*
* @example
* <!--Hello-->
* <!--[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.
*/
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?]|Array<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'
}