@scalar/code-highlight 0.4.16 → 0.4.17

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.
@@ -43,41 +43,27 @@ var lowlightLanguageMappings = {
43
43
  "c++": "cpp",
44
44
  "node": "javascript"
45
45
  };
46
- // ../../node_modules/.pnpm/hast-util-to-text@4.0.3/node_modules/hast-util-to-text/lib/index.js
46
+ // ../../node_modules/.pnpm/hast-util-to-text@4.0.5/node_modules/hast-util-to-text/lib/index.js
47
47
  /**
48
48
  * @import {Comment, Element, Nodes, Parents, Text} from 'hast'
49
49
  */
50
50
  /**
51
- * @typedef {BreakForce | BreakNumber | BreakValue | undefined} BreakAfter
52
- * Any value for a break after.
53
- */
54
- /**
55
- * @typedef {BreakNumber | BreakValue | undefined} BreakBefore
56
- * Any value for a break before.
57
- */
58
- /**
59
- * @typedef {'\n'} BreakForce
51
+ * @typedef BreakForce
60
52
  * Forced break.
53
+ * @property {'\t' | '\n'} value
54
+ * Value.
61
55
  */
62
56
  /**
63
57
  * @typedef {0 | 1 | 2} BreakNumber
64
58
  * Specific break:
65
59
  *
66
- * * `0` — space
60
+ * * `0` — collapsible space
67
61
  * * `1` — line ending
68
62
  * * `2` — blank line
69
63
  */
70
64
  /**
71
- * @typedef {boolean} BreakValue
72
- * Whether there was a break.
73
- */
74
- /**
75
65
  * @typedef CollectionInfo
76
66
  * Info on current collection.
77
- * @property {BreakAfter} breakAfter
78
- * Whether there was a break after.
79
- * @property {BreakBefore} breakBefore
80
- * Whether there was a break before.
81
67
  * @property {boolean | undefined} [rowAfter]
82
68
  * Whether a row comes later in the nearest table, after the current node or
83
69
  * after the row group it is in.
@@ -85,6 +71,10 @@ var lowlightLanguageMappings = {
85
71
  * Current whitespace setting.
86
72
  */
87
73
  /**
74
+ * @typedef {BreakForce | BreakNumber | string} Item
75
+ * Item in a collection.
76
+ */
77
+ /**
88
78
  * @typedef Options
89
79
  * Configuration.
90
80
  * @property {Whitespace | null | undefined} [whitespace='normal']
@@ -94,8 +84,15 @@ var lowlightLanguageMappings = {
94
84
  * @typedef {'normal' | 'nowrap' | 'pre' | 'pre-wrap'} Whitespace
95
85
  * Valid and useful whitespace values (from CSS).
96
86
  */
87
+ /** @type {BreakForce} */
88
+ var forcedLineFeed = { value: "\n" };
89
+ /** @type {BreakForce} */
90
+ var forcedTab = { value: " " };
97
91
  var searchLineFeeds = /\n/g;
98
92
  var searchTabOrSpaces = /[\t ]+/g;
93
+ var startsWithTabOrSpace = /^[\t ]/;
94
+ var endsWithTabOrSpace = /[\t ]$/;
95
+ var trimTabOrSpaces = /^[\t ]+|[\t ]+$/g;
99
96
  var whitespaceOnly = /^[\t\n\f\r ]+$/;
100
97
  var tablePartNames = /* @__PURE__ */ new Set([
101
98
  "caption",
@@ -198,51 +195,37 @@ var blockOrCaptionNames = /* @__PURE__ */ new Set([
198
195
  function toText(tree, options) {
199
196
  const options_ = options || {};
200
197
  const children = "children" in tree ? tree.children : [];
201
- let block = false;
198
+ const inline = tree.type === "element" && tree.tagName !== "p" && tree.tagName !== "table" && !blockOrCaptionNames.has(tree.tagName) && !tablePartNames.has(tree.tagName);
202
199
  let inTable = false;
203
- if (tree.type === "element") {
204
- block = blockOrCaptionNames.has(tree.tagName);
205
- inTable = tree.tagName === "table" || rowGroup(tree);
206
- }
207
- const whitespace = inferWhitespace(tree, {
208
- breakAfter: false,
209
- breakBefore: false,
210
- whitespace: options_.whitespace || "normal"
211
- });
212
- /** @type {Array<BreakNumber | string>} */
200
+ if (tree.type === "element") inTable = tree.tagName === "table" || rowGroup(tree);
201
+ const whitespace = inferWhitespace(tree, { whitespace: options_.whitespace || "normal" });
202
+ /** @type {Array<Item>} */
213
203
  const results = [];
214
- if (tree.type === "text" || tree.type === "comment") append(results, collectText(tree, {
215
- breakAfter: true,
216
- breakBefore: true,
204
+ if (tree.type === "text" || tree.type === "comment") append(results, collectText(tree));
205
+ let index = -1;
206
+ while (++index < children.length) append(results, renderedTextCollection(children[index], index, tree, {
207
+ rowAfter: inTable ? inferRowAfter(tree, index, void 0) : void 0,
217
208
  whitespace
218
209
  }));
219
- let index = -1;
220
- while (++index < children.length) {
221
- const next = children[index + 1];
222
- const breakAfter = next ? next.type === "element" && next.tagName === "br" : block;
223
- const breakBefore = index ? void 0 : block;
224
- append(results, renderedTextCollection(children[index], index, tree, {
225
- breakAfter,
226
- breakBefore,
227
- rowAfter: inTable ? inferRowAfter(tree, index, void 0) : void 0,
228
- whitespace
229
- }));
230
- }
210
+ let force = false;
231
211
  /** @type {Array<string>} */
232
212
  const result = [];
233
213
  /** @type {number | undefined} */
234
- let count;
214
+ let count = inline ? -1 : void 0;
235
215
  index = -1;
236
216
  while (++index < results.length) {
237
217
  const value = results[index];
238
218
  if (typeof value === "number") {
239
219
  if (count !== void 0 && value > count) count = value;
240
220
  } else if (value) {
241
- if (count !== void 0 && count > -1) result.push("\n".repeat(count) || " ");
221
+ const forceBefore = force;
222
+ force = typeof value === "object";
223
+ if (count !== void 0 && count > (force || forceBefore ? 0 : -1) && (result.length > 0 || count === 0)) result.push("\n".repeat(count) || " ");
242
224
  count = -1;
243
- result.push(value);
225
+ result.push(typeof value === "object" ? value.value : value);
244
226
  }
245
227
  }
228
+ if (inline && count === 0 && !force) result.push(" ");
246
229
  return result.join("");
247
230
  }
248
231
  /**
@@ -256,7 +239,7 @@ function toText(tree, options) {
256
239
  * Parent of `node`.
257
240
  * @param {CollectionInfo} info
258
241
  * Info on current collection.
259
- * @returns {Array<BreakNumber | string>}
242
+ * @returns {Array<Item>}
260
243
  * Items.
261
244
  */
262
245
  function renderedTextCollection(node, index, parent, info) {
@@ -267,7 +250,7 @@ function renderedTextCollection(node, index, parent, info) {
267
250
  const after = parent.children[index + 1];
268
251
  if ((!before || before.type === "element" && tablePartNames.has(before.tagName)) && (!after || after.type === "element" && tablePartNames.has(after.tagName))) return [];
269
252
  }
270
- return info.whitespace === "normal" ? collectText(node, info) : collectPreText(node);
253
+ return info.whitespace === "normal" ? collectText(node) : collectPreText(node);
271
254
  }
272
255
  return [];
273
256
  }
@@ -282,7 +265,7 @@ function renderedTextCollection(node, index, parent, info) {
282
265
  * Parent of `node`.
283
266
  * @param {CollectionInfo} info
284
267
  * Info on current collection.
285
- * @returns {Array<BreakNumber | string>}
268
+ * @returns {Array<Item>}
286
269
  * Items.
287
270
  */
288
271
  function collectElement(node, position, parent, info) {
@@ -290,15 +273,15 @@ function collectElement(node, position, parent, info) {
290
273
  const children = node.children || [];
291
274
  const inTable = node.tagName === "table" || rowGroup(node);
292
275
  let index = -1;
293
- /** @type {Array<BreakNumber | string>} */
276
+ /** @type {Array<Item>} */
294
277
  const items = [];
295
278
  if (notRenderedNames.has(node.tagName) || node.properties?.hidden || node.tagName === "dialog" && !node.properties?.open) return items;
296
279
  /** @type {BreakNumber | undefined} */
297
280
  let prefix;
298
281
  /** @type {BreakForce | BreakNumber | undefined} */
299
282
  let suffix;
300
- if (node.tagName === "br") suffix = "\n";
301
- else if (row(node) && (info.rowAfter || after(parent, position, row))) suffix = "\n";
283
+ if (node.tagName === "br") suffix = forcedLineFeed;
284
+ else if (row(node) && (info.rowAfter || after(parent, position, row))) suffix = forcedLineFeed;
302
285
  else if (node.tagName === "p") {
303
286
  prefix = 2;
304
287
  suffix = 2;
@@ -306,18 +289,11 @@ function collectElement(node, position, parent, info) {
306
289
  prefix = 1;
307
290
  suffix = 1;
308
291
  }
309
- while (++index < children.length) {
310
- const next = children[index + 1];
311
- const breakAfter = next ? next.type === "element" && next.tagName === "br" : suffix;
312
- const breakBefore = index ? void 0 : prefix;
313
- append(items, renderedTextCollection(children[index], index, node, {
314
- breakAfter,
315
- breakBefore,
316
- rowAfter: inTable ? inferRowAfter(node, index, info.rowAfter) : void 0,
317
- whitespace
318
- }));
319
- }
320
- if (cell(node) && after(parent, position, cell)) items.push(" ");
292
+ while (++index < children.length) append(items, renderedTextCollection(children[index], index, node, {
293
+ rowAfter: inTable ? inferRowAfter(node, index, info.rowAfter) : void 0,
294
+ whitespace
295
+ }));
296
+ if (cell(node) && after(parent, position, cell)) items.push(forcedTab);
321
297
  if (prefix) items.unshift(prefix);
322
298
  if (suffix) items.push(suffix);
323
299
  return items;
@@ -341,23 +317,22 @@ function collectElement(node, position, parent, info) {
341
317
  *
342
318
  * @param {Comment | Text} node
343
319
  * Text node.
344
- * @param {CollectionInfo} info
345
- * Info on current collection.
346
- * @returns {Array<BreakNumber | string>}
320
+ * @returns {Array<Item>}
347
321
  * Result.
348
322
  */
349
- function collectText(node, info) {
350
- const value = String(node.value);
323
+ function collectText(node) {
324
+ const value = String(node.value).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, "");
351
325
  /** @type {Array<string>} */
352
326
  const lines = [];
353
- /** @type {Array<BreakNumber | string>} */
327
+ /** @type {Array<Item>} */
354
328
  const result = [];
355
329
  let start = 0;
330
+ if (startsWithTabOrSpace.test(value)) result.push(0);
356
331
  while (start <= value.length) {
357
332
  searchLineFeeds.lastIndex = start;
358
333
  const match = searchLineFeeds.exec(value);
359
334
  const end = match && "index" in match ? match.index : value.length;
360
- lines.push(trimAndCollapseSpacesAndTabs(value.slice(start, end).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ""), start === 0 ? info.breakBefore : true, end === value.length ? info.breakAfter : true));
335
+ lines.push(trimAndCollapseSpacesAndTabs(value.slice(start, end)));
361
336
  start = end + 1;
362
337
  }
363
338
  let index = -1;
@@ -371,6 +346,7 @@ function collectText(node, info) {
371
346
  result.push(lines[index]);
372
347
  join = 0;
373
348
  } else if (index === 0 || index === lines.length - 1) result.push(0);
349
+ if (endsWithTabOrSpace.test(value)) result.push(0);
374
350
  return result;
375
351
  }
376
352
  /**
@@ -378,7 +354,7 @@ function collectText(node, info) {
378
354
  *
379
355
  * @param {Text} node
380
356
  * Text node.
381
- * @returns {Array<BreakNumber | string>}
357
+ * @returns {Array<Item>}
382
358
  * Result.
383
359
  */
384
360
  function collectPreText(node) {
@@ -392,31 +368,16 @@ function collectPreText(node) {
392
368
  * context—is collapsed to have zero advance width. (It is invisible,
393
369
  * but retains its soft wrap opportunity, if any.)
394
370
  *
371
+ * Note: spaces at the start and end of the node are handled in
372
+ * `collectText`.
373
+ *
395
374
  * @param {string} value
396
375
  * Value to collapse.
397
- * @param {BreakBefore} breakBefore
398
- * Whether there was a break before.
399
- * @param {BreakAfter} breakAfter
400
- * Whether there was a break after.
401
376
  * @returns {string}
402
377
  * Result.
403
378
  */
404
- function trimAndCollapseSpacesAndTabs(value, breakBefore, breakAfter) {
405
- /** @type {Array<string>} */
406
- const result = [];
407
- let start = 0;
408
- /** @type {number | undefined} */
409
- let end;
410
- while (start < value.length) {
411
- searchTabOrSpaces.lastIndex = start;
412
- const match = searchTabOrSpaces.exec(value);
413
- end = match ? match.index : value.length;
414
- if (!start && !end && match && !breakBefore) result.push("");
415
- if (start !== end) result.push(value.slice(start, end));
416
- start = match ? end + match[0].length : end;
417
- }
418
- if (start !== end && !breakAfter) result.push("");
419
- return result.join(" ");
379
+ function trimAndCollapseSpacesAndTabs(value) {
380
+ return value.replace(trimTabOrSpaces, "").replace(searchTabOrSpaces, " ");
420
381
  }
421
382
  /**
422
383
  * Figure out the whitespace of a node.