Skip to content

Commit

Permalink
refactor: parsing attributes in sixHex transformer
Browse files Browse the repository at this point in the history
use vanilla js instead of posthtml-attrs-parser
  • Loading branch information
cossssmin committed Oct 8, 2022
1 parent 5a968cd commit a8a1299
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/transformers/sixHex.js
@@ -1,6 +1,5 @@
const {get} = require('lodash')
const posthtml = require('posthtml')
const parseAttrs = require('posthtml-attrs-parser')
const {conv} = require('color-shorthand-hex-to-six-digit')

module.exports = async (html, config = {}) => {
Expand All @@ -13,18 +12,16 @@ module.exports = async (html, config = {}) => {
}

const sixHex = () => tree => {
const process = node => {
const attrs = parseAttrs(node.attrs)

const targets = ['bgcolor', 'color']
const targets = new Set(['bgcolor', 'color'])

targets.forEach(attribute => {
if (attrs[attribute]) {
attrs[attribute] = conv(attrs[attribute])
}
})

node.attrs = attrs.compose()
const process = node => {
if (node.attrs) {
Object.entries(node.attrs).forEach(([name, value]) => {
if (targets.has(name) && node.attrs[name]) {
node.attrs[name] = conv(value)
}
})
}

return node
}
Expand Down

0 comments on commit a8a1299

Please sign in to comment.