Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-remark-prism-js): add additional html escapes #20156

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/gatsby-remark-prismjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ plugins: [
host: "localhost",
global: false,
},
// By default the HTML entities <>&'" are escaped.
// Add additional HTML escapes by providing a mapping
// of HTML entities and their escape value IE: { '}': '&#123;' }
escapeEntities: {},
},
},
],
Expand Down
16 changes: 16 additions & 0 deletions packages/gatsby-remark-prismjs/src/__tests__/escape-html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const escapeHtml = require(`../escape-html`)

describe(`escaping html entities`, () => {
it(`escapes base html when no additional escapes are passed in`, () => {
const code = `hello world&><"'`
expect(escapeHtml(code)).toBe(`hello world&amp;&gt;&lt;&quot;&#39;`)
})

it(`escapes additional html entities when passed in`, () => {
const code = `hello world{`
const mapping = {
"{": `&#123;`,
}
expect(escapeHtml(code, mapping)).toBe(`hello world&#123;`)
})
})
12 changes: 7 additions & 5 deletions packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int sum(a, b) {
}
`
expect(
highlightCode(language, code, lineNumbersHighlight)
highlightCode(language, code, {}, lineNumbersHighlight)
).toMatchSnapshot()
})

Expand Down Expand Up @@ -48,7 +48,7 @@ class Counter extends React.Component {

export default Counter
`
const processed = highlightCode(language, code, lineNumbersHighlight)
const processed = highlightCode(language, code, {}, lineNumbersHighlight)

expect(processed).toMatchSnapshot()
// expect spans to not contain \n as it would break line highlighting
Expand All @@ -62,7 +62,7 @@ export default Counter
const highlightCode = require(`../highlight-code`)
const language = `text`
const code = `<button />`
expect(highlightCode(language, code, [], true)).toMatch(
expect(highlightCode(language, code, {}, [], true)).toMatch(
`&lt;button /&gt;`
)
expect(console.warn).toHaveBeenCalledWith(
Expand Down Expand Up @@ -141,15 +141,17 @@ export default Counter
const language = `javascript`
const linesToHighlight = [1]
const code = `const a = 1\nconst b = 2`
expect(highlightCode(language, code, linesToHighlight)).not.toMatch(/\n$/)
expect(highlightCode(language, code, {}, linesToHighlight)).not.toMatch(
/\n$/
)
})

it(`a trailing newline is preserved`, () => {
const highlightCode = require(`../highlight-code`)
const language = `javascript`
const linesToHighlight = [1]
const code = `const a = 1\nconst b = 2\n`
expect(highlightCode(language, code, linesToHighlight)).toMatch(
expect(highlightCode(language, code, {}, linesToHighlight)).toMatch(
/[^\n]\n$/
)
})
Expand Down
26 changes: 26 additions & 0 deletions packages/gatsby-remark-prismjs/src/escape-html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = (code, additionalHtmlEscapes = {}) => {
const baseHTMLEscapes = {
"&": `&amp;`,
">": `&gt;`,
"<": `&lt;`,
'"': `&quot;`,
"'": `&#39;`,
}

const htmlEscapes = {
...additionalHtmlEscapes,
...baseHTMLEscapes,
}

const escapedChars = char => htmlEscapes[char]

const chars = Object.keys(htmlEscapes)

const charsRe = new RegExp(`[${chars.join()}]`, `g`)

const rehasUnescapedChars = new RegExp(charsRe.source)

return code && rehasUnescapedChars.test(code)
? code.replace(charsRe, escapedChars)
: code
}
5 changes: 3 additions & 2 deletions packages/gatsby-remark-prismjs/src/highlight-code.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const Prism = require(`prismjs`)
const _ = require(`lodash`)
const loadPrismLanguage = require(`./load-prism-language`)
const handleDirectives = require(`./directives`)
const escapeHTML = require(`./escape-html`)
const unsupportedLanguages = new Set()

module.exports = (
language,
code,
additionalEscapeCharacters = {},
lineNumbersHighlight = [],
noInlineHighlight = false
) => {
Expand Down Expand Up @@ -34,7 +35,7 @@ module.exports = (
console.warn(message, `applying generic code block`)
unsupportedLanguages.add(lang)
}
return _.escape(code)
return escapeHTML(code, additionalEscapeCharacters)
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/gatsby-remark-prismjs/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const visit = require(`unist-util-visit`)

const parseOptions = require(`./parse-options`)
const loadLanguageExtension = require(`./load-prism-language-extension`)
const highlightCode = require(`./highlight-code`)
Expand All @@ -20,6 +19,7 @@ module.exports = (
host: `localhost`,
global: false,
},
escapeEntities = {},
} = {}
) => {
const normalizeLanguage = lang => {
Expand Down Expand Up @@ -94,7 +94,7 @@ module.exports = (
+ `<pre${numLinesStyle} class="${className}${numLinesClass}">`
+ `<code class="${className}">`
+ `${useCommandLine ? commandLine(node.value, outputLines, promptUser, promptHost) : ``}`
+ `${highlightCode(languageName, node.value, highlightLines, noInlineHighlight)}`
+ `${highlightCode(languageName, node.value, escapeEntities, highlightLines, noInlineHighlight)}`
+ `</code>`
+ `${numLinesNumber}`
+ `</pre>`
Expand All @@ -118,7 +118,8 @@ module.exports = (
node.type = `html`
node.value = `<code class="${className}">${highlightCode(
languageName,
node.value
node.value,
escapeEntities
)}</code>`
})
}
Expand Down