Skip to content

Commit e09437f

Browse files
committed
escape HTML entities in code parsed from markdown, fixes #2744
1 parent 676936e commit e09437f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

plugin/markdown/markdown.esm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/markdown/markdown.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/markdown/plugin.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const SCRIPT_END_PLACEHOLDER = '__SCRIPT_END__';
1515

1616
const CODE_LINE_NUMBER_REGEX = /\[([\s\d,|-]*)\]/;
1717

18+
const HTML_ESCAPE_MAP = {
19+
'&': '&',
20+
'<': '&lt;',
21+
'>': '&gt;',
22+
'"': '&quot;',
23+
"'": '&#39;'
24+
};
25+
1826
const Plugin = () => {
1927

2028
// The reveal.js instance this plugin is attached to
@@ -399,6 +407,12 @@ const Plugin = () => {
399407

400408
}
401409

410+
function escapeForHTML( input ) {
411+
412+
return input.replace( /([&<>'"])/g, char => HTML_ESCAPE_MAP[char] );
413+
414+
}
415+
402416
return {
403417
id: 'markdown',
404418

@@ -427,6 +441,11 @@ const Plugin = () => {
427441
language = language.replace( CODE_LINE_NUMBER_REGEX, '' ).trim();
428442
}
429443

444+
// Escape before this gets injected into the DOM to
445+
// avoid having the HTML parser alter our code before
446+
// highlight.js is able to read it
447+
code = escapeForHTML( code );
448+
430449
return `<pre><code ${lineNumbers} class="${language}">${code}</code></pre>`;
431450
};
432451

0 commit comments

Comments
 (0)