@alloy-js/markdown 0.26.0 → 0.27.0-dev.5
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/README.md +45 -0
- package/dist/dev/src/components/Code.js +20 -5
- package/dist/dev/src/components/Code.js.map +1 -1
- package/dist/dev/src/components/Heading.js +66 -2
- package/dist/dev/src/components/Heading.js.map +1 -1
- package/dist/dev/src/components/Link.js +33 -1
- package/dist/dev/src/components/Link.js.map +1 -1
- package/dist/dev/src/components/SourceFile.js +27 -6
- package/dist/dev/src/components/SourceFile.js.map +1 -1
- package/dist/dev/src/components/index.js +2 -0
- package/dist/dev/src/components/index.js.map +1 -1
- package/dist/dev/src/components/inline-code.js +28 -0
- package/dist/dev/src/components/inline-code.js.map +1 -0
- package/dist/dev/src/components/table.js +98 -0
- package/dist/dev/src/components/table.js.map +1 -0
- package/dist/dev/src/context/source-file.js +50 -0
- package/dist/dev/src/context/source-file.js.map +1 -0
- package/dist/dev/src/context/table.js +8 -0
- package/dist/dev/src/context/table.js.map +1 -0
- package/dist/dev/src/symbols/heading-symbol.js +22 -0
- package/dist/dev/src/symbols/heading-symbol.js.map +1 -0
- package/dist/dev/test/Code.test.js +150 -6
- package/dist/dev/test/Code.test.js.map +1 -1
- package/dist/dev/test/heading.test.js +774 -0
- package/dist/dev/test/heading.test.js.map +1 -0
- package/dist/dev/test/inline-code.test.js +236 -0
- package/dist/dev/test/inline-code.test.js.map +1 -0
- package/dist/dev/test/table.test.js +499 -0
- package/dist/dev/test/table.test.js.map +1 -0
- package/dist/dev/test/vitest.d.js.map +1 -1
- package/dist/src/components/Code.d.ts +10 -3
- package/dist/src/components/Code.d.ts.map +1 -1
- package/dist/src/components/Code.js +19 -4
- package/dist/src/components/Code.js.map +1 -1
- package/dist/src/components/Heading.d.ts +10 -2
- package/dist/src/components/Heading.d.ts.map +1 -1
- package/dist/src/components/Heading.js +54 -2
- package/dist/src/components/Heading.js.map +1 -1
- package/dist/src/components/Link.d.ts +9 -1
- package/dist/src/components/Link.d.ts.map +1 -1
- package/dist/src/components/Link.js +33 -1
- package/dist/src/components/Link.js.map +1 -1
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +17 -4
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/index.d.ts +2 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +2 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/components/inline-code.d.ts +16 -0
- package/dist/src/components/inline-code.d.ts.map +1 -0
- package/dist/src/components/inline-code.js +28 -0
- package/dist/src/components/inline-code.js.map +1 -0
- package/dist/src/components/table.d.ts +37 -0
- package/dist/src/components/table.d.ts.map +1 -0
- package/dist/src/components/table.js +74 -0
- package/dist/src/components/table.js.map +1 -0
- package/dist/src/context/source-file.d.ts +19 -0
- package/dist/src/context/source-file.d.ts.map +1 -0
- package/dist/src/context/source-file.js +50 -0
- package/dist/src/context/source-file.js.map +1 -0
- package/dist/src/context/table.d.ts +4 -0
- package/dist/src/context/table.d.ts.map +1 -0
- package/dist/src/context/table.js +8 -0
- package/dist/src/context/table.js.map +1 -0
- package/dist/src/symbols/heading-symbol.d.ts +14 -0
- package/dist/src/symbols/heading-symbol.d.ts.map +1 -0
- package/dist/src/symbols/heading-symbol.js +22 -0
- package/dist/src/symbols/heading-symbol.js.map +1 -0
- package/dist/test/Code.test.js +107 -3
- package/dist/test/Code.test.js.map +1 -1
- package/dist/test/heading.test.d.ts +2 -0
- package/dist/test/heading.test.d.ts.map +1 -0
- package/dist/test/heading.test.js +494 -0
- package/dist/test/heading.test.js.map +1 -0
- package/dist/test/inline-code.test.d.ts +2 -0
- package/dist/test/inline-code.test.d.ts.map +1 -0
- package/dist/test/inline-code.test.js +148 -0
- package/dist/test/inline-code.test.js.map +1 -0
- package/dist/test/table.test.d.ts +2 -0
- package/dist/test/table.test.d.ts.map +1 -0
- package/dist/test/table.test.js +303 -0
- package/dist/test/table.test.js.map +1 -0
- package/dist/test/vitest.d.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/docs/index.md +92 -0
- package/docs/tables.md +81 -0
- package/package.json +12 -9
- package/src/components/Code.tsx +35 -8
- package/src/components/Heading.tsx +103 -4
- package/src/components/Link.tsx +61 -2
- package/src/components/SourceFile.tsx +19 -2
- package/src/components/index.ts +2 -0
- package/src/components/inline-code.tsx +51 -0
- package/src/components/table.tsx +113 -0
- package/src/context/source-file.ts +79 -0
- package/src/context/table.ts +8 -0
- package/src/symbols/heading-symbol.ts +38 -0
- package/test/Code.test.tsx +102 -2
- package/test/heading.test.tsx +464 -0
- package/test/inline-code.test.tsx +166 -0
- package/test/table.test.tsx +282 -0
- package/test/vitest.d.ts +4 -1
- package/api-extractor.json +0 -4
- package/temp/api.json +0 -1361
- package/tsdoc-metadata.json +0 -11
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getDiagnosticsForTree,
|
|
3
|
+
Output,
|
|
4
|
+
printTree,
|
|
5
|
+
ref,
|
|
6
|
+
refkey,
|
|
7
|
+
renderTree,
|
|
8
|
+
SourceDirectory,
|
|
9
|
+
Show,
|
|
10
|
+
} from "@alloy-js/core";
|
|
11
|
+
import { PackageDirectory } from "@alloy-js/typescript";
|
|
12
|
+
import { expect, it } from "vitest";
|
|
13
|
+
|
|
14
|
+
import { TableContext } from "../src/context/table.js";
|
|
15
|
+
import {
|
|
16
|
+
Heading,
|
|
17
|
+
InlineCode,
|
|
18
|
+
Link,
|
|
19
|
+
Section,
|
|
20
|
+
SourceFile,
|
|
21
|
+
} from "../src/index.js";
|
|
22
|
+
import { mdTest } from "./utils.jsx";
|
|
23
|
+
|
|
24
|
+
it("renders Markdown files inside a TypeScript package directory", () => {
|
|
25
|
+
const key = refkey();
|
|
26
|
+
expect(
|
|
27
|
+
<Output>
|
|
28
|
+
<PackageDirectory name="example" path="example" version="1.0.0">
|
|
29
|
+
<SourceFile path="README.md">Plain README</SourceFile>
|
|
30
|
+
<SourceFile path="guide.md">
|
|
31
|
+
<Heading refkey={key}>Guide</Heading>
|
|
32
|
+
</SourceFile>
|
|
33
|
+
<SourceFile path="index.md">{key}</SourceFile>
|
|
34
|
+
</PackageDirectory>
|
|
35
|
+
</Output>,
|
|
36
|
+
).toRenderTo({
|
|
37
|
+
"example/README.md": "Plain README",
|
|
38
|
+
"example/guide.md": "# Guide",
|
|
39
|
+
"example/index.md": "[Guide](guide.md#guide)",
|
|
40
|
+
"example/package.json": expect.anything(),
|
|
41
|
+
"example/tsconfig.json": expect.anything(),
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("requires explicit IDs for references after opaque heading children", () => {
|
|
46
|
+
const key = refkey();
|
|
47
|
+
expect(() =>
|
|
48
|
+
renderTree(
|
|
49
|
+
mdTest([
|
|
50
|
+
<Heading id="code-api">
|
|
51
|
+
<InlineCode>API</InlineCode>
|
|
52
|
+
</Heading>,
|
|
53
|
+
<Heading refkey={key}>API</Heading>,
|
|
54
|
+
key,
|
|
55
|
+
]),
|
|
56
|
+
),
|
|
57
|
+
).toThrow("requires an explicit id after a heading with non-string children");
|
|
58
|
+
expect(
|
|
59
|
+
mdTest([
|
|
60
|
+
<Heading id="code-api">
|
|
61
|
+
<InlineCode>API</InlineCode>
|
|
62
|
+
</Heading>,
|
|
63
|
+
<Heading refkey={key} id="plain-api">
|
|
64
|
+
API
|
|
65
|
+
</Heading>,
|
|
66
|
+
key,
|
|
67
|
+
]),
|
|
68
|
+
).toRenderTo(`
|
|
69
|
+
<a id="code-api"></a>
|
|
70
|
+
|
|
71
|
+
# \`API\`
|
|
72
|
+
|
|
73
|
+
<a id="plain-api"></a>
|
|
74
|
+
|
|
75
|
+
# API
|
|
76
|
+
|
|
77
|
+
[API](#plain-api)
|
|
78
|
+
`);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("updates explicit heading IDs and their references reactively", () => {
|
|
82
|
+
const anchor = ref("old");
|
|
83
|
+
const key = refkey();
|
|
84
|
+
const tree = renderTree(
|
|
85
|
+
mdTest([
|
|
86
|
+
<Heading refkey={key} id={anchor.value}>
|
|
87
|
+
Title
|
|
88
|
+
</Heading>,
|
|
89
|
+
key,
|
|
90
|
+
]),
|
|
91
|
+
);
|
|
92
|
+
expect(printTree(tree)).toBe('<a id="old"></a>\n\n# Title\n\n[Title](#old)');
|
|
93
|
+
anchor.value = "new";
|
|
94
|
+
expect(printTree(tree)).toBe('<a id="new"></a>\n\n# Title\n\n[Title](#new)');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("validates reactive heading IDs", () => {
|
|
98
|
+
const anchor = ref("valid");
|
|
99
|
+
const tree = renderTree(mdTest([<Heading id={anchor.value}>Title</Heading>]));
|
|
100
|
+
expect(() => {
|
|
101
|
+
anchor.value = 'bad"id';
|
|
102
|
+
printTree(tree);
|
|
103
|
+
}).toThrow("Invalid Markdown heading ID");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("rejects collisions introduced by a reactive heading ID", () => {
|
|
107
|
+
const anchor = ref("old");
|
|
108
|
+
const tree = renderTree(
|
|
109
|
+
mdTest([
|
|
110
|
+
<Heading id={anchor.value}>First</Heading>,
|
|
111
|
+
<Heading>Second</Heading>,
|
|
112
|
+
]),
|
|
113
|
+
);
|
|
114
|
+
expect(() => {
|
|
115
|
+
anchor.value = "second";
|
|
116
|
+
printTree(tree);
|
|
117
|
+
}).toThrow("Duplicate Markdown heading ID");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("updates selected link refkeys and unresolved diagnostics reactively", () => {
|
|
121
|
+
const first = refkey();
|
|
122
|
+
const second = refkey();
|
|
123
|
+
const missing = refkey("missing-selected-heading");
|
|
124
|
+
const selected = ref(first);
|
|
125
|
+
const tree = renderTree(
|
|
126
|
+
mdTest([
|
|
127
|
+
<Heading refkey={first}>First</Heading>,
|
|
128
|
+
<Heading refkey={second}>Second</Heading>,
|
|
129
|
+
<Link refkey={selected.value} />,
|
|
130
|
+
]),
|
|
131
|
+
);
|
|
132
|
+
expect(printTree(tree)).toBe("# First\n\n# Second\n\n[First](#first)");
|
|
133
|
+
selected.value = second;
|
|
134
|
+
expect(printTree(tree)).toBe("# First\n\n# Second\n\n[Second](#second)");
|
|
135
|
+
expect(getDiagnosticsForTree(tree)).toEqual([]);
|
|
136
|
+
selected.value = missing;
|
|
137
|
+
expect(printTree(tree)).toBe(
|
|
138
|
+
"# First\n\n# Second\n\n<Unresolved Symbol: refkey[smissing-selected-heading]>",
|
|
139
|
+
);
|
|
140
|
+
expect(getDiagnosticsForTree(tree)).toMatchObject([
|
|
141
|
+
{
|
|
142
|
+
severity: "warning",
|
|
143
|
+
message: "Unresolved refkey: refkey[smissing-selected-heading]",
|
|
144
|
+
},
|
|
145
|
+
]);
|
|
146
|
+
selected.value = first;
|
|
147
|
+
expect(printTree(tree)).toBe("# First\n\n# Second\n\n[First](#first)");
|
|
148
|
+
expect(getDiagnosticsForTree(tree)).toEqual([]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("resolves forward and backward references in the same file", () => {
|
|
152
|
+
const key = refkey();
|
|
153
|
+
const template = mdTest([
|
|
154
|
+
<Link refkey={key} />,
|
|
155
|
+
<Heading refkey={key}>Hello world!</Heading>,
|
|
156
|
+
key,
|
|
157
|
+
<Link refkey={key} title="Back to top" />,
|
|
158
|
+
]);
|
|
159
|
+
expect(template).toRenderTo(`
|
|
160
|
+
[Hello world!](#hello-world)
|
|
161
|
+
|
|
162
|
+
# Hello world!
|
|
163
|
+
|
|
164
|
+
[Hello world!](#hello-world)
|
|
165
|
+
|
|
166
|
+
[Back to top](#hello-world)
|
|
167
|
+
`);
|
|
168
|
+
expect(getDiagnosticsForTree(renderTree(template))).toEqual([]);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("resolves relative links across directories and encodes paths and fragments", () => {
|
|
172
|
+
const first = refkey();
|
|
173
|
+
const second = refkey();
|
|
174
|
+
const template = (
|
|
175
|
+
<Output>
|
|
176
|
+
<SourceDirectory path="guide">
|
|
177
|
+
<SourceFile path="intro.md">
|
|
178
|
+
<Heading refkey={first}>Introduction</Heading>
|
|
179
|
+
{second}
|
|
180
|
+
</SourceFile>
|
|
181
|
+
</SourceDirectory>
|
|
182
|
+
<SourceDirectory path="reference">
|
|
183
|
+
<SourceFile path="API types).md">
|
|
184
|
+
<Heading refkey={second}>Café</Heading>
|
|
185
|
+
{first}
|
|
186
|
+
</SourceFile>
|
|
187
|
+
</SourceDirectory>
|
|
188
|
+
</Output>
|
|
189
|
+
);
|
|
190
|
+
expect(template).toRenderTo({
|
|
191
|
+
"guide/intro.md":
|
|
192
|
+
"# Introduction\n\n[Café](../reference/API%20types%29.md#caf%C3%A9)",
|
|
193
|
+
"reference/API types).md":
|
|
194
|
+
"# Café\n\n[Introduction](../guide/intro.md#introduction)",
|
|
195
|
+
});
|
|
196
|
+
expect(getDiagnosticsForTree(renderTree(template))).toEqual([]);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("counts unreferenced and nested section headings when deconflicting slugs", () => {
|
|
200
|
+
const second = refkey();
|
|
201
|
+
const third = refkey();
|
|
202
|
+
expect(
|
|
203
|
+
mdTest([
|
|
204
|
+
<Section heading="Hello">Body</Section>,
|
|
205
|
+
<Heading refkey={second}>Hello</Heading>,
|
|
206
|
+
<Heading>Hello-1</Heading>,
|
|
207
|
+
<Heading refkey={third}>Hello</Heading>,
|
|
208
|
+
second,
|
|
209
|
+
third,
|
|
210
|
+
]),
|
|
211
|
+
).toRenderTo(`
|
|
212
|
+
# Hello
|
|
213
|
+
|
|
214
|
+
Body
|
|
215
|
+
|
|
216
|
+
# Hello
|
|
217
|
+
|
|
218
|
+
# Hello-1
|
|
219
|
+
|
|
220
|
+
# Hello
|
|
221
|
+
|
|
222
|
+
[Hello](#hello-1)
|
|
223
|
+
|
|
224
|
+
[Hello](#hello-2)
|
|
225
|
+
`);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("uses explicit IDs independently of heading text", () => {
|
|
229
|
+
const key = refkey();
|
|
230
|
+
expect(
|
|
231
|
+
mdTest([
|
|
232
|
+
<Link refkey={key} />,
|
|
233
|
+
<Heading refkey={key} id="stable-api">
|
|
234
|
+
New display name
|
|
235
|
+
</Heading>,
|
|
236
|
+
]),
|
|
237
|
+
).toRenderTo(`
|
|
238
|
+
[New display name](#stable-api)
|
|
239
|
+
|
|
240
|
+
<a id="stable-api"></a>
|
|
241
|
+
|
|
242
|
+
# New display name
|
|
243
|
+
`);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("keeps heading ID allocation local to each file", () => {
|
|
247
|
+
const key = refkey();
|
|
248
|
+
expect(
|
|
249
|
+
<Output>
|
|
250
|
+
<SourceFile path="one.md">
|
|
251
|
+
<Heading>Same</Heading>
|
|
252
|
+
{key}
|
|
253
|
+
</SourceFile>
|
|
254
|
+
<SourceFile path="two.md">
|
|
255
|
+
<Heading refkey={key}>Same</Heading>
|
|
256
|
+
</SourceFile>
|
|
257
|
+
</Output>,
|
|
258
|
+
).toRenderTo({
|
|
259
|
+
"one.md": "# Same\n\n[Same](two.md#same)",
|
|
260
|
+
"two.md": "# Same",
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("reports unresolved refkeys using core diagnostics", () => {
|
|
265
|
+
const key = refkey("missing-heading");
|
|
266
|
+
const tree = renderTree(mdTest([<Link refkey={key} />]));
|
|
267
|
+
expect(tree).toRenderTo("<Unresolved Symbol: refkey[smissing-heading]>");
|
|
268
|
+
expect(getDiagnosticsForTree(tree)).toMatchObject([
|
|
269
|
+
{
|
|
270
|
+
severity: "warning",
|
|
271
|
+
message: "Unresolved refkey: refkey[smissing-heading]",
|
|
272
|
+
},
|
|
273
|
+
]);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it.each(["", 'bad"id'])("rejects an invalid explicit heading ID %j", (id) => {
|
|
277
|
+
expect(() => renderTree(mdTest([<Heading id={id}>Title</Heading>]))).toThrow(
|
|
278
|
+
"Invalid Markdown heading ID",
|
|
279
|
+
);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("provides an explicit anchor when the automatic slug would be empty", () => {
|
|
283
|
+
const key = refkey();
|
|
284
|
+
expect(mdTest([<Heading refkey={key}>!!!</Heading>, key])).toRenderTo(`
|
|
285
|
+
<a id="section"></a>
|
|
286
|
+
|
|
287
|
+
# !!!
|
|
288
|
+
|
|
289
|
+
[!!!](#section)
|
|
290
|
+
`);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("rejects colliding explicit and automatic IDs", () => {
|
|
294
|
+
expect(() =>
|
|
295
|
+
renderTree(
|
|
296
|
+
mdTest([<Heading id="second">First</Heading>, <Heading>Second</Heading>]),
|
|
297
|
+
),
|
|
298
|
+
).toThrow("Duplicate Markdown heading ID");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it("preserves direct href links", () => {
|
|
302
|
+
expect(
|
|
303
|
+
mdTest([<Link href="https://example.com" title="Example" />]),
|
|
304
|
+
).toRenderTo("[Example](https://example.com)");
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("trims heading text and escapes the default link label", () => {
|
|
308
|
+
const name = "";
|
|
309
|
+
const key = refkey();
|
|
310
|
+
const path = refkey();
|
|
311
|
+
expect(
|
|
312
|
+
mdTest([
|
|
313
|
+
<Heading refkey={key}>Model {name}</Heading>,
|
|
314
|
+
<Heading refkey={path}>Use ~/config_file</Heading>,
|
|
315
|
+
key,
|
|
316
|
+
path,
|
|
317
|
+
]),
|
|
318
|
+
).toRenderTo(`
|
|
319
|
+
# Model
|
|
320
|
+
|
|
321
|
+
# Use ~/config_file
|
|
322
|
+
|
|
323
|
+
[Model](#model)
|
|
324
|
+
|
|
325
|
+
[Use \\~/config\\_file](#use-config_file)
|
|
326
|
+
`);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("requires an explicit ID to reference a heading with JSX children", () => {
|
|
330
|
+
const key = refkey();
|
|
331
|
+
expect(() =>
|
|
332
|
+
renderTree(
|
|
333
|
+
mdTest([
|
|
334
|
+
<Heading refkey={key}>
|
|
335
|
+
Use <InlineCode>Foo</InlineCode>
|
|
336
|
+
</Heading>,
|
|
337
|
+
]),
|
|
338
|
+
),
|
|
339
|
+
).toThrow("requires an explicit id");
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("renders JSX heading children once and links to their explicit ID", () => {
|
|
343
|
+
const key = refkey();
|
|
344
|
+
let calls = 0;
|
|
345
|
+
function Name() {
|
|
346
|
+
calls++;
|
|
347
|
+
return <InlineCode>Foo</InlineCode>;
|
|
348
|
+
}
|
|
349
|
+
expect(
|
|
350
|
+
mdTest([
|
|
351
|
+
<Heading refkey={key} id="foo">
|
|
352
|
+
Use <Name />
|
|
353
|
+
</Heading>,
|
|
354
|
+
<Link refkey={key} title="Foo" />,
|
|
355
|
+
]),
|
|
356
|
+
).toRenderTo(`
|
|
357
|
+
<a id="foo"></a>
|
|
358
|
+
|
|
359
|
+
# Use \`Foo\`
|
|
360
|
+
|
|
361
|
+
[Foo](#foo)
|
|
362
|
+
`);
|
|
363
|
+
expect(calls).toBe(1);
|
|
364
|
+
expect(() =>
|
|
365
|
+
renderTree(
|
|
366
|
+
mdTest([
|
|
367
|
+
<Heading refkey={key} id="foo">
|
|
368
|
+
<InlineCode>Foo</InlineCode>
|
|
369
|
+
</Heading>,
|
|
370
|
+
key,
|
|
371
|
+
]),
|
|
372
|
+
),
|
|
373
|
+
).toThrow("requires a title");
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("updates a reactive heading and its reference label and slug", () => {
|
|
377
|
+
const name = ref("Alpha");
|
|
378
|
+
const key = refkey();
|
|
379
|
+
const tree = renderTree(
|
|
380
|
+
mdTest([<Heading refkey={key}>{name.value}</Heading>, key]),
|
|
381
|
+
);
|
|
382
|
+
expect(printTree(tree)).toBe("# Alpha\n\n[Alpha](#alpha)");
|
|
383
|
+
name.value = "Beta";
|
|
384
|
+
expect(printTree(tree)).toBe("# Beta\n\n[Beta](#beta)");
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("reclaims duplicate suffixes when an earlier heading is removed", () => {
|
|
388
|
+
const show = ref(true);
|
|
389
|
+
const key = refkey();
|
|
390
|
+
const tree = renderTree(
|
|
391
|
+
mdTest([
|
|
392
|
+
<Show when={show.value}>
|
|
393
|
+
<Heading>Hello</Heading>
|
|
394
|
+
</Show>,
|
|
395
|
+
<Heading refkey={key}>Hello</Heading>,
|
|
396
|
+
key,
|
|
397
|
+
]),
|
|
398
|
+
);
|
|
399
|
+
expect(printTree(tree)).toBe("# Hello\n\n# Hello\n\n[Hello](#hello-1)");
|
|
400
|
+
show.value = false;
|
|
401
|
+
expect(printTree(tree)).toBe("# Hello\n\n[Hello](#hello)");
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it("forgets removed headings when the only heading remounts", () => {
|
|
405
|
+
const show = ref(true);
|
|
406
|
+
const key = refkey();
|
|
407
|
+
const tree = renderTree(
|
|
408
|
+
mdTest([
|
|
409
|
+
<Show when={show.value}>
|
|
410
|
+
<Heading refkey={key}>Hello</Heading>
|
|
411
|
+
<hbr />
|
|
412
|
+
<hbr />
|
|
413
|
+
{key}
|
|
414
|
+
</Show>,
|
|
415
|
+
]),
|
|
416
|
+
);
|
|
417
|
+
expect(printTree(tree)).toBe("# Hello\n\n[Hello](#hello)");
|
|
418
|
+
show.value = false;
|
|
419
|
+
expect(printTree(tree)).toBe("");
|
|
420
|
+
show.value = true;
|
|
421
|
+
expect(printTree(tree)).toBe("# Hello\n\n[Hello](#hello)");
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("allocates punctuation fallback IDs after automatic IDs", () => {
|
|
425
|
+
const empty = refkey();
|
|
426
|
+
const key = refkey();
|
|
427
|
+
expect(
|
|
428
|
+
mdTest([
|
|
429
|
+
<Heading refkey={empty}>!!!</Heading>,
|
|
430
|
+
<Heading refkey={key}>Section</Heading>,
|
|
431
|
+
empty,
|
|
432
|
+
key,
|
|
433
|
+
]),
|
|
434
|
+
).toRenderTo(`
|
|
435
|
+
<a id="section-1"></a>
|
|
436
|
+
|
|
437
|
+
# !!!
|
|
438
|
+
|
|
439
|
+
# Section
|
|
440
|
+
|
|
441
|
+
[!!!](#section-1)
|
|
442
|
+
|
|
443
|
+
[Section](#section)
|
|
444
|
+
`);
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it("escapes default label pipes only in a table cell context", () => {
|
|
448
|
+
const key = refkey();
|
|
449
|
+
expect(
|
|
450
|
+
mdTest([
|
|
451
|
+
<Heading refkey={key}>A | B</Heading>,
|
|
452
|
+
<TableContext.Provider value={true}>
|
|
453
|
+
<Link refkey={key} />
|
|
454
|
+
</TableContext.Provider>,
|
|
455
|
+
<Link refkey={key} />,
|
|
456
|
+
]),
|
|
457
|
+
).toRenderTo(`
|
|
458
|
+
# A | B
|
|
459
|
+
|
|
460
|
+
[A \\| B](#a--b)
|
|
461
|
+
|
|
462
|
+
[A | B](#a--b)
|
|
463
|
+
`);
|
|
464
|
+
});
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
List,
|
|
4
|
+
printTree,
|
|
5
|
+
ref,
|
|
6
|
+
renderTree,
|
|
7
|
+
useContext,
|
|
8
|
+
} from "@alloy-js/core";
|
|
9
|
+
import { marked } from "marked";
|
|
10
|
+
import { expect, it } from "vitest";
|
|
11
|
+
|
|
12
|
+
import { TableContext } from "../src/context/table.js";
|
|
13
|
+
import { Heading, InlineCode } from "../src/index.js";
|
|
14
|
+
import { mdTest } from "./utils.jsx";
|
|
15
|
+
|
|
16
|
+
it.each([
|
|
17
|
+
["plain", "`plain`"],
|
|
18
|
+
["a``b`c", "```a``b`c```"],
|
|
19
|
+
["``", "``` `` ```"],
|
|
20
|
+
["`name", "`` `name ``"],
|
|
21
|
+
["name`", "`` name` ``"],
|
|
22
|
+
[" name ", "` name `"],
|
|
23
|
+
[" name", "` name`"],
|
|
24
|
+
["name ", "`name `"],
|
|
25
|
+
[" ", "` `"],
|
|
26
|
+
["a\r\nb\rc\nd", "`a b c d`"],
|
|
27
|
+
["a \nb", "`a b`"],
|
|
28
|
+
["\nname\n", "` name `"],
|
|
29
|
+
["| & < > [label] \\", "`| & < > [label] \\`"],
|
|
30
|
+
])("preserves parsed code-span text for %j", (text, expected) => {
|
|
31
|
+
const output = printTree(renderTree(<InlineCode>{text}</InlineCode>));
|
|
32
|
+
expect(output).toBe(expected);
|
|
33
|
+
expect(marked.Lexer.lexInline(output)).toMatchObject([
|
|
34
|
+
{ type: "codespan", text: text.replace(/\r\n|\r|\n/g, " ") },
|
|
35
|
+
]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("renders an empty span as inline HTML", () => {
|
|
39
|
+
expect(mdTest(<InlineCode />)).toRenderTo("<code></code>");
|
|
40
|
+
expect(marked.parseInline(printTree(renderTree(<InlineCode />)))).toBe(
|
|
41
|
+
"<code></code>",
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("escapes direct string pipes only within the table context", () => {
|
|
46
|
+
expect(
|
|
47
|
+
<>
|
|
48
|
+
<TableContext.Provider value={true}>
|
|
49
|
+
<InlineCode>{"string | null"}</InlineCode>{" "}
|
|
50
|
+
<InlineCode>{"^(a|b)\\.json$"}</InlineCode>
|
|
51
|
+
</TableContext.Provider>{" "}
|
|
52
|
+
<InlineCode>{"string | null"}</InlineCode>
|
|
53
|
+
</>,
|
|
54
|
+
).toRenderTo("`string \\| null` `^(a\\|b)\\.json$` `string | null`");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("leaves opaque JSX pipes unchanged inside table cells", () => {
|
|
58
|
+
function Content() {
|
|
59
|
+
return "a|b";
|
|
60
|
+
}
|
|
61
|
+
expect(
|
|
62
|
+
<TableContext.Provider value={true}>
|
|
63
|
+
<InlineCode>
|
|
64
|
+
<Content />
|
|
65
|
+
</InlineCode>
|
|
66
|
+
</TableContext.Provider>,
|
|
67
|
+
).toRenderTo("`a|b`");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("escapes updated direct string pipes inside table cells", () => {
|
|
71
|
+
const text = ref("first");
|
|
72
|
+
const tree = renderTree(
|
|
73
|
+
<TableContext.Provider value={true}>
|
|
74
|
+
<InlineCode>{text.value}</InlineCode>
|
|
75
|
+
</TableContext.Provider>,
|
|
76
|
+
);
|
|
77
|
+
expect(printTree(tree)).toBe("`first`");
|
|
78
|
+
text.value = "a|b";
|
|
79
|
+
expect(printTree(tree)).toBe("`a\\|b`");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("leaves JSX text unescaped while preserving its context", () => {
|
|
83
|
+
const Name = createContext<string>();
|
|
84
|
+
function NamePart() {
|
|
85
|
+
return useContext(Name);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const output = printTree(
|
|
89
|
+
renderTree(
|
|
90
|
+
<Name.Provider value="`b">
|
|
91
|
+
<InlineCode>
|
|
92
|
+
{"a`"}
|
|
93
|
+
<NamePart />
|
|
94
|
+
</InlineCode>
|
|
95
|
+
</Name.Provider>,
|
|
96
|
+
),
|
|
97
|
+
);
|
|
98
|
+
expect(output).toBe("`a``b`");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("preserves JSX formatting without normalizing its line breaks", () => {
|
|
102
|
+
const output = printTree(
|
|
103
|
+
renderTree(
|
|
104
|
+
<InlineCode>
|
|
105
|
+
<List>
|
|
106
|
+
<>first</>
|
|
107
|
+
<>second</>
|
|
108
|
+
</List>
|
|
109
|
+
</InlineCode>,
|
|
110
|
+
),
|
|
111
|
+
);
|
|
112
|
+
expect(output).toBe("`first\nsecond`");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("composes in a heading and a link label without treating content as markup", () => {
|
|
116
|
+
const output = printTree(
|
|
117
|
+
renderTree(
|
|
118
|
+
<Heading level={2}>
|
|
119
|
+
[<InlineCode>{"a`b [label]"}</InlineCode>](https://example.com)
|
|
120
|
+
</Heading>,
|
|
121
|
+
),
|
|
122
|
+
);
|
|
123
|
+
expect(output).toBe("## [``a`b [label]``](https://example.com)");
|
|
124
|
+
expect(marked.lexer(output)[0]).toMatchObject({
|
|
125
|
+
type: "heading",
|
|
126
|
+
depth: 2,
|
|
127
|
+
tokens: [
|
|
128
|
+
{
|
|
129
|
+
type: "link",
|
|
130
|
+
href: "https://example.com",
|
|
131
|
+
tokens: [{ type: "codespan", text: "a`b [label]" }],
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("updates delimiters and padding when a direct string child changes", () => {
|
|
138
|
+
const text = ref("name");
|
|
139
|
+
const tree = renderTree(<InlineCode>{text.value}</InlineCode>);
|
|
140
|
+
expect(printTree(tree)).toBe("`name`");
|
|
141
|
+
|
|
142
|
+
text.value = "`name``";
|
|
143
|
+
expect(printTree(tree)).toBe("``` `name`` ```");
|
|
144
|
+
|
|
145
|
+
text.value = " name ";
|
|
146
|
+
expect(printTree(tree)).toBe("` name `");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("leaves reactive JSX content unescaped", () => {
|
|
150
|
+
const text = ref("name");
|
|
151
|
+
function Name() {
|
|
152
|
+
return text;
|
|
153
|
+
}
|
|
154
|
+
const tree = renderTree(
|
|
155
|
+
<InlineCode>
|
|
156
|
+
<Name />
|
|
157
|
+
</InlineCode>,
|
|
158
|
+
);
|
|
159
|
+
expect(printTree(tree)).toBe("`name`");
|
|
160
|
+
|
|
161
|
+
text.value = "`name``";
|
|
162
|
+
expect(printTree(tree)).toBe("``name```");
|
|
163
|
+
|
|
164
|
+
text.value = " name ";
|
|
165
|
+
expect(printTree(tree)).toBe("` name `");
|
|
166
|
+
});
|