svelte-highlight 7.18.0 → 7.18.1

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.
Files changed (2) hide show
  1. package/package.json +1 -4
  2. package/preprocessor.js +162 -11
package/package.json CHANGED
@@ -1,13 +1,10 @@
1
1
  {
2
2
  "name": "svelte-highlight",
3
- "version": "7.18.0",
3
+ "version": "7.18.1",
4
4
  "license": "MIT",
5
5
  "description": "Svelte component library for highlighting code using highlight.js",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
7
7
  "type": "module",
8
- "dependencies": {
9
- "magic-string": "^0.30.17"
10
- },
11
8
  "repository": {
12
9
  "type": "git",
13
10
  "url": "git+https://github.com/metonym/svelte-highlight.git"
package/preprocessor.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import path from "node:path";
2
2
  import { pathToFileURL } from "node:url";
3
- import MagicString from "magic-string";
4
3
  import { parse } from "svelte/compiler";
5
4
  import { ensureRegistered, registry } from "./registry.js";
6
5
 
@@ -252,6 +251,151 @@ function renderStatic(language, code) {
252
251
  );
253
252
  }
254
253
 
254
+ const BASE64_VLQ_CHARS =
255
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
256
+
257
+ /** @param {number} value */
258
+ function encodeVlq(value) {
259
+ let vlq = value < 0 ? (-value << 1) + 1 : value << 1;
260
+ let result = "";
261
+ do {
262
+ let digit = vlq & 31;
263
+ vlq >>>= 5;
264
+ if (vlq > 0) digit |= 32;
265
+ result += BASE64_VLQ_CHARS[digit];
266
+ } while (vlq > 0);
267
+ return result;
268
+ }
269
+
270
+ /** @param {string} content */
271
+ function computeLineStarts(content) {
272
+ const starts = [0];
273
+ for (let i = 0; i < content.length; i += 1) {
274
+ if (content[i] === "\n") starts.push(i + 1);
275
+ }
276
+ return starts;
277
+ }
278
+
279
+ /**
280
+ * @param {number[]} lineStarts
281
+ * @param {number} index
282
+ */
283
+ function positionAt(lineStarts, index) {
284
+ let lo = 0;
285
+ let hi = lineStarts.length - 1;
286
+ while (lo < hi) {
287
+ const mid = (lo + hi + 1) >> 1;
288
+ if ((lineStarts[mid] ?? 0) <= index) lo = mid;
289
+ else hi = mid - 1;
290
+ }
291
+ return { line: lo, column: index - (lineStarts[lo] ?? 0) };
292
+ }
293
+
294
+ /**
295
+ * Splices non-overlapping replacements into `content` and produces a matching
296
+ * (source-map v3) sourcemap. Every edit here replaces a whole element with a
297
+ * self-contained HTML string, so there's nothing finer-grained to map inside
298
+ * a replacement - one segment per edit boundary is as accurate as mapping
299
+ * every character, and far cheaper to produce.
300
+ *
301
+ * @param {string} content
302
+ * @param {{ start: number; end: number; replacement: string }[]} edits sorted by `start`, non-overlapping
303
+ */
304
+ function applyEdits(content, edits) {
305
+ const lineStarts = computeLineStarts(content);
306
+ /** @type {string[]} */
307
+ const out = [];
308
+ /** @type {{ genCol: number; line: number; col: number }[][]} */
309
+ const segmentsByLine = [[]];
310
+
311
+ let genLine = 0;
312
+ let genCol = 0;
313
+
314
+ /**
315
+ * @param {number} line
316
+ * @param {number} col
317
+ */
318
+ function mark(line, col) {
319
+ segmentsByLine[genLine]?.push({ genCol, line, col });
320
+ }
321
+
322
+ /**
323
+ * Appends `text` to the output, advancing genLine/genCol across any
324
+ * newlines it contains. When `origLine` is given (unedited text only -
325
+ * a replacement's interior lines have no original counterpart), marks
326
+ * the start of each new line at column 0 against the next original line.
327
+ *
328
+ * @param {string} text
329
+ * @param {number} [origLine]
330
+ */
331
+ function advance(text, origLine) {
332
+ let from = 0;
333
+ let line = origLine ?? 0;
334
+ for (let i = 0; i < text.length; i += 1) {
335
+ if (text[i] !== "\n") continue;
336
+ out.push(text.slice(from, i + 1));
337
+ from = i + 1;
338
+ genLine += 1;
339
+ genCol = 0;
340
+ segmentsByLine.push([]);
341
+ if (origLine !== undefined) {
342
+ line += 1;
343
+ mark(line, 0);
344
+ }
345
+ }
346
+ out.push(text.slice(from));
347
+ genCol += text.length - from;
348
+ }
349
+
350
+ /**
351
+ * @param {number} start
352
+ * @param {number} end
353
+ */
354
+ function copyUnedited(start, end) {
355
+ if (start === end) return;
356
+ const pos = positionAt(lineStarts, start);
357
+ mark(pos.line, pos.column);
358
+ advance(content.slice(start, end), pos.line);
359
+ }
360
+
361
+ let cursor = 0;
362
+ for (const edit of edits) {
363
+ copyUnedited(cursor, edit.start);
364
+
365
+ const pos = positionAt(lineStarts, edit.start);
366
+ mark(pos.line, pos.column);
367
+ advance(edit.replacement);
368
+
369
+ cursor = edit.end;
370
+ }
371
+ copyUnedited(cursor, content.length);
372
+
373
+ let mappings = "";
374
+ let prevLine = 0;
375
+ let prevCol = 0;
376
+ for (const [lineIndex, segments] of segmentsByLine.entries()) {
377
+ if (lineIndex > 0) mappings += ";";
378
+ let prevGenCol = 0;
379
+ for (let i = 0; i < segments.length; i += 1) {
380
+ const segment = segments[i];
381
+ if (!segment) continue;
382
+ mappings += i === 0 ? "" : ",";
383
+ mappings += encodeVlq(segment.genCol - prevGenCol);
384
+ mappings += encodeVlq(0); // single source, index never changes
385
+ mappings += encodeVlq(segment.line - prevLine);
386
+ mappings += encodeVlq(segment.col - prevCol);
387
+ prevGenCol = segment.genCol;
388
+ prevLine = segment.line;
389
+ prevCol = segment.col;
390
+ }
391
+ }
392
+
393
+ return {
394
+ code: out.join(""),
395
+ map: { version: 3, sources: [""], names: [], mappings },
396
+ };
397
+ }
398
+
255
399
  /**
256
400
  * @typedef {{
257
401
  * onWarn?: (message: string, details: { filename?: string | undefined; line: number; cause: unknown }) => void;
@@ -348,27 +492,34 @@ export function highlightStatic(options = {}) {
348
492
  }),
349
493
  );
350
494
 
351
- const ms = new MagicString(content);
352
- let changed = false;
495
+ /** @type {{ start: number; end: number; replacement: string }[]} */
496
+ const edits = [];
353
497
 
354
498
  for (const [index, match] of matches.entries()) {
355
499
  const html = htmlByMatch[index];
356
500
  if (!html) continue;
357
501
 
358
- try {
359
- ms.overwrite(match.element.start, match.element.end, html);
360
- changed = true;
361
- } catch (cause) {
502
+ const { start, end } = match.element;
503
+ const previous = edits.at(-1);
504
+
505
+ // Matched elements can't nest (they require an empty slot) and
506
+ // collectElements visits them in source order, so this should
507
+ // never actually trigger - kept as a defensive fallback so one
508
+ // malformed range can't take down the whole preprocessor pass.
509
+ if (start >= end || (previous && start < previous.end)) {
362
510
  warn("failed to apply the static replacement", {
363
511
  filename,
364
- line: locate(content, match.element.start).line,
365
- cause,
512
+ line: locate(content, start).line,
513
+ cause: new Error("invalid or overlapping replacement range"),
366
514
  });
515
+ continue;
367
516
  }
517
+
518
+ edits.push({ start, end, replacement: html });
368
519
  }
369
520
 
370
- if (!changed) return;
371
- return { code: ms.toString(), map: ms.generateMap({ hires: true }) };
521
+ if (edits.length === 0) return;
522
+ return applyEdits(content, edits);
372
523
  },
373
524
  };
374
525
  }