util.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. export function isNode() {
  2. /* globals module */
  3. return typeof module !== 'undefined' && typeof module.exports === 'object';
  4. }
  5. export function isWorker() {
  6. return typeof document === 'undefined' && typeof self !== 'undefined';
  7. }
  8. /**
  9. * Browser Only - Gets the language for this block of code
  10. *
  11. * @param {Element} block
  12. * @return {string|null}
  13. */
  14. export function getLanguageForBlock(block) {
  15. // If this doesn't have a language but the parent does then use that.
  16. //
  17. // This means if for example you have: <pre data-language="php">
  18. // with a bunch of <code> blocks inside then you do not have
  19. // to specify the language for each block.
  20. let language = block.getAttribute('data-language') || block.parentNode.getAttribute('data-language');
  21. // This adds support for specifying language via a CSS class.
  22. //
  23. // You can use the Google Code Prettify style: <pre class="lang-php">
  24. // or the HTML5 style: <pre><code class="language-php">
  25. if (!language) {
  26. const pattern = /\blang(?:uage)?-(\w+)/;
  27. const match = block.className.match(pattern) || block.parentNode.className.match(pattern);
  28. if (match) {
  29. language = match[1];
  30. }
  31. }
  32. if (language) {
  33. return language.toLowerCase();
  34. }
  35. return null;
  36. }
  37. /**
  38. * Determines if two different matches have complete overlap with each other
  39. *
  40. * @param {number} start1 start position of existing match
  41. * @param {number} end1 end position of existing match
  42. * @param {number} start2 start position of new match
  43. * @param {number} end2 end position of new match
  44. * @return {boolean}
  45. */
  46. export function hasCompleteOverlap(start1, end1, start2, end2) {
  47. // If the starting and end positions are exactly the same
  48. // then the first one should stay and this one should be ignored.
  49. if (start2 === start1 && end2 === end1) {
  50. return false;
  51. }
  52. return start2 <= start1 && end2 >= end1;
  53. }
  54. /**
  55. * Encodes < and > as html entities
  56. *
  57. * @param {string} code
  58. * @return {string}
  59. */
  60. export function htmlEntities(code) {
  61. return code.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/&(?![\w\#]+;)/g, '&amp;');
  62. }
  63. /**
  64. * Finds out the position of group match for a regular expression
  65. *
  66. * @see http://stackoverflow.com/questions/1985594/how-to-find-index-of-groups-in-match
  67. * @param {Object} match
  68. * @param {number} groupNumber
  69. * @return {number}
  70. */
  71. export function indexOfGroup(match, groupNumber) {
  72. let index = 0;
  73. for (let i = 1; i < groupNumber; ++i) {
  74. if (match[i]) {
  75. index += match[i].length;
  76. }
  77. }
  78. return index;
  79. }
  80. /**
  81. * Determines if a new match intersects with an existing one
  82. *
  83. * @param {number} start1 start position of existing match
  84. * @param {number} end1 end position of existing match
  85. * @param {number} start2 start position of new match
  86. * @param {number} end2 end position of new match
  87. * @return {boolean}
  88. */
  89. export function intersects(start1, end1, start2, end2) {
  90. if (start2 >= start1 && start2 < end1) {
  91. return true;
  92. }
  93. return end2 > start1 && end2 < end1;
  94. }
  95. /**
  96. * Sorts an objects keys by index descending
  97. *
  98. * @param {Object} object
  99. * @return {Array}
  100. */
  101. export function keys(object) {
  102. const locations = [];
  103. for (const location in object) {
  104. if (object.hasOwnProperty(location)) {
  105. locations.push(location);
  106. }
  107. }
  108. // numeric descending
  109. return locations.sort((a, b) => b - a);
  110. }
  111. /**
  112. * Substring replace call to replace part of a string at a certain position
  113. *
  114. * @param {number} position the position where the replacement
  115. * should happen
  116. * @param {string} replace the text we want to replace
  117. * @param {string} replaceWith the text we want to replace it with
  118. * @param {string} code the code we are doing the replacing in
  119. * @return {string}
  120. */
  121. export function replaceAtPosition(position, replace, replaceWith, code) {
  122. const subString = code.substr(position);
  123. // This is needed to fix an issue where $ signs do not render in the
  124. // highlighted code
  125. //
  126. // @see https://github.com/ccampbell/rainbow/issues/208
  127. replaceWith = replaceWith.replace(/\$/g, '$$$$')
  128. return code.substr(0, position) + subString.replace(replace, replaceWith);
  129. }
  130. /**
  131. * Creates a usable web worker from an anonymous function
  132. *
  133. * mostly borrowed from https://github.com/zevero/worker-create
  134. *
  135. * @param {Function} fn
  136. * @param {Prism} Prism
  137. * @return {Worker}
  138. */
  139. export function createWorker(fn, Prism) {
  140. if (isNode()) {
  141. /* globals global, require, __filename */
  142. global.Worker = require('webworker-threads').Worker;
  143. return new Worker(__filename);
  144. }
  145. const prismFunction = Prism.toString();
  146. let code = keys.toString();
  147. code += htmlEntities.toString();
  148. code += hasCompleteOverlap.toString();
  149. code += intersects.toString();
  150. code += replaceAtPosition.toString();
  151. code += indexOfGroup.toString();
  152. code += prismFunction;
  153. const fullString = `${code}\tthis.onmessage=${fn.toString()}`;
  154. const blob = new Blob([fullString], { type: 'text/javascript' });
  155. return new Worker((window.URL || window.webkitURL).createObjectURL(blob));
  156. }