1
0
mirror of synced 2025-12-20 10:28:40 -05:00
Files
docs/lib/render-content/renderContent.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

106 lines
3.0 KiB
JavaScript

import liquid from './liquid.js'
import cheerio from 'cheerio'
import xHtmlEntities from 'html-entities'
import stripHtmlComments from 'strip-html-comments'
import createProcessor from './create-processor.js'
const Entities = xHtmlEntities.XmlEntities
const entities = new Entities()
// 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. :]
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.cheerioObject) {
return cheerio.load(html, { xmlMode: true })
}
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()
}
renderContent.liquid = liquid
export default renderContent