forked from rehypejs/rehype-minify
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (63 loc) · 1.76 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
/**
* rehype plugin to minify color attributes.
*
* ## What is this?
*
* This package is a plugin that can minify the value of the `content` attribute
* of `<meta>` elements with a `name` attribute whose value is either
* `theme-color` or `msapplication-TileColor`, and thus represents a CSS color.
*
* ## When should I use this?
*
* You can use this plugin when you want to improve the size of HTML documents.
*
* ## API
*
* ### `unified().use(rehypeMinifyMetaColor)`
*
* Minify color attributes.
* There are no options.
*
* @example
* <meta name="theme-color" content="#0000ff">
* <meta name="msapplication-TileColor" content="#ff0000">
*/
/**
* @typedef {import('hast').Root} Root
*/
import CleanCSS from 'clean-css'
import {visit} from 'unist-util-visit'
import {isElement} from 'hast-util-is-element'
import {hasProperty} from 'hast-util-has-property'
const clean = new CleanCSS()
const prefix = '*{color:'
const suffix = '}'
/**
* Minify color attributes.
*
* @type {import('unified').Plugin<Array<void>, Root>}
*/
export default function rehypeMinifyMetaColor() {
return (tree) => {
visit(tree, 'element', (node) => {
const props = node.properties || {}
const name = props.name
if (
isElement(node, 'meta') &&
(name === 'msapplication-TileColor' || name === 'theme-color') &&
hasProperty(node, 'content')
) {
let value = props.content
if (typeof value === 'string') {
try {
const output = clean.minify(prefix + value + suffix)
value = output.styles.slice(prefix.length, -suffix.length)
// Potential third party errors?
/* c8 ignore next */
} catch {}
props.content = value
}
}
})
}
}