@mdsvex/source-map 0.0.0 → 0.1.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/LICENSE +21 -0
- package/package.json +38 -7
- package/src/compose_mappings.ts +156 -0
- package/src/edit.ts +95 -0
- package/src/index.ts +4 -0
- package/src/keys.ts +101 -0
- package/src/pfm_to_svelte.ts +883 -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 +307 -0
- package/test/pfm_to_svelte_config.spec.ts +339 -0
- package/test/pfm_to_svelte_extended.spec.ts +205 -0
- package/test/pfm_to_svelte_live.spec.ts +84 -0
- package/test/v3_to_volar.spec.ts +135 -0
- package/tsconfig.json +11 -0
- package/README.md +0 -3
|
@@ -0,0 +1,307 @@
|
|
|
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
|
+
// the metadata export compile emits, with bare keys
|
|
85
|
+
expect(result.code).toContain("<script module>");
|
|
86
|
+
expect(result.code).toContain('export const metadata = {title: "hello"};');
|
|
87
|
+
expect(result.code).toContain("</script>");
|
|
88
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
89
|
+
expect(result.metadata).toEqual({ title: "hello" });
|
|
90
|
+
|
|
91
|
+
// title key maps back to source
|
|
92
|
+
const titleMapping = findContentByGenerated(
|
|
93
|
+
result.code,
|
|
94
|
+
result.mappings,
|
|
95
|
+
"title",
|
|
96
|
+
);
|
|
97
|
+
expect(titleMapping).toBeDefined();
|
|
98
|
+
expect(srcSlice(source, titleMapping!)).toBe("title");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("imports only", () => {
|
|
102
|
+
const source =
|
|
103
|
+
'import Component from "./Component.svelte"\n\n# Hello\n';
|
|
104
|
+
const result = pfmToSvelte(source);
|
|
105
|
+
assertMappingsValid(source, result);
|
|
106
|
+
|
|
107
|
+
expect(result.code).toContain("<script");
|
|
108
|
+
expect(result.code).toContain(
|
|
109
|
+
'import Component from "./Component.svelte"',
|
|
110
|
+
);
|
|
111
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
112
|
+
|
|
113
|
+
// import maps back to source
|
|
114
|
+
const importMapping = findContentBySource(
|
|
115
|
+
source,
|
|
116
|
+
result.mappings,
|
|
117
|
+
"import Component",
|
|
118
|
+
);
|
|
119
|
+
expect(importMapping).toBeDefined();
|
|
120
|
+
expect(genSlice(result.code, importMapping!)).toContain(
|
|
121
|
+
"import Component",
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("frontmatter + imports", () => {
|
|
126
|
+
const source =
|
|
127
|
+
'---\ntitle: hello\n---\nimport A from "./A.svelte"\n\n# {title}\n';
|
|
128
|
+
const result = pfmToSvelte(source);
|
|
129
|
+
assertMappingsValid(source, result);
|
|
130
|
+
|
|
131
|
+
expect(result.code).toContain('import A from "./A.svelte"');
|
|
132
|
+
expect(result.code).toContain("export const metadata = {title: ");
|
|
133
|
+
|
|
134
|
+
// frontmatter exports in <script module>, imports in <script>
|
|
135
|
+
const module = /<script module>[^]*?<\/script>/.exec(result.code)![0];
|
|
136
|
+
const instance = /<script>[^]*?<\/script>/.exec(result.code)![0];
|
|
137
|
+
expect(module).toContain("export const metadata");
|
|
138
|
+
expect(instance).toContain('import A from "./A.svelte"');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("multiple imports", () => {
|
|
142
|
+
const source =
|
|
143
|
+
'import A from "./A.svelte"\nimport B from "./B.svelte"\n\n# Hello\n';
|
|
144
|
+
const result = pfmToSvelte(source);
|
|
145
|
+
assertMappingsValid(source, result);
|
|
146
|
+
|
|
147
|
+
expect(result.code).toContain('import A from "./A.svelte"');
|
|
148
|
+
expect(result.code).toContain('import B from "./B.svelte"');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("multi-key frontmatter", () => {
|
|
152
|
+
const source =
|
|
153
|
+
"---\ntitle: hello\ncount: 42\ndescription: world\n---\n\n# Hello\n";
|
|
154
|
+
const result = pfmToSvelte(source);
|
|
155
|
+
assertMappingsValid(source, result);
|
|
156
|
+
|
|
157
|
+
expect(result.code).toContain(
|
|
158
|
+
'export const metadata = {title: "hello", count: 42, description: "world"};',
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// each key maps to its source position
|
|
162
|
+
for (const keyName of ["title", "count", "description"]) {
|
|
163
|
+
const m = findContentBySource(source, result.mappings, keyName);
|
|
164
|
+
expect(m).toBeDefined();
|
|
165
|
+
expect(srcSlice(source, m!)).toBe(keyName);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("frontmatter with block YAML value", () => {
|
|
170
|
+
const source =
|
|
171
|
+
"---\ntitle: hello\nbody: |\n line1\n line2\ntags: foo\n---\n\n# Hello\n";
|
|
172
|
+
const result = pfmToSvelte(source);
|
|
173
|
+
assertMappingsValid(source, result);
|
|
174
|
+
|
|
175
|
+
// the core yaml parser reads the block, its lines are no keys
|
|
176
|
+
expect(result.metadata).toEqual({
|
|
177
|
+
title: "hello",
|
|
178
|
+
body: "line1\nline2\n",
|
|
179
|
+
tags: "foo",
|
|
180
|
+
});
|
|
181
|
+
expect(result.code).toContain('body: "line1\\nline2\\n"');
|
|
182
|
+
for (const key of ["title", "body", "tags"]) {
|
|
183
|
+
const m = findContentBySource(source, result.mappings, key);
|
|
184
|
+
expect(srcSlice(source, m!)).toBe(key);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("empty frontmatter", () => {
|
|
189
|
+
const source = "---\n---\n\n# Hello\n";
|
|
190
|
+
const result = pfmToSvelte(source);
|
|
191
|
+
assertMappingsValid(source, result);
|
|
192
|
+
|
|
193
|
+
// empty frontmatter is empty metadata
|
|
194
|
+
expect(result.code).toContain("export const metadata = {};");
|
|
195
|
+
expect(result.metadata).toEqual({});
|
|
196
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("existing script tag in body", () => {
|
|
200
|
+
const source = "<script>\nlet x = 1;\n</script>\n\n# Hello\n";
|
|
201
|
+
const result = pfmToSvelte(source);
|
|
202
|
+
assertMappingsValid(source, result);
|
|
203
|
+
|
|
204
|
+
// script content should be merged
|
|
205
|
+
expect(result.code).toContain("<script");
|
|
206
|
+
expect(result.code).toContain("let x = 1;");
|
|
207
|
+
expect(result.code).toContain("<h1>Hello</h1>");
|
|
208
|
+
|
|
209
|
+
// should NOT have duplicate <script> tags
|
|
210
|
+
const scriptCount = (result.code.match(/<script[^>]*>/g) || []).length;
|
|
211
|
+
expect(scriptCount).toBe(1);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("frontmatter + existing script tag", () => {
|
|
215
|
+
const source =
|
|
216
|
+
"---\ntitle: hello\n---\n\n<script>\nlet x = 1;\n</script>\n\n# Hello\n";
|
|
217
|
+
const result = pfmToSvelte(source);
|
|
218
|
+
assertMappingsValid(source, result);
|
|
219
|
+
|
|
220
|
+
expect(result.code).toContain("export const metadata = {title: ");
|
|
221
|
+
expect(result.code).toContain("let x = 1;");
|
|
222
|
+
|
|
223
|
+
// two script blocks: <script module> for frontmatter + <script> for user code
|
|
224
|
+
const scriptCount = (result.code.match(/<script[^>]*>/g) || []).length;
|
|
225
|
+
expect(scriptCount).toBe(2);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("body with mustache expression", () => {
|
|
229
|
+
const source = "{name}\n";
|
|
230
|
+
const result = pfmToSvelte(source);
|
|
231
|
+
assertMappingsValid(source, result);
|
|
232
|
+
|
|
233
|
+
expect(result.code).toContain("{name}");
|
|
234
|
+
|
|
235
|
+
// expression mapped with CI_SVELTE capabilities
|
|
236
|
+
const nameMapping = findContentBySource(
|
|
237
|
+
source,
|
|
238
|
+
result.mappings,
|
|
239
|
+
"name",
|
|
240
|
+
);
|
|
241
|
+
expect(nameMapping).toBeDefined();
|
|
242
|
+
expect(nameMapping!.data.completion).toBe(true);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("body with Svelte component", () => {
|
|
246
|
+
const source =
|
|
247
|
+
'import Counter from "./Counter.svelte"\n\n<Counter value={count} />\n';
|
|
248
|
+
const result = pfmToSvelte(source);
|
|
249
|
+
assertMappingsValid(source, result);
|
|
250
|
+
|
|
251
|
+
expect(result.code).toContain("<Counter");
|
|
252
|
+
expect(result.code).toContain("value={count}");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("frontmatter keys that are not identifiers stay quoted and mapped", () => {
|
|
256
|
+
const source = "---\ntitle: hello\nmy-key: value\n$valid: yes\n---\n\n# Hello\n";
|
|
257
|
+
const result = pfmToSvelte(source);
|
|
258
|
+
assertMappingsValid(source, result);
|
|
259
|
+
|
|
260
|
+
expect(result.code).toContain(
|
|
261
|
+
'export const metadata = {title: "hello", "my-key": "value", $valid: "yes"};',
|
|
262
|
+
);
|
|
263
|
+
const m = findContentBySource(source, result.mappings, "my-key");
|
|
264
|
+
expect(srcSlice(source, m!)).toBe("my-key");
|
|
265
|
+
expect(genSlice(result.code, m!)).toBe('"my-key"');
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("preserves mapping accuracy for identity-mapped imports", () => {
|
|
269
|
+
const source = 'import { onMount } from "svelte"\n\n# Hello\n';
|
|
270
|
+
const result = pfmToSvelte(source);
|
|
271
|
+
assertMappingsValid(source, result);
|
|
272
|
+
|
|
273
|
+
const importMapping = findContentBySource(
|
|
274
|
+
source,
|
|
275
|
+
result.mappings,
|
|
276
|
+
"import",
|
|
277
|
+
);
|
|
278
|
+
expect(importMapping).toBeDefined();
|
|
279
|
+
|
|
280
|
+
// source and generated text should be identical
|
|
281
|
+
const srcText = srcSlice(source, importMapping!);
|
|
282
|
+
const genText = genSlice(result.code, importMapping!);
|
|
283
|
+
expect(genText).toBe(srcText);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("self-closing component tag mapping is identity (no space mismatch)", () => {
|
|
287
|
+
// Without space before />
|
|
288
|
+
const noSpace = '<Test value={42} label="BOO"/>\n';
|
|
289
|
+
const r1 = pfmToSvelte(noSpace);
|
|
290
|
+
const m1 = byRole(r1.mappings, "content").find(
|
|
291
|
+
(m) => srcSlice(noSpace, m).includes("Test"),
|
|
292
|
+
);
|
|
293
|
+
expect(m1).toBeDefined();
|
|
294
|
+
expect(srcSlice(noSpace, m1!)).toBe(genSlice(r1.code, m1!));
|
|
295
|
+
expect(m1!.generatedLengths).toBeUndefined();
|
|
296
|
+
|
|
297
|
+
// With space before />
|
|
298
|
+
const withSpace = '<Test value={42} label="BOO" />\n';
|
|
299
|
+
const r2 = pfmToSvelte(withSpace);
|
|
300
|
+
const m2 = byRole(r2.mappings, "content").find(
|
|
301
|
+
(m) => srcSlice(withSpace, m).includes("Test"),
|
|
302
|
+
);
|
|
303
|
+
expect(m2).toBeDefined();
|
|
304
|
+
expect(srcSlice(withSpace, m2!)).toBe(genSlice(r2.code, m2!));
|
|
305
|
+
expect(m2!.generatedLengths).toBeUndefined();
|
|
306
|
+
});
|
|
307
|
+
});
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { pfmToSvelte, template_value_at } from "../src/pfm_to_svelte";
|
|
3
|
+
import type {
|
|
4
|
+
EditorMappingData,
|
|
5
|
+
PfmToSvelteOptions,
|
|
6
|
+
PfmToSvelteResult,
|
|
7
|
+
} from "../src/pfm_to_svelte";
|
|
8
|
+
import type { Mapping, MappingData } from "@mdsvex/render/mappings";
|
|
9
|
+
|
|
10
|
+
function src_of(source: string, m: Mapping<MappingData>) {
|
|
11
|
+
return source.slice(m.sourceOffsets[0], m.sourceOffsets[0] + m.lengths[0]);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function gen_of(code: string, m: Mapping<MappingData>) {
|
|
15
|
+
const len = m.generatedLengths ? m.generatedLengths[0] : m.lengths[0];
|
|
16
|
+
return code.slice(m.generatedOffsets[0], m.generatedOffsets[0] + len);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** the source, generated text and capabilities of each mapping the converter added */
|
|
20
|
+
function added(source: string, r: PfmToSvelteResult) {
|
|
21
|
+
return r.mappings
|
|
22
|
+
.filter((m) => (m.data as EditorMappingData).editor)
|
|
23
|
+
.map((m) => {
|
|
24
|
+
const d = m.data;
|
|
25
|
+
const caps =
|
|
26
|
+
(d.verification ? "v" : "") +
|
|
27
|
+
(d.completion ? "c" : "") +
|
|
28
|
+
(d.semantic ? "s" : "") +
|
|
29
|
+
(d.navigation ? "n" : "");
|
|
30
|
+
return [src_of(source, m), gen_of(r.code, m), caps];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function valid(source: string, r: PfmToSvelteResult) {
|
|
35
|
+
for (const m of r.mappings) {
|
|
36
|
+
const len = m.generatedLengths ? m.generatedLengths[0] : m.lengths[0];
|
|
37
|
+
expect(m.sourceOffsets[0] + m.lengths[0]).toBeLessThanOrEqual(source.length);
|
|
38
|
+
expect(m.generatedOffsets[0] + len).toBeLessThanOrEqual(r.code.length);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const FILES: Record<string, string> = {
|
|
43
|
+
"mdsvex:template/docs": "./lib/Docs.svelte",
|
|
44
|
+
"mdsvex:template-directives/docs": "./lib/directives.js",
|
|
45
|
+
"mdsvex:components": "./lib/markdown.js",
|
|
46
|
+
"mdsvex:directives": "./lib/root-directives.js",
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const docs: PfmToSvelteOptions = {
|
|
50
|
+
compile: {
|
|
51
|
+
templates: {
|
|
52
|
+
docs: {
|
|
53
|
+
specifier: "mdsvex:template/docs",
|
|
54
|
+
components: ["h2"],
|
|
55
|
+
directives: [
|
|
56
|
+
{ specifier: "mdsvex:template-directives/docs", names: ["Callout"] },
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
components: [{ specifier: "mdsvex:components", names: ["img", "a"] }],
|
|
61
|
+
},
|
|
62
|
+
resolve: (id) => FILES[id],
|
|
63
|
+
report_directives: true,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
describe("pfmToSvelte with a config", () => {
|
|
67
|
+
it("compiles as core does, imports pointing at the resolved files", () => {
|
|
68
|
+
const source = "---\ntemplate: docs\n---\n\n## Two\n\n\n";
|
|
69
|
+
const r = pfmToSvelte(source, docs);
|
|
70
|
+
valid(source, r);
|
|
71
|
+
expect(r.template).toBe("docs");
|
|
72
|
+
expect(r.code).toContain('import Template_MDSVEX from "./lib/Docs.svelte";');
|
|
73
|
+
expect(r.code).toContain(
|
|
74
|
+
'import { h2 as H2_MDSVEX_T } from "./lib/Docs.svelte";',
|
|
75
|
+
);
|
|
76
|
+
expect(r.code).toContain(
|
|
77
|
+
'import { img as Img_MDSVEX_G } from "./lib/markdown.js";',
|
|
78
|
+
);
|
|
79
|
+
expect(r.code).not.toContain("mdsvex:");
|
|
80
|
+
expect(r.code).toContain("<Template_MDSVEX {...metadata} {...__mdsvex_props}>");
|
|
81
|
+
expect(r.code).toContain("<H2_MDSVEX_T level={2}>Two</H2_MDSVEX_T>");
|
|
82
|
+
expect(r.diagnostics).toEqual([]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("keeps the virtual id of an import it can not resolve", () => {
|
|
86
|
+
const source = "## Two\n\n\n";
|
|
87
|
+
const r = pfmToSvelte(source, {
|
|
88
|
+
...docs,
|
|
89
|
+
compile: { ...docs.compile, default_template: "docs" },
|
|
90
|
+
resolve: (id, name) => (name === "h2" ? "./lib/Docs.svelte" : undefined),
|
|
91
|
+
});
|
|
92
|
+
expect(r.code).toContain(
|
|
93
|
+
'import { h2 as H2_MDSVEX_T } from "./lib/Docs.svelte";',
|
|
94
|
+
);
|
|
95
|
+
expect(r.code).toContain(
|
|
96
|
+
"import { default as Template_MDSVEX } from 'mdsvex:template/docs';",
|
|
97
|
+
);
|
|
98
|
+
expect(r.code).toContain("import { img as Img_MDSVEX_G } from 'mdsvex:components';");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("points each replaced element and directive at its export", () => {
|
|
102
|
+
const source =
|
|
103
|
+
"---\ntemplate: docs\n---\n\n## Two\n\n [l](/x)\n\n:::Callout[Hi](kind=warn)\nbody\n:::\n";
|
|
104
|
+
const r = pfmToSvelte(source, docs);
|
|
105
|
+
valid(source, r);
|
|
106
|
+
const hovers = added(source, r).filter(([, , caps]) => caps === "sn");
|
|
107
|
+
expect(hovers).toContainEqual(["##", "h2", "sn"]);
|
|
108
|
+
expect(hovers).toContainEqual(["!", "img", "sn"]);
|
|
109
|
+
expect(hovers).toContainEqual(["](/x)", "a", "sn"]);
|
|
110
|
+
expect(hovers).toContainEqual(["Callout", "Callout", "sn"]);
|
|
111
|
+
// the template value points at the template
|
|
112
|
+
expect(hovers).toContainEqual(["docs", "Template_MDSVEX", "sn"]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("drops the capabilities of syntax that maps onto a whole component tag", () => {
|
|
116
|
+
const source = "---\ntemplate: docs\n---\n\n## Two\n";
|
|
117
|
+
const r = pfmToSvelte(source, docs);
|
|
118
|
+
const open = r.mappings.find((m) => gen_of(r.code, m) === "<H2_MDSVEX_T level={2}>");
|
|
119
|
+
expect(open).toBeDefined();
|
|
120
|
+
const d = open!.data;
|
|
121
|
+
expect(d.verification || d.semantic || d.navigation || d.completion).toBeFalsy();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("maps directive args to the props they become, checked", () => {
|
|
125
|
+
const source =
|
|
126
|
+
'---\ntemplate: docs\n---\n\n:::Callout[Hi](kind=warn, title="a b")\nbody\n:::\n';
|
|
127
|
+
const r = pfmToSvelte(source, {
|
|
128
|
+
...docs,
|
|
129
|
+
compile: {
|
|
130
|
+
...docs.compile,
|
|
131
|
+
templates: {
|
|
132
|
+
docs: {
|
|
133
|
+
...docs.compile!.templates!.docs,
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
valid(source, r);
|
|
139
|
+
expect(r.code).toContain('<Callout_MDSVEX_D_T kind="warn" title="a b">');
|
|
140
|
+
const args = added(source, r).filter(([, , caps]) => caps === "vsn");
|
|
141
|
+
expect(args).toEqual([
|
|
142
|
+
["kind", "kind", "vsn"],
|
|
143
|
+
["warn", "warn", "vsn"],
|
|
144
|
+
["title", "title", "vsn"],
|
|
145
|
+
["a b", "a b", "vsn"],
|
|
146
|
+
]);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("checks typed attribute values of a replaced element but not their names", () => {
|
|
150
|
+
const source = '<script>\nlet u = "/a.png";\n</script>\n\n<img src={u} loading="lazy" />\n';
|
|
151
|
+
const r = pfmToSvelte(source, {
|
|
152
|
+
...docs,
|
|
153
|
+
compile: { ...docs.compile, component_mode: "all" },
|
|
154
|
+
});
|
|
155
|
+
valid(source, r);
|
|
156
|
+
expect(r.code).toContain('<Img_MDSVEX_G src={u} loading="lazy" />');
|
|
157
|
+
const attrs = added(source, r).filter(([, , caps]) => caps.includes("c"));
|
|
158
|
+
expect(attrs).toEqual([
|
|
159
|
+
["src", "src", "csn"],
|
|
160
|
+
["{u}", "{u}", "vcsn"],
|
|
161
|
+
["loading", "loading", "csn"],
|
|
162
|
+
['"lazy"', '"lazy"', "vcsn"],
|
|
163
|
+
]);
|
|
164
|
+
// the tag name hovers the replacement
|
|
165
|
+
expect(added(source, r)).toContainEqual(["img", "img", "sn"]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("checks the frontmatter against the props of the template", () => {
|
|
169
|
+
const source = "---\ntitle: Hi\nmy-key: 1\ntemplate: docs\n---\n\n# x\n";
|
|
170
|
+
const r = pfmToSvelte(source, docs);
|
|
171
|
+
valid(source, r);
|
|
172
|
+
expect(r.code).toContain(
|
|
173
|
+
';({ title: "Hi", "my-key": 1, template: "docs" }) satisfies ' +
|
|
174
|
+
"Partial<import('svelte').ComponentProps<typeof Template_MDSVEX>> & Record<string, unknown>;",
|
|
175
|
+
);
|
|
176
|
+
const checks = added(source, r).filter(([, , caps]) => caps === "v");
|
|
177
|
+
expect(checks).toEqual([
|
|
178
|
+
["title", "title", "v"],
|
|
179
|
+
["my-key", '"my-key"', "v"],
|
|
180
|
+
["template", "template", "v"],
|
|
181
|
+
]);
|
|
182
|
+
// the check sits in the instance script, where the template is in scope
|
|
183
|
+
const instance = /<script>[^]*?<\/script>/.exec(r.code)![0];
|
|
184
|
+
expect(instance).toContain("satisfies");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("types the template key as the union of template names and false", () => {
|
|
188
|
+
const source = "---\ntitle: Hi\ntemplate: docs\n---\n\n# x\n";
|
|
189
|
+
const r = pfmToSvelte(source, docs);
|
|
190
|
+
expect(r.code).toContain(
|
|
191
|
+
'export const metadata = {title: "Hi", template: "docs" as "docs" | false};',
|
|
192
|
+
);
|
|
193
|
+
expect(r.frontmatter).toEqual({ start: 4, end: 29 });
|
|
194
|
+
// without templates the key is whatever the frontmatter holds
|
|
195
|
+
expect(pfmToSvelte(source).code).toContain(
|
|
196
|
+
'export const metadata = {title: "Hi", template: "docs"};',
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("emits a props type for every template and directive the config knows", () => {
|
|
201
|
+
const r = pfmToSvelte("# x\n", {
|
|
202
|
+
...docs,
|
|
203
|
+
compile: {
|
|
204
|
+
...docs.compile,
|
|
205
|
+
directives: [{ specifier: "mdsvex:directives", names: ["box"] }],
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
expect(r.probes).toEqual([
|
|
209
|
+
{ kind: "template", name: "docs", alias: "__mdsvex_props_0" },
|
|
210
|
+
{ kind: "directive", name: "Callout", template: "docs", alias: "__mdsvex_props_1" },
|
|
211
|
+
{ kind: "directive", name: "box", alias: "__mdsvex_props_2" },
|
|
212
|
+
]);
|
|
213
|
+
// a document with no instance script gets one to hold them
|
|
214
|
+
expect(r.code).toContain(
|
|
215
|
+
'<script lang="ts">\n' +
|
|
216
|
+
"type __mdsvex_props_0 = import('svelte').ComponentProps<typeof import(\"./lib/Docs.svelte\")[\"default\"]>;\n" +
|
|
217
|
+
"type __mdsvex_props_1 = import('svelte').ComponentProps<typeof import(\"./lib/directives.js\")[\"Callout\"]>;\n" +
|
|
218
|
+
"type __mdsvex_props_2 = import('svelte').ComponentProps<typeof import(\"./lib/root-directives.js\")[\"box\"]>;\n" +
|
|
219
|
+
"</script>\n<h1>x</h1>",
|
|
220
|
+
);
|
|
221
|
+
expect(pfmToSvelte("# x\n").probes).toEqual([]);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("checks nothing without a template", () => {
|
|
225
|
+
const source = "---\ntitle: Hi\n---\n\n# x\n";
|
|
226
|
+
const r = pfmToSvelte(source, docs);
|
|
227
|
+
expect(r.code).not.toContain("satisfies");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("passes select_template through, as the plugin binds it", () => {
|
|
231
|
+
const source = "## Two\n";
|
|
232
|
+
const r = pfmToSvelte(source, {
|
|
233
|
+
...docs,
|
|
234
|
+
compile: { ...docs.compile, select_template: () => "docs" },
|
|
235
|
+
});
|
|
236
|
+
expect(r.template).toBe("docs");
|
|
237
|
+
expect(r.code).toContain("<H2_MDSVEX_T");
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
describe("pfmToSvelte compile diagnostics", () => {
|
|
242
|
+
it("reports a directive no component renders and renders its children", () => {
|
|
243
|
+
const source = "# t\n\n:::thing[x]\nbody\n:::\n";
|
|
244
|
+
const r = pfmToSvelte(source, docs);
|
|
245
|
+
expect(r.diagnostics).toHaveLength(1);
|
|
246
|
+
const [d] = r.diagnostics;
|
|
247
|
+
expect(source.slice(d.start, d.end)).toBe(":::thing");
|
|
248
|
+
expect(d.severity).toBe("error");
|
|
249
|
+
expect(d.message).toContain(":::thing");
|
|
250
|
+
expect(r.code).toContain("<p>body</p>");
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("never reports directives without a known config", () => {
|
|
254
|
+
const source = "# t\n\n:::thing[x]\nbody\n:::\n\n:inline[y]\n";
|
|
255
|
+
const r = pfmToSvelte(source);
|
|
256
|
+
expect(r.diagnostics).toEqual([]);
|
|
257
|
+
expect(r.code).toContain("<p>body</p>");
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("reports an argument a snippet takes and still renders", () => {
|
|
261
|
+
const source = "::leaf[x](children=a)\n\n# after\n";
|
|
262
|
+
const r = pfmToSvelte(source, {
|
|
263
|
+
compile: { directives: [{ specifier: "mdsvex:directives", names: ["leaf"] }] },
|
|
264
|
+
resolve: (id) => FILES[id],
|
|
265
|
+
});
|
|
266
|
+
expect(r.diagnostics).toHaveLength(1);
|
|
267
|
+
expect(r.diagnostics[0].message).toContain("children");
|
|
268
|
+
expect(r.code).toContain("<h1>after</h1>");
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("reports an unknown template at its line and renders without one", () => {
|
|
272
|
+
const source = "---\ntitle: x\ntemplate: nope\n---\n\n## Two\n";
|
|
273
|
+
const r = pfmToSvelte(source, docs);
|
|
274
|
+
expect(r.diagnostics).toHaveLength(1);
|
|
275
|
+
const [d] = r.diagnostics;
|
|
276
|
+
expect(source.slice(d.start, d.end)).toBe("nope");
|
|
277
|
+
expect(d.message).toContain('Unknown template "nope"');
|
|
278
|
+
expect(r.template).toBeUndefined();
|
|
279
|
+
expect(r.code).not.toContain("Template_MDSVEX");
|
|
280
|
+
expect(r.metadata).toEqual({ title: "x", template: "nope" });
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("reports frontmatter the core parser rejects and types metadata loosely", () => {
|
|
284
|
+
const source = "---\ntitle: x\nanchor: &a 1\n---\n\n{metadata.anything}\n";
|
|
285
|
+
const r = pfmToSvelte(source);
|
|
286
|
+
expect(r.diagnostics).toHaveLength(1);
|
|
287
|
+
expect(source.slice(r.diagnostics[0].start, r.diagnostics[0].end)).toBe(
|
|
288
|
+
"anchor: &a 1",
|
|
289
|
+
);
|
|
290
|
+
expect(r.metadata).toBeUndefined();
|
|
291
|
+
expect(r.code).toContain("export const metadata: Record<string, any> = {};");
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("leaves rejected frontmatter to a custom parser", () => {
|
|
295
|
+
const source = "---\nanchor: &a 1\n---\n\n# x\n";
|
|
296
|
+
const r = pfmToSvelte(source, { lenient_frontmatter: true });
|
|
297
|
+
expect(r.diagnostics).toEqual([]);
|
|
298
|
+
expect(r.code).toContain("export const metadata: Record<string, any> = {};");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it("reports an element all mode keeps as a warning", () => {
|
|
302
|
+
const source = "<img src=\"/a.png\" use:act />\n";
|
|
303
|
+
const r = pfmToSvelte(source, {
|
|
304
|
+
...docs,
|
|
305
|
+
compile: { ...docs.compile, component_mode: "all" },
|
|
306
|
+
});
|
|
307
|
+
expect(r.diagnostics).toHaveLength(1);
|
|
308
|
+
const [d] = r.diagnostics;
|
|
309
|
+
expect(d.severity).toBe("warning");
|
|
310
|
+
expect(source.slice(d.start, d.end)).toBe("<img");
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
describe("template_value_at", () => {
|
|
315
|
+
const source = "---\ntitle: x\ntemplate: docs # main\n'template':\n---\n";
|
|
316
|
+
const frontmatter = { start: 4, end: source.indexOf("\n---", 4) };
|
|
317
|
+
const value = (offset: number) => {
|
|
318
|
+
const r = template_value_at(source, offset, frontmatter);
|
|
319
|
+
return r && [source.slice(r.start, r.end), r.start];
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
it("gives the value of the template key the offset is in, without a comment", () => {
|
|
323
|
+
const at = source.indexOf("docs");
|
|
324
|
+
expect(value(at)).toEqual(["docs", at]);
|
|
325
|
+
expect(value(at + 2)).toEqual(["docs", at]);
|
|
326
|
+
expect(value(at + 4)).toEqual(["docs", at]);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("gives an empty range for a key with no value yet", () => {
|
|
330
|
+
const end = source.indexOf("\n---");
|
|
331
|
+
expect(value(end)).toEqual(["", end]);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("is null before the value, on another key or outside the frontmatter", () => {
|
|
335
|
+
expect(value(source.indexOf("template:") + 3)).toBeNull();
|
|
336
|
+
expect(value(source.indexOf("title") + 8)).toBeNull();
|
|
337
|
+
expect(value(source.length)).toBeNull();
|
|
338
|
+
});
|
|
339
|
+
});
|