Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown: Fixed markdown not working in NodeJS #2977

Merged
merged 1 commit into from Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 57 additions & 6 deletions components/prism-markdown.js
Expand Up @@ -350,15 +350,66 @@
});
}
} else {
// get the textContent of the given env HTML
var tempContainer = document.createElement('div');
tempContainer.innerHTML = env.content;
var code = tempContainer.textContent;

env.content = Prism.highlight(code, grammar, codeLang);
env.content = Prism.highlight(textContent(env.content), grammar, codeLang);
}
});

var tagPattern = RegExp(Prism.languages.markup.tag.pattern.source, 'gi');

/**
* A list of known entity names.
*
* This will always be incomplete to save space. The current list is the one used by lowdash's unescape function.
*
* @see {@link https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/unescape.js#L2}
*/
var KNOWN_ENTITY_NAMES = {
'amp': '&',
'lt': '<',
'gt': '>',
'quot': '"',
};

// IE 11 doesn't support `String.fromCodePoint`
var fromCodePoint = String.fromCodePoint || String.fromCharCode;

/**
* Returns the text content of a given HTML source code string.
*
* @param {string} html
* @returns {string}
*/
function textContent(html) {
// remove all tags
var text = html.replace(tagPattern, '');

// decode known entities
text = text.replace(/&(\w{1,8}|#x?[\da-f]{1,8});/gi, function (m, code) {
code = code.toLowerCase();

if (code[0] === '#') {
var value;
if (code[1] === 'x') {
value = parseInt(code.slice(2), 16);
} else {
value = Number(code.slice(1));
}

return fromCodePoint(value);
} else {
var known = KNOWN_ENTITY_NAMES[code];
if (known) {
return known;
}

// unable to decode
return m;
}
});

return text;
}

Prism.languages.md = Prism.languages.markdown;

}(Prism));
2 changes: 1 addition & 1 deletion components/prism-markdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/languages/markdown/code-block_feature.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.