import liquid from './liquid.js'
import cheerio from 'cheerio'
import { decode } from 'html-entities'
import stripHtmlComments from 'strip-html-comments'
import createProcessor from './create-processor.js'
// used below to remove extra newlines in TOC lists
const endLine = '\r?\n'
const blankLine = '\\s*?[\r\n]*'
const startNextLine = '[^\\S\r\n]*?[-\\*] and entities like <
template = template.replace(
/``` ?shell\r?\n\s*?(\S[\s\S]*?)\r?\n.*?```/gm,
'
'
)
// clean up empty lines in TOC lists left by unrendered list items (due to productVersions)
// for example, remove the blank line here:
// - foo
//
// - bar
if (template.includes('')) {
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()
if (options.textOnly) {
html = fastTextOnly(html)
}
return html.trim()
} catch (error) {
if (options.filename) {
console.error(`renderContent failed on file: ${options.filename}`)
}
throw error
}
}
// Given a piece of HTML return it without HTML. E.g.
// `$1
Foo & bar
` becomes `Foo & bar` // and `A link andcode` becomes `A link and code`.
// Take advantage of the subtle fact that a lot of the times, the html value
// we get here is a single line that starts with `` and ends with `
` // and contains no longer HTML tags. function fastTextOnly(html) { if (!html) return '' if (html.startsWith('') && html.endsWith('
')) { const middle = html.slice(3, -4) if (!middle.includes('<')) return decode(middle.trim()) } return cheerio.load(html, { xmlMode: true }).text().trim() } renderContent.liquid = liquid export default renderContent