diff --git a/lib/render-content/renderContent.js b/lib/render-content/renderContent.js
index a5db3a2ddb..c22ace3d2d 100644
--- a/lib/render-content/renderContent.js
+++ b/lib/render-content/renderContent.js
@@ -1,6 +1,6 @@
import liquid from './liquid.js'
import cheerio from 'cheerio'
-import { encode } from 'html-entities'
+import { encode, decode } from 'html-entities'
import stripHtmlComments from 'strip-html-comments'
import createProcessor from './create-processor.js'
@@ -52,17 +52,21 @@ async function renderContent(template = '', context = {}, options = {}) {
let html = vFile.toString()
// Remove unwanted newlines (which appear as spaces) from inline tags inside tables
- if (html.includes('
')) html = removeNewlinesFromInlineTags(html)
+ if (html.includes('')) {
+ html = removeNewlinesFromInlineTags(html)
+ }
if (options.textOnly) {
- html = cheerio.load(html).text().trim()
+ html = fastTextOnly(html)
}
if (options.cheerioObject) {
return cheerio.load(html, { xmlMode: true })
}
- if (options.encodeEntities) html = encode(html)
+ if (options.encodeEntities) {
+ html = encode(html)
+ }
return html.trim()
} catch (error) {
@@ -85,6 +89,21 @@ function removeNewlinesFromInlineTags(html) {
return $('body').html()
}
+// Given a piece of HTML return it without HTML. E.g.
+// `Foo & bar
` becomes `Foo & bar`
+// and `A link and code` 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