1
0
mirror of synced 2025-12-23 03:44:00 -05:00
Files
docs/lib/render-content/renderContent.js
Jason Etcovitch 989006bab5 Move the rewriteLocalLinks behavior to an AST pipeline plugin (#17550)
* 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
2021-01-29 15:36:17 +00:00

101 lines
2.9 KiB
JavaScript

const liquid = require('./liquid')
const cheerio = require('cheerio')
const Entities = require('html-entities').XmlEntities
const entities = new Entities()
const stripHtmlComments = require('strip-html-comments')
const createProcessor = require('./create-processor')
// used below to remove extra newlines in TOC lists
const endLine = '</a>\r?\n'
const blankLine = '\\s*?[\r\n]*'
const startNextLine = '[^\\S\r\n]*?[-\\*] <a'
const blankLineInList = new RegExp(
`(${endLine})${blankLine}(${startNextLine})`,
'mg'
)
// used below to remove unwanted newlines from inline tags in tables
const inlineTags = ['a', 'code', 'em']
const inlineTagString = `(?:${inlineTags.join('|')})`
const inlineTagRegex = new RegExp(`\n?(</?${inlineTagString}>?)\n?`, 'gm')
// parse multiple times because some templates contain more templates. :]
module.exports = async function renderContent (
template,
context = {},
options = {}
) {
try {
// remove any newlines that precede html comments, then remove the comments
if (template) {
template = stripHtmlComments(template.replace(/\n<!--/g, '<!--'))
}
template = await liquid.parseAndRender(template, context)
// this workaround loses syntax highlighting but correctly handles tags like <em> and entities like &lt;
template = template.replace(
/``` ?shell\r?\n\s*?(\S[\s\S]*?)\r?\n.*?```/gm,
'<pre><code class="hljs language-shell">$1</code></pre>'
)
// clean up empty lines in TOC lists left by unrendered list items (due to productVersions)
// for example, remove the blank line here:
// - <a>foo</a>
//
// - <a>bar</a>
if (template.includes('</a>')) {
template = template.replace(blankLineInList, '$1$2')
}
// this removes any extra newlines left by (now resolved) liquid
// statements so that extra space doesn't mess with list numbering
template = template.replace(/(\r?\n){3}/g, '\n\n')
const processor = createProcessor(context)
const vFile = await processor.process(template)
let html = vFile.toString()
// Remove unwanted newlines (which appear as spaces) from inline tags inside tables
if (html.includes('<table>')) html = removeNewlinesFromInlineTags(html)
if (options.textOnly) {
html = cheerio
.load(html)
.text()
.trim()
}
if (options.encodeEntities) html = entities.encode(html)
return html.trim()
} catch (error) {
if (options.filename) {
console.error(`renderContent failed on file: ${options.filename}`)
}
throw error
}
}
function removeNewlinesFromInlineTags (html) {
const $ = cheerio.load(html)
// see https://cheerio.js.org/#html-htmlstring-
$(inlineTags.join(','))
.parents('td')
.get()
.map(tag =>
$(tag).html(
$(tag)
.html()
.replace(inlineTagRegex, '$1')
)
)
return $('body').html()
}
Object.assign(module.exports, {
liquid
})