forked from rehypejs/rehype-minify
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (52 loc) · 1.41 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
/**
* rehype plugin to remove `type` and `language` attributes on
* JavaScript scripts.
*
* ## What is this?
*
* This package is a plugin that removes `type` and/or `language` attributes
* on JavaScript scripts, as they are unneeded.
* This plugin does not touch other `<script>` elements (such as JavaScript
* modules or non-JavaScript scripts).
*
* ## When should I use this?
*
* You can use this plugin when you want to improve the transfer size of HTML
* documents.
*
* ## API
*
* ### `unified().use(rehypeRemoveScriptTypeJavaScript)`
*
* Remove `type` and `language` attributes on JavaScript scripts.
* There are no options.
*
* @example
* <script type="text/javascript"></script>
* <script language="javascript1.5"></script>
* <script type="module"></script>
*/
/**
* @typedef {import('hast').Root} Root
*/
import {visit} from 'unist-util-visit'
import {isJavaScript} from 'hast-util-is-javascript'
/**
* Remove `type` and `language` on JavaScript scripts.
*
* @type {import('unified').Plugin<Array<void>, Root>}
*/
export default function rehypeRemoveScriptTypeJavaScript() {
return (tree) => {
visit(tree, 'element', (node) => {
if (isJavaScript(node) && node.properties) {
if ('type' in node.properties) {
node.properties.type = null
}
if ('language' in node.properties) {
node.properties.language = null
}
}
})
}
}