Skip to content

Commit 151121c

Browse files
Markdown: Fixed markdown not working in NodeJS (#2977)
1 parent 3432b4b commit 151121c

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

components/prism-markdown.js

+57-6
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,66 @@
350350
});
351351
}
352352
} else {
353-
// get the textContent of the given env HTML
354-
var tempContainer = document.createElement('div');
355-
tempContainer.innerHTML = env.content;
356-
var code = tempContainer.textContent;
357-
358-
env.content = Prism.highlight(code, grammar, codeLang);
353+
env.content = Prism.highlight(textContent(env.content), grammar, codeLang);
359354
}
360355
});
361356

357+
var tagPattern = RegExp(Prism.languages.markup.tag.pattern.source, 'gi');
358+
359+
/**
360+
* A list of known entity names.
361+
*
362+
* This will always be incomplete to save space. The current list is the one used by lowdash's unescape function.
363+
*
364+
* @see {@link https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/unescape.js#L2}
365+
*/
366+
var KNOWN_ENTITY_NAMES = {
367+
'amp': '&',
368+
'lt': '<',
369+
'gt': '>',
370+
'quot': '"',
371+
};
372+
373+
// IE 11 doesn't support `String.fromCodePoint`
374+
var fromCodePoint = String.fromCodePoint || String.fromCharCode;
375+
376+
/**
377+
* Returns the text content of a given HTML source code string.
378+
*
379+
* @param {string} html
380+
* @returns {string}
381+
*/
382+
function textContent(html) {
383+
// remove all tags
384+
var text = html.replace(tagPattern, '');
385+
386+
// decode known entities
387+
text = text.replace(/&(\w{1,8}|#x?[\da-f]{1,8});/gi, function (m, code) {
388+
code = code.toLowerCase();
389+
390+
if (code[0] === '#') {
391+
var value;
392+
if (code[1] === 'x') {
393+
value = parseInt(code.slice(2), 16);
394+
} else {
395+
value = Number(code.slice(1));
396+
}
397+
398+
return fromCodePoint(value);
399+
} else {
400+
var known = KNOWN_ENTITY_NAMES[code];
401+
if (known) {
402+
return known;
403+
}
404+
405+
// unable to decode
406+
return m;
407+
}
408+
});
409+
410+
return text;
411+
}
412+
362413
Prism.languages.md = Prism.languages.markdown;
363414

364415
}(Prism));

components/prism-markdown.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/markdown/code-block_feature.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)