forked from rehypejs/rehype-minify
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (45 loc) · 1.22 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
/**
* rehype plugin to remove the contents of external JavaScript `<script>`s.
*
* ## What is this?
*
* This package is a plugin that removes the contents of JavaScript `<script>`s
* that also have an `src` attribute.
* Scripts are supposed to be either external (with `src`) or internal (with
* code in them), and both is nonsensical.
*
* ## When should I use this?
*
* You can use this plugin when you want to improve the transfer size of HTML
* documents.
*
* ## API
*
* ### `unified().use(rehypeRemoveExternalScriptContent)`
*
* Remove the contents of external JavaScript `<script>`s.
* There are no options.
*
* @example
* <script src="index.js">console.log(1);</script>
*/
/**
* @typedef {import('hast').Root} Root
*/
import {visit} from 'unist-util-visit'
import {isJavaScript} from 'hast-util-is-javascript'
import {hasProperty} from 'hast-util-has-property'
/**
* Remove content of external JavaScript `script` elements.
*
* @type {import('unified').Plugin<Array<void>, Root>}
*/
export default function rehypeRemoveExternalScriptContent() {
return (tree) => {
visit(tree, 'element', (node) => {
if (isJavaScript(node) && hasProperty(node, 'src')) {
node.children = []
}
})
}
}