@mdsvex/render 1.0.0-next.0 → 1.0.0-next.2
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.
- package/dist/component.d.ts +43 -0
- package/dist/component.js +32 -11
- package/dist/entities.d.ts +2 -0
- package/dist/html_cursor.d.ts +397 -0
- package/dist/html_cursor.js +4754 -436
- package/dist/index.d.ts +9 -0
- package/dist/index.js +24 -18
- package/dist/mappings.d.ts +117 -0
- package/dist/mappings.js +151 -8
- package/dist/scope.d.ts +52 -0
- package/dist/sourcemap.d.ts +108 -0
- package/dist/sourcemap.js +832 -68
- package/dist/stream.d.ts +87 -0
- package/dist/stream.js +123 -0
- package/package.json +22 -14
- package/src/Node.svelte +290 -114
- package/src/component.ts +31 -8
- package/src/entities.ts +226 -0
- package/src/html_cursor.ts +7166 -593
- package/src/index.ts +36 -9
- package/src/mappings.ts +239 -4
- package/src/scope.ts +127 -0
- package/src/sourcemap.ts +1713 -99
- package/src/stream.ts +223 -0
- package/CHANGELOG.md +0 -16
- package/test/_gen_chain.mjs +0 -67
- package/test/_vite_debug.mjs +0 -91
- package/test/_vite_debug2.mjs +0 -34
- package/test/component.spec.ts +0 -120
- package/test/html_cursor.spec.ts +0 -231
- package/test/mappings.spec.ts +0 -436
- package/test/wire_tree_builder.spec.ts +0 -139
- package/tsconfig.json +0 -11
- package/vite.config.build.ts +0 -33
package/test/html_cursor.spec.ts
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { PFMParser } from '@mdsvex/parse';
|
|
3
|
-
import { TreeBuilder } from '@mdsvex/parse/tree-builder';
|
|
4
|
-
import { Cursor } from '@mdsvex/parse/cursor';
|
|
5
|
-
import { CursorHTMLRenderer } from '../src/html_cursor';
|
|
6
|
-
|
|
7
|
-
// Helpers ��
|
|
8
|
-
|
|
9
|
-
function render(source: string): string {
|
|
10
|
-
const tree = new TreeBuilder(source.length >> 3 || 128);
|
|
11
|
-
const parser = new PFMParser(tree);
|
|
12
|
-
parser.parse(source);
|
|
13
|
-
const renderer = new CursorHTMLRenderer({ cache: false });
|
|
14
|
-
renderer.update(tree.get_buffer(), source);
|
|
15
|
-
return renderer.html;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Tests ��
|
|
19
|
-
|
|
20
|
-
describe('CursorHTMLRenderer', () => {
|
|
21
|
-
it('simple paragraph', () => {
|
|
22
|
-
expect(render('hello world\n')).toBe('<p>hello world</p>');
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('multiple paragraphs', () => {
|
|
26
|
-
const html = render('first\n\nsecond\n');
|
|
27
|
-
expect(html).toContain('<p>first</p>');
|
|
28
|
-
expect(html).toContain('<p>second</p>');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('h1', () => expect(render('# Hello\n')).toBe('<h1>Hello</h1>'));
|
|
32
|
-
it('h2', () => expect(render('## Title\n')).toBe('<h2>Title</h2>'));
|
|
33
|
-
it('h3-h6', () => {
|
|
34
|
-
expect(render('### H3\n')).toBe('<h3>H3</h3>');
|
|
35
|
-
expect(render('#### H4\n')).toBe('<h4>H4</h4>');
|
|
36
|
-
expect(render('##### H5\n')).toBe('<h5>H5</h5>');
|
|
37
|
-
expect(render('###### H6\n')).toBe('<h6>H6</h6>');
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('emphasis', () => expect(render('_hello_\n')).toContain('<em>hello</em>'));
|
|
41
|
-
it('strong', () =>
|
|
42
|
-
expect(render('*bold*\n')).toContain('<strong>bold</strong>'));
|
|
43
|
-
it('mixed emphasis', () => {
|
|
44
|
-
const html = render('before _middle_ after\n');
|
|
45
|
-
expect(html).toContain('before ');
|
|
46
|
-
expect(html).toContain('<em>middle</em>');
|
|
47
|
-
expect(html).toContain(' after');
|
|
48
|
-
});
|
|
49
|
-
it('nested emphasis', () => {
|
|
50
|
-
const html = render('_em *strong* em_\n');
|
|
51
|
-
expect(html).toContain('<em>');
|
|
52
|
-
expect(html).toContain('<strong>strong</strong>');
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('code span', () =>
|
|
56
|
-
expect(render('use `code` here\n')).toContain('<code>code</code>'));
|
|
57
|
-
it('code fence with info', () => {
|
|
58
|
-
const html = render('```js\nconst x = 1;\n```\n');
|
|
59
|
-
expect(html).toContain('class="language-js"');
|
|
60
|
-
expect(html).toContain('const x = 1;');
|
|
61
|
-
});
|
|
62
|
-
it('code fence no info', () => {
|
|
63
|
-
expect(render('```\nhello\n```\n')).toContain(
|
|
64
|
-
'<pre><code>hello</code></pre>'
|
|
65
|
-
);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('block quote', () => {
|
|
69
|
-
const html = render('> quoted\n');
|
|
70
|
-
expect(html).toContain('<blockquote>');
|
|
71
|
-
expect(html).toContain('<p>quoted</p>');
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('link', () => {
|
|
75
|
-
expect(render('[click](https://example.com)\n')).toContain(
|
|
76
|
-
'<a href="https://example.com">click</a>'
|
|
77
|
-
);
|
|
78
|
-
});
|
|
79
|
-
it('link with title', () => {
|
|
80
|
-
expect(render('[text](url "title")\n')).toContain('title="title"');
|
|
81
|
-
});
|
|
82
|
-
it('image', () => {
|
|
83
|
-
expect(render('\n')).toContain(
|
|
84
|
-
'<img src="image.png" alt="alt text"'
|
|
85
|
-
);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('unordered list (tight)', () => {
|
|
89
|
-
const html = render('- one\n- two\n');
|
|
90
|
-
expect(html).toContain('<ul>');
|
|
91
|
-
expect(html).toContain('<li>one</li>');
|
|
92
|
-
expect(html).toContain('<li>two</li>');
|
|
93
|
-
expect(html).not.toContain('<p>');
|
|
94
|
-
});
|
|
95
|
-
it('ordered list (tight)', () => {
|
|
96
|
-
const html = render('1. one\n2. two\n');
|
|
97
|
-
expect(html).toContain('<ol>');
|
|
98
|
-
expect(html).toContain('<li>one</li>');
|
|
99
|
-
expect(html).toContain('<li>two</li>');
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('thematic break', () => expect(render('---\n')).toContain('<hr />'));
|
|
103
|
-
it('strikethrough', () =>
|
|
104
|
-
expect(render('~~deleted~~\n')).toContain('<del>deleted</del>'));
|
|
105
|
-
it('superscript', () =>
|
|
106
|
-
expect(render('^super^\n')).toContain('<sup>super</sup>'));
|
|
107
|
-
|
|
108
|
-
it('table', () => {
|
|
109
|
-
const html = render('| foo | bar |\n| --- | --- |\n| baz | bim |\n');
|
|
110
|
-
expect(html).toContain('<table>');
|
|
111
|
-
expect(html).toContain('<th>foo</th>');
|
|
112
|
-
expect(html).toContain('<td>baz</td>');
|
|
113
|
-
});
|
|
114
|
-
it('table with alignment', () => {
|
|
115
|
-
const html = render(
|
|
116
|
-
'| left | center | right |\n| :--- | :---: | ---: |\n| a | b | c |\n'
|
|
117
|
-
);
|
|
118
|
-
expect(html).toContain('align="left"');
|
|
119
|
-
expect(html).toContain('align="center"');
|
|
120
|
-
expect(html).toContain('align="right"');
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('html self-closing', () =>
|
|
124
|
-
expect(render('text <br /> more\n')).toContain('<br />'));
|
|
125
|
-
it('html paired', () =>
|
|
126
|
-
expect(render('text <span>inside</span> end\n')).toContain(
|
|
127
|
-
'<span>inside</span>'
|
|
128
|
-
));
|
|
129
|
-
it('html block', () => {
|
|
130
|
-
const html = render('<section>\n\n# Heading\n\nParagraph.\n\n</section>\n');
|
|
131
|
-
expect(html).toContain('<section>');
|
|
132
|
-
expect(html).toContain('<h1>Heading</h1>');
|
|
133
|
-
expect(html).toContain('<p>Paragraph.</p>');
|
|
134
|
-
});
|
|
135
|
-
it('html comment', () =>
|
|
136
|
-
expect(render('text <!-- hidden --> more\n')).toContain('<!-- hidden -->'));
|
|
137
|
-
|
|
138
|
-
it('complex document', () => {
|
|
139
|
-
const html = render(
|
|
140
|
-
'# Title\n\n' +
|
|
141
|
-
'A paragraph with _emphasis_ and *bold*.\n\n' +
|
|
142
|
-
'> A block quote with `code`\n\n' +
|
|
143
|
-
'```js\nconst x = 1;\n```\n\n' +
|
|
144
|
-
'- item one\n- item two\n\n' +
|
|
145
|
-
'| a | b |\n| --- | --- |\n| 1 | 2 |\n\n' +
|
|
146
|
-
'[link](url) and \n\n' +
|
|
147
|
-
'---\n'
|
|
148
|
-
);
|
|
149
|
-
expect(html).toContain('<h1>Title</h1>');
|
|
150
|
-
expect(html).toContain('<em>emphasis</em>');
|
|
151
|
-
expect(html).toContain('<strong>bold</strong>');
|
|
152
|
-
expect(html).toContain('<blockquote>');
|
|
153
|
-
expect(html).toContain('<code>code</code>');
|
|
154
|
-
expect(html).toContain('class="language-js"');
|
|
155
|
-
expect(html).toContain('<li>item one</li>');
|
|
156
|
-
expect(html).toContain('<a href="url">link</a>');
|
|
157
|
-
expect(html).toContain('<img src="src" alt="img"');
|
|
158
|
-
expect(html).toContain('<hr />');
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('escapes HTML entities in text', () => {
|
|
162
|
-
expect(render('1 < 2 and 3 > 1\n')).toContain('<');
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it('escapes HTML in code spans', () => {
|
|
166
|
-
expect(render('`<div>`\n')).toContain('<code><div></code>');
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
// Svelte syntax
|
|
170
|
-
|
|
171
|
-
it('mustache expression', () => {
|
|
172
|
-
expect(render('hello {name} world')).toBe('<p>hello {name} world</p>');
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('svelte void tag {@html}', () => {
|
|
176
|
-
expect(render('{@html "<b>bold</b>"}')).toBe(
|
|
177
|
-
'<p>{@html "<b>bold</b>"}</p>'
|
|
178
|
-
);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it('svelte void tag {@debug}', () => {
|
|
182
|
-
expect(render('{@debug myVar}')).toBe('<p>{@debug myVar}</p>');
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
it('svelte void tag {@const} no expression', () => {
|
|
186
|
-
expect(render('{@debug}')).toBe('<p>{@debug}</p>');
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('simple if block', () => {
|
|
190
|
-
const html = render('{#if show}\nhello\n{/if}');
|
|
191
|
-
expect(html).toBe('{#if show}\n<p>hello</p>{/if}');
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it('if/else block', () => {
|
|
195
|
-
const html = render('{#if a}\nfirst\n{:else}\nsecond\n{/if}');
|
|
196
|
-
expect(html).toBe('{#if a}\n<p>first</p>{:else}\n<p>second</p>{/if}');
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it('if/else if/else block', () => {
|
|
200
|
-
const html = render(
|
|
201
|
-
'{#if a}\none\n{:else if b}\ntwo\n{:else}\nthree\n{/if}'
|
|
202
|
-
);
|
|
203
|
-
expect(html).toBe(
|
|
204
|
-
'{#if a}\n<p>one</p>{:else if b}\n<p>two</p>{:else}\n<p>three</p>{/if}'
|
|
205
|
-
);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it('each block', () => {
|
|
209
|
-
const html = render('{#each items as item}\n{item}\n{/each}');
|
|
210
|
-
expect(html).toBe('{#each items as item}\n<p>{item}</p>{/each}');
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it('await/then/catch block', () => {
|
|
214
|
-
const html = render(
|
|
215
|
-
'{#await promise}\nloading\n{:then value}\ndone\n{:catch error}\nfail\n{/await}'
|
|
216
|
-
);
|
|
217
|
-
expect(html).toBe(
|
|
218
|
-
'{#await promise}\n<p>loading</p>{:then value}\n<p>done</p>{:catch error}\n<p>fail</p>{/await}'
|
|
219
|
-
);
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
it('nested blocks', () => {
|
|
223
|
-
const html = render('{#if a}\n{#if b}\ninner\n{/if}\n{/if}');
|
|
224
|
-
expect(html).toBe('{#if a}\n{#if b}\n<p>inner</p>{/if}{/if}');
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
it('block with markdown content', () => {
|
|
228
|
-
const html = render('{#if show}\n# Title\n{/if}');
|
|
229
|
-
expect(html).toBe('{#if show}\n<h1>Title</h1>{/if}');
|
|
230
|
-
});
|
|
231
|
-
});
|
package/test/mappings.spec.ts
DELETED
|
@@ -1,436 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { PFMParser } from '@mdsvex/parse';
|
|
3
|
-
import { TreeBuilder } from '@mdsvex/parse/tree-builder';
|
|
4
|
-
import { CursorHTMLRenderer } from '../src/html_cursor';
|
|
5
|
-
import type { Mapping, MappingData, MappingRole } from '../src/mappings';
|
|
6
|
-
import {
|
|
7
|
-
build_line_starts,
|
|
8
|
-
offset_to_position,
|
|
9
|
-
mappings_to_v3,
|
|
10
|
-
} from '../src/sourcemap';
|
|
11
|
-
|
|
12
|
-
// ── vlq decoder (for testing round-trips) ──
|
|
13
|
-
|
|
14
|
-
const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
15
|
-
const B64_LOOKUP = new Uint8Array(128);
|
|
16
|
-
for (let i = 0; i < B64.length; i++) B64_LOOKUP[B64.charCodeAt(i)] = i;
|
|
17
|
-
|
|
18
|
-
function decodeVLQMappings(mappings: string): number[][][] {
|
|
19
|
-
const lines: number[][][] = [];
|
|
20
|
-
let line: number[][] = [];
|
|
21
|
-
let i = 0;
|
|
22
|
-
let gen_col = 0, src_idx = 0, src_line = 0, src_col = 0;
|
|
23
|
-
|
|
24
|
-
while (i < mappings.length) {
|
|
25
|
-
const ch = mappings[i];
|
|
26
|
-
if (ch === ';') {
|
|
27
|
-
lines.push(line);
|
|
28
|
-
line = [];
|
|
29
|
-
gen_col = 0;
|
|
30
|
-
i++;
|
|
31
|
-
} else if (ch === ',') {
|
|
32
|
-
i++;
|
|
33
|
-
} else {
|
|
34
|
-
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;
|
|
37
|
-
do {
|
|
38
|
-
digit = B64_LOOKUP[mappings.charCodeAt(i++)];
|
|
39
|
-
value |= (digit & 0x1f) << shift;
|
|
40
|
-
shift += 5;
|
|
41
|
-
} while (digit & 0x20);
|
|
42
|
-
seg.push(value & 1 ? -(value >>> 1) : value >>> 1);
|
|
43
|
-
}
|
|
44
|
-
if (seg.length >= 1) seg[0] = gen_col += seg[0];
|
|
45
|
-
if (seg.length >= 2) seg[1] = src_idx += seg[1];
|
|
46
|
-
if (seg.length >= 3) seg[2] = src_line += seg[2];
|
|
47
|
-
if (seg.length >= 4) seg[3] = src_col += seg[3];
|
|
48
|
-
line.push(seg);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
lines.push(line);
|
|
52
|
-
return lines;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// ── helpers ──
|
|
56
|
-
|
|
57
|
-
function renderMapped(source: string) {
|
|
58
|
-
const tree = new TreeBuilder(source.length >> 3 || 128);
|
|
59
|
-
const parser = new PFMParser(tree);
|
|
60
|
-
parser.parse(source);
|
|
61
|
-
const renderer = new CursorHTMLRenderer({ cache: false });
|
|
62
|
-
const { mappings } = renderer.update_mapped(tree.get_buffer(), source);
|
|
63
|
-
return { html: renderer.html, mappings, source };
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function byRole(mappings: Mapping<MappingData>[], role: MappingRole) {
|
|
67
|
-
return mappings.filter(m => m.data.role === role);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function srcSlice(source: string, m: Mapping<MappingData>) {
|
|
71
|
-
return source.slice(m.sourceOffsets[0], m.sourceOffsets[0] + m.lengths[0]);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function genSlice(html: string, m: Mapping<MappingData>) {
|
|
75
|
-
const len = m.generatedLengths ? m.generatedLengths[0] : m.lengths[0];
|
|
76
|
-
return html.slice(m.generatedOffsets[0], m.generatedOffsets[0] + len);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** verify every mapping has valid offsets. */
|
|
80
|
-
function assertMappingsValid(
|
|
81
|
-
source: string,
|
|
82
|
-
html: string,
|
|
83
|
-
mappings: Mapping<MappingData>[],
|
|
84
|
-
) {
|
|
85
|
-
for (const m of mappings) {
|
|
86
|
-
for (let i = 0; i < m.sourceOffsets.length; i++) {
|
|
87
|
-
const srcOff = m.sourceOffsets[i];
|
|
88
|
-
const srcLen = m.lengths[i];
|
|
89
|
-
const genOff = m.generatedOffsets[i];
|
|
90
|
-
const genLen = m.generatedLengths ? m.generatedLengths[i] : m.lengths[i];
|
|
91
|
-
|
|
92
|
-
expect(srcOff).toBeGreaterThanOrEqual(0);
|
|
93
|
-
expect(srcOff + srcLen).toBeLessThanOrEqual(source.length);
|
|
94
|
-
expect(genOff).toBeGreaterThanOrEqual(0);
|
|
95
|
-
expect(genOff + genLen).toBeLessThanOrEqual(html.length);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// ── mapping collection tests ──
|
|
101
|
-
|
|
102
|
-
describe('Mapping collection', () => {
|
|
103
|
-
it('paragraph emits node + content mappings', () => {
|
|
104
|
-
const { html, mappings, source } = renderMapped('hello world\n');
|
|
105
|
-
assertMappingsValid(source, html, mappings);
|
|
106
|
-
|
|
107
|
-
const nodes = byRole(mappings, 'node');
|
|
108
|
-
const contents = byRole(mappings, 'content');
|
|
109
|
-
expect(nodes.length).toBeGreaterThanOrEqual(1);
|
|
110
|
-
expect(contents.length).toBe(1);
|
|
111
|
-
|
|
112
|
-
// content maps text accurately
|
|
113
|
-
expect(srcSlice(source, contents[0])).toBe('hello world');
|
|
114
|
-
expect(genSlice(html, contents[0])).toBe('hello world');
|
|
115
|
-
|
|
116
|
-
// node span covers full <p>...</p>
|
|
117
|
-
const pNode = nodes.find(m => genSlice(html, m).startsWith('<p'));
|
|
118
|
-
expect(pNode).toBeDefined();
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('heading emits node + open_syntax + close_syntax + content', () => {
|
|
122
|
-
const { html, mappings, source } = renderMapped('### hello\n');
|
|
123
|
-
assertMappingsValid(source, html, mappings);
|
|
124
|
-
|
|
125
|
-
const nodes = byRole(mappings, 'node');
|
|
126
|
-
const opens = byRole(mappings, 'open_syntax');
|
|
127
|
-
const closes = byRole(mappings, 'close_syntax');
|
|
128
|
-
const contents = byRole(mappings, 'content');
|
|
129
|
-
|
|
130
|
-
expect(nodes.length).toBeGreaterThanOrEqual(1);
|
|
131
|
-
expect(opens.length).toBeGreaterThanOrEqual(1);
|
|
132
|
-
expect(contents.length).toBe(1);
|
|
133
|
-
|
|
134
|
-
// content: hello → hello
|
|
135
|
-
expect(srcSlice(source, contents[0])).toBe('hello');
|
|
136
|
-
|
|
137
|
-
// open_syntax: <h3> maps to ### (source marker)
|
|
138
|
-
const headingOpen = opens.find(m => genSlice(html, m).startsWith('<h'));
|
|
139
|
-
expect(headingOpen).toBeDefined();
|
|
140
|
-
expect(srcSlice(source, headingOpen!)).toBe('### ');
|
|
141
|
-
|
|
142
|
-
// node span covers full <h3>hello</h3>
|
|
143
|
-
const headingNode = nodes.find(m => genSlice(html, m).includes('<h3>'));
|
|
144
|
-
expect(headingNode).toBeDefined();
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it('emphasis emits open/close syntax mapping to markers', () => {
|
|
148
|
-
const { html, mappings, source } = renderMapped('_hello_\n');
|
|
149
|
-
assertMappingsValid(source, html, mappings);
|
|
150
|
-
|
|
151
|
-
const opens = byRole(mappings, 'open_syntax');
|
|
152
|
-
const closes = byRole(mappings, 'close_syntax');
|
|
153
|
-
|
|
154
|
-
// find the emphasis open_syntax (maps <em> → _)
|
|
155
|
-
const emOpen = opens.find(m => genSlice(html, m).includes('<em'));
|
|
156
|
-
expect(emOpen).toBeDefined();
|
|
157
|
-
expect(srcSlice(source, emOpen!)).toBe('_');
|
|
158
|
-
|
|
159
|
-
const emClose = closes.find(m => genSlice(html, m).includes('</em>'));
|
|
160
|
-
expect(emClose).toBeDefined();
|
|
161
|
-
expect(srcSlice(source, emClose!)).toBe('_');
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it('code span emits content with CODE capabilities', () => {
|
|
165
|
-
const { html, mappings, source } = renderMapped('use `code` here\n');
|
|
166
|
-
assertMappingsValid(source, html, mappings);
|
|
167
|
-
|
|
168
|
-
const contents = byRole(mappings, 'content');
|
|
169
|
-
const codeContent = contents.find(m => srcSlice(source, m) === 'code');
|
|
170
|
-
expect(codeContent).toBeDefined();
|
|
171
|
-
expect(codeContent!.data.semantic).toBe(true);
|
|
172
|
-
expect(codeContent!.data.verification).toBeUndefined();
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('code fence emits content + node + syntax', () => {
|
|
176
|
-
const { html, mappings, source } = renderMapped('```js\nconst x = 1;\n```\n');
|
|
177
|
-
assertMappingsValid(source, html, mappings);
|
|
178
|
-
|
|
179
|
-
const contents = byRole(mappings, 'content');
|
|
180
|
-
const codeContent = contents.find(m => srcSlice(source, m).includes('const x'));
|
|
181
|
-
expect(codeContent).toBeDefined();
|
|
182
|
-
|
|
183
|
-
const opens = byRole(mappings, 'open_syntax');
|
|
184
|
-
expect(opens.length).toBeGreaterThanOrEqual(1);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('uses generatedLengths when escape changes length', () => {
|
|
188
|
-
const { html, mappings, source } = renderMapped('a & b\n');
|
|
189
|
-
assertMappingsValid(source, html, mappings);
|
|
190
|
-
|
|
191
|
-
const contents = byRole(mappings, 'content');
|
|
192
|
-
const text_content = contents[0];
|
|
193
|
-
expect(text_content.generatedLengths).toBeDefined();
|
|
194
|
-
expect(text_content.generatedLengths![0]).toBeGreaterThan(text_content.lengths[0]);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it('no generatedLengths when escape does not change length', () => {
|
|
198
|
-
const { html, mappings, source } = renderMapped('hello\n');
|
|
199
|
-
assertMappingsValid(source, html, mappings);
|
|
200
|
-
|
|
201
|
-
const contents = byRole(mappings, 'content');
|
|
202
|
-
expect(contents[0].generatedLengths).toBeUndefined();
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it('nodeIndex is stable and matches cursor index', () => {
|
|
206
|
-
const { mappings } = renderMapped('# Title\n\npara\n');
|
|
207
|
-
|
|
208
|
-
// all mappings for the same node should share nodeIndex
|
|
209
|
-
const byNode = new Map<number, MappingData[]>();
|
|
210
|
-
for (const m of mappings) {
|
|
211
|
-
const arr = byNode.get(m.data.nodeIndex) ?? [];
|
|
212
|
-
arr.push(m.data);
|
|
213
|
-
byNode.set(m.data.nodeIndex, arr);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// each node should have at most one of each role
|
|
217
|
-
for (const [, entries] of byNode) {
|
|
218
|
-
const roles = entries.map(e => e.role);
|
|
219
|
-
const unique = new Set(roles);
|
|
220
|
-
expect(unique.size).toBe(roles.length);
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it('close_syntax source range does not span entire document for container nodes', () => {
|
|
225
|
-
const { html, mappings, source } = renderMapped('# Title\n\nparagraph text\n');
|
|
226
|
-
assertMappingsValid(source, html, mappings);
|
|
227
|
-
|
|
228
|
-
const closes = byRole(mappings, 'close_syntax');
|
|
229
|
-
for (const m of closes) {
|
|
230
|
-
// no close_syntax source range should be more than half the document —
|
|
231
|
-
// a bogus range from offset 0 to the node end would be caught here
|
|
232
|
-
expect(m.lengths[0]).toBeLessThan(source.length / 2);
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
it('paragraph close_syntax is zero-width (no markdown close marker)', () => {
|
|
237
|
-
const { html, mappings, source } = renderMapped('hello world\n');
|
|
238
|
-
assertMappingsValid(source, html, mappings);
|
|
239
|
-
|
|
240
|
-
const closes = byRole(mappings, 'close_syntax');
|
|
241
|
-
const paraClose = closes.find(m => genSlice(html, m).includes('</p>'));
|
|
242
|
-
expect(paraClose).toBeDefined();
|
|
243
|
-
// paragraphs have no closing syntax in markdown — source length should be 0
|
|
244
|
-
expect(paraClose!.lengths[0]).toBe(0);
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
it('paragraph open_syntax is zero-width (no markdown open marker)', () => {
|
|
248
|
-
const { html, mappings, source } = renderMapped('hello world\n');
|
|
249
|
-
assertMappingsValid(source, html, mappings);
|
|
250
|
-
|
|
251
|
-
const opens = byRole(mappings, 'open_syntax');
|
|
252
|
-
const paraOpen = opens.find(m => genSlice(html, m).includes('<p'));
|
|
253
|
-
expect(paraOpen).toBeDefined();
|
|
254
|
-
expect(paraOpen!.lengths[0]).toBe(0);
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
it('heading open_syntax maps to marker, close_syntax is zero-width', () => {
|
|
258
|
-
const { html, mappings, source } = renderMapped('### hello\n');
|
|
259
|
-
assertMappingsValid(source, html, mappings);
|
|
260
|
-
|
|
261
|
-
const opens = byRole(mappings, 'open_syntax');
|
|
262
|
-
const headingOpen = opens.find(m => genSlice(html, m).startsWith('<h'));
|
|
263
|
-
expect(headingOpen).toBeDefined();
|
|
264
|
-
expect(srcSlice(source, headingOpen!)).toBe('### ');
|
|
265
|
-
|
|
266
|
-
const closes = byRole(mappings, 'close_syntax');
|
|
267
|
-
const headingClose = closes.find(m => genSlice(html, m).includes('</h'));
|
|
268
|
-
expect(headingClose).toBeDefined();
|
|
269
|
-
expect(headingClose!.lengths[0]).toBe(0);
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
it('code fence open_syntax maps to opening fence, close_syntax to closing fence', () => {
|
|
273
|
-
const src = '```js\ncode\n```\n';
|
|
274
|
-
const { html, mappings, source } = renderMapped(src);
|
|
275
|
-
assertMappingsValid(source, html, mappings);
|
|
276
|
-
|
|
277
|
-
const opens = byRole(mappings, 'open_syntax');
|
|
278
|
-
const fenceOpen = opens.find(m => genSlice(html, m).includes('<pre>'));
|
|
279
|
-
expect(fenceOpen).toBeDefined();
|
|
280
|
-
expect(srcSlice(source, fenceOpen!)).toContain('```js');
|
|
281
|
-
|
|
282
|
-
const closes = byRole(mappings, 'close_syntax');
|
|
283
|
-
const fenceClose = closes.find(m => genSlice(html, m).includes('</code></pre>'));
|
|
284
|
-
expect(fenceClose).toBeDefined();
|
|
285
|
-
expect(srcSlice(source, fenceClose!)).toContain('```');
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
it('emphasis open/close syntax maps to single marker chars', () => {
|
|
289
|
-
const { html, mappings, source } = renderMapped('_hello_\n');
|
|
290
|
-
assertMappingsValid(source, html, mappings);
|
|
291
|
-
|
|
292
|
-
const opens = byRole(mappings, 'open_syntax');
|
|
293
|
-
const emOpen = opens.find(m => genSlice(html, m).includes('<em'));
|
|
294
|
-
expect(emOpen).toBeDefined();
|
|
295
|
-
expect(srcSlice(source, emOpen!)).toBe('_');
|
|
296
|
-
expect(emOpen!.lengths[0]).toBe(1);
|
|
297
|
-
|
|
298
|
-
const closes = byRole(mappings, 'close_syntax');
|
|
299
|
-
const emClose = closes.find(m => genSlice(html, m).includes('</em>'));
|
|
300
|
-
expect(emClose).toBeDefined();
|
|
301
|
-
expect(srcSlice(source, emClose!)).toBe('_');
|
|
302
|
-
expect(emClose!.lengths[0]).toBe(1);
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
it('mustache emits content + node + syntax', () => {
|
|
306
|
-
const { html, mappings, source } = renderMapped('{name}\n');
|
|
307
|
-
assertMappingsValid(source, html, mappings);
|
|
308
|
-
|
|
309
|
-
const contents = byRole(mappings, 'content');
|
|
310
|
-
const svelte = contents.filter(m => m.data.completion === true);
|
|
311
|
-
expect(svelte.length).toBeGreaterThanOrEqual(1);
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
it('unmapped render path is unaffected', () => {
|
|
315
|
-
const tree = new TreeBuilder(128);
|
|
316
|
-
const parser = new PFMParser(tree);
|
|
317
|
-
parser.parse('hello world\n');
|
|
318
|
-
const renderer = new CursorHTMLRenderer({ cache: false });
|
|
319
|
-
renderer.update(tree.get_buffer(), 'hello world\n');
|
|
320
|
-
expect(renderer.html).toBe('<p>hello world</p>');
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
// ── line starts tests ──
|
|
325
|
-
|
|
326
|
-
describe('build_line_starts', () => {
|
|
327
|
-
it('single line', () => {
|
|
328
|
-
const ls = build_line_starts('hello');
|
|
329
|
-
expect(Array.from(ls)).toEqual([0]);
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
it('multiple lines', () => {
|
|
333
|
-
const ls = build_line_starts('a\nb\nc\n');
|
|
334
|
-
expect(Array.from(ls)).toEqual([0, 2, 4, 6]);
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
it('empty string', () => {
|
|
338
|
-
const ls = build_line_starts('');
|
|
339
|
-
expect(Array.from(ls)).toEqual([0]);
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
describe('offset_to_position', () => {
|
|
344
|
-
const ls = build_line_starts('ab\ncd\nef\n');
|
|
345
|
-
|
|
346
|
-
it('start of first line', () => {
|
|
347
|
-
expect(offset_to_position(ls, 0)).toEqual([0, 0]);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it('middle of first line', () => {
|
|
351
|
-
expect(offset_to_position(ls, 1)).toEqual([0, 1]);
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
it('start of second line', () => {
|
|
355
|
-
expect(offset_to_position(ls, 3)).toEqual([1, 0]);
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
it('middle of third line', () => {
|
|
359
|
-
expect(offset_to_position(ls, 7)).toEqual([2, 1]);
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
// ── v3 conversion tests ──
|
|
364
|
-
|
|
365
|
-
describe('mappings_to_v3', () => {
|
|
366
|
-
it('produces valid v3 structure', () => {
|
|
367
|
-
const { html, mappings, source } = renderMapped('hello\n');
|
|
368
|
-
const v3 = mappings_to_v3(mappings, source, html, 'test.md');
|
|
369
|
-
|
|
370
|
-
expect(v3.version).toBe(3);
|
|
371
|
-
expect(v3.file).toBe('test.md');
|
|
372
|
-
expect(v3.sources).toEqual(['test.md']);
|
|
373
|
-
expect(v3.sourcesContent).toEqual([source]);
|
|
374
|
-
expect(v3.names).toEqual([]);
|
|
375
|
-
expect(typeof v3.mappings).toBe('string');
|
|
376
|
-
expect(v3.mappings.length).toBeGreaterThan(0);
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
it('uses basename for sources', () => {
|
|
380
|
-
const { html, mappings, source } = renderMapped('hello\n');
|
|
381
|
-
const v3 = mappings_to_v3(mappings, source, html, '/path/to/file.md');
|
|
382
|
-
|
|
383
|
-
expect(v3.file).toBe('file.md');
|
|
384
|
-
expect(v3.sources).toEqual(['file.md']);
|
|
385
|
-
});
|
|
386
|
-
|
|
387
|
-
it('v3 excludes syntax spans', () => {
|
|
388
|
-
const { html, mappings, source } = renderMapped('### hello\n');
|
|
389
|
-
|
|
390
|
-
// enriched mappings have syntax spans
|
|
391
|
-
const opens = byRole(mappings, 'open_syntax');
|
|
392
|
-
expect(opens.length).toBeGreaterThan(0);
|
|
393
|
-
|
|
394
|
-
// v3 should only have content + node anchors
|
|
395
|
-
const v3 = mappings_to_v3(mappings, source, html);
|
|
396
|
-
expect(v3.mappings).toMatch(/^[A-Za-z0-9+/,;]*$/);
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
it('vlq decodes correctly and maps to right positions', () => {
|
|
400
|
-
const { html, mappings, source } = renderMapped('hello world\n');
|
|
401
|
-
const v3 = mappings_to_v3(mappings, source, html);
|
|
402
|
-
|
|
403
|
-
const decoded = decodeVLQMappings(v3.mappings);
|
|
404
|
-
expect(decoded.length).toBeGreaterThan(0);
|
|
405
|
-
expect(decoded[0].length).toBeGreaterThan(0);
|
|
406
|
-
|
|
407
|
-
// 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');
|
|
409
|
-
expect(textSeg).toBeDefined();
|
|
410
|
-
expect(textSeg!.length).toBe(4);
|
|
411
|
-
|
|
412
|
-
// source position should point to 'hello' in source
|
|
413
|
-
const src_line = textSeg![2];
|
|
414
|
-
const src_col = textSeg![3];
|
|
415
|
-
const src_lines = source.split('\n');
|
|
416
|
-
expect(src_lines[src_line].slice(src_col, src_col + 5)).toBe('hello');
|
|
417
|
-
});
|
|
418
|
-
|
|
419
|
-
it('survives svelte compiler remapping', () => {
|
|
420
|
-
const svelte = require('svelte/compiler');
|
|
421
|
-
// single-element output — Svelte generates per-element mappings for these
|
|
422
|
-
const src = 'hello world\n';
|
|
423
|
-
const { html, mappings, source } = renderMapped(src);
|
|
424
|
-
const v3 = mappings_to_v3(mappings, source, html, 'test.md');
|
|
425
|
-
|
|
426
|
-
const result = svelte.compile(html, {
|
|
427
|
-
filename: 'test.md',
|
|
428
|
-
generate: 'client',
|
|
429
|
-
sourcemap: v3,
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
expect(result.js.map).toBeDefined();
|
|
433
|
-
expect(result.js.map.mappings.length).toBeGreaterThan(0);
|
|
434
|
-
expect(result.js.map.sources).toContain('test.md');
|
|
435
|
-
});
|
|
436
|
-
});
|