-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add feature to provide additional html escape sequences
address test failures
- Loading branch information
1 parent
2b2adcd
commit d3e4ba6
Showing
6 changed files
with
49 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
16 changes: 16 additions & 0 deletions
16
packages/gatsby-remark-prismjs/src/__tests__/escape-html.js
This file contains 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,16 @@ | ||
const escapeHtml = require(`../escape-html`) | ||
|
||
describe(`escaping html entities`, () => { | ||
it(`returns code unchanged when no escapes are provided`, () => { | ||
const code = `hello world&` | ||
expect(escapeHtml(code)).toBe(code) | ||
}) | ||
|
||
it(`escapes html`, () => { | ||
const code = `&hello world&` | ||
const mapping = { | ||
"&": `&`, | ||
} | ||
expect(escapeHtml(code, mapping)).toBe(`&hello world&`) | ||
}) | ||
}) |
This file contains 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 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,15 @@ | ||
const isEmpty = require(`lodash/isEmpty`) | ||
|
||
module.exports = (code, additionalEscapeCharacters = {}) => { | ||
if (isEmpty(additionalEscapeCharacters)) return code | ||
const escapedChars = char => additionalEscapeCharacters[char] | ||
const chars = Object.keys(additionalEscapeCharacters) | ||
|
||
const charsRe = new RegExp(`[${chars.join()}]`, `g`) | ||
|
||
const rehasUnescapedChars = new RegExp(charsRe.source) | ||
|
||
return code && rehasUnescapedChars.test(code) | ||
? code.replace(charsRe, escapedChars) | ||
: code | ||
} |
This file contains 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 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