-
Notifications
You must be signed in to change notification settings - Fork 10.3k
feat(gatsby-remark-prism-js): add additional html escapes #20156
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
Merged
gatsbybot
merged 5 commits into
gatsbyjs:master
from
patricksimonian:feat/gatsby-remark-prismjs-html-escape
Dec 20, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d3e4ba6
add feature to provide additional html escape sequences
patricksimonian 92648b4
Merge remote-tracking branch 'upstream/master' into feat/gatsby-remar…
6c64c14
modify escapehtml routine to combine the functionality of lodash.escape
patricksimonian 0cede80
Merge branch 'feat/gatsby-remark-prismjs-html-escape' of https://gith…
patricksimonian 8883269
add test for base html escapes being used with additional escapes
patricksimonian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/gatsby-remark-prismjs/src/__tests__/escape-html.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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&><"'{` | ||
| ) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| module.exports = (code, additionalHtmlEscapes = {}) => { | ||
| const baseHTMLEscapes = { | ||
| "&": `&`, | ||
| ">": `>`, | ||
| "<": `<`, | ||
| '"': `"`, | ||
| "'": `'`, | ||
| } | ||
|
|
||
| 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) | ||
patricksimonian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return code && rehasUnescapedChars.test(code) | ||
| ? code.replace(charsRe, escapedChars) | ||
| : code | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.