@mdsvex/render 1.0.0-next.1 → 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.
@@ -1,67 +0,0 @@
1
- import { PFMParser } from '@mdsvex/parse';
2
- import { TreeBuilder } from '@mdsvex/parse/tree-builder';
3
- import { CursorHTMLRenderer } from '../src/html_cursor.ts';
4
- import { mappings_to_v3 } from '../src/sourcemap.ts';
5
- import * as svelte from 'svelte/compiler';
6
- import remapping from '@ampproject/remapping';
7
- import { writeFileSync } from 'node:fs';
8
- import { resolve } from 'node:path';
9
-
10
- const source = `<script>
11
- let name = 'world';
12
- let count = 0;
13
- </script>
14
-
15
- # Hello {name}
16
-
17
- The count is {count}.
18
-
19
- Some _formatted_ text with *bold* and \`code\`.
20
- `;
21
-
22
- const outDir = resolve(import.meta.dirname, '../../..');
23
-
24
- // Step 1: PFM → HTML + map (HTML → markdown)
25
- const tree = new TreeBuilder(128);
26
- const parser = new PFMParser(tree);
27
- parser.parse(source);
28
- const renderer = new CursorHTMLRenderer({ cache: false });
29
- const { mappings } = renderer.update_mapped(tree.get_buffer(), source);
30
- const html = renderer.html;
31
- const pfmMap = mappings_to_v3(mappings, source, html, 'test.svx');
32
-
33
- // Step 2: Svelte compile (produces JS → HTML map)
34
- // Don't pass sourcemap option — compiler doesn't chain, just compile clean
35
- const compiled = svelte.compile(html, {
36
- filename: 'test.svx',
37
- generate: 'client',
38
- });
39
-
40
- const compilerMap = compiled.js.map;
41
-
42
- // Step 3: Chain the two maps: (JS → HTML) + (HTML → markdown) = (JS → markdown)
43
- // @ampproject/remapping takes the outermost map and a loader for inner maps
44
- const chained = remapping(
45
- { ...compilerMap, version: 3 },
46
- (file) => {
47
- // When remapping asks "give me the sourcemap for test.svx", return our PFM map
48
- if (file === 'test.svx') {
49
- return { ...pfmMap, version: 3 };
50
- }
51
- return null;
52
- }
53
- );
54
-
55
- // Fix up sourcesContent to have the original markdown
56
- chained.sourcesContent = [source];
57
-
58
- writeFileSync(`${outDir}/test.svx`, source);
59
- writeFileSync(`${outDir}/test.svx.js`, compiled.js.code + '\n//# sourceMappingURL=test.svx.js.map\n');
60
- writeFileSync(`${outDir}/test.svx.js.map`, JSON.stringify(chained, null, 2));
61
-
62
- console.log('source:', source.length, 'bytes');
63
- console.log('js: ', compiled.js.code.length, 'bytes');
64
- console.log('compiler map:', compilerMap.mappings.length, 'VLQ chars');
65
- console.log('chained map: ', chained.mappings.length, 'VLQ chars');
66
- console.log('sources:', chained.sources);
67
- console.log('files: test.svx, test.svx.js, test.svx.js.map');
@@ -1,91 +0,0 @@
1
- import { PFMParser } from '@mdsvex/parse';
2
- import { TreeBuilder } from '@mdsvex/parse/tree-builder';
3
- import { CursorHTMLRenderer } from '../src/html_cursor.ts';
4
- import { mappings_to_v3 } from '../src/sourcemap.ts';
5
- import * as svelte from 'svelte/compiler';
6
- import remapping from '@ampproject/remapping';
7
- import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
8
-
9
- const FILE = '/src/routes/+page.svx';
10
-
11
- const md = `<script>
12
- let name = 'world';
13
- </script>
14
-
15
- # Hello {name}
16
-
17
- Some _formatted_ text.
18
- `;
19
-
20
- // Step 1: mdsvex vite plugin transform
21
- const tree = new TreeBuilder(128);
22
- const parser = new PFMParser(tree);
23
- parser.parse(md);
24
- const renderer = new CursorHTMLRenderer({ cache: false });
25
- const { mappings } = renderer.update_mapped(tree.get_buffer(), md);
26
- const html = renderer.html;
27
- const mdsvexMap = mappings_to_v3(mappings, md, html, FILE);
28
-
29
- console.log('=== Step 1: mdsvex transform ===');
30
- console.log('map.sources:', mdsvexMap.sources);
31
- console.log('map.file:', mdsvexMap.file);
32
- console.log('map.sourcesContent present:', !!mdsvexMap.sourcesContent?.[0]);
33
- console.log('map.mappings length:', mdsvexMap.mappings.length);
34
-
35
- // Step 2: svelte.preprocess (vite-plugin-svelte preprocess stage)
36
- // In Vite, this gets the code from step 1's output
37
- const preprocessed = await svelte.preprocess(html, [/* no preprocessors */], { filename: FILE });
38
- console.log('\n=== Step 2: svelte.preprocess ===');
39
- console.log('map present:', !!preprocessed.map);
40
- // If no preprocessors change the code, map may be null/identity
41
-
42
- // Step 3: svelte.compile
43
- const compiled = svelte.compile(preprocessed.code, {
44
- filename: FILE,
45
- generate: 'client',
46
- });
47
- const svelteMap = compiled.js.map;
48
-
49
- console.log('\n=== Step 3: svelte.compile ===');
50
- console.log('map.sources:', svelteMap.sources);
51
- console.log('map.file:', svelteMap.file);
52
- console.log('map.mappings length:', svelteMap.mappings.length);
53
-
54
- // Step 4: What Vite's getCombinedSourcemap does
55
- // It chains: [compile_map, preprocess_map, mdsvex_map]
56
- // Since preprocess_map is null/identity, it's effectively [compile_map, mdsvex_map]
57
- console.log('\n=== Step 4: Chain (simulating getCombinedSourcemap) ===');
58
-
59
- const chained = remapping([svelteMap, mdsvexMap], () => null);
60
- console.log('chained.sources:', chained.sources);
61
- console.log('chained.file:', chained.file);
62
- console.log('chained.sourcesContent?.[0]?.slice(0,50):', chained.sourcesContent?.[0]?.slice(0, 50));
63
- console.log('chained.mappings length:', chained.mappings.length);
64
-
65
- // Step 5: verify trace
66
- console.log('\n=== Trace verification ===');
67
- const t = new TraceMap(chained);
68
- const jsLines = compiled.js.code.split('\n');
69
- const mdLines = md.split('\n');
70
-
71
- // Find the 'let name' line in JS
72
- for (let line = 0; line < jsLines.length; line++) {
73
- const idx = jsLines[line].indexOf('name');
74
- if (idx !== -1 && jsLines[line].includes("'world'")) {
75
- const orig = originalPositionFor(t, { line: line + 1, column: idx });
76
- console.log(`js ${line+1}:${idx} "${jsLines[line].trim()}" → ${orig.source}:${orig.line}:${orig.column}`);
77
- if (orig.line) {
78
- console.log(` source content: "${mdLines[orig.line-1]?.trim()}"`);
79
- }
80
- }
81
- }
82
-
83
- // What the browser sees:
84
- console.log('\n=== What browser devtools see ===');
85
- console.log('Source URL in map:', chained.sources?.[0]);
86
- console.log('Can browser fetch this?', chained.sources?.[0]?.startsWith('/') ? 'Absolute path — Vite resolves via /@fs/ or relative' : 'Relative path');
87
- console.log('sourcesContent included:', !!chained.sourcesContent?.[0]);
88
- if (chained.sourcesContent?.[0]) {
89
- const content = chained.sourcesContent[0];
90
- console.log('sourcesContent is:', content.includes('<script>') && content.includes('# Hello') ? 'ORIGINAL MARKDOWN ✓' : content.includes('<h1>') ? 'INTERMEDIATE HTML ✗' : 'UNKNOWN');
91
- }
@@ -1,34 +0,0 @@
1
- import remapping from '@ampproject/remapping';
2
- import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
3
-
4
- // Simulate the mismatch: svelte uses basename, mdsvex uses full path
5
- const svelteMap = {
6
- version: 3,
7
- sources: ['+page.svx'], // basename — what svelte.compile produces
8
- mappings: 'AAAA',
9
- names: [],
10
- };
11
-
12
- const mdsvexMap = {
13
- version: 3,
14
- sources: ['/src/routes/+page.svx'], // full path — what we produce
15
- sourcesContent: ['# Hello World\n'],
16
- mappings: 'AAAA',
17
- names: [],
18
- };
19
-
20
- // Try chaining with loader (how Vite does it internally)
21
- console.log('=== Loader-based chaining ===');
22
- const chained1 = remapping(svelteMap, (file) => {
23
- console.log(' loader asked for:', JSON.stringify(file));
24
- if (file === '+page.svx') return mdsvexMap;
25
- return null;
26
- });
27
- console.log('result sources:', chained1.sources);
28
- console.log('result sourcesContent:', chained1.sourcesContent);
29
-
30
- // Try array-based chaining
31
- console.log('\n=== Array-based chaining ===');
32
- const chained2 = remapping([svelteMap, mdsvexMap], () => null);
33
- console.log('result sources:', chained2.sources);
34
- console.log('result sourcesContent:', chained2.sourcesContent);
@@ -1,120 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { PFMParser, WireEmitter } from '@mdsvex/parse';
3
- import { TreeBuilder } from '@mdsvex/parse/tree-builder';
4
- import { WireTreeBuilder } from '@mdsvex/parse/wire-tree-builder';
5
- import { ComponentRenderer } from '../src/component';
6
-
7
- // Helpers
8
-
9
- function parse_to_buf(source: string) {
10
- const tree = new TreeBuilder(source.length >> 3 || 128);
11
- const parser = new PFMParser(tree);
12
- parser.parse(source);
13
- return { buf: tree.get_buffer(), source };
14
- }
15
-
16
- // ComponentRenderer
17
-
18
- describe('ComponentRenderer', () => {
19
- it('produces blocks with buffer indices', () => {
20
- const { buf, source } = parse_to_buf('# Hello\n\nworld\n');
21
- const renderer = new ComponentRenderer();
22
- renderer.update(buf, source);
23
-
24
- expect(renderer.blocks.length).toBe(2);
25
- // Verify the indices point to real nodes
26
- expect(buf.kind_at(renderer.blocks[0].idx)).toBe(3); // heading
27
- expect(buf.kind_at(renderer.blocks[1].idx)).toBe(7); // paragraph
28
- });
29
-
30
- it('each block has a unique idx', () => {
31
- const { buf, source } = parse_to_buf('# Hello\n\nworld\n');
32
- const renderer = new ComponentRenderer();
33
- renderer.update(buf, source);
34
-
35
- expect(renderer.blocks[0].idx).not.toBe(renderer.blocks[1].idx);
36
- });
37
-
38
- it('blocks have version 0 on first update', () => {
39
- const { buf, source } = parse_to_buf('# Hello\n');
40
- const renderer = new ComponentRenderer();
41
- renderer.update(buf, source);
42
-
43
- expect(renderer.blocks[0].v).toBe(0);
44
- });
45
-
46
- it('skips line_break nodes', () => {
47
- const { buf, source } = parse_to_buf('# Hello\n\nworld\n');
48
- const renderer = new ComponentRenderer();
49
- renderer.update(buf, source);
50
-
51
- for (const block of renderer.blocks) {
52
- expect(buf.kind_at(block.idx)).not.toBe(6); // line_break
53
- }
54
- });
55
-
56
- it('incremental: closed blocks stop version bumping', () => {
57
- const tree = new TreeBuilder(128);
58
- const parser = new PFMParser(tree);
59
- const renderer = new ComponentRenderer();
60
-
61
- parser.init();
62
-
63
- parser.feed('# First\n\n');
64
- renderer.update(tree.get_buffer(), '# First\n\n');
65
- const heading_v = renderer.blocks[0]?.v;
66
-
67
- parser.feed('second\n');
68
- parser.finish();
69
- renderer.update(tree.get_buffer(), '# First\n\nsecond\n');
70
-
71
- expect(renderer.blocks[0].v).toBe(heading_v);
72
- expect(renderer.blocks.length).toBe(2);
73
- });
74
-
75
- it('incremental: open blocks bump version', () => {
76
- const tree = new TreeBuilder(128);
77
- const parser = new PFMParser(tree);
78
- const renderer = new ComponentRenderer();
79
-
80
- parser.init();
81
-
82
- parser.feed('hel');
83
- renderer.update(tree.get_buffer(), 'hel');
84
- const v1 = renderer.blocks[0]?.v ?? -1;
85
-
86
- parser.feed('lo\n');
87
- renderer.update(tree.get_buffer(), 'hello\n');
88
-
89
- expect(renderer.blocks[0].v).toBeGreaterThan(v1);
90
- });
91
-
92
- it('returns new array reference on change', () => {
93
- const { buf, source } = parse_to_buf('hello\n');
94
- const renderer = new ComponentRenderer();
95
- const first = renderer.update(buf, source);
96
- const second = renderer.update(buf, source);
97
-
98
- // Second update with same closed doc, no change, same reference
99
- expect(second).toBe(first);
100
- });
101
-
102
- it('reset clears all state', () => {
103
- const { buf, source } = parse_to_buf('hello\n');
104
- const renderer = new ComponentRenderer();
105
- renderer.update(buf, source);
106
- expect(renderer.blocks.length).toBe(1);
107
-
108
- renderer.reset();
109
- expect(renderer.blocks.length).toBe(0);
110
- });
111
-
112
- it('stores buffer and source references', () => {
113
- const { buf, source } = parse_to_buf('# Title\n');
114
- const renderer = new ComponentRenderer();
115
- renderer.update(buf, source);
116
-
117
- expect(renderer.buf).toBe(buf);
118
- expect(renderer.source).toBe(source);
119
- });
120
- });
@@ -1,90 +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 {
6
- _escape_code,
7
- _escape_code_text,
8
- _escape_text_html,
9
- escape,
10
- escape_text,
11
- } from '../src/html_cursor';
12
-
13
- const DOCS = [
14
- 'a & b < c > d "e"\n\nplain\n',
15
- '`x < y && "z"` and `a\nb` then & more\n',
16
- '```js\nif (a < b && c > "d") {}\n```\n\ntext " after\n',
17
- '<div>\n\n& inside <em>html</em>\n\n</div>\n\n> quote & "q"\n',
18
- '| a & b | "c" |\n|:-|-:|\n| <x> | y > z |\n',
19
- 'no escapes here at all\n\n- item\n- item two\n',
20
- '&&&<<<>>>"""\n',
21
- 'a &lt; b &copy; & c &#123; &#x7d; &MadeUp; &copy\n\n`&lt; {x}` &amp;\n',
22
- '<div>\n\na &mdash; b & "c"\n\n</div>\n\n&#12345678; &#; &;\n',
23
- '```js\nconst s = "&amp;" && 1;\n```\n\n&copy; `&lt;` &copy;\n',
24
- '{x} `{ a: "1" }` {y} and `}{`\n\n```js {1}\nconst o = { a: 1 } && "b";\n```\n\n{z} } {\n',
25
- ];
26
-
27
- function cursor_for(source: string): { c: Cursor } {
28
- const tree = new TreeBuilder(source.length >> 3 || 128);
29
- new PFMParser(tree).parse(source);
30
- return { c: new Cursor(tree.get_buffer(), source) };
31
- }
32
-
33
- function indices(c: Cursor): number[] {
34
- const out: number[] = [];
35
- const walk = (): void => {
36
- out.push(c.index);
37
- if (!c.goto_first_child()) return;
38
- do walk();
39
- while (c.goto_next_sibling());
40
- c.goto_parent();
41
- };
42
- c.reset();
43
- walk();
44
- return out;
45
- }
46
-
47
- function check(c: Cursor, order: number[]): void {
48
- for (const idx of order) {
49
- (c as unknown as { idx: number }).idx = idx;
50
- expect(escape_text(c)).toBe(_escape_text_html(c.text()));
51
- // code shares the index, so its lookups interleave with the text ones
52
- expect(_escape_code_text(c)).toBe(_escape_code(c.text()));
53
- }
54
- }
55
-
56
- describe('escape_text', () => {
57
- for (const doc of DOCS) {
58
- it(`matches _escape_text_html(text()) in any visit order: ${JSON.stringify(doc)}`, () => {
59
- const { c } = cursor_for(doc);
60
- const order = indices(c);
61
- check(c, order);
62
- check(c, order.slice().reverse());
63
- // interleave front and back so the index keeps moving backwards
64
- const mixed: number[] = [];
65
- for (let i = 0, j = order.length - 1; i <= j; i++, j--) {
66
- mixed.push(order[i]);
67
- if (i !== j) mixed.push(order[j]);
68
- }
69
- check(c, mixed);
70
- });
71
- }
72
-
73
- it('escapes an & a backslash escaped, the slice no longer shows it', () => {
74
- const { c } = cursor_for('a \\&copy; b \\\\&copy;\n');
75
- const out = indices(c).map((idx) => {
76
- c.move_to(idx);
77
- return escape_text(c);
78
- });
79
- expect(out).toContain('&amp;copy; b ');
80
- expect(out).toContain('\\&copy;');
81
- });
82
-
83
- it('follows the cursor to a new source', () => {
84
- const a = cursor_for(DOCS[0]);
85
- const b = cursor_for(DOCS[2]);
86
- check(a.c, indices(a.c));
87
- check(b.c, indices(b.c));
88
- check(a.c, indices(a.c));
89
- });
90
- });
@@ -1,242 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { PFMParser } from '@mdsvex/parse';
3
- import { TreeBuilder } from '@mdsvex/parse/tree-builder';
4
- import { ComponentScope, CursorHTMLRenderer } from '../src/html_cursor';
5
- import type {
6
- CodeHighlighter,
7
- HighlightedBlock,
8
- HighlightWarningCode,
9
- } from '../src/html_cursor';
10
-
11
- /** a highlighter that shows what it was asked, braces as entities */
12
- function stub(): CodeHighlighter & {
13
- calls: string[];
14
- warned: [HighlightWarningCode, number][];
15
- } {
16
- const esc = (s: string) =>
17
- s
18
- .replace(/&/g, '&amp;')
19
- .replace(/</g, '&lt;')
20
- .replace(/{/g, '&#123;')
21
- .replace(/}/g, '&#125;');
22
- return {
23
- calls: [],
24
- warned: [],
25
- block(code, lang, meta, start): HighlightedBlock | null {
26
- this.calls.push(`block ${lang}|${meta}|${start}`);
27
- if (lang === 'plain') return null;
28
- if (lang === 'wrap')
29
- return {
30
- before: '',
31
- attributes: null,
32
- body: `<div>${esc(code)}</div>`,
33
- after: '',
34
- props: '',
35
- code,
36
- code_template: null,
37
- live: null,
38
- dropped: null,
39
- };
40
- if (lang === 'live') {
41
- // the first {...} of the code stays live
42
- const at = code.indexOf('{');
43
- const end = code.indexOf('}', at) + 1;
44
- const head = '<code>' + esc(code.slice(0, at));
45
- return {
46
- before: '',
47
- attributes: '',
48
- body: head + code.slice(at, end) + esc(code.slice(end)) + '</code>',
49
- after: '',
50
- props: '',
51
- code,
52
- code_template: '`live`',
53
- live: [head.length, at, end - at],
54
- dropped: null,
55
- };
56
- }
57
- return {
58
- before: '<figure>',
59
- attributes: ` class="hl ${lang}"`,
60
- body: `<code>${esc(code)}</code>`,
61
- after: '</figure>',
62
- props: ' title={"t"}',
63
- code: code.toUpperCase(),
64
- code_template: null,
65
- live: null,
66
- dropped: meta.includes('drop') ? ['dropped one'] : null,
67
- };
68
- },
69
- inline(code, lang, start) {
70
- this.calls.push(`inline ${lang}|${start}`);
71
- if (lang === 'plain') return null;
72
- if (lang === 'wrap')
73
- return { attributes: null, body: `<b>${esc(code)}</b>` };
74
- return { attributes: ` class="i ${lang}"`, body: esc(code) };
75
- },
76
- warn(code, _message, start) {
77
- this.warned.push([code, start]);
78
- },
79
- };
80
- }
81
-
82
- type Walk = (r: CursorHTMLRenderer, buf: any, src: string) => void;
83
- const WALKS: [string, Walk, boolean][] = [
84
- ['fold', (r, buf, src) => r.update(buf, src), false],
85
- ['cached', (r, buf, src) => r.update(buf, src), true],
86
- ['mapped', (r, buf, src) => r.update_mapped(buf, src), false],
87
- ['trace', (r, buf, src) => r.update_trace(buf, src), false],
88
- ['v3', (r, buf, src) => r.update_v3(buf, src, src), false],
89
- ];
90
-
91
- function render_all(
92
- source: string,
93
- highlight: CodeHighlighter,
94
- scope?: ComponentScope
95
- ): string {
96
- const outs: string[] = [];
97
- for (const [name, walk, cache] of WALKS) {
98
- // the cached render ignores replacements
99
- if (cache && scope) continue;
100
- const tree = new TreeBuilder(128);
101
- new PFMParser(tree).parse(source);
102
- const r = new CursorHTMLRenderer({ cache });
103
- r.highlight = highlight;
104
- if (scope) r.scope = scope;
105
- walk(r, tree.get_buffer(), source);
106
- outs.push(r.html);
107
- expect(r.highlight, name).toBe(highlight);
108
- }
109
- for (const out of outs) expect(out).toBe(outs[0]);
110
- return outs[0];
111
- }
112
-
113
- describe('highlighting', () => {
114
- it('a fence renders as the highlighter has it, in every walk', () => {
115
- const h = stub();
116
- expect(render_all('```ts a {1}\nx {y}\n```\n', h)).toBe(
117
- '<figure><pre class="hl ts"><code>x &#123;y&#125;</code></pre></figure>'
118
- );
119
- expect(new Set(h.calls)).toEqual(new Set(['block ts|a {1}|0']));
120
- });
121
-
122
- it('a fence with no info has no language', () => {
123
- const h = stub();
124
- render_all('a\n\n```\nx\n```\n', h);
125
- expect(new Set(h.calls)).toEqual(new Set(['block ||3']));
126
- });
127
-
128
- it('null renders plain', () => {
129
- expect(render_all('```plain x\n{a}\n```\n\n`#!plain b`\n', stub())).toBe(
130
- '<pre><code class="language-plain">&#123;a&#125;</code></pre><p><code>b</code></p>'
131
- );
132
- });
133
-
134
- it('a block that is not one pre renders its body alone', () => {
135
- expect(render_all('```wrap\na\n```\n', stub())).toBe('<div>a</div>');
136
- });
137
-
138
- it('a code span with a #! hint highlights, one without does not', () => {
139
- const h = stub();
140
- expect(render_all('`#!ts {a}` `{b}` `#!wrap c`\n', h)).toBe(
141
- '<p><code class="i ts">&#123;a&#125;</code> <code>&#123;b&#125;</code> <b>c</b></p>'
142
- );
143
- expect(new Set(h.calls)).toEqual(
144
- new Set(['inline ts|0', 'inline wrap|17'])
145
- );
146
- });
147
-
148
- it('a code span hint keeps its newlines as spaces', () => {
149
- expect(render_all('`#!ts a\nb`\n', stub())).toBe(
150
- '<p><code class="i ts">a b</code></p>'
151
- );
152
- });
153
-
154
- it('a pre replacement gets the props and the <pre> as children, no figure', () => {
155
- const h = stub();
156
- const scope = new ComponentScope(
157
- [{ specifier: 'm', names: ['pre', 'code'] }],
158
- 'G'
159
- );
160
- expect(
161
- render_all(
162
- '```ts title="t" drop\na\n```\n\n```wrap\nb\n```\n\n`#!ts c`\n',
163
- h,
164
- scope
165
- )
166
- ).toBe(
167
- "<script>\nimport { pre as Pre_MDSVEX_G, code as Code_MDSVEX_G } from 'm';\n</script>" +
168
- '<Pre_MDSVEX_G lang={"ts"} meta={"title=\\"t\\" drop"} title={"t"} code={"A"}><pre class="hl ts"><code>a</code></pre></Pre_MDSVEX_G>' +
169
- '<Pre_MDSVEX_G lang={"wrap"} code={"b"}><div>b</div></Pre_MDSVEX_G>' +
170
- '<p><Code_MDSVEX_G lang={"ts"} class="i ts">c</Code_MDSVEX_G></p>'
171
- );
172
- expect(h.warned.every(([code]) => code === 'meta_prop_ignored')).toBe(true);
173
- expect(h.warned.length).toBeGreaterThan(0);
174
- });
175
-
176
- it('a code replacement warns when the span is not one <code>', () => {
177
- const h = stub();
178
- const scope = new ComponentScope(
179
- [{ specifier: 'm', names: ['code'] }],
180
- 'G'
181
- );
182
- expect(render_all('`#!wrap a`\n', h, scope)).toContain('<p><b>a</b></p>');
183
- expect(new Set(h.warned.map(([c]) => c))).toEqual(
184
- new Set(['code_replacement_skipped'])
185
- );
186
- });
187
-
188
- it('the fence body maps as one code content record', () => {
189
- const source = '```ts\nlet a\n```\n';
190
- const tree = new TreeBuilder(128);
191
- new PFMParser(tree).parse(source);
192
- const r = new CursorHTMLRenderer({ cache: false });
193
- r.highlight = stub();
194
- const { mappings } = r.update_mapped(tree.get_buffer(), source);
195
- const content = mappings.filter((m) => m.data.role === 'content');
196
- expect(content).toHaveLength(1);
197
- const [m] = content;
198
- expect(
199
- source.slice(m.sourceOffsets[0], m.sourceOffsets[0] + m.lengths[0])
200
- ).toBe('let a');
201
- expect(
202
- r.html.slice(
203
- m.generatedOffsets[0],
204
- m.generatedOffsets[0] + m.generatedLengths![0]
205
- )
206
- ).toBe('<code>let a</code>');
207
- });
208
-
209
- it('a live expression maps one to one as svelte content in every walk', () => {
210
- const source = '> ```live\n> a {b.c} d\n> ```\n';
211
- const scope = new ComponentScope([{ specifier: 'm', names: ['pre'] }], 'G');
212
- expect(render_all(source, stub())).toContain(
213
- '<pre><code>a {b.c} d</code></pre>'
214
- );
215
- expect(render_all(source, stub(), scope)).toContain(
216
- '<Pre_MDSVEX_G lang={"live"} code={`live`}><pre><code>a {b.c} d</code></pre></Pre_MDSVEX_G>'
217
- );
218
- for (const s of [null, scope]) {
219
- const tree = new TreeBuilder(128);
220
- new PFMParser(tree).parse(source);
221
- const r = new CursorHTMLRenderer({ cache: false });
222
- r.highlight = stub();
223
- if (s) r.scope = s;
224
- const { mappings } = r.update_mapped(tree.get_buffer(), source);
225
- const content = mappings
226
- .filter((m) => m.data.role === 'content')
227
- .map((m) => [
228
- source.slice(m.sourceOffsets[0], m.sourceOffsets[0] + m.lengths[0]),
229
- r.html.slice(
230
- m.generatedOffsets[0],
231
- m.generatedOffsets[0] + (m.generatedLengths?.[0] ?? m.lengths[0])
232
- ),
233
- m.data.verification === true,
234
- ]);
235
- expect(content).toEqual([
236
- ['> a ', '<code>a ', false],
237
- ['{b.c}', '{b.c}', true],
238
- [' d', ' d</code>', false],
239
- ]);
240
- }
241
- });
242
- });