@mdsvex/source-map 0.0.0 → 0.1.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 +13 -0
- package/LICENSE +21 -0
- package/package.json +37 -7
- package/src/compose_mappings.ts +156 -0
- package/src/index.ts +4 -0
- package/src/pfm_to_svelte.ts +304 -0
- package/src/v3_to_volar.ts +115 -0
- package/test/compose_mappings.spec.ts +161 -0
- package/test/end_to_end.spec.ts +419 -0
- package/test/pfm_to_svelte.spec.ts +298 -0
- package/test/pfm_to_svelte_extended.spec.ts +198 -0
- package/test/v3_to_volar.spec.ts +135 -0
- package/tsconfig.json +11 -0
- package/README.md +0 -3
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { pfmToSvelte } from "../src/pfm_to_svelte";
|
|
3
|
+
import type { PfmToSvelteResult } from "../src/pfm_to_svelte";
|
|
4
|
+
import type { Mapping, MappingData } from "@mdsvex/render/mappings";
|
|
5
|
+
|
|
6
|
+
// ── helpers ──
|
|
7
|
+
|
|
8
|
+
function byRole(mappings: Mapping<MappingData>[], role: string) {
|
|
9
|
+
return mappings.filter((m) => m.data.role === role);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function srcSlice(source: string, m: Mapping<MappingData>) {
|
|
13
|
+
return source.slice(m.sourceOffsets[0], m.sourceOffsets[0] + m.lengths[0]);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function genSlice(code: string, m: Mapping<MappingData>) {
|
|
17
|
+
const len = m.generatedLengths ? m.generatedLengths[0] : m.lengths[0];
|
|
18
|
+
return code.slice(m.generatedOffsets[0], m.generatedOffsets[0] + len);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** find a content mapping whose source text contains `substr`. */
|
|
22
|
+
function findContentBySource(
|
|
23
|
+
source: string,
|
|
24
|
+
mappings: Mapping<MappingData>[],
|
|
25
|
+
substr: string,
|
|
26
|
+
): Mapping<MappingData> | undefined {
|
|
27
|
+
return byRole(mappings, "content").find((m) =>
|
|
28
|
+
srcSlice(source, m).includes(substr),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** find a content mapping whose generated text contains `substr`. */
|
|
33
|
+
function findContentByGenerated(
|
|
34
|
+
code: string,
|
|
35
|
+
mappings: Mapping<MappingData>[],
|
|
36
|
+
substr: string,
|
|
37
|
+
): Mapping<MappingData> | undefined {
|
|
38
|
+
return byRole(mappings, "content").find((m) =>
|
|
39
|
+
genSlice(code, m).includes(substr),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** assert all mappings have valid offsets. */
|
|
44
|
+
function assertMappingsValid(
|
|
45
|
+
source: string,
|
|
46
|
+
result: PfmToSvelteResult,
|
|
47
|
+
) {
|
|
48
|
+
const { code, mappings } = result;
|
|
49
|
+
for (const m of mappings) {
|
|
50
|
+
for (let i = 0; i < m.sourceOffsets.length; i++) {
|
|
51
|
+
const srcOff = m.sourceOffsets[i];
|
|
52
|
+
const srcLen = m.lengths[i];
|
|
53
|
+
const genOff = m.generatedOffsets[i];
|
|
54
|
+
const genLen = m.generatedLengths ? m.generatedLengths[i] : m.lengths[i];
|
|
55
|
+
|
|
56
|
+
expect(srcOff).toBeGreaterThanOrEqual(0);
|
|
57
|
+
expect(srcOff + srcLen).toBeLessThanOrEqual(source.length);
|
|
58
|
+
expect(genOff).toBeGreaterThanOrEqual(0);
|
|
59
|
+
expect(genOff + genLen).toBeLessThanOrEqual(code.length);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// ── tests ──
|
|
65
|
+
|
|
66
|
+
describe("pfmToSvelte", () => {
|
|
67
|
+
it("bare markdown (no frontmatter, no imports)", () => {
|
|
68
|
+
const source = "# Hello\n\nWorld\n";
|
|
69
|
+
const result = pfmToSvelte(source);
|
|
70
|
+
assertMappingsValid(source, result);
|
|
71
|
+
|
|
72
|
+
// no <script> block
|
|
73
|
+
expect(result.code).not.toContain("<script");
|
|
74
|
+
// body rendered
|
|
75
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
76
|
+
expect(result.code).toContain("<p>World</p>");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("frontmatter only", () => {
|
|
80
|
+
const source = "---\ntitle: hello\n---\n\n# Hello\n";
|
|
81
|
+
const result = pfmToSvelte(source);
|
|
82
|
+
assertMappingsValid(source, result);
|
|
83
|
+
|
|
84
|
+
expect(result.code).toContain("<script");
|
|
85
|
+
expect(result.code).toContain("export const title = ");
|
|
86
|
+
expect(result.code).toContain("</script>");
|
|
87
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
88
|
+
|
|
89
|
+
// title key maps back to source
|
|
90
|
+
const titleMapping = findContentByGenerated(
|
|
91
|
+
result.code,
|
|
92
|
+
result.mappings,
|
|
93
|
+
"title",
|
|
94
|
+
);
|
|
95
|
+
expect(titleMapping).toBeDefined();
|
|
96
|
+
expect(srcSlice(source, titleMapping!)).toBe("title");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("imports only", () => {
|
|
100
|
+
const source =
|
|
101
|
+
'import Component from "./Component.svelte"\n\n# Hello\n';
|
|
102
|
+
const result = pfmToSvelte(source);
|
|
103
|
+
assertMappingsValid(source, result);
|
|
104
|
+
|
|
105
|
+
expect(result.code).toContain("<script");
|
|
106
|
+
expect(result.code).toContain(
|
|
107
|
+
'import Component from "./Component.svelte"',
|
|
108
|
+
);
|
|
109
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
110
|
+
|
|
111
|
+
// import maps back to source
|
|
112
|
+
const importMapping = findContentBySource(
|
|
113
|
+
source,
|
|
114
|
+
result.mappings,
|
|
115
|
+
"import Component",
|
|
116
|
+
);
|
|
117
|
+
expect(importMapping).toBeDefined();
|
|
118
|
+
expect(genSlice(result.code, importMapping!)).toContain(
|
|
119
|
+
"import Component",
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("frontmatter + imports", () => {
|
|
124
|
+
const source =
|
|
125
|
+
'---\ntitle: hello\n---\nimport A from "./A.svelte"\n\n# {title}\n';
|
|
126
|
+
const result = pfmToSvelte(source);
|
|
127
|
+
assertMappingsValid(source, result);
|
|
128
|
+
|
|
129
|
+
expect(result.code).toContain("<script");
|
|
130
|
+
expect(result.code).toContain('import A from "./A.svelte"');
|
|
131
|
+
expect(result.code).toContain("export const title = ");
|
|
132
|
+
|
|
133
|
+
// frontmatter exports in <script module>, imports in <script>
|
|
134
|
+
const moduleIdx = result.code.indexOf("<script module");
|
|
135
|
+
const scriptIdx = result.code.indexOf("<script lang");
|
|
136
|
+
expect(moduleIdx).toBeLessThan(scriptIdx);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("multiple imports", () => {
|
|
140
|
+
const source =
|
|
141
|
+
'import A from "./A.svelte"\nimport B from "./B.svelte"\n\n# Hello\n';
|
|
142
|
+
const result = pfmToSvelte(source);
|
|
143
|
+
assertMappingsValid(source, result);
|
|
144
|
+
|
|
145
|
+
expect(result.code).toContain('import A from "./A.svelte"');
|
|
146
|
+
expect(result.code).toContain('import B from "./B.svelte"');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("multi-key frontmatter", () => {
|
|
150
|
+
const source =
|
|
151
|
+
"---\ntitle: hello\ncount: 42\ndescription: world\n---\n\n# Hello\n";
|
|
152
|
+
const result = pfmToSvelte(source);
|
|
153
|
+
assertMappingsValid(source, result);
|
|
154
|
+
|
|
155
|
+
expect(result.code).toContain("export const title = ");
|
|
156
|
+
expect(result.code).toContain("export const count = ");
|
|
157
|
+
expect(result.code).toContain("const description = ");
|
|
158
|
+
|
|
159
|
+
// each key maps to its source position
|
|
160
|
+
for (const keyName of ["title", "count", "description"]) {
|
|
161
|
+
const m = findContentBySource(source, result.mappings, keyName);
|
|
162
|
+
expect(m).toBeDefined();
|
|
163
|
+
expect(srcSlice(source, m!)).toBe(keyName);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("frontmatter with block YAML value", () => {
|
|
168
|
+
const source =
|
|
169
|
+
"---\ntitle: hello\nbody: |\n line1\n line2\ntags: foo\n---\n\n# Hello\n";
|
|
170
|
+
const result = pfmToSvelte(source);
|
|
171
|
+
assertMappingsValid(source, result);
|
|
172
|
+
|
|
173
|
+
// should extract title, body, tags (not line1, line2)
|
|
174
|
+
expect(result.code).toContain("export const title = ");
|
|
175
|
+
expect(result.code).toContain("const body = ");
|
|
176
|
+
expect(result.code).toContain("const tags = ");
|
|
177
|
+
expect(result.code).not.toContain("let line1");
|
|
178
|
+
expect(result.code).not.toContain("let line2");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("empty frontmatter", () => {
|
|
182
|
+
const source = "---\n---\n\n# Hello\n";
|
|
183
|
+
const result = pfmToSvelte(source);
|
|
184
|
+
assertMappingsValid(source, result);
|
|
185
|
+
|
|
186
|
+
// empty frontmatter still generates a script block (but with no declarations)
|
|
187
|
+
expect(result.code).toContain("<script");
|
|
188
|
+
expect(result.code).not.toContain("export const ");
|
|
189
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("existing script tag in body", () => {
|
|
193
|
+
const source = "<script>\nlet x = 1;\n</script>\n\n# Hello\n";
|
|
194
|
+
const result = pfmToSvelte(source);
|
|
195
|
+
assertMappingsValid(source, result);
|
|
196
|
+
|
|
197
|
+
// script content should be merged
|
|
198
|
+
expect(result.code).toContain("<script");
|
|
199
|
+
expect(result.code).toContain("let x = 1;");
|
|
200
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
201
|
+
|
|
202
|
+
// should NOT have duplicate <script> tags
|
|
203
|
+
const scriptCount = (result.code.match(/<script[^>]*>/g) || []).length;
|
|
204
|
+
expect(scriptCount).toBe(1);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("frontmatter + existing script tag", () => {
|
|
208
|
+
const source =
|
|
209
|
+
"---\ntitle: hello\n---\n\n<script>\nlet x = 1;\n</script>\n\n# Hello\n";
|
|
210
|
+
const result = pfmToSvelte(source);
|
|
211
|
+
assertMappingsValid(source, result);
|
|
212
|
+
|
|
213
|
+
expect(result.code).toContain("export const title = ");
|
|
214
|
+
expect(result.code).toContain("let x = 1;");
|
|
215
|
+
|
|
216
|
+
// two script blocks: <script module> for frontmatter + <script> for user code
|
|
217
|
+
const scriptCount = (result.code.match(/<script[^>]*>/g) || []).length;
|
|
218
|
+
expect(scriptCount).toBe(2);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("body with mustache expression", () => {
|
|
222
|
+
const source = "{name}\n";
|
|
223
|
+
const result = pfmToSvelte(source);
|
|
224
|
+
assertMappingsValid(source, result);
|
|
225
|
+
|
|
226
|
+
expect(result.code).toContain("{name}");
|
|
227
|
+
|
|
228
|
+
// expression mapped with CI_SVELTE capabilities
|
|
229
|
+
const nameMapping = findContentBySource(
|
|
230
|
+
source,
|
|
231
|
+
result.mappings,
|
|
232
|
+
"name",
|
|
233
|
+
);
|
|
234
|
+
expect(nameMapping).toBeDefined();
|
|
235
|
+
expect(nameMapping!.data.completion).toBe(true);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("body with Svelte component", () => {
|
|
239
|
+
const source =
|
|
240
|
+
'import Counter from "./Counter.svelte"\n\n<Counter value={count} />\n';
|
|
241
|
+
const result = pfmToSvelte(source);
|
|
242
|
+
assertMappingsValid(source, result);
|
|
243
|
+
|
|
244
|
+
expect(result.code).toContain("<Counter");
|
|
245
|
+
expect(result.code).toContain("value={count}");
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("frontmatter keys with invalid JS identifiers are skipped", () => {
|
|
249
|
+
const source = "---\ntitle: hello\nmy-key: value\n$valid: yes\n---\n\n# Hello\n";
|
|
250
|
+
const result = pfmToSvelte(source);
|
|
251
|
+
assertMappingsValid(source, result);
|
|
252
|
+
|
|
253
|
+
expect(result.code).toContain("export const title = ");
|
|
254
|
+
expect(result.code).toContain("export const $valid = ");
|
|
255
|
+
// hyphenated key should be skipped
|
|
256
|
+
expect(result.code).not.toContain("my-key");
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("preserves mapping accuracy for identity-mapped imports", () => {
|
|
260
|
+
const source = 'import { onMount } from "svelte"\n\n# Hello\n';
|
|
261
|
+
const result = pfmToSvelte(source);
|
|
262
|
+
assertMappingsValid(source, result);
|
|
263
|
+
|
|
264
|
+
const importMapping = findContentBySource(
|
|
265
|
+
source,
|
|
266
|
+
result.mappings,
|
|
267
|
+
"import",
|
|
268
|
+
);
|
|
269
|
+
expect(importMapping).toBeDefined();
|
|
270
|
+
|
|
271
|
+
// source and generated text should be identical
|
|
272
|
+
const srcText = srcSlice(source, importMapping!);
|
|
273
|
+
const genText = genSlice(result.code, importMapping!);
|
|
274
|
+
expect(genText).toBe(srcText);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("self-closing component tag mapping is identity (no space mismatch)", () => {
|
|
278
|
+
// Without space before />
|
|
279
|
+
const noSpace = '<Test value={42} label="BOO"/>\n';
|
|
280
|
+
const r1 = pfmToSvelte(noSpace);
|
|
281
|
+
const m1 = byRole(r1.mappings, "content").find(
|
|
282
|
+
(m) => srcSlice(noSpace, m).includes("Test"),
|
|
283
|
+
);
|
|
284
|
+
expect(m1).toBeDefined();
|
|
285
|
+
expect(srcSlice(noSpace, m1!)).toBe(genSlice(r1.code, m1!));
|
|
286
|
+
expect(m1!.generatedLengths).toBeUndefined();
|
|
287
|
+
|
|
288
|
+
// With space before />
|
|
289
|
+
const withSpace = '<Test value={42} label="BOO" />\n';
|
|
290
|
+
const r2 = pfmToSvelte(withSpace);
|
|
291
|
+
const m2 = byRole(r2.mappings, "content").find(
|
|
292
|
+
(m) => srcSlice(withSpace, m).includes("Test"),
|
|
293
|
+
);
|
|
294
|
+
expect(m2).toBeDefined();
|
|
295
|
+
expect(srcSlice(withSpace, m2!)).toBe(genSlice(r2.code, m2!));
|
|
296
|
+
expect(m2!.generatedLengths).toBeUndefined();
|
|
297
|
+
});
|
|
298
|
+
});
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extended test fixtures for pfmToSvelte — covers edge cases,
|
|
3
|
+
* control flow, nested expressions, style blocks, and complex documents.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, it, expect } from "vitest";
|
|
6
|
+
import { pfmToSvelte } from "../src/pfm_to_svelte";
|
|
7
|
+
import type { PfmToSvelteResult } from "../src/pfm_to_svelte";
|
|
8
|
+
import type { Mapping, MappingData } from "@mdsvex/render/mappings";
|
|
9
|
+
|
|
10
|
+
function assertValid(source: string, result: PfmToSvelteResult) {
|
|
11
|
+
for (const m of result.mappings) {
|
|
12
|
+
for (let i = 0; i < m.sourceOffsets.length; i++) {
|
|
13
|
+
expect(m.sourceOffsets[i]).toBeGreaterThanOrEqual(0);
|
|
14
|
+
expect(m.sourceOffsets[i] + m.lengths[i]).toBeLessThanOrEqual(source.length);
|
|
15
|
+
expect(m.generatedOffsets[i]).toBeGreaterThanOrEqual(0);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("pfmToSvelte — control flow blocks", () => {
|
|
21
|
+
it("if/else block", () => {
|
|
22
|
+
const source = "---\nshow: true\n---\n\n{#if show}\nVisible\n{:else}\nHidden\n{/if}\n";
|
|
23
|
+
const result = pfmToSvelte(source);
|
|
24
|
+
assertValid(source, result);
|
|
25
|
+
expect(result.code).toContain("{#if");
|
|
26
|
+
expect(result.code).toContain("{:else}");
|
|
27
|
+
expect(result.code).toContain("{/if}");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("each block", () => {
|
|
31
|
+
const source = "---\nitems: [1, 2, 3]\n---\n\n{#each items as item}\n- {item}\n{/each}\n";
|
|
32
|
+
const result = pfmToSvelte(source);
|
|
33
|
+
assertValid(source, result);
|
|
34
|
+
expect(result.code).toContain("{#each");
|
|
35
|
+
expect(result.code).toContain("{/each}");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("nested if inside each", () => {
|
|
39
|
+
const source = "{#each items as item}\n{#if item > 0}\n{item}\n{/if}\n{/each}\n";
|
|
40
|
+
const result = pfmToSvelte(source);
|
|
41
|
+
assertValid(source, result);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("pfmToSvelte — style blocks", () => {
|
|
46
|
+
it("extracts style block positions", () => {
|
|
47
|
+
const source = "<style>\nh1 { color: red; }\n</style>\n\n# Hello\n";
|
|
48
|
+
const result = pfmToSvelte(source);
|
|
49
|
+
assertValid(source, result);
|
|
50
|
+
|
|
51
|
+
expect(result.styleBlocks.length).toBe(1);
|
|
52
|
+
const sb = result.styleBlocks[0];
|
|
53
|
+
const cssContent = source.slice(sb.sourceStart, sb.sourceEnd);
|
|
54
|
+
expect(cssContent).toContain("h1 { color: red; }");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("multiple style blocks", () => {
|
|
58
|
+
const source = "<style>\nh1 { color: red; }\n</style>\n\n# Hello\n\n<style>\np { margin: 0; }\n</style>\n";
|
|
59
|
+
const result = pfmToSvelte(source);
|
|
60
|
+
assertValid(source, result);
|
|
61
|
+
expect(result.styleBlocks.length).toBe(2);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("no style blocks when none present", () => {
|
|
65
|
+
const source = "# Hello\n\nWorld\n";
|
|
66
|
+
const result = pfmToSvelte(source);
|
|
67
|
+
expect(result.styleBlocks.length).toBe(0);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("pfmToSvelte — excluded regions", () => {
|
|
72
|
+
it("excludes frontmatter", () => {
|
|
73
|
+
const source = "---\ntitle: hello\n---\n\n# Hello\n";
|
|
74
|
+
const result = pfmToSvelte(source);
|
|
75
|
+
expect(result.excludedRegions.length).toBeGreaterThanOrEqual(1);
|
|
76
|
+
const fmRegion = result.excludedRegions[0];
|
|
77
|
+
expect(source.slice(fmRegion.start, fmRegion.end)).toContain("---");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("excludes script blocks", () => {
|
|
81
|
+
const source = "<script>\nlet x = 1;\n</script>\n\n# Hello\n";
|
|
82
|
+
const result = pfmToSvelte(source);
|
|
83
|
+
const scriptRegion = result.excludedRegions.find(r =>
|
|
84
|
+
source.slice(r.start, r.end).includes("script"),
|
|
85
|
+
);
|
|
86
|
+
expect(scriptRegion).toBeDefined();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("excludes style blocks", () => {
|
|
90
|
+
const source = "<style>\nh1 { color: red; }\n</style>\n\n# Hello\n";
|
|
91
|
+
const result = pfmToSvelte(source);
|
|
92
|
+
const styleRegion = result.excludedRegions.find(r =>
|
|
93
|
+
source.slice(r.start, r.end).includes("style"),
|
|
94
|
+
);
|
|
95
|
+
expect(styleRegion).toBeDefined();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("excludes import statements", () => {
|
|
99
|
+
const source = 'import A from "./A.svelte"\n\n# Hello\n';
|
|
100
|
+
const result = pfmToSvelte(source);
|
|
101
|
+
const importRegion = result.excludedRegions.find(r =>
|
|
102
|
+
source.slice(r.start, r.end).includes("import"),
|
|
103
|
+
);
|
|
104
|
+
expect(importRegion).toBeDefined();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("pfmToSvelte — complex documents", () => {
|
|
109
|
+
it("full-featured document", () => {
|
|
110
|
+
const source = `---
|
|
111
|
+
title: My Page
|
|
112
|
+
count: 42
|
|
113
|
+
draft: true
|
|
114
|
+
---
|
|
115
|
+
import Counter from "./Counter.svelte"
|
|
116
|
+
|
|
117
|
+
<script lang="ts">
|
|
118
|
+
const doubled = count * 2;
|
|
119
|
+
</script>
|
|
120
|
+
|
|
121
|
+
<style>
|
|
122
|
+
h1 { color: blue; }
|
|
123
|
+
p { margin: 1rem; }
|
|
124
|
+
</style>
|
|
125
|
+
|
|
126
|
+
# {title}
|
|
127
|
+
|
|
128
|
+
{#if !draft}
|
|
129
|
+
<Counter value={count} />
|
|
130
|
+
{:else}
|
|
131
|
+
**Draft mode** — count is {count}
|
|
132
|
+
{/if}
|
|
133
|
+
|
|
134
|
+
The doubled value is {doubled}.
|
|
135
|
+
|
|
136
|
+
> A blockquote with {title}
|
|
137
|
+
|
|
138
|
+
- Item {count}
|
|
139
|
+
- Item {count + 1}
|
|
140
|
+
|
|
141
|
+
\`\`\`ts
|
|
142
|
+
const x: number = 42;
|
|
143
|
+
\`\`\`
|
|
144
|
+
`;
|
|
145
|
+
const result = pfmToSvelte(source);
|
|
146
|
+
assertValid(source, result);
|
|
147
|
+
|
|
148
|
+
// script block merged
|
|
149
|
+
expect(result.code).toContain("const doubled = count * 2");
|
|
150
|
+
expect(result.code).toContain('import Counter from "./Counter.svelte"');
|
|
151
|
+
expect(result.code).toContain("export const title = ");
|
|
152
|
+
expect(result.code).toContain("export const count = ");
|
|
153
|
+
expect(result.code).toContain("export const draft = ");
|
|
154
|
+
|
|
155
|
+
// style block detected
|
|
156
|
+
expect(result.styleBlocks.length).toBe(1);
|
|
157
|
+
|
|
158
|
+
// excluded regions cover frontmatter, imports, script, style
|
|
159
|
+
expect(result.excludedRegions.length).toBeGreaterThanOrEqual(3);
|
|
160
|
+
|
|
161
|
+
// body preserved
|
|
162
|
+
expect(result.code).toContain("{#if");
|
|
163
|
+
expect(result.code).toContain("<Counter");
|
|
164
|
+
expect(result.code).toContain("{:else}");
|
|
165
|
+
|
|
166
|
+
// two script blocks: <script module> for frontmatter + <script> for user code
|
|
167
|
+
const scriptCount = (result.code.match(/<script[^>]*>/g) || []).length;
|
|
168
|
+
expect(scriptCount).toBe(2);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("empty document", () => {
|
|
172
|
+
const result = pfmToSvelte("\n");
|
|
173
|
+
expect(result.code).toBeDefined();
|
|
174
|
+
expect(result.mappings).toBeDefined();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("frontmatter only, no body", () => {
|
|
178
|
+
const source = "---\ntitle: hello\n---\n";
|
|
179
|
+
const result = pfmToSvelte(source);
|
|
180
|
+
assertValid(source, result);
|
|
181
|
+
expect(result.code).toContain("export const title = ");
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("frontmatter with all YAML scalar types", () => {
|
|
185
|
+
const source = "---\nstr: hello world\nnum: 3.14\nbool: false\nnull_val: null\ntilde_null: ~\nempty:\nint: -42\nsci: 1e10\n---\n\n# Test\n";
|
|
186
|
+
const result = pfmToSvelte(source);
|
|
187
|
+
assertValid(source, result);
|
|
188
|
+
|
|
189
|
+
expect(result.code).toContain('export const str = "hello world"');
|
|
190
|
+
expect(result.code).toContain("export const num = 3.14");
|
|
191
|
+
expect(result.code).toContain("export const bool = false");
|
|
192
|
+
expect(result.code).toContain("export const null_val = null");
|
|
193
|
+
expect(result.code).toContain("export const tilde_null = null");
|
|
194
|
+
expect(result.code).toContain("export const empty = null");
|
|
195
|
+
expect(result.code).toContain("export const int = -42");
|
|
196
|
+
expect(result.code).toContain("export const sci = 1e10");
|
|
197
|
+
});
|
|
198
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { v3ToVolarMappings } from "../src/v3_to_volar";
|
|
3
|
+
import type { SourceMapV3 } from "../src/v3_to_volar";
|
|
4
|
+
import { CI_SVELTE, CI_TEXT } from "@mdsvex/render/mappings";
|
|
5
|
+
|
|
6
|
+
// ── helpers ──
|
|
7
|
+
|
|
8
|
+
/** Create a minimal v3 map with raw VLQ mappings string. */
|
|
9
|
+
function makeV3(
|
|
10
|
+
mappings: string,
|
|
11
|
+
sources = ["input.svelte"],
|
|
12
|
+
): SourceMapV3 {
|
|
13
|
+
return {
|
|
14
|
+
version: 3,
|
|
15
|
+
sources,
|
|
16
|
+
mappings,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ── tests ──
|
|
21
|
+
|
|
22
|
+
describe("v3ToVolarMappings", () => {
|
|
23
|
+
it("single point mapping", () => {
|
|
24
|
+
// VLQ "AAAA" = gen col 0, src idx 0, src line 0, src col 0
|
|
25
|
+
const source = "hello";
|
|
26
|
+
const generated = "hello";
|
|
27
|
+
const v3 = makeV3("AAAA");
|
|
28
|
+
|
|
29
|
+
const mappings = v3ToVolarMappings(v3, generated, source);
|
|
30
|
+
|
|
31
|
+
expect(mappings.length).toBeGreaterThanOrEqual(1);
|
|
32
|
+
expect(mappings[0].sourceOffsets[0]).toBe(0);
|
|
33
|
+
expect(mappings[0].generatedOffsets[0]).toBe(0);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("identity mapping produces correct offsets", () => {
|
|
37
|
+
// Map "hello" at 0,0 → 0,0 — 5 consecutive characters
|
|
38
|
+
// AAAA,CAAC,CAAC,CAAC,CAAC = 5 points at offsets 0,1,2,3,4
|
|
39
|
+
const source = "hello";
|
|
40
|
+
const generated = "hello";
|
|
41
|
+
const v3 = makeV3("AAAA,CAAC,CAAC,CAAC,CAAC");
|
|
42
|
+
|
|
43
|
+
const mappings = v3ToVolarMappings(v3, generated, source);
|
|
44
|
+
|
|
45
|
+
// should coalesce into one range
|
|
46
|
+
expect(mappings.length).toBe(1);
|
|
47
|
+
expect(mappings[0].sourceOffsets[0]).toBe(0);
|
|
48
|
+
expect(mappings[0].generatedOffsets[0]).toBe(0);
|
|
49
|
+
expect(mappings[0].lengths[0]).toBe(5);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("coalesces consecutive identity-mapped points", () => {
|
|
53
|
+
// Generate per-character mappings for a 10-char string
|
|
54
|
+
// AAAA, then 9x CAAC
|
|
55
|
+
const segments = ["AAAA"];
|
|
56
|
+
for (let i = 1; i < 10; i++) segments.push("CAAC");
|
|
57
|
+
const v3 = makeV3(segments.join(","));
|
|
58
|
+
|
|
59
|
+
const source = "0123456789";
|
|
60
|
+
const generated = "0123456789";
|
|
61
|
+
|
|
62
|
+
const mappings = v3ToVolarMappings(v3, generated, source);
|
|
63
|
+
|
|
64
|
+
// all 10 points should coalesce into 1 range
|
|
65
|
+
expect(mappings.length).toBe(1);
|
|
66
|
+
expect(mappings[0].lengths[0]).toBe(10);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("handles multi-line mappings", () => {
|
|
70
|
+
const source = "ab\ncd\n";
|
|
71
|
+
const generated = "ab\ncd\n";
|
|
72
|
+
// line 0: col 0→0,0 col 1→0,1; line 1: col 0→1,0 col 1→1,1
|
|
73
|
+
const v3 = makeV3("AAAA,CAAC;AAAA,CAAC");
|
|
74
|
+
|
|
75
|
+
const mappings = v3ToVolarMappings(v3, generated, source);
|
|
76
|
+
|
|
77
|
+
// should produce 2 ranges (one per line, since newline breaks the identity run)
|
|
78
|
+
expect(mappings.length).toBeGreaterThanOrEqual(1);
|
|
79
|
+
|
|
80
|
+
// first range starts at offset 0
|
|
81
|
+
expect(mappings[0].sourceOffsets[0]).toBe(0);
|
|
82
|
+
expect(mappings[0].generatedOffsets[0]).toBe(0);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("respects defaultData parameter", () => {
|
|
86
|
+
const v3 = makeV3("AAAA");
|
|
87
|
+
const mappings = v3ToVolarMappings(v3, "x", "x", CI_TEXT);
|
|
88
|
+
|
|
89
|
+
expect(mappings[0].data).toEqual(CI_TEXT);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("defaults to CI_SVELTE when no defaultData", () => {
|
|
93
|
+
const v3 = makeV3("AAAA");
|
|
94
|
+
const mappings = v3ToVolarMappings(v3, "x", "x");
|
|
95
|
+
|
|
96
|
+
expect(mappings[0].data).toEqual(CI_SVELTE);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("handles offset mapping (source offset != generated offset)", () => {
|
|
100
|
+
// gen col 2, src idx 0, src line 0, src col 0 = generated starts at col 2
|
|
101
|
+
// VLQ: E=2, A=0, A=0, A=0 → "EAAA"
|
|
102
|
+
const source = "hello";
|
|
103
|
+
const generated = " hello";
|
|
104
|
+
const v3 = makeV3("EAAA");
|
|
105
|
+
|
|
106
|
+
const mappings = v3ToVolarMappings(v3, generated, source);
|
|
107
|
+
|
|
108
|
+
expect(mappings.length).toBe(1);
|
|
109
|
+
expect(mappings[0].generatedOffsets[0]).toBe(2);
|
|
110
|
+
expect(mappings[0].sourceOffsets[0]).toBe(0);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("non-identity gap produces separate ranges", () => {
|
|
114
|
+
// Two identity points with a gap: offset 0→0 and offset 5→5
|
|
115
|
+
// AAAA, then jump to col 5 with src col 5
|
|
116
|
+
// Delta from 0 to 5: gen col delta=5(K), src col delta=5(K)
|
|
117
|
+
const v3 = makeV3("AAAA,KAAK");
|
|
118
|
+
|
|
119
|
+
const source = "0123456789";
|
|
120
|
+
const generated = "0123456789";
|
|
121
|
+
|
|
122
|
+
const mappings = v3ToVolarMappings(v3, generated, source);
|
|
123
|
+
|
|
124
|
+
// should be 2 separate ranges (the gap breaks the run)
|
|
125
|
+
expect(mappings.length).toBe(2);
|
|
126
|
+
expect(mappings[0].generatedOffsets[0]).toBe(0);
|
|
127
|
+
expect(mappings[1].generatedOffsets[0]).toBe(5);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("handles empty mappings string", () => {
|
|
131
|
+
const v3 = makeV3("");
|
|
132
|
+
const mappings = v3ToVolarMappings(v3, "hello", "hello");
|
|
133
|
+
expect(mappings.length).toBe(0);
|
|
134
|
+
});
|
|
135
|
+
});
|
package/tsconfig.json
ADDED
package/README.md
DELETED