Skip to content

Commit

Permalink
Unescaped Markup: Refactoring (#2445)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Nov 4, 2020
1 parent 89ebb0b commit fc60282
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
62 changes: 40 additions & 22 deletions plugins/unescaped-markup/prism-unescaped-markup.js
Expand Up @@ -4,41 +4,59 @@
return;
}

// https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}


Prism.plugins.UnescapedMarkup = true;

Prism.hooks.add('before-highlightall', function (env) {
env.selector += ", [class*='lang-'] script[type='text/plain'], [class*='language-'] script[type='text/plain']" +
", script[type='text/plain'][class*='lang-'], script[type='text/plain'][class*='language-']";
env.selector += ', [class*="lang-"] script[type="text/plain"]'
+ ', [class*="language-"] script[type="text/plain"]'
+ ', script[type="text/plain"][class*="lang-"]'
+ ', script[type="text/plain"][class*="language-"]';
});

Prism.hooks.add('before-sanity-check', function (env) {
if ((env.element.matches || env.element.msMatchesSelector).call(env.element, "script[type='text/plain']")) {
var code = document.createElement("code");
var pre = document.createElement("pre");

pre.className = code.className = env.element.className;

if (env.element.dataset) {
Object.keys(env.element.dataset).forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(env.element.dataset, key)) {
pre.dataset[key] = env.element.dataset[key];
}
});
}
/** @type {HTMLElement} */
var element = env.element;

if (element.matches('script[type="text/plain"]')) {
// found a <script type="text/plain" ...> element
// we convert this element to a regular <pre><code> code block

env.code = env.code.replace(/&lt;\/script(>|&gt;)/gi, "</scri" + "pt>");
code.textContent = env.code;
var code = document.createElement('code');
var pre = document.createElement('pre');

// copy class name
pre.className = code.className = element.className;

// copy all "data-" attributes
var dataset = element.dataset;
Object.keys(dataset || {}).forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(dataset, key)) {
pre.dataset[key] = dataset[key];
}
});

code.textContent = env.code = env.code.replace(/&lt;\/script(?:>|&gt;)/gi, '</scri' + 'pt>');

// change DOM
pre.appendChild(code);
env.element.parentNode.replaceChild(pre, env.element);
element.parentNode.replaceChild(pre, element);
env.element = code;
return;
}

var pre = env.element.parentNode;
if (!env.code && pre && pre.nodeName.toLowerCase() == 'pre' &&
env.element.childNodes.length && env.element.childNodes[0].nodeName == "#comment") {
env.element.textContent = env.code = env.element.childNodes[0].textContent;
if (!env.code) {
// no code
var childNodes = element.childNodes;
if (childNodes.length === 1 && childNodes[0].nodeName == '#comment') {
// the only child is a comment -> use the comment's text
element.textContent = env.code = childNodes[0].textContent;
}
}
});
}());
2 changes: 1 addition & 1 deletion plugins/unescaped-markup/prism-unescaped-markup.min.js

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

0 comments on commit fc60282

Please sign in to comment.