Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove attributes transformer
- Loading branch information
Showing
2 changed files
with
54 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,56 @@ | ||
const posthtml = require('posthtml') | ||
const {get, merge} = require('lodash') | ||
const removeAttributes = require('posthtml-remove-attributes') | ||
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({name: 'style'}, {name: 'class'}) | ||
attributes.push('style', 'class') | ||
|
||
// Allow omitting `value` key when removing empty attributes | ||
attributes.forEach(attr => { | ||
attr.value = attr.value || '' | ||
}) | ||
html = await posthtml([ | ||
removeAttributes(attributes, posthtmlOptions) | ||
]).process(html, posthtmlOptions).then(result => result.html) | ||
|
||
return posthtml([removeAttributes(attributes)]).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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters