-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape Nunjucks for special tags (#1049)
* Allow nunjucks that is called multiple times to support escaping for special tags * Remove the need to prune raw tags, simplify logic of replacement * Fix style and remove nested nunjucks escape test
- Loading branch information
Showing
9 changed files
with
124 additions
and
33 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
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
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,14 @@ | ||
const START_ESCAPE_STR = '{% raw %}'; | ||
const END_ESCAPE_STR = '{% endraw %}'; | ||
const REGEX = new RegExp('{% *raw *%}(.*?){% *endraw *%}', 'gs'); | ||
|
||
function preEscapeRawTags(pageData) { | ||
return pageData.replace(REGEX, `${START_ESCAPE_STR}$&${END_ESCAPE_STR}`); | ||
} | ||
|
||
module.exports = { | ||
renderEscaped(nunjucks, pageData, variableMap = {}, options = {}) { | ||
const escapedPage = preEscapeRawTags(pageData); | ||
return nunjucks.renderString(escapedPage, variableMap, options); | ||
}, | ||
}; |
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
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,23 @@ | ||
const nunjucks = require('nunjucks'); | ||
const nunjuckUtils = require('../../src/lib/markbind/src/utils/nunjuckUtils'); | ||
|
||
test('Escaping nunjucks raw tags', () => { | ||
const escapedString = 'This is a content with escaped data {%raw%} CONTENT {%endraw%}'; | ||
const escapedContent = nunjuckUtils.renderEscaped(nunjucks, escapedString); | ||
|
||
expect(escapedContent).toBe(escapedString); | ||
}); | ||
|
||
test('Escaping nunjucks with new lines', () => { | ||
const escapedString = 'This is a content with escaped data\n {%raw%} \nCONTENT\n {%endraw%}'; | ||
const escapedContent = nunjuckUtils.renderEscaped(nunjucks, escapedString); | ||
|
||
expect(escapedContent).toBe(escapedString); | ||
}); | ||
|
||
test('Escaping multiple nunjucks raw tags', () => { | ||
const escapedString = 'Multiple escapes: {%raw%} first {%endraw%} {%raw%} second {%endraw%}'; | ||
const escapedContent = nunjuckUtils.renderEscaped(nunjucks, escapedString); | ||
|
||
expect(escapedContent).toBe(escapedString); | ||
}); |