prism-file-highlight.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. (function () {
  2. if (typeof self === 'undefined' || !self.Prism || !self.document) {
  3. return;
  4. }
  5. var Prism = window.Prism;
  6. var LOADING_MESSAGE = 'Loading…';
  7. var FAILURE_MESSAGE = function (status, message) {
  8. return '✖ Error ' + status + ' while fetching file: ' + message;
  9. };
  10. var FAILURE_EMPTY_MESSAGE = '✖ Error: File does not exist or is empty';
  11. var EXTENSIONS = {
  12. 'js': 'javascript',
  13. 'py': 'python',
  14. 'rb': 'ruby',
  15. 'ps1': 'powershell',
  16. 'psm1': 'powershell',
  17. 'sh': 'bash',
  18. 'bat': 'batch',
  19. 'h': 'c',
  20. 'tex': 'latex'
  21. };
  22. var STATUS_ATTR = 'data-src-status';
  23. var STATUS_LOADING = 'loading';
  24. var STATUS_LOADED = 'loaded';
  25. var STATUS_FAILED = 'failed';
  26. var SELECTOR = 'pre[data-src]:not([' + STATUS_ATTR + '="' + STATUS_LOADED + '"])'
  27. + ':not([' + STATUS_ATTR + '="' + STATUS_LOADING + '"])';
  28. var lang = /\blang(?:uage)?-([\w-]+)\b/i;
  29. /**
  30. * Sets the Prism `language-xxxx` or `lang-xxxx` class to the given language.
  31. *
  32. * @param {HTMLElement} element
  33. * @param {string} language
  34. * @returns {void}
  35. */
  36. function setLanguageClass(element, language) {
  37. var className = element.className;
  38. className = className.replace(lang, ' ') + ' language-' + language;
  39. element.className = className.replace(/\s+/g, ' ').trim();
  40. }
  41. Prism.hooks.add('before-highlightall', function (env) {
  42. env.selector += ', ' + SELECTOR;
  43. });
  44. Prism.hooks.add('before-sanity-check', function (env) {
  45. var pre = /** @type {HTMLPreElement} */ (env.element);
  46. if (pre.matches(SELECTOR)) {
  47. env.code = ''; // fast-path the whole thing and go to complete
  48. pre.setAttribute(STATUS_ATTR, STATUS_LOADING); // mark as loading
  49. // add code element with loading message
  50. var code = pre.appendChild(document.createElement('CODE'));
  51. code.textContent = LOADING_MESSAGE;
  52. var src = pre.getAttribute('data-src');
  53. var language = env.language;
  54. if (language === 'none') {
  55. // the language might be 'none' because there is no language set;
  56. // in this case, we want to use the extension as the language
  57. var extension = (/\.(\w+)$/.exec(src) || [, 'none'])[1];
  58. language = EXTENSIONS[extension] || extension;
  59. }
  60. // set language classes
  61. setLanguageClass(code, language);
  62. setLanguageClass(pre, language);
  63. // preload the language
  64. var autoloader = Prism.plugins.autoloader;
  65. if (autoloader) {
  66. autoloader.loadLanguages(language);
  67. }
  68. // load file
  69. var xhr = new XMLHttpRequest();
  70. xhr.open('GET', src, true);
  71. xhr.onreadystatechange = function () {
  72. if (xhr.readyState == 4) {
  73. if (xhr.status < 400 && xhr.responseText) {
  74. // mark as loaded
  75. pre.setAttribute(STATUS_ATTR, STATUS_LOADED);
  76. // highlight code
  77. code.textContent = xhr.responseText;
  78. Prism.highlightElement(code);
  79. } else {
  80. // mark as failed
  81. pre.setAttribute(STATUS_ATTR, STATUS_FAILED);
  82. if (xhr.status >= 400) {
  83. code.textContent = FAILURE_MESSAGE(xhr.status, xhr.statusText);
  84. } else {
  85. code.textContent = FAILURE_EMPTY_MESSAGE;
  86. }
  87. }
  88. }
  89. };
  90. xhr.send(null);
  91. }
  92. });
  93. Prism.plugins.fileHighlight = {
  94. /**
  95. * Executes the File Highlight plugin for all matching `pre` elements under the given container.
  96. *
  97. * Note: Elements which are already loaded or currently loading will not be touched by this method.
  98. *
  99. * @param {ParentNode} [container=document]
  100. */
  101. highlight: function highlight(container) {
  102. var elements = (container || document).querySelectorAll(SELECTOR);
  103. for (var i = 0, element; element = elements[i++];) {
  104. Prism.highlightElement(element);
  105. }
  106. }
  107. };
  108. var logged = false;
  109. /** @deprecated Use `Prism.plugins.fileHighlight.highlight` instead. */
  110. Prism.fileHighlight = function () {
  111. if (!logged) {
  112. console.warn('Prism.fileHighlight is deprecated. Use `Prism.plugins.fileHighlight.highlight` instead.');
  113. logged = true;
  114. }
  115. Prism.plugins.fileHighlight.highlight.apply(this, arguments);
  116. }
  117. })();