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
This commit is contained in:
33
lib/render-content/plugins/wrap-in-element.js
Normal file
33
lib/render-content/plugins/wrap-in-element.js
Normal file
@@ -0,0 +1,33 @@
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user