diff --git a/packages/gatsby-remark-prismjs/README.md b/packages/gatsby-remark-prismjs/README.md index 600191a1d1920..4a3e5f0851e16 100644 --- a/packages/gatsby-remark-prismjs/README.md +++ b/packages/gatsby-remark-prismjs/README.md @@ -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: { '}': '{' } + escapeEntities: {}, }, }, ], diff --git a/packages/gatsby-remark-prismjs/src/__tests__/escape-html.js b/packages/gatsby-remark-prismjs/src/__tests__/escape-html.js new file mode 100644 index 0000000000000..549883af87880 --- /dev/null +++ b/packages/gatsby-remark-prismjs/src/__tests__/escape-html.js @@ -0,0 +1,26 @@ +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&><"'`) + }) + + it(`escapes additional html entities when passed in`, () => { + const code = `hello world{` + const mapping = { + "{": `{`, + } + expect(escapeHtml(code, mapping)).toBe(`hello world{`) + }) + + it(`escapes base html entities and additional html entities`, () => { + const code = `hello world&><"'{` + const mapping = { + "{": `{`, + } + expect(escapeHtml(code, mapping)).toBe( + `hello world&><"'{` + ) + }) +}) diff --git a/packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js b/packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js index 4928b422ea4b0..12758649854fc 100644 --- a/packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js +++ b/packages/gatsby-remark-prismjs/src/__tests__/highlight-code.js @@ -13,7 +13,7 @@ int sum(a, b) { } ` expect( - highlightCode(language, code, lineNumbersHighlight) + highlightCode(language, code, {}, lineNumbersHighlight) ).toMatchSnapshot() }) @@ -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 @@ -62,7 +62,7 @@ export default Counter const highlightCode = require(`../highlight-code`) const language = `text` const code = `