prism-unescaped-markup.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. (function () {
  2. if (typeof self === 'undefined' || !self.Prism || !self.document) {
  3. return;
  4. }
  5. Prism.plugins.UnescapedMarkup = true;
  6. Prism.hooks.add('before-highlightall', function (env) {
  7. env.selector += ", [class*='lang-'] script[type='text/plain'], [class*='language-'] script[type='text/plain']" +
  8. ", script[type='text/plain'][class*='lang-'], script[type='text/plain'][class*='language-']";
  9. });
  10. Prism.hooks.add('before-sanity-check', function (env) {
  11. if ((env.element.matches || env.element.msMatchesSelector).call(env.element, "script[type='text/plain']")) {
  12. var code = document.createElement("code");
  13. var pre = document.createElement("pre");
  14. pre.className = code.className = env.element.className;
  15. if (env.element.dataset) {
  16. Object.keys(env.element.dataset).forEach(function (key) {
  17. if (Object.prototype.hasOwnProperty.call(env.element.dataset, key)) {
  18. pre.dataset[key] = env.element.dataset[key];
  19. }
  20. });
  21. }
  22. env.code = env.code.replace(/&lt;\/script(>|&gt;)/gi, "</scri" + "pt>");
  23. code.textContent = env.code;
  24. pre.appendChild(code);
  25. env.element.parentNode.replaceChild(pre, env.element);
  26. env.element = code;
  27. return;
  28. }
  29. var pre = env.element.parentNode;
  30. if (!env.code && pre && pre.nodeName.toLowerCase() == 'pre' &&
  31. env.element.childNodes.length && env.element.childNodes[0].nodeName == "#comment") {
  32. env.element.textContent = env.code = env.element.childNodes[0].textContent;
  33. }
  34. });
  35. }());