1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/lib/render-content/plugins/wrap-in-element.js
Jason Etcovitch bd63f47636 Remove cheerio from Page#render (#17566)
* Write our plugin

* Include it

* Move the RegEx

* Don't rewriteLocalLinks with cheerio anymore

* Process after HTML ast is generated

* Use the same logic as before, just to see if it'll pass

* Don't require languageCode/version

* Only work on local links

* Needs an href

* Only update href if there's a new one to use

* Check for node.properties

* Some links are just mean

* Move use-english-headings to be a plugin

* Bail if no englishHeadings were passed

* Install rehype-wrap

* Wrap ol > li img in div.procedural-image-wrapper

* Test for platform without cheerio

* Use a plugin for rewriteAssetPathsToS3

* Remove cheerio from page.render

* Fix require paths

* SImplify

* Fix some 🐛s

* Use our own rehype-wrap

* Move rewriteAssetPathsToS3 after HTML AST

* Remove some console logs

* Fix check for includesPlatformSpecificContent

* Rename ast => tree
2021-01-29 15:44:50 -05:00

34 lines
848 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(options.selector, tree)) {
visit(tree, match, (node, i, parent) => {
const wrapper = parseSelector('div')
wrapper.children = [node]
parent.children[i] = wrapper
})
}
}
}