1
0
mirror of synced 2025-12-19 18:10:59 -05:00
Files
docs/lib/render-content/plugins/wrap-in-element.js
2021-04-21 15:00:51 -04:00

34 lines
860 B
JavaScript

const visit = require('unist-util-visit')
const { selectAll } = require('hast-util-select')
const parseSelector = require('hast-util-parse-selector')
/*
* Attacher
*/
module.exports = options => {
options = options || {}
const selector = options.selector || options.select || 'body'
const wrapper = options.wrapper || options.wrap
/*
* Transformer
*/
return tree => {
if (typeof wrapper !== 'string') {
throw new TypeError('Expected a `string` as wrapper')
}
if (typeof selector !== 'string') {
throw new TypeError('Expected a `string` as selector')
}
for (const match of selectAll(selector, tree)) {
visit(tree, match, (node, i, parent) => {
const parsedWrapper = parseSelector(wrapper)
parsedWrapper.children = [node]
parent.children[i] = parsedWrapper
})
}
}
}