@mdsvex/render 0.0.0 → 1.0.0-next.0
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/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/dist/component.js +24 -0
- package/dist/html_cursor.js +510 -0
- package/dist/index.js +27 -0
- package/dist/mappings.js +23 -0
- package/dist/sourcemap.js +78 -0
- package/package.json +48 -7
- package/src/Node.svelte +245 -0
- package/src/component.ts +88 -0
- package/src/html_cursor.ts +855 -0
- package/src/index.ts +26 -0
- package/src/mappings.ts +77 -0
- package/src/sourcemap.ts +176 -0
- package/test/_gen_chain.mjs +67 -0
- package/test/_vite_debug.mjs +91 -0
- package/test/_vite_debug2.mjs +34 -0
- package/test/component.spec.ts +120 -0
- package/test/html_cursor.spec.ts +231 -0
- package/test/mappings.spec.ts +436 -0
- package/test/wire_tree_builder.spec.ts +139 -0
- package/tsconfig.json +11 -0
- package/vite.config.build.ts +33 -0
- package/README.md +0 -3
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export { CursorHTMLRenderer } from "./html_cursor";
|
|
2
|
+
export type { CursorBlockEntry, PendingMapping } from "./html_cursor";
|
|
3
|
+
export {
|
|
4
|
+
_emit,
|
|
5
|
+
_node,
|
|
6
|
+
_children,
|
|
7
|
+
_resolve_mappings,
|
|
8
|
+
escape,
|
|
9
|
+
NONE,
|
|
10
|
+
K_ROOT,
|
|
11
|
+
K_TEXT,
|
|
12
|
+
K_HTML,
|
|
13
|
+
K_LINE_BREAK,
|
|
14
|
+
K_FRONTMATTER,
|
|
15
|
+
K_IMPORT_STATEMENT,
|
|
16
|
+
} from "./html_cursor";
|
|
17
|
+
export { ComponentRenderer } from "./component";
|
|
18
|
+
export type { ComponentBlock } from "./component";
|
|
19
|
+
export { CI_TEXT, CI_CODE, CI_SVELTE, CI_STRUCTURE } from "./mappings";
|
|
20
|
+
export type { Mapping, CodeInformation, MappingRole, MappingData } from "./mappings";
|
|
21
|
+
export {
|
|
22
|
+
build_line_starts,
|
|
23
|
+
offset_to_position,
|
|
24
|
+
mappings_to_v3,
|
|
25
|
+
} from "./sourcemap";
|
|
26
|
+
export type { SourceMapV3 } from "./sourcemap";
|
package/src/mappings.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* volar-compatible source mapping types.
|
|
3
|
+
*
|
|
4
|
+
* structurally compatible with @volar/source-map Mapping and
|
|
5
|
+
* @volar/language-core CodeInformation, no runtime dependency.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface Mapping<Data = unknown> {
|
|
9
|
+
sourceOffsets: number[];
|
|
10
|
+
generatedOffsets: number[];
|
|
11
|
+
lengths: number[];
|
|
12
|
+
generatedLengths?: number[];
|
|
13
|
+
data: Data;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CodeInformation {
|
|
17
|
+
verification?:
|
|
18
|
+
| boolean
|
|
19
|
+
| {
|
|
20
|
+
shouldReport?(
|
|
21
|
+
source: string | undefined,
|
|
22
|
+
code: string | number | undefined,
|
|
23
|
+
): boolean;
|
|
24
|
+
};
|
|
25
|
+
completion?: boolean | { isAdditional?: boolean; onlyImport?: boolean };
|
|
26
|
+
semantic?: boolean | { shouldHighlight?(): boolean };
|
|
27
|
+
navigation?:
|
|
28
|
+
| boolean
|
|
29
|
+
| {
|
|
30
|
+
shouldRename?(): boolean;
|
|
31
|
+
resolveRenameNewName?(newName: string): string;
|
|
32
|
+
resolveRenameEditText?(newText: string): string;
|
|
33
|
+
};
|
|
34
|
+
structure?: boolean;
|
|
35
|
+
format?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// CodeInformation presets
|
|
39
|
+
|
|
40
|
+
/** text content: verification, semantic, navigation. */
|
|
41
|
+
export const CI_TEXT: CodeInformation = {
|
|
42
|
+
verification: true,
|
|
43
|
+
semantic: true,
|
|
44
|
+
navigation: true,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** code content (code spans, fences): semantic + navigation only. */
|
|
48
|
+
export const CI_CODE: CodeInformation = {
|
|
49
|
+
semantic: true,
|
|
50
|
+
navigation: true,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** svelte expressions: full capabilities. */
|
|
54
|
+
export const CI_SVELTE: CodeInformation = {
|
|
55
|
+
verification: true,
|
|
56
|
+
completion: true,
|
|
57
|
+
semantic: true,
|
|
58
|
+
navigation: true,
|
|
59
|
+
structure: true,
|
|
60
|
+
format: true,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** generated structural elements: structure only. */
|
|
64
|
+
export const CI_STRUCTURE: CodeInformation = {
|
|
65
|
+
structure: true,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// mapping identity
|
|
69
|
+
|
|
70
|
+
export type MappingRole = "node" | "content" | "open_syntax" | "close_syntax";
|
|
71
|
+
|
|
72
|
+
export interface MappingData extends CodeInformation {
|
|
73
|
+
/** node buffer index: stable, monotonic in document order. */
|
|
74
|
+
nodeIndex: number;
|
|
75
|
+
/** what this mapping represents within the node. */
|
|
76
|
+
role: MappingRole;
|
|
77
|
+
}
|
package/src/sourcemap.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sourcemap utilities: line-starts, offset to position, vlq, v3 conversion.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Mapping, MappingData } from "./mappings";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/** build an array of byte offsets where each line begins. line 0 starts at 0. */
|
|
9
|
+
export function build_line_starts(source: string): Uint32Array {
|
|
10
|
+
const starts: number[] = [0];
|
|
11
|
+
for (let i = 0; i < source.length; i++) {
|
|
12
|
+
if (source.charCodeAt(i) === 10) starts.push(i + 1);
|
|
13
|
+
}
|
|
14
|
+
return new Uint32Array(starts);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** convert a byte offset to [line, column] using a precomputed line_starts table. */
|
|
18
|
+
export function offset_to_position(
|
|
19
|
+
line_starts: Uint32Array,
|
|
20
|
+
offset: number,
|
|
21
|
+
): [line: number, col: number] {
|
|
22
|
+
let lo = 0,
|
|
23
|
+
hi = line_starts.length;
|
|
24
|
+
while (lo < hi) {
|
|
25
|
+
const mid = (lo + hi) >>> 1;
|
|
26
|
+
if (line_starts[mid] <= offset) lo = mid + 1;
|
|
27
|
+
else hi = mid;
|
|
28
|
+
}
|
|
29
|
+
const line = lo - 1;
|
|
30
|
+
return [line, offset - line_starts[line]];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const VLQ_CHARS =
|
|
35
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
36
|
+
|
|
37
|
+
function vlq_encode(value: number): string {
|
|
38
|
+
let vlq = value < 0 ? (-value << 1) | 1 : value << 1;
|
|
39
|
+
let result = "";
|
|
40
|
+
do {
|
|
41
|
+
let digit = vlq & 0x1f;
|
|
42
|
+
vlq >>>= 5;
|
|
43
|
+
if (vlq > 0) digit |= 0x20;
|
|
44
|
+
result += VLQ_CHARS[digit];
|
|
45
|
+
} while (vlq > 0);
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
export interface SourceMapV3 {
|
|
51
|
+
version: 3;
|
|
52
|
+
file?: string;
|
|
53
|
+
sources: string[];
|
|
54
|
+
sourcesContent: (string | null)[];
|
|
55
|
+
names: string[];
|
|
56
|
+
mappings: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* convert Mapping<MappingData>[] to a v3 sourcemap.
|
|
61
|
+
*
|
|
62
|
+
* emits content spans (for cursor placement) and node span start anchors
|
|
63
|
+
* (for block identification). skips open_syntax and close_syntax to avoid
|
|
64
|
+
* overlapping ranges that cause nondeterministic cursor jumps in devtools.
|
|
65
|
+
*/
|
|
66
|
+
export function mappings_to_v3(
|
|
67
|
+
mappings: Mapping<MappingData>[],
|
|
68
|
+
source: string,
|
|
69
|
+
generated: string,
|
|
70
|
+
file?: string,
|
|
71
|
+
): SourceMapV3 {
|
|
72
|
+
const src_lines = build_line_starts(source);
|
|
73
|
+
const gen_lines = build_line_starts(generated);
|
|
74
|
+
|
|
75
|
+
interface Segment {
|
|
76
|
+
gen_offset: number;
|
|
77
|
+
src_offset: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const segments: Segment[] = [];
|
|
81
|
+
for (const m of mappings) {
|
|
82
|
+
const role = m.data.role;
|
|
83
|
+
if (role === "open_syntax" || role === "close_syntax") continue;
|
|
84
|
+
|
|
85
|
+
for (let i = 0; i < m.sourceOffsets.length; i++) {
|
|
86
|
+
const gen_start = m.generatedOffsets[i];
|
|
87
|
+
const src_start = m.sourceOffsets[i];
|
|
88
|
+
|
|
89
|
+
if (role === "node") {
|
|
90
|
+
// collapse node span to start anchor
|
|
91
|
+
segments.push({ gen_offset: gen_start, src_offset: src_start });
|
|
92
|
+
} else {
|
|
93
|
+
const gen_len = m.generatedLengths
|
|
94
|
+
? m.generatedLengths[i]
|
|
95
|
+
: m.lengths[i];
|
|
96
|
+
const src_len = m.lengths[i];
|
|
97
|
+
|
|
98
|
+
if (!m.generatedLengths && src_len === gen_len) {
|
|
99
|
+
// identity-mapped content (source text = generated text).
|
|
100
|
+
// emit per-character segments so downstream chaining
|
|
101
|
+
// preserves column precision. VLQ is compact for identity
|
|
102
|
+
// runs (CAAC repeated).
|
|
103
|
+
for (let d = 0; d < gen_len; d++) {
|
|
104
|
+
segments.push({
|
|
105
|
+
gen_offset: gen_start + d,
|
|
106
|
+
src_offset: src_start + d,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
// escaped/transformed content, lengths differ.
|
|
111
|
+
// emit start point only.
|
|
112
|
+
segments.push({
|
|
113
|
+
gen_offset: gen_start,
|
|
114
|
+
src_offset: src_start,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// sort by generated offset
|
|
122
|
+
segments.sort((a, b) => a.gen_offset - b.gen_offset);
|
|
123
|
+
|
|
124
|
+
// encode as vlq
|
|
125
|
+
let prev_gen_col = 0;
|
|
126
|
+
let prev_src_line = 0;
|
|
127
|
+
let prev_src_col = 0;
|
|
128
|
+
let prev_gen_line = 0;
|
|
129
|
+
let result = "";
|
|
130
|
+
|
|
131
|
+
for (const seg of segments) {
|
|
132
|
+
const [gen_line, gen_col] = offset_to_position(
|
|
133
|
+
gen_lines,
|
|
134
|
+
seg.gen_offset,
|
|
135
|
+
);
|
|
136
|
+
const [src_line, src_col] = offset_to_position(
|
|
137
|
+
src_lines,
|
|
138
|
+
seg.src_offset,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// emit line separators
|
|
142
|
+
while (prev_gen_line < gen_line) {
|
|
143
|
+
result += ";";
|
|
144
|
+
prev_gen_line++;
|
|
145
|
+
prev_gen_col = 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// comma separator between segments on same line
|
|
149
|
+
if (result.length > 0 && result[result.length - 1] !== ";") {
|
|
150
|
+
result += ",";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// 4-field segment: gen_col, source_idx(0), src_line, src_col
|
|
154
|
+
result += vlq_encode(gen_col - prev_gen_col);
|
|
155
|
+
result += vlq_encode(0); // source index delta (always 0, single source)
|
|
156
|
+
result += vlq_encode(src_line - prev_src_line);
|
|
157
|
+
result += vlq_encode(src_col - prev_src_col);
|
|
158
|
+
|
|
159
|
+
prev_gen_col = gen_col;
|
|
160
|
+
prev_src_line = src_line;
|
|
161
|
+
prev_src_col = src_col;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// use basename to match svelte compiler convention, vite resolves relative
|
|
165
|
+
// to the served JS file, so the browser can find the source.
|
|
166
|
+
const basename = file ? file.split(/[/\\]/).pop()! : "input.md";
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
version: 3,
|
|
170
|
+
file: basename,
|
|
171
|
+
sources: [basename],
|
|
172
|
+
sourcesContent: [source],
|
|
173
|
+
names: [],
|
|
174
|
+
mappings: result,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
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');
|
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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);
|
|
@@ -0,0 +1,120 @@
|
|
|
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._kinds[renderer.blocks[0].idx]).toBe(3); // heading
|
|
27
|
+
expect(buf._kinds[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._kinds[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
|
+
});
|