@scalar/code-highlight 0.4.3 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"rehype-alert.js","names":[],"sources":["../../src/rehype-alert/rehype-alert.ts"],"sourcesContent":["import type { Element } from 'hast'\nimport type { Root } from 'mdast'\nimport { visit } from 'unist-util-visit'\n\nconst ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution', 'success'] as const\n\n// Simple whitespace check function\nfunction isWhitespace(node: any): boolean {\n return node.type === 'text' && typeof node.value === 'string' && /^\\s*$/.test(node.value)\n}\n\nexport function rehypeAlert() {\n return (tree: Root) => {\n visit(tree, 'element', (node: Element, index, parent: any) => {\n if (node.tagName !== 'blockquote' || typeof index !== 'number' || !parent || parent.type !== 'root') {\n return\n }\n\n // Find the first non-whitespace child\n const headIndex = node.children.findIndex((child) => !isWhitespace(child))\n if (headIndex === -1) {\n return\n }\n\n const head = node.children[headIndex]\n\n if (!head || head.type !== 'element' || head.tagName !== 'p') {\n return\n }\n\n const text = head.children[0]\n if (!text || text.type !== 'text' || !text.value.startsWith('[!')) {\n return\n }\n\n const end = text.value.indexOf(']')\n if (end === -1) {\n return\n }\n\n // Extract the alert type\n const alertType = text.value.slice(2, end).toLowerCase() as (typeof ALERT_TYPES)[number]\n if (!ALERT_TYPES.includes(alertType)) {\n return\n }\n\n // Remove the blockquote if it's empty\n if (end + 1 === text.value.length) {\n const next = head.children[1]\n if (next) {\n if (next.type !== 'element' || next.tagName !== 'br') {\n return\n }\n if (!head.children[2]) {\n return\n }\n head.children = head.children.slice(2)\n const node = head.children[0]\n if (node && node.type === 'text' && node.value.charAt(0) === '\\n') {\n node.value = node.value.slice(1)\n }\n } else {\n const skipped = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1])\n const nextIndex = skipped ? headIndex + 2 : headIndex + 1\n if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== 'element') {\n return\n }\n\n node.children = node.children.slice(nextIndex)\n }\n } else if (\n text.value.charAt(end + 1) === '\\n' &&\n // Check if the next character is a newline or a non-whitespace character\n (end + 2 === text.value.length || !/^\\s*$/.test(text.value.slice(end + 2)))\n ) {\n text.value = text.value.slice(end + 2)\n } else {\n // Remove the alert marker if it's not followed by a newline or a non-whitespace character\n text.value = text.value.replace(/^\\s*\\[!.*?\\]\\s*/, '')\n }\n\n // Extract content from paragraphs to avoid wrapping in <p> tags\n const contentChildren = []\n for (let i = headIndex; i < node.children.length; i++) {\n const child = node.children[i]\n if (child?.type === 'element' && child.tagName === 'p' && child.children) {\n contentChildren.push(...child.children)\n } else {\n contentChildren.push(child)\n }\n }\n\n // Replace the blockquote with a div containing the alert\n parent.children[index] = {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert', `markdown-alert-${alertType}`] },\n children: [\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-icon'] },\n children: [],\n },\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-content'] },\n children: [{ type: 'text', value: ' ' }, ...contentChildren],\n },\n ],\n }\n })\n }\n}\n"],"mappings":";;AAIA,IAAM,cAAc;CAAC;CAAQ;CAAO;CAAa;CAAW;CAAW;CAAU;AAGjF,SAAS,aAAa,MAAoB;AACxC,QAAO,KAAK,SAAS,UAAU,OAAO,KAAK,UAAU,YAAY,QAAQ,KAAK,KAAK,MAAM;;AAG3F,SAAgB,cAAc;AAC5B,SAAQ,SAAe;AACrB,QAAM,MAAM,YAAY,MAAe,OAAO,WAAgB;AAC5D,OAAI,KAAK,YAAY,gBAAgB,OAAO,UAAU,YAAY,CAAC,UAAU,OAAO,SAAS,OAC3F;GAIF,MAAM,YAAY,KAAK,SAAS,WAAW,UAAU,CAAC,aAAa,MAAM,CAAC;AAC1E,OAAI,cAAc,GAChB;GAGF,MAAM,OAAO,KAAK,SAAS;AAE3B,OAAI,CAAC,QAAQ,KAAK,SAAS,aAAa,KAAK,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AAS;AAC5B,QAAI,OAAO,SAAS,aAAa,MAAM,YAAY,OAAO,MAAM,SAC9D,iBAAgB,KAAK,GAAG,MAAM,SAAS;QAEvC,iBAAgB,KAAK,MAAM;;AAK/B,UAAO,SAAS,SAAS;IACvB,MAAM;IACN,SAAS;IACT,YAAY,EAAE,WAAW,CAAC,kBAAkB,kBAAkB,YAAY,EAAE;IAC5E,UAAU,CACR;KACE,MAAM;KACN,SAAS;KACT,YAAY,EAAE,WAAW,CAAC,sBAAsB,EAAE;KAClD,UAAU,EAAE;KACb,EACD;KACE,MAAM;KACN,SAAS;KACT,YAAY,EAAE,WAAW,CAAC,yBAAyB,EAAE;KACrD,UAAU,CAAC;MAAE,MAAM;MAAQ,OAAO;MAAK,EAAE,GAAG,gBAAgB;KAC7D,CACF;IACF;IACD"}
1
+ {"version":3,"file":"rehype-alert.js","names":[],"sources":["../../src/rehype-alert/rehype-alert.ts"],"sourcesContent":["import type { Element } from 'hast'\nimport type { Root } from 'mdast'\nimport { visit } from 'unist-util-visit'\n\nconst ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution', 'success'] as const\n\n// Simple whitespace check function\nfunction isWhitespace(node: any): boolean {\n return node.type === 'text' && typeof node.value === 'string' && /^\\s*$/.test(node.value)\n}\n\nexport function rehypeAlert() {\n return (tree: Root) => {\n visit(tree, 'element', (node: Element, index, parent: any) => {\n if (node.tagName !== 'blockquote' || typeof index !== 'number' || !parent || parent.type !== 'root') {\n return\n }\n\n // Find the first non-whitespace child\n const headIndex = node.children.findIndex((child) => !isWhitespace(child))\n if (headIndex === -1) {\n return\n }\n\n const head = node.children[headIndex]\n\n if (!head || head.type !== 'element' || head.tagName !== 'p') {\n return\n }\n\n const text = head.children[0]\n if (!text || text.type !== 'text' || !text.value.startsWith('[!')) {\n return\n }\n\n const end = text.value.indexOf(']')\n if (end === -1) {\n return\n }\n\n // Extract the alert type\n const alertType = text.value.slice(2, end).toLowerCase() as (typeof ALERT_TYPES)[number]\n if (!ALERT_TYPES.includes(alertType)) {\n return\n }\n\n // Remove the blockquote if it's empty\n if (end + 1 === text.value.length) {\n const next = head.children[1]\n if (next) {\n if (next.type !== 'element' || next.tagName !== 'br') {\n return\n }\n if (!head.children[2]) {\n return\n }\n head.children = head.children.slice(2)\n const node = head.children[0]\n if (node && node.type === 'text' && node.value.charAt(0) === '\\n') {\n node.value = node.value.slice(1)\n }\n } else {\n const skipped = headIndex + 1 < node.children.length && isWhitespace(node.children[headIndex + 1])\n const nextIndex = skipped ? headIndex + 2 : headIndex + 1\n if (nextIndex >= node.children.length || node.children[nextIndex]?.type !== 'element') {\n return\n }\n\n node.children = node.children.slice(nextIndex)\n }\n } else if (\n text.value.charAt(end + 1) === '\\n' &&\n // Check if the next character is a newline or a non-whitespace character\n (end + 2 === text.value.length || !/^\\s*$/.test(text.value.slice(end + 2)))\n ) {\n text.value = text.value.slice(end + 2)\n } else {\n // Remove the alert marker if it's not followed by a newline or a non-whitespace character\n text.value = text.value.replace(/^\\s*\\[!.*?\\]\\s*/, '')\n }\n\n // Extract content from paragraphs to avoid wrapping in <p> tags\n const contentChildren = []\n for (let i = headIndex; i < node.children.length; i++) {\n const child = node.children[i]\n if (child?.type === 'element' && child.tagName === 'p' && child.children) {\n contentChildren.push(...child.children)\n } else {\n contentChildren.push(child)\n }\n }\n\n // Replace the blockquote with a div containing the alert\n parent.children[index] = {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert', `markdown-alert-${alertType}`] },\n children: [\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-icon'] },\n children: [],\n },\n {\n type: 'element',\n tagName: 'div',\n properties: { className: ['markdown-alert-content'] },\n children: [{ type: 'text', value: ' ' }, ...contentChildren],\n },\n ],\n }\n })\n }\n}\n"],"mappings":";;AAIA,IAAM,cAAc;CAAC;CAAQ;CAAO;CAAa;CAAW;CAAW;AAAS;AAGhF,SAAS,aAAa,MAAoB;CACxC,OAAO,KAAK,SAAS,UAAU,OAAO,KAAK,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AGF,KAAK,WAAW,KAAK,SAAS,MAAM,SAAS;IAC/C;GACF,OAAO,IACL,KAAK,MAAM,OAAO,MAAM,CAAC,MAAM,SAE9B,MAAM,MAAM,KAAK,MAAM,UAAU,CAAC,QAAQ,KAAK,KAAK,MAAM,MAAM,MAAM,CAAC,CAAC,IAEzE,KAAK,QAAQ,KAAK,MAAM,MAAM,MAAM,CAAC;QAGrC,KAAK,QAAQ,KAAK,MAAM,QAAQ,mBAAmB,EAAE;GAIvD,MAAM,kBAAkB,CAAC;GACzB,KAAK,IAAI,IAAI,WAAW,IAAI,KAAK,SAAS,QAAQ,KAAK;IACrD,MAAM,QAAQ,KAAK,SAAS;IAC5B,IAAI,OAAO,SAAS,aAAa,MAAM,YAAY,OAAO,MAAM,UAC9D,gBAAgB,KAAK,GAAG,MAAM,QAAQ;SAEtC,gBAAgB,KAAK,KAAK;GAE9B;GAGA,OAAO,SAAS,SAAS;IACvB,MAAM;IACN,SAAS;IACT,YAAY,EAAE,WAAW,CAAC,kBAAkB,kBAAkB,WAAW,EAAE;IAC3E,UAAU,CACR;KACE,MAAM;KACN,SAAS;KACT,YAAY,EAAE,WAAW,CAAC,qBAAqB,EAAE;KACjD,UAAU,CAAC;IACb,GACA;KACE,MAAM;KACN,SAAS;KACT,YAAY,EAAE,WAAW,CAAC,wBAAwB,EAAE;KACpD,UAAU,CAAC;MAAE,MAAM;MAAQ,OAAO;KAAI,GAAG,GAAG,eAAe;IAC7D,CACF;GACF;EACF,CAAC;CACH;AACF"}
@@ -6,7 +6,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
9
+ var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
10
10
  var __exportAll = (all, no_symbols) => {
11
11
  let target = {};
12
12
  for (var name in all) __defProp(target, name, {
@@ -126,7 +126,14 @@ var lowlightLanguageMappings = {
126
126
  * @returns
127
127
  * A child (matching `test`, if given) or `undefined`.
128
128
  */
129
- var findAfter = (function(parent, index, test) {
129
+ var findAfter = (
130
+ /**
131
+ * @param {UnistParent} parent
132
+ * @param {UnistNode | number} index
133
+ * @param {Test} [test]
134
+ * @returns {UnistNode | undefined}
135
+ */
136
+ function(parent, index, test) {
130
137
  const is = convert(test);
131
138
  if (!parent || !parent.type || !parent.children) throw new Error("Expected parent node");
132
139
  if (typeof index === "number") {
@@ -203,7 +210,16 @@ var findAfter = (function(parent, index, test) {
203
210
  * When an incorrect `test`, `index`, or `parent` is given; there is no error
204
211
  * thrown when `element` is not a node or not an element.
205
212
  */
206
- var isElement = (function(element, test, index, parent, context) {
213
+ var isElement = (
214
+ /**
215
+ * @param {unknown} [element]
216
+ * @param {Test | undefined} [test]
217
+ * @param {number | null | undefined} [index]
218
+ * @param {Parents | null | undefined} [parent]
219
+ * @param {unknown} [context]
220
+ * @returns {boolean}
221
+ */
222
+ function(element, test, index, parent, context) {
207
223
  const check = convertElement(test);
208
224
  if (index !== null && index !== void 0 && (typeof index !== "number" || index < 0 || index === Number.POSITIVE_INFINITY)) throw new Error("Expected positive finite `index`");
209
225
  if (parent !== null && parent !== void 0 && (!parent.type || !parent.children)) throw new Error("Expected valid `parent`");
@@ -224,7 +240,12 @@ var isElement = (function(element, test, index, parent, context) {
224
240
  * @returns
225
241
  * A check.
226
242
  */
227
- var convertElement = (function(test) {
243
+ var convertElement = (
244
+ /**
245
+ * @param {Test | null | undefined} [test]
246
+ * @returns {Check}
247
+ */
248
+ function(test) {
228
249
  if (test === null || test === void 0) return element;
229
250
  if (typeof test === "string") return tagNameFactory(test);
230
251
  if (typeof test === "object") return anyFactory(test);
@@ -1081,6 +1102,7 @@ var core_default = (/* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exp
1081
1102
  begin: beginShebang,
1082
1103
  end: /$/,
1083
1104
  relevance: 0,
1105
+ /** @type {ModeCallback} */
1084
1106
  "on:begin": (m, resp) => {
1085
1107
  if (m.index !== 0) resp.ignoreMatch();
1086
1108
  }
@@ -1171,7 +1193,7 @@ var core_default = (/* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exp
1171
1193
  relevance: 0
1172
1194
  };
1173
1195
  var METHOD_GUARD = {
1174
- begin: "\\.\\s*" + UNDERSCORE_IDENT_RE,
1196
+ begin: "\\.\\s*[a-zA-Z_]\\w*",
1175
1197
  relevance: 0
1176
1198
  };
1177
1199
  /**
@@ -1183,15 +1205,17 @@ var core_default = (/* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exp
1183
1205
  */
1184
1206
  var END_SAME_AS_BEGIN = function(mode) {
1185
1207
  return Object.assign(mode, {
1208
+ /** @type {ModeCallback} */
1186
1209
  "on:begin": (m, resp) => {
1187
1210
  resp.data._beginMatch = m[1];
1188
1211
  },
1212
+ /** @type {ModeCallback} */
1189
1213
  "on:end": (m, resp) => {
1190
1214
  if (resp.data._beginMatch !== m[1]) resp.ignoreMatch();
1191
1215
  }
1192
1216
  });
1193
1217
  };
1194
- var MODES = /* @__PURE__ */ Object.freeze({
1218
+ var MODES = /*#__PURE__*/ Object.freeze({
1195
1219
  __proto__: null,
1196
1220
  APOS_STRING_MODE,
1197
1221
  BACKSLASH_ESCAPE,
@@ -1548,7 +1572,8 @@ var core_default = (/* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exp
1548
1572
  }
1549
1573
  compile() {
1550
1574
  if (this.regexes.length === 0) this.exec = () => null;
1551
- this.matcherRe = langRe(_rewriteBackreferences(this.regexes.map((el) => el[1]), { joinWith: "|" }), true);
1575
+ const terminators = this.regexes.map((el) => el[1]);
1576
+ this.matcherRe = langRe(_rewriteBackreferences(terminators, { joinWith: "|" }), true);
1552
1577
  this.lastIndex = 0;
1553
1578
  }
1554
1579
  /** @param {string} s */