Highlight.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default,
  21. highlightProps: () => highlightProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_utils = require("../utils");
  26. const [name, bem] = (0, import_utils.createNamespace)("highlight");
  27. const highlightProps = {
  28. autoEscape: import_utils.truthProp,
  29. caseSensitive: Boolean,
  30. highlightClass: String,
  31. highlightTag: (0, import_utils.makeStringProp)("span"),
  32. keywords: (0, import_utils.makeRequiredProp)([String, Array]),
  33. sourceString: (0, import_utils.makeStringProp)(""),
  34. tag: (0, import_utils.makeStringProp)("div"),
  35. unhighlightClass: String,
  36. unhighlightTag: (0, import_utils.makeStringProp)("span")
  37. };
  38. var stdin_default = (0, import_vue.defineComponent)({
  39. name,
  40. props: highlightProps,
  41. setup(props) {
  42. const highlightChunks = (0, import_vue.computed)(() => {
  43. const {
  44. autoEscape,
  45. caseSensitive,
  46. keywords,
  47. sourceString
  48. } = props;
  49. const flags = caseSensitive ? "g" : "gi";
  50. const _keywords = Array.isArray(keywords) ? keywords : [keywords];
  51. let chunks = _keywords.filter((keyword) => keyword).reduce((chunks2, keyword) => {
  52. if (autoEscape) {
  53. keyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  54. }
  55. const regex = new RegExp(keyword, flags);
  56. let match;
  57. while (match = regex.exec(sourceString)) {
  58. const start = match.index;
  59. const end = regex.lastIndex;
  60. if (start >= end) {
  61. regex.lastIndex++;
  62. continue;
  63. }
  64. chunks2.push({
  65. start,
  66. end,
  67. highlight: true
  68. });
  69. }
  70. return chunks2;
  71. }, []);
  72. chunks = chunks.sort((a, b) => a.start - b.start).reduce((chunks2, currentChunk) => {
  73. const prevChunk = chunks2[chunks2.length - 1];
  74. if (!prevChunk || currentChunk.start > prevChunk.end) {
  75. const unhighlightStart = prevChunk ? prevChunk.end : 0;
  76. const unhighlightEnd = currentChunk.start;
  77. if (unhighlightStart !== unhighlightEnd) {
  78. chunks2.push({
  79. start: unhighlightStart,
  80. end: unhighlightEnd,
  81. highlight: false
  82. });
  83. }
  84. chunks2.push(currentChunk);
  85. } else {
  86. prevChunk.end = Math.max(prevChunk.end, currentChunk.end);
  87. }
  88. return chunks2;
  89. }, []);
  90. const lastChunk = chunks[chunks.length - 1];
  91. if (!lastChunk) {
  92. chunks.push({
  93. start: 0,
  94. end: sourceString.length,
  95. highlight: false
  96. });
  97. }
  98. if (lastChunk && lastChunk.end < sourceString.length) {
  99. chunks.push({
  100. start: lastChunk.end,
  101. end: sourceString.length,
  102. highlight: false
  103. });
  104. }
  105. return chunks;
  106. });
  107. const renderContent = () => {
  108. const {
  109. sourceString,
  110. highlightClass,
  111. unhighlightClass,
  112. highlightTag,
  113. unhighlightTag
  114. } = props;
  115. return highlightChunks.value.map((chunk) => {
  116. const {
  117. start,
  118. end,
  119. highlight
  120. } = chunk;
  121. const text = sourceString.slice(start, end);
  122. if (highlight) {
  123. return (0, import_vue.createVNode)(highlightTag, {
  124. "class": [bem("tag"), highlightClass]
  125. }, {
  126. default: () => [text]
  127. });
  128. }
  129. return (0, import_vue.createVNode)(unhighlightTag, {
  130. "class": unhighlightClass
  131. }, {
  132. default: () => [text]
  133. });
  134. });
  135. };
  136. return () => {
  137. const {
  138. tag
  139. } = props;
  140. return (0, import_vue.createVNode)(tag, {
  141. "class": bem()
  142. }, {
  143. default: () => [renderContent()]
  144. });
  145. };
  146. }
  147. });