@mdsvex/render 1.0.0-next.0 → 1.0.0-next.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.
@@ -1,12 +1,40 @@
1
1
  import { describe, it, expect } from 'vitest';
2
2
  import { PFMParser } from '@mdsvex/parse';
3
3
  import { TreeBuilder } from '@mdsvex/parse/tree-builder';
4
- import { CursorHTMLRenderer } from '../src/html_cursor';
4
+ import {
5
+ CursorHTMLRenderer,
6
+ NONE,
7
+ _emit,
8
+ _out_offsets,
9
+ _resolve_mappings,
10
+ } from '../src/html_cursor';
5
11
  import type { Mapping, MappingData, MappingRole } from '../src/mappings';
12
+ import {
13
+ CI_TEXT,
14
+ CI_CODE,
15
+ CI_SVELTE,
16
+ CI_STRUCTURE,
17
+ MapSink,
18
+ P_CODE,
19
+ P_STRUCTURE,
20
+ P_SVELTE,
21
+ P_TEXT,
22
+ R_CLOSE_SYNTAX,
23
+ R_CONTENT,
24
+ R_NODE,
25
+ R_OPEN_SYNTAX,
26
+ data_text,
27
+ data_code,
28
+ data_svelte,
29
+ data_structure,
30
+ record_code,
31
+ record_data,
32
+ } from '../src/mappings';
6
33
  import {
7
34
  build_line_starts,
8
35
  offset_to_position,
9
36
  mappings_to_v3,
37
+ records_to_v3,
10
38
  } from '../src/sourcemap';
11
39
 
12
40
  // ── vlq decoder (for testing round-trips) ──
@@ -19,7 +47,10 @@ function decodeVLQMappings(mappings: string): number[][][] {
19
47
  const lines: number[][][] = [];
20
48
  let line: number[][] = [];
21
49
  let i = 0;
22
- let gen_col = 0, src_idx = 0, src_line = 0, src_col = 0;
50
+ let gen_col = 0,
51
+ src_idx = 0,
52
+ src_line = 0,
53
+ src_col = 0;
23
54
 
24
55
  while (i < mappings.length) {
25
56
  const ch = mappings[i];
@@ -32,8 +63,17 @@ function decodeVLQMappings(mappings: string): number[][][] {
32
63
  i++;
33
64
  } else {
34
65
  const seg: number[] = [];
35
- for (let f = 0; f < 5 && i < mappings.length && mappings[i] !== ',' && mappings[i] !== ';'; f++) {
36
- let value = 0, shift = 0, digit: number;
66
+ for (
67
+ let f = 0;
68
+ f < 5 &&
69
+ i < mappings.length &&
70
+ mappings[i] !== ',' &&
71
+ mappings[i] !== ';';
72
+ f++
73
+ ) {
74
+ let value = 0,
75
+ shift = 0,
76
+ digit: number;
37
77
  do {
38
78
  digit = B64_LOOKUP[mappings.charCodeAt(i++)];
39
79
  value |= (digit & 0x1f) << shift;
@@ -64,7 +104,7 @@ function renderMapped(source: string) {
64
104
  }
65
105
 
66
106
  function byRole(mappings: Mapping<MappingData>[], role: MappingRole) {
67
- return mappings.filter(m => m.data.role === role);
107
+ return mappings.filter((m) => m.data.role === role);
68
108
  }
69
109
 
70
110
  function srcSlice(source: string, m: Mapping<MappingData>) {
@@ -80,7 +120,7 @@ function genSlice(html: string, m: Mapping<MappingData>) {
80
120
  function assertMappingsValid(
81
121
  source: string,
82
122
  html: string,
83
- mappings: Mapping<MappingData>[],
123
+ mappings: Mapping<MappingData>[]
84
124
  ) {
85
125
  for (const m of mappings) {
86
126
  for (let i = 0; i < m.sourceOffsets.length; i++) {
@@ -114,7 +154,7 @@ describe('Mapping collection', () => {
114
154
  expect(genSlice(html, contents[0])).toBe('hello world');
115
155
 
116
156
  // node span covers full <p>...</p>
117
- const pNode = nodes.find(m => genSlice(html, m).startsWith('<p'));
157
+ const pNode = nodes.find((m) => genSlice(html, m).startsWith('<p'));
118
158
  expect(pNode).toBeDefined();
119
159
  });
120
160
 
@@ -135,12 +175,12 @@ describe('Mapping collection', () => {
135
175
  expect(srcSlice(source, contents[0])).toBe('hello');
136
176
 
137
177
  // open_syntax: <h3> maps to ### (source marker)
138
- const headingOpen = opens.find(m => genSlice(html, m).startsWith('<h'));
178
+ const headingOpen = opens.find((m) => genSlice(html, m).startsWith('<h'));
139
179
  expect(headingOpen).toBeDefined();
140
180
  expect(srcSlice(source, headingOpen!)).toBe('### ');
141
181
 
142
182
  // node span covers full <h3>hello</h3>
143
- const headingNode = nodes.find(m => genSlice(html, m).includes('<h3>'));
183
+ const headingNode = nodes.find((m) => genSlice(html, m).includes('<h3>'));
144
184
  expect(headingNode).toBeDefined();
145
185
  });
146
186
 
@@ -152,11 +192,11 @@ describe('Mapping collection', () => {
152
192
  const closes = byRole(mappings, 'close_syntax');
153
193
 
154
194
  // find the emphasis open_syntax (maps <em> → _)
155
- const emOpen = opens.find(m => genSlice(html, m).includes('<em'));
195
+ const emOpen = opens.find((m) => genSlice(html, m).includes('<em'));
156
196
  expect(emOpen).toBeDefined();
157
197
  expect(srcSlice(source, emOpen!)).toBe('_');
158
198
 
159
- const emClose = closes.find(m => genSlice(html, m).includes('</em>'));
199
+ const emClose = closes.find((m) => genSlice(html, m).includes('</em>'));
160
200
  expect(emClose).toBeDefined();
161
201
  expect(srcSlice(source, emClose!)).toBe('_');
162
202
  });
@@ -166,18 +206,22 @@ describe('Mapping collection', () => {
166
206
  assertMappingsValid(source, html, mappings);
167
207
 
168
208
  const contents = byRole(mappings, 'content');
169
- const codeContent = contents.find(m => srcSlice(source, m) === 'code');
209
+ const codeContent = contents.find((m) => srcSlice(source, m) === 'code');
170
210
  expect(codeContent).toBeDefined();
171
211
  expect(codeContent!.data.semantic).toBe(true);
172
212
  expect(codeContent!.data.verification).toBeUndefined();
173
213
  });
174
214
 
175
215
  it('code fence emits content + node + syntax', () => {
176
- const { html, mappings, source } = renderMapped('```js\nconst x = 1;\n```\n');
216
+ const { html, mappings, source } = renderMapped(
217
+ '```js\nconst x = 1;\n```\n'
218
+ );
177
219
  assertMappingsValid(source, html, mappings);
178
220
 
179
221
  const contents = byRole(mappings, 'content');
180
- const codeContent = contents.find(m => srcSlice(source, m).includes('const x'));
222
+ const codeContent = contents.find((m) =>
223
+ srcSlice(source, m).includes('const x')
224
+ );
181
225
  expect(codeContent).toBeDefined();
182
226
 
183
227
  const opens = byRole(mappings, 'open_syntax');
@@ -191,7 +235,9 @@ describe('Mapping collection', () => {
191
235
  const contents = byRole(mappings, 'content');
192
236
  const text_content = contents[0];
193
237
  expect(text_content.generatedLengths).toBeDefined();
194
- expect(text_content.generatedLengths![0]).toBeGreaterThan(text_content.lengths[0]);
238
+ expect(text_content.generatedLengths![0]).toBeGreaterThan(
239
+ text_content.lengths[0]
240
+ );
195
241
  });
196
242
 
197
243
  it('no generatedLengths when escape does not change length', () => {
@@ -215,14 +261,16 @@ describe('Mapping collection', () => {
215
261
 
216
262
  // each node should have at most one of each role
217
263
  for (const [, entries] of byNode) {
218
- const roles = entries.map(e => e.role);
264
+ const roles = entries.map((e) => e.role);
219
265
  const unique = new Set(roles);
220
266
  expect(unique.size).toBe(roles.length);
221
267
  }
222
268
  });
223
269
 
224
270
  it('close_syntax source range does not span entire document for container nodes', () => {
225
- const { html, mappings, source } = renderMapped('# Title\n\nparagraph text\n');
271
+ const { html, mappings, source } = renderMapped(
272
+ '# Title\n\nparagraph text\n'
273
+ );
226
274
  assertMappingsValid(source, html, mappings);
227
275
 
228
276
  const closes = byRole(mappings, 'close_syntax');
@@ -238,7 +286,7 @@ describe('Mapping collection', () => {
238
286
  assertMappingsValid(source, html, mappings);
239
287
 
240
288
  const closes = byRole(mappings, 'close_syntax');
241
- const paraClose = closes.find(m => genSlice(html, m).includes('</p>'));
289
+ const paraClose = closes.find((m) => genSlice(html, m).includes('</p>'));
242
290
  expect(paraClose).toBeDefined();
243
291
  // paragraphs have no closing syntax in markdown — source length should be 0
244
292
  expect(paraClose!.lengths[0]).toBe(0);
@@ -249,7 +297,7 @@ describe('Mapping collection', () => {
249
297
  assertMappingsValid(source, html, mappings);
250
298
 
251
299
  const opens = byRole(mappings, 'open_syntax');
252
- const paraOpen = opens.find(m => genSlice(html, m).includes('<p'));
300
+ const paraOpen = opens.find((m) => genSlice(html, m).includes('<p'));
253
301
  expect(paraOpen).toBeDefined();
254
302
  expect(paraOpen!.lengths[0]).toBe(0);
255
303
  });
@@ -259,12 +307,12 @@ describe('Mapping collection', () => {
259
307
  assertMappingsValid(source, html, mappings);
260
308
 
261
309
  const opens = byRole(mappings, 'open_syntax');
262
- const headingOpen = opens.find(m => genSlice(html, m).startsWith('<h'));
310
+ const headingOpen = opens.find((m) => genSlice(html, m).startsWith('<h'));
263
311
  expect(headingOpen).toBeDefined();
264
312
  expect(srcSlice(source, headingOpen!)).toBe('### ');
265
313
 
266
314
  const closes = byRole(mappings, 'close_syntax');
267
- const headingClose = closes.find(m => genSlice(html, m).includes('</h'));
315
+ const headingClose = closes.find((m) => genSlice(html, m).includes('</h'));
268
316
  expect(headingClose).toBeDefined();
269
317
  expect(headingClose!.lengths[0]).toBe(0);
270
318
  });
@@ -275,12 +323,14 @@ describe('Mapping collection', () => {
275
323
  assertMappingsValid(source, html, mappings);
276
324
 
277
325
  const opens = byRole(mappings, 'open_syntax');
278
- const fenceOpen = opens.find(m => genSlice(html, m).includes('<pre>'));
326
+ const fenceOpen = opens.find((m) => genSlice(html, m).includes('<pre>'));
279
327
  expect(fenceOpen).toBeDefined();
280
328
  expect(srcSlice(source, fenceOpen!)).toContain('```js');
281
329
 
282
330
  const closes = byRole(mappings, 'close_syntax');
283
- const fenceClose = closes.find(m => genSlice(html, m).includes('</code></pre>'));
331
+ const fenceClose = closes.find((m) =>
332
+ genSlice(html, m).includes('</code></pre>')
333
+ );
284
334
  expect(fenceClose).toBeDefined();
285
335
  expect(srcSlice(source, fenceClose!)).toContain('```');
286
336
  });
@@ -290,13 +340,13 @@ describe('Mapping collection', () => {
290
340
  assertMappingsValid(source, html, mappings);
291
341
 
292
342
  const opens = byRole(mappings, 'open_syntax');
293
- const emOpen = opens.find(m => genSlice(html, m).includes('<em'));
343
+ const emOpen = opens.find((m) => genSlice(html, m).includes('<em'));
294
344
  expect(emOpen).toBeDefined();
295
345
  expect(srcSlice(source, emOpen!)).toBe('_');
296
346
  expect(emOpen!.lengths[0]).toBe(1);
297
347
 
298
348
  const closes = byRole(mappings, 'close_syntax');
299
- const emClose = closes.find(m => genSlice(html, m).includes('</em>'));
349
+ const emClose = closes.find((m) => genSlice(html, m).includes('</em>'));
300
350
  expect(emClose).toBeDefined();
301
351
  expect(srcSlice(source, emClose!)).toBe('_');
302
352
  expect(emClose!.lengths[0]).toBe(1);
@@ -307,7 +357,7 @@ describe('Mapping collection', () => {
307
357
  assertMappingsValid(source, html, mappings);
308
358
 
309
359
  const contents = byRole(mappings, 'content');
310
- const svelte = contents.filter(m => m.data.completion === true);
360
+ const svelte = contents.filter((m) => m.data.completion === true);
311
361
  expect(svelte.length).toBeGreaterThanOrEqual(1);
312
362
  });
313
363
 
@@ -338,6 +388,17 @@ describe('build_line_starts', () => {
338
388
  const ls = build_line_starts('');
339
389
  expect(Array.from(ls)).toEqual([0]);
340
390
  });
391
+
392
+ it('crlf and bare cr', () => {
393
+ const ls = build_line_starts('a\r\nb\rc\n\r');
394
+ expect(Array.from(ls)).toEqual([0, 3, 5, 7, 8]);
395
+ });
396
+
397
+ it('many lines', () => {
398
+ const ls = build_line_starts('x\n'.repeat(1000));
399
+ expect(ls.length).toBe(1001);
400
+ expect(ls[1000]).toBe(2000);
401
+ });
341
402
  });
342
403
 
343
404
  describe('offset_to_position', () => {
@@ -405,7 +466,9 @@ describe('mappings_to_v3', () => {
405
466
  expect(decoded[0].length).toBeGreaterThan(0);
406
467
 
407
468
  // find the segment that maps the text content
408
- const textSeg = decoded[0].find(s => s.length >= 1 && html.slice(s[0], s[0] + 5) === 'hello');
469
+ const textSeg = decoded[0].find(
470
+ (s) => s.length >= 1 && html.slice(s[0], s[0] + 5) === 'hello'
471
+ );
409
472
  expect(textSeg).toBeDefined();
410
473
  expect(textSeg!.length).toBe(4);
411
474
 
@@ -416,6 +479,55 @@ describe('mappings_to_v3', () => {
416
479
  expect(src_lines[src_line].slice(src_col, src_col + 5)).toBe('hello');
417
480
  });
418
481
 
482
+ it('maps every character of long and multi-line runs listed before their node', () => {
483
+ const line = 'abcdefghijklmnopqrstuvwxyz0123456789';
484
+ const source = `${line}\n${line}\n`;
485
+ const html = `<p>${line}\n${line}</p>`;
486
+ const content: Mapping<MappingData> = {
487
+ sourceOffsets: [0],
488
+ generatedOffsets: [3],
489
+ lengths: [source.length - 1],
490
+ data: { role: 'content' } as MappingData,
491
+ };
492
+ const node: Mapping<MappingData> = {
493
+ sourceOffsets: [0],
494
+ generatedOffsets: [0],
495
+ lengths: [source.length],
496
+ data: { role: 'node' } as MappingData,
497
+ };
498
+ const v3 = mappings_to_v3([content, node], source, html);
499
+ const decoded = decodeVLQMappings(v3.mappings);
500
+ const src_lines = source.split('\n');
501
+ const gen_lines = html.split('\n');
502
+ let chars = 0;
503
+ for (let l = 0; l < decoded.length; l++) {
504
+ for (const [gen_col, , src_line, src_col] of decoded[l]) {
505
+ if (l === 0 && gen_col === 0) continue;
506
+ expect(gen_lines[l][gen_col]).toBe(src_lines[src_line][src_col]);
507
+ chars++;
508
+ }
509
+ }
510
+ expect(decoded[0][0]).toEqual([0, 0, 0, 0]);
511
+ expect(chars).toBe(source.length - 1);
512
+ });
513
+
514
+ it('does not depend on the order of the mappings', () => {
515
+ const source = 'ab'.repeat(3000);
516
+ const sorted: Mapping<MappingData>[] = [];
517
+ for (let i = 0; i < 3000; i++) {
518
+ sorted.push({
519
+ sourceOffsets: [i * 2],
520
+ generatedOffsets: [i * 2],
521
+ lengths: [1],
522
+ data: { role: 'content' } as MappingData,
523
+ });
524
+ }
525
+ const reversed = sorted.slice().reverse();
526
+ expect(mappings_to_v3(reversed, source, source).mappings).toBe(
527
+ mappings_to_v3(sorted, source, source).mappings
528
+ );
529
+ });
530
+
419
531
  it('survives svelte compiler remapping', () => {
420
532
  const svelte = require('svelte/compiler');
421
533
  // single-element output — Svelte generates per-element mappings for these
@@ -434,3 +546,158 @@ describe('mappings_to_v3', () => {
434
546
  expect(result.js.map.sources).toContain('test.md');
435
547
  });
436
548
  });
549
+
550
+ describe('mapping data factories', () => {
551
+ it('match the CI presets, key order included', () => {
552
+ const pairs = [
553
+ [data_text, CI_TEXT],
554
+ [data_code, CI_CODE],
555
+ [data_svelte, CI_SVELTE],
556
+ [data_structure, CI_STRUCTURE],
557
+ ] as const;
558
+ for (const [make, preset] of pairs) {
559
+ const data = make(7, 'content');
560
+ const spread = { ...preset, nodeIndex: 7, role: 'content' };
561
+ expect(data).toEqual(spread);
562
+ expect(Object.keys(data)).toEqual(Object.keys(spread));
563
+ }
564
+ });
565
+ });
566
+
567
+ const RECORD_DOCS = [
568
+ 'hello\n',
569
+ '# Heading *em* **strong**\n\npara with `code` and <b>html</b>\n',
570
+ '- one\n- two\n\n1. a\n2. b\n\n> quote\n',
571
+ '```js\nlet x = 1 < 2;\n```\n\n| a | b |\n| :- | -: |\n| 1 | {x} |\n',
572
+ '<script>\nlet a = 1;\n</script>\n\n{#if a}\n_y_ {@html z}\n{:else}\nno\n{/if}\n\n<Foo bar={1} />\n',
573
+ '![alt *x*](src "t") [link](href) <!-- c --> a & b < c "q"\n\n---\n',
574
+ '',
575
+ ];
576
+
577
+ describe('record_data', () => {
578
+ it('builds the data objects the presets build', () => {
579
+ const presets = [data_text, data_code, data_svelte, data_structure];
580
+ const roles: MappingRole[] = [
581
+ 'node',
582
+ 'content',
583
+ 'open_syntax',
584
+ 'close_syntax',
585
+ ];
586
+ for (let p = 0; p < presets.length; p++) {
587
+ for (let r = 0; r < roles.length; r++) {
588
+ const data = record_data(record_code(p, r), 3);
589
+ const want = presets[p](3, roles[r]);
590
+ expect(data).toEqual(want);
591
+ expect(Object.keys(data)).toEqual(Object.keys(want));
592
+ }
593
+ }
594
+ });
595
+ });
596
+
597
+ describe('update_v3', () => {
598
+ it.each(RECORD_DOCS)(
599
+ 'equals mappings_to_v3 over update_mapped: %j',
600
+ (source) => {
601
+ const tree = new TreeBuilder(source.length >> 3 || 128);
602
+ new PFMParser(tree).parse(source);
603
+ const renderer = new CursorHTMLRenderer({ cache: false });
604
+ const { mappings } = renderer.update_mapped(tree.get_buffer(), source);
605
+ const html = renderer.html;
606
+ const want = mappings_to_v3(mappings, source, html, '/a/doc.svx');
607
+
608
+ const fused = new CursorHTMLRenderer({ cache: false });
609
+ const got = fused.update_v3(
610
+ tree.get_buffer(),
611
+ source,
612
+ source,
613
+ '/a/doc.svx'
614
+ );
615
+ expect(fused.html).toBe(html);
616
+ expect(JSON.stringify(got)).toBe(JSON.stringify(want));
617
+ }
618
+ );
619
+
620
+ it('leaves update_mapped unchanged after a fused render', () => {
621
+ const source = RECORD_DOCS[1];
622
+ const tree = new TreeBuilder(128);
623
+ new PFMParser(tree).parse(source);
624
+ const renderer = new CursorHTMLRenderer({ cache: false });
625
+ const before = renderer.update_mapped(tree.get_buffer(), source).mappings;
626
+ renderer.update_v3(tree.get_buffer(), source, source);
627
+ const after = renderer.update_mapped(tree.get_buffer(), source).mappings;
628
+ expect(after).toEqual(before);
629
+ expect(byRole(after, 'open_syntax').length).toBeGreaterThan(0);
630
+ });
631
+ });
632
+
633
+ describe('records_to_v3', () => {
634
+ const source = 'abcdef\nghijkl\nmnopqr\n';
635
+
636
+ function both(out: string[], fill: (sink: MapSink) => void) {
637
+ const sink = new MapSink();
638
+ sink.begin(true);
639
+ fill(sink);
640
+ const generated = out.join('');
641
+ const want = mappings_to_v3(
642
+ _resolve_mappings(out, sink),
643
+ source,
644
+ generated,
645
+ 'x.md'
646
+ );
647
+ const got = records_to_v3(
648
+ sink,
649
+ _out_offsets(out),
650
+ source,
651
+ generated,
652
+ 'x.md'
653
+ );
654
+ expect(JSON.stringify(got)).toBe(JSON.stringify(want));
655
+ return got;
656
+ }
657
+
658
+ it('encodes identity runs, anchors and nodes like the object path', () => {
659
+ const out = ['<p>', 'abcdef', '</p>\n', 'gh&amp;', '<br />'];
660
+ const map = both(out, (sink) => {
661
+ _emit(sink, 0, 3, 0, 14, 0, record_code(P_TEXT, R_NODE));
662
+ _emit(sink, 1, 2, 0, 6, 1, record_code(P_TEXT, R_CONTENT));
663
+ _emit(sink, 3, 4, 7, 10, 2, record_code(P_TEXT, R_CONTENT));
664
+ _emit(sink, 0, 1, 0, 0, 0, record_code(P_STRUCTURE, R_OPEN_SYNTAX));
665
+ _emit(sink, 2, 3, 6, 7, 0, record_code(P_STRUCTURE, R_CLOSE_SYNTAX));
666
+ });
667
+ expect(map.mappings.length).toBeGreaterThan(0);
668
+ });
669
+
670
+ it('falls back to single characters when runs overlap', () => {
671
+ both(['abc', 'def', 'X'], (sink) => {
672
+ _emit(sink, 0, 2, 0, 6, 0, record_code(P_TEXT, R_CONTENT));
673
+ _emit(sink, 1, 2, 8, 9, 1, record_code(P_CODE, R_CONTENT));
674
+ _emit(sink, 2, 3, 14, 15, 2, record_code(P_SVELTE, R_NODE));
675
+ });
676
+ });
677
+
678
+ it('skips empty ranges, missing sources and empty identity runs', () => {
679
+ both(['', 'ab', '', 'c'], (sink) => {
680
+ _emit(sink, 0, 1, 0, 0, 0, record_code(P_TEXT, R_CONTENT));
681
+ _emit(sink, 1, 1, 0, 2, 0, record_code(P_TEXT, R_CONTENT));
682
+ _emit(sink, 1, 2, NONE, 2, 0, record_code(P_TEXT, R_CONTENT));
683
+ _emit(sink, 1, 2, 0, 2, 0, record_code(P_TEXT, R_CONTENT));
684
+ _emit(sink, 2, 3, 3, 3, -1, record_code(P_STRUCTURE, R_NODE));
685
+ _emit(sink, 3, 4, 7, 8, -1, record_code(P_SVELTE, R_CONTENT));
686
+ });
687
+ });
688
+
689
+ it('grows the sink past its first buffer', () => {
690
+ const out: string[] = [];
691
+ both(out, (sink) => {
692
+ for (let i = 0; i < 2000; i++) {
693
+ out.push(i % 3 === 0 ? '&amp;' : 'a');
694
+ const s = i % 20;
695
+ _emit(sink, i, i + 1, s, s + 1, i, record_code(P_TEXT, R_CONTENT));
696
+ }
697
+ });
698
+ });
699
+
700
+ it('gives the empty map for no records', () => {
701
+ expect(both(['<hr />'], () => {}).mappings).toBe('');
702
+ });
703
+ });