/
removeAttributes.js
56 lines (47 loc) · 1.81 KB
/
removeAttributes.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
const posthtml = require('posthtml')
const {get, merge} = require('lodash')
const defaultConfig = require('../generators/posthtml/defaultConfig')
module.exports = async (html, config = {}, direct = false) => {
const attributes = direct ? (Array.isArray(config) ? [...config] : []) : get(config, 'removeAttributes', [])
const posthtmlOptions = merge(defaultConfig, get(config, 'build.posthtml.options', {}))
attributes.push('style', 'class')
html = await posthtml([
removeAttributes(attributes, posthtmlOptions)
]).process(html, posthtmlOptions).then(result => result.html)
return html
}
/**
* Remove empty attributes with PostHTML
*
* Condition 1:
* `boolean` is for attributes without ="" (respects `recognizeNoValueAttribute` in PostHTML)
* `''` if the attribute included ="", i.e. style=""
*
* Condition 2: attribute value is a string and matches the one on the node
*
* Condition 3: same as 2, but for regular expressions
*/
const removeAttributes = (attributes = {}, posthtmlOptions = {}) => tree => {
const process = node => {
const normalizedAttrs = attributes.map(attribute => {
return {
name: get(attribute, 'name', typeof attribute === 'string' ? attribute : false),
value: get(attribute, 'value', get(posthtmlOptions, 'recognizeNoValueAttributes', true))
}
})
if (node.attrs) {
normalizedAttrs.forEach(attr => {
const targetAttrValue = get(node.attrs, attr.name)
if (
typeof targetAttrValue === 'boolean' || targetAttrValue === '' ||
(typeof attr.value === 'string' && node.attrs[attr.name] === attr.value) ||
(attr.value instanceof RegExp && attr.value.test(node.attrs[attr.name]))
) {
node.attrs[attr.name] = false
}
})
}
return node
}
return tree.walk(process)
}