Skip to content

Commit

Permalink
escape HTML entities in code parsed from markdown, fixes hakimel#2744
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimel committed Sep 9, 2020
1 parent 676936e commit e09437f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugin/markdown/markdown.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion plugin/markdown/markdown.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions plugin/markdown/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const SCRIPT_END_PLACEHOLDER = '__SCRIPT_END__';

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

const HTML_ESCAPE_MAP = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
};

const Plugin = () => {

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

}

function escapeForHTML( input ) {

return input.replace( /([&<>'"])/g, char => HTML_ESCAPE_MAP[char] );

}

return {
id: 'markdown',

Expand Down Expand Up @@ -427,6 +441,11 @@ const Plugin = () => {
language = language.replace( CODE_LINE_NUMBER_REGEX, '' ).trim();
}

// Escape before this gets injected into the DOM to
// avoid having the HTML parser alter our code before
// highlight.js is able to read it
code = escapeForHTML( code );

return `<pre><code ${lineNumbers} class="${language}">${code}</code></pre>`;
};

Expand Down

0 comments on commit e09437f

Please sign in to comment.