@fuzdev/fuz_ui 0.203.1 → 0.205.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.
Files changed (137) hide show
  1. package/dist/Alert.svelte +31 -17
  2. package/dist/Alert.svelte.d.ts +8 -2
  3. package/dist/Alert.svelte.d.ts.map +1 -1
  4. package/dist/ApiModule.svelte +14 -1
  5. package/dist/ApiModule.svelte.d.ts.map +1 -1
  6. package/dist/Card.svelte +28 -10
  7. package/dist/Card.svelte.d.ts +7 -2
  8. package/dist/Card.svelte.d.ts.map +1 -1
  9. package/dist/CopyToClipboard.svelte +5 -1
  10. package/dist/CopyToClipboard.svelte.d.ts.map +1 -1
  11. package/dist/DeclarationDetail.svelte +15 -4
  12. package/dist/DeclarationDetail.svelte.d.ts.map +1 -1
  13. package/dist/DocsLink.svelte +15 -4
  14. package/dist/DocsLink.svelte.d.ts +8 -1
  15. package/dist/DocsLink.svelte.d.ts.map +1 -1
  16. package/dist/DocsMenu.svelte +1 -1
  17. package/dist/DocsPageLinks.svelte +1 -1
  18. package/dist/EcosystemLinks.svelte +5 -2
  19. package/dist/EcosystemLinks.svelte.d.ts +2 -1
  20. package/dist/EcosystemLinks.svelte.d.ts.map +1 -1
  21. package/dist/EcosystemLinksPanel.svelte +4 -2
  22. package/dist/EcosystemLinksPanel.svelte.d.ts +2 -1
  23. package/dist/EcosystemLinksPanel.svelte.d.ts.map +1 -1
  24. package/dist/Hashlink.svelte +1 -1
  25. package/dist/ImgOrSvg.svelte +6 -5
  26. package/dist/ImgOrSvg.svelte.d.ts +2 -1
  27. package/dist/ImgOrSvg.svelte.d.ts.map +1 -1
  28. package/dist/LibraryDetail.svelte +5 -2
  29. package/dist/LibraryDetail.svelte.d.ts +3 -1
  30. package/dist/LibraryDetail.svelte.d.ts.map +1 -1
  31. package/dist/LibrarySummary.svelte +4 -2
  32. package/dist/LibrarySummary.svelte.d.ts +2 -1
  33. package/dist/LibrarySummary.svelte.d.ts.map +1 -1
  34. package/dist/PasteFromClipboard.svelte +3 -1
  35. package/dist/PasteFromClipboard.svelte.d.ts.map +1 -1
  36. package/dist/PendingButton.svelte +0 -2
  37. package/dist/PendingButton.svelte.d.ts.map +1 -1
  38. package/dist/StyleVariableButton.svelte +5 -1
  39. package/dist/StyleVariableButton.svelte.d.ts.map +1 -1
  40. package/dist/Svg.svelte +1 -1
  41. package/dist/TomeContent.svelte +5 -2
  42. package/dist/TomeContent.svelte.d.ts +2 -1
  43. package/dist/TomeContent.svelte.d.ts.map +1 -1
  44. package/dist/TypeLink.svelte +13 -4
  45. package/dist/TypeLink.svelte.d.ts +7 -1
  46. package/dist/TypeLink.svelte.d.ts.map +1 -1
  47. package/package.json +12 -36
  48. package/dist/Mdz.svelte +0 -34
  49. package/dist/Mdz.svelte.d.ts +0 -11
  50. package/dist/Mdz.svelte.d.ts.map +0 -1
  51. package/dist/MdzNodeView.svelte +0 -107
  52. package/dist/MdzNodeView.svelte.d.ts +0 -9
  53. package/dist/MdzNodeView.svelte.d.ts.map +0 -1
  54. package/dist/MdzPrecompiled.svelte +0 -30
  55. package/dist/MdzPrecompiled.svelte.d.ts +0 -11
  56. package/dist/MdzPrecompiled.svelte.d.ts.map +0 -1
  57. package/dist/MdzRoot.svelte +0 -30
  58. package/dist/MdzRoot.svelte.d.ts +0 -12
  59. package/dist/MdzRoot.svelte.d.ts.map +0 -1
  60. package/dist/MdzStream.svelte +0 -32
  61. package/dist/MdzStream.svelte.d.ts +0 -12
  62. package/dist/MdzStream.svelte.d.ts.map +0 -1
  63. package/dist/MdzStreamNodeView.svelte +0 -106
  64. package/dist/MdzStreamNodeView.svelte.d.ts +0 -9
  65. package/dist/MdzStreamNodeView.svelte.d.ts.map +0 -1
  66. package/dist/mdz.d.ts +0 -112
  67. package/dist/mdz.d.ts.map +0 -1
  68. package/dist/mdz.js +0 -1186
  69. package/dist/mdz_components.d.ts +0 -63
  70. package/dist/mdz_components.d.ts.map +0 -1
  71. package/dist/mdz_components.js +0 -32
  72. package/dist/mdz_helpers.d.ts +0 -164
  73. package/dist/mdz_helpers.d.ts.map +0 -1
  74. package/dist/mdz_helpers.js +0 -424
  75. package/dist/mdz_lexer.d.ts +0 -93
  76. package/dist/mdz_lexer.d.ts.map +0 -1
  77. package/dist/mdz_lexer.js +0 -732
  78. package/dist/mdz_opcodes.d.ts +0 -174
  79. package/dist/mdz_opcodes.d.ts.map +0 -1
  80. package/dist/mdz_opcodes.js +0 -14
  81. package/dist/mdz_opcodes_to_nodes.d.ts +0 -20
  82. package/dist/mdz_opcodes_to_nodes.d.ts.map +0 -1
  83. package/dist/mdz_opcodes_to_nodes.js +0 -332
  84. package/dist/mdz_stream_parser.d.ts +0 -80
  85. package/dist/mdz_stream_parser.d.ts.map +0 -1
  86. package/dist/mdz_stream_parser.js +0 -354
  87. package/dist/mdz_stream_parser_block.d.ts +0 -76
  88. package/dist/mdz_stream_parser_block.d.ts.map +0 -1
  89. package/dist/mdz_stream_parser_block.js +0 -458
  90. package/dist/mdz_stream_parser_inline.d.ts +0 -52
  91. package/dist/mdz_stream_parser_inline.d.ts.map +0 -1
  92. package/dist/mdz_stream_parser_inline.js +0 -378
  93. package/dist/mdz_stream_parser_link.d.ts +0 -22
  94. package/dist/mdz_stream_parser_link.d.ts.map +0 -1
  95. package/dist/mdz_stream_parser_link.js +0 -215
  96. package/dist/mdz_stream_parser_state.d.ts +0 -187
  97. package/dist/mdz_stream_parser_state.d.ts.map +0 -1
  98. package/dist/mdz_stream_parser_state.js +0 -398
  99. package/dist/mdz_stream_parser_text.d.ts +0 -14
  100. package/dist/mdz_stream_parser_text.d.ts.map +0 -1
  101. package/dist/mdz_stream_parser_text.js +0 -50
  102. package/dist/mdz_stream_parser_url.d.ts +0 -60
  103. package/dist/mdz_stream_parser_url.d.ts.map +0 -1
  104. package/dist/mdz_stream_parser_url.js +0 -307
  105. package/dist/mdz_stream_state.svelte.d.ts +0 -44
  106. package/dist/mdz_stream_state.svelte.d.ts.map +0 -1
  107. package/dist/mdz_stream_state.svelte.js +0 -357
  108. package/dist/mdz_to_svelte.d.ts +0 -41
  109. package/dist/mdz_to_svelte.d.ts.map +0 -1
  110. package/dist/mdz_to_svelte.js +0 -100
  111. package/dist/mdz_token_parser.d.ts +0 -14
  112. package/dist/mdz_token_parser.d.ts.map +0 -1
  113. package/dist/mdz_token_parser.js +0 -344
  114. package/dist/svelte_preprocess_mdz.d.ts +0 -65
  115. package/dist/svelte_preprocess_mdz.d.ts.map +0 -1
  116. package/dist/svelte_preprocess_mdz.js +0 -529
  117. package/dist/tsdoc_mdz.d.ts +0 -45
  118. package/dist/tsdoc_mdz.d.ts.map +0 -1
  119. package/dist/tsdoc_mdz.js +0 -88
  120. package/src/lib/mdz.ts +0 -1532
  121. package/src/lib/mdz_components.ts +0 -64
  122. package/src/lib/mdz_helpers.ts +0 -449
  123. package/src/lib/mdz_lexer.ts +0 -1012
  124. package/src/lib/mdz_opcodes.ts +0 -205
  125. package/src/lib/mdz_opcodes_to_nodes.ts +0 -375
  126. package/src/lib/mdz_stream_parser.ts +0 -415
  127. package/src/lib/mdz_stream_parser_block.ts +0 -532
  128. package/src/lib/mdz_stream_parser_inline.ts +0 -414
  129. package/src/lib/mdz_stream_parser_link.ts +0 -271
  130. package/src/lib/mdz_stream_parser_state.ts +0 -539
  131. package/src/lib/mdz_stream_parser_text.ts +0 -77
  132. package/src/lib/mdz_stream_parser_url.ts +0 -365
  133. package/src/lib/mdz_stream_state.svelte.ts +0 -387
  134. package/src/lib/mdz_to_svelte.ts +0 -141
  135. package/src/lib/mdz_token_parser.ts +0 -434
  136. package/src/lib/svelte_preprocess_mdz.ts +0 -742
  137. package/src/lib/tsdoc_mdz.ts +0 -97
@@ -1,387 +0,0 @@
1
- /**
2
- * Reactive state for the mdz streaming renderer.
3
- *
4
- * Maintains a reactive tree of `MdzStreamNode` objects that Svelte 5 can
5
- * efficiently update. Opcodes from `MdzStreamParser` are applied via
6
- * `apply()` or `apply_batch()`, mutating the tree in place.
7
- *
8
- * @module
9
- */
10
-
11
- import {DEV} from 'esm-env';
12
-
13
- import type {MdzNodeId, MdzNodeType, MdzOpcode} from './mdz_opcodes.js';
14
-
15
- /**
16
- * A reactive node in the stream renderer tree.
17
- * Fields are `$state` so Svelte re-renders only what changes.
18
- */
19
- export class MdzStreamNode {
20
- id: MdzNodeId;
21
- type: MdzNodeType;
22
- content: string = $state('');
23
- children: Array<MdzStreamNode> = $state([]);
24
- // metadata
25
- level?: number;
26
- reference?: string = $state();
27
- link_type?: string = $state();
28
- name?: string;
29
- lang?: string | null;
30
- heading_id?: string = $state();
31
-
32
- constructor(id: MdzNodeId, type: MdzNodeType) {
33
- this.id = id;
34
- this.type = type;
35
- }
36
- }
37
-
38
- /**
39
- * Reactive state manager for streaming mdz content.
40
- * Apply opcodes to incrementally build and update the render tree.
41
- */
42
- export class MdzStreamState {
43
- root: Array<MdzStreamNode> = $state([]);
44
-
45
- /** Node lookup by ID. */
46
- #nodes = new Map<MdzNodeId, MdzStreamNode>();
47
-
48
- /** Parent tracking for re-parenting during revert. */
49
- #parents = new Map<MdzNodeId, MdzNodeId | null>();
50
-
51
- /** Stack of open container IDs (for knowing current parent). */
52
- #stack: Array<MdzNodeId> = [];
53
-
54
- /**
55
- * Register a new node and attach it to its current parent (innermost open
56
- * container, or root). Shared by `open`, `text`, and `void` paths.
57
- */
58
- #attach(node: MdzStreamNode): void {
59
- this.#nodes.set(node.id, node);
60
- const parent_id = this.#stack.length > 0 ? this.#stack[this.#stack.length - 1]! : null;
61
- this.#parents.set(node.id, parent_id);
62
- if (parent_id !== null) {
63
- const parent = this.#nodes.get(parent_id);
64
- if (DEV && !parent) {
65
- throw new Error(`MdzStreamState: parent id ${parent_id} on stack but missing from nodes`);
66
- }
67
- if (parent) parent.children.push(node);
68
- } else {
69
- this.root.push(node);
70
- }
71
- }
72
-
73
- /**
74
- * Apply a single opcode.
75
- */
76
- apply(opcode: MdzOpcode): void {
77
- switch (opcode.type) {
78
- case 'open': {
79
- if (DEV && this.#nodes.has(opcode.id)) {
80
- throw new Error(
81
- `MdzStreamState: duplicate open for id ${opcode.id} (node_type '${opcode.node_type}')`,
82
- );
83
- }
84
- const node = new MdzStreamNode(opcode.id, opcode.node_type);
85
- if (opcode.level !== undefined) node.level = opcode.level;
86
- if (opcode.name !== undefined) node.name = opcode.name;
87
- if (opcode.lang !== undefined) node.lang = opcode.lang;
88
- this.#attach(node);
89
- this.#stack.push(opcode.id);
90
- break;
91
- }
92
-
93
- case 'close': {
94
- if (DEV) {
95
- if (!this.#nodes.has(opcode.id)) {
96
- throw new Error(`MdzStreamState: close for unknown id ${opcode.id}`);
97
- }
98
- if (!this.#stack.includes(opcode.id)) {
99
- throw new Error(`MdzStreamState: close for id ${opcode.id} which is not on the stack`);
100
- }
101
- }
102
- const node = this.#nodes.get(opcode.id);
103
- if (node) {
104
- if (opcode.discard) {
105
- // drop the node and its descendants from its parent's children
106
- const parent_id = this.#parents.get(opcode.id);
107
- const parent_children =
108
- parent_id !== null && parent_id !== undefined
109
- ? this.#nodes.get(parent_id)?.children
110
- : this.root;
111
- if (parent_children) {
112
- const idx = parent_children.indexOf(node);
113
- if (idx !== -1) parent_children.splice(idx, 1);
114
- }
115
- } else {
116
- // apply deferred metadata
117
- if (opcode.reference !== undefined) node.reference = opcode.reference;
118
- if (opcode.link_type !== undefined) node.link_type = opcode.link_type;
119
- if (opcode.heading_id !== undefined) node.heading_id = opcode.heading_id;
120
- // Code/Codeblock: collapse text children into `content` so renderers
121
- // can read it directly without walking children every render.
122
- if (node.type === 'Code' || node.type === 'Codeblock') {
123
- let content = '';
124
- for (const child of node.children) content += child.content;
125
- node.content = content;
126
- }
127
- }
128
- }
129
- // pop from stack
130
- const idx = this.#stack.lastIndexOf(opcode.id);
131
- if (idx !== -1) this.#stack.splice(idx, 1);
132
- // cleanup: closed nodes have no further opcode interactions.
133
- // children are retained in the render tree via children arrays
134
- // (discarded nodes' subtrees also need full cleanup, which the
135
- // traversal in `#cleanup_node` already handles).
136
- this.#cleanup_node(opcode.id);
137
- break;
138
- }
139
-
140
- case 'text': {
141
- if (DEV && this.#nodes.has(opcode.id)) {
142
- throw new Error(
143
- `MdzStreamState: duplicate text for id ${opcode.id} (text_type '${opcode.text_type}')`,
144
- );
145
- }
146
- const node = new MdzStreamNode(opcode.id, opcode.text_type);
147
- node.content = opcode.content;
148
- this.#attach(node);
149
- break;
150
- }
151
-
152
- case 'append_text': {
153
- if (DEV && !this.#nodes.has(opcode.id)) {
154
- throw new Error(`MdzStreamState: append_text for unknown id ${opcode.id}`);
155
- }
156
- const node = this.#nodes.get(opcode.id);
157
- if (node) {
158
- node.content += opcode.content;
159
- }
160
- break;
161
- }
162
-
163
- case 'trim_text': {
164
- if (DEV && !this.#nodes.has(opcode.id)) {
165
- throw new Error(`MdzStreamState: trim_text for unknown id ${opcode.id}`);
166
- }
167
- const node = this.#nodes.get(opcode.id);
168
- if (node) {
169
- node.content = node.content.slice(0, node.content.length - opcode.count);
170
- if (node.content.length === 0) {
171
- const parent_id = this.#parents.get(opcode.id);
172
- const parent_children =
173
- parent_id !== null && parent_id !== undefined
174
- ? this.#nodes.get(parent_id)?.children
175
- : this.root;
176
- if (parent_children) {
177
- const idx = parent_children.indexOf(node);
178
- if (idx !== -1) parent_children.splice(idx, 1);
179
- }
180
- this.#nodes.delete(opcode.id);
181
- this.#parents.delete(opcode.id);
182
- }
183
- }
184
- break;
185
- }
186
-
187
- case 'void': {
188
- if (DEV && this.#nodes.has(opcode.id)) {
189
- throw new Error(
190
- `MdzStreamState: duplicate void for id ${opcode.id} (node_type '${opcode.node_type}')`,
191
- );
192
- }
193
- const node = new MdzStreamNode(opcode.id, opcode.node_type);
194
- this.#attach(node);
195
- break;
196
- }
197
-
198
- case 'revert': {
199
- if (DEV) {
200
- if (!this.#nodes.has(opcode.id)) {
201
- throw new Error(`MdzStreamState: revert for unknown id ${opcode.id}`);
202
- }
203
- if (!this.#stack.includes(opcode.id)) {
204
- throw new Error(`MdzStreamState: revert for id ${opcode.id} which is not on the stack`);
205
- }
206
- }
207
- const node = this.#nodes.get(opcode.id);
208
- if (!node) break;
209
-
210
- const parent_id = this.#parents.get(opcode.id);
211
- const parent_children =
212
- parent_id !== null && parent_id !== undefined
213
- ? this.#nodes.get(parent_id)?.children
214
- : this.root;
215
-
216
- if (!parent_children) {
217
- if (DEV) {
218
- throw new Error(
219
- `MdzStreamState: revert for id ${opcode.id} but parent has no children array`,
220
- );
221
- }
222
- break;
223
- }
224
-
225
- // find the node in parent's children
226
- let node_idx = -1;
227
- for (let i = 0; i < parent_children.length; i++) {
228
- if (parent_children[i]!.id === opcode.id) {
229
- node_idx = i;
230
- break;
231
- }
232
- }
233
- if (node_idx === -1) {
234
- if (DEV) {
235
- throw new Error(
236
- `MdzStreamState: revert for id ${opcode.id} but node not found in parent's children`,
237
- );
238
- }
239
- break;
240
- }
241
-
242
- // collect replacement content: literal delimiter text + re-parented children
243
- const replacement_nodes: Array<MdzStreamNode> = [];
244
- if (opcode.replacement_text) {
245
- const text_node = new MdzStreamNode(opcode.id, 'Text');
246
- text_node.content = opcode.replacement_text;
247
- replacement_nodes.push(text_node);
248
- }
249
- for (const child of node.children) {
250
- replacement_nodes.push(child);
251
- }
252
-
253
- if (opcode.wrap_node_type != null && opcode.wrap_id != null) {
254
- if (DEV && this.#nodes.has(opcode.wrap_id)) {
255
- throw new Error(
256
- `MdzStreamState: revert wrap_id ${opcode.wrap_id} already exists in nodes`,
257
- );
258
- }
259
- // block-level revert: wrap content in a new container and push onto stack
260
- const wrapper = new MdzStreamNode(opcode.wrap_id, opcode.wrap_node_type);
261
- for (const rn of replacement_nodes) {
262
- wrapper.children.push(rn);
263
- }
264
- for (const child of node.children) {
265
- this.#parents.set(child.id, opcode.wrap_id);
266
- }
267
- this.#nodes.set(opcode.wrap_id, wrapper);
268
- this.#parents.set(opcode.wrap_id, parent_id ?? null);
269
- parent_children.splice(node_idx, 1, wrapper);
270
- this.#stack.push(opcode.wrap_id);
271
- } else {
272
- // inline revert: re-parent directly to grandparent
273
- for (const child of node.children) {
274
- this.#parents.set(child.id, parent_id ?? null);
275
- }
276
- parent_children.splice(node_idx, 1, ...replacement_nodes);
277
- }
278
-
279
- // remove reverted node from stack
280
- const stack_idx = this.#stack.lastIndexOf(opcode.id);
281
- if (stack_idx !== -1) this.#stack.splice(stack_idx, 1);
282
-
283
- // cleanup
284
- this.#nodes.delete(opcode.id);
285
- this.#parents.delete(opcode.id);
286
- break;
287
- }
288
-
289
- case 'wrap': {
290
- if (DEV) {
291
- if (!this.#nodes.has(opcode.target_id)) {
292
- throw new Error(`MdzStreamState: wrap target_id ${opcode.target_id} not in nodes`);
293
- }
294
- if (this.#nodes.has(opcode.id)) {
295
- throw new Error(`MdzStreamState: wrap id ${opcode.id} already exists in nodes`);
296
- }
297
- }
298
- const target = this.#nodes.get(opcode.target_id);
299
- if (!target) break;
300
-
301
- const parent_id = this.#parents.get(opcode.target_id);
302
- const parent_children =
303
- parent_id !== null && parent_id !== undefined
304
- ? this.#nodes.get(parent_id)?.children
305
- : this.root;
306
-
307
- if (!parent_children) break;
308
-
309
- // find target in parent's children
310
- let target_idx = -1;
311
- for (let i = 0; i < parent_children.length; i++) {
312
- if (parent_children[i]!.id === opcode.target_id) {
313
- target_idx = i;
314
- break;
315
- }
316
- }
317
- if (target_idx === -1) break;
318
-
319
- // handle trailing punctuation trim
320
- let trimmed_node: MdzStreamNode | null = null;
321
- if (opcode.trim_end && opcode.trim_end > 0 && opcode.trim_id != null) {
322
- const trimmed_content = target.content.slice(target.content.length - opcode.trim_end);
323
- target.content = target.content.slice(0, target.content.length - opcode.trim_end);
324
- trimmed_node = new MdzStreamNode(opcode.trim_id, 'Text');
325
- trimmed_node.content = trimmed_content;
326
- this.#nodes.set(opcode.trim_id, trimmed_node);
327
- this.#parents.set(opcode.trim_id, parent_id ?? null);
328
- }
329
-
330
- // create Link wrapper containing target
331
- const link = new MdzStreamNode(opcode.id, opcode.node_type);
332
- link.reference = opcode.reference;
333
- link.link_type = opcode.link_type;
334
- link.children.push(target);
335
- this.#nodes.set(opcode.id, link);
336
- this.#parents.set(opcode.id, parent_id ?? null);
337
- this.#parents.set(opcode.target_id, opcode.id);
338
-
339
- // replace target with [Link, trimmed?] in parent's children
340
- if (trimmed_node) {
341
- parent_children.splice(target_idx, 1, link, trimmed_node);
342
- } else {
343
- parent_children[target_idx] = link;
344
- }
345
-
346
- // cleanup: Link is complete (no close opcode coming).
347
- // trim_id (if any) stays in maps — cleaned when parent closes.
348
- this.#cleanup_node(opcode.id);
349
- break;
350
- }
351
- }
352
- }
353
-
354
- /**
355
- * Remove a node and its descendants from the lookup maps. Iterative to avoid
356
- * stack overflow on deep trees.
357
- *
358
- * Parser invariant: once a container closes, no future opcode targets that
359
- * subtree (no `append_text` to a closed Text, no nested `wrap`/`revert`).
360
- * Children stay reachable via `parent.children` arrays — only the lookup
361
- * maps are pruned. If this invariant is violated, the misbehaving opcode
362
- * triggers a DEV throw in `apply()` and silently no-ops in production.
363
- */
364
- #cleanup_node(id: MdzNodeId): void {
365
- const queue: Array<MdzNodeId> = [id];
366
- while (queue.length > 0) {
367
- const current_id = queue.pop()!;
368
- const node = this.#nodes.get(current_id);
369
- if (node) {
370
- for (const child of node.children) {
371
- queue.push(child.id);
372
- }
373
- }
374
- this.#nodes.delete(current_id);
375
- this.#parents.delete(current_id);
376
- }
377
- }
378
-
379
- /**
380
- * Apply a batch of opcodes.
381
- */
382
- apply_batch(opcodes: Array<MdzOpcode>): void {
383
- for (const op of opcodes) {
384
- this.apply(op);
385
- }
386
- }
387
- }
@@ -1,141 +0,0 @@
1
- /**
2
- * Converts parsed `MdzNode` arrays to Svelte markup strings.
3
- *
4
- * Used by the `svelte_preprocess_mdz` preprocessor to expand static `<Mdz content="...">` usages
5
- * into pre-rendered Svelte markup at build time. The output for each node type matches what
6
- * `MdzNodeView.svelte` renders at runtime, so precompiled and runtime rendering are identical.
7
- *
8
- * @module
9
- */
10
-
11
- import {UnreachableError} from '@fuzdev/fuz_util/error.js';
12
- import {escape_svelte_text} from '@fuzdev/fuz_util/svelte_preprocess_helpers.js';
13
- import {escape_js_string} from '@fuzdev/fuz_util/string.js';
14
-
15
- import type {MdzNode} from './mdz.js';
16
- import {resolve_relative_path} from './mdz_helpers.js';
17
-
18
- /**
19
- * Result of converting `MdzNode` arrays to Svelte markup.
20
- */
21
- export interface MdzToSvelteResult {
22
- /** Generated Svelte markup string. */
23
- markup: string;
24
-
25
- /** Required imports: `Map<local_name, {path, kind}>`. */
26
- imports: Map<string, {path: string; kind: 'default' | 'named'}>;
27
-
28
- /** Whether content references unconfigured Component or Element tags. */
29
- has_unconfigured_tags: boolean;
30
- }
31
-
32
- /**
33
- * Converts an array of `MdzNode` to a Svelte markup string.
34
- *
35
- * Each node type produces output matching what `MdzNodeView.svelte` renders at runtime.
36
- * Collects required imports and flags unconfigured component/element references.
37
- *
38
- * @param nodes - parsed mdz nodes to render
39
- * @param components - Component name to import path mapping (e.g., `{Alert: '$lib/Alert.svelte'}`)
40
- * If content references a component not in this map, `has_unconfigured_tags` is set.
41
- * @param elements - Allowed HTML element names (e.g., `new Set(['aside', 'details'])`)
42
- * If content references an element not in this set, `has_unconfigured_tags` is set.
43
- * @param base - Base path for resolving relative links (e.g., `'/docs/mdz/'`)
44
- * When provided, relative references (`./`, `../`) are resolved to absolute paths
45
- * and passed through `resolve()`. Trailing slash recommended.
46
- */
47
- export const mdz_to_svelte = (
48
- nodes: Array<MdzNode>,
49
- components: Record<string, string>,
50
- elements: ReadonlySet<string>,
51
- base?: string,
52
- ): MdzToSvelteResult => {
53
- const imports: Map<string, {path: string; kind: 'default' | 'named'}> = new Map();
54
- let has_unconfigured_tags = false;
55
-
56
- const render_nodes = (children: Array<MdzNode>): string => {
57
- return children.map((child) => render_node(child)).join('');
58
- };
59
-
60
- const render_node = (node: MdzNode): string => {
61
- switch (node.type) {
62
- case 'Text':
63
- return escape_svelte_text(node.content);
64
-
65
- case 'Code':
66
- imports.set('DocsLink', {path: '@fuzdev/fuz_ui/DocsLink.svelte', kind: 'default'});
67
- return `<DocsLink reference={'${escape_js_string(node.content)}'} />`;
68
-
69
- case 'Codeblock': {
70
- imports.set('Code', {path: '@fuzdev/fuz_code/Code.svelte', kind: 'default'});
71
- const lang_attr =
72
- node.lang === null ? 'lang={null}' : `lang={'${escape_js_string(node.lang)}'}`;
73
- return `<Code ${lang_attr} content={'${escape_js_string(node.content)}'} />`;
74
- }
75
-
76
- case 'Bold':
77
- return `<strong>${render_nodes(node.children)}</strong>`;
78
-
79
- case 'Italic':
80
- return `<em>${render_nodes(node.children)}</em>`;
81
-
82
- case 'Strikethrough':
83
- return `<s>${render_nodes(node.children)}</s>`;
84
-
85
- case 'Link': {
86
- const children_markup = render_nodes(node.children);
87
- if (node.link_type === 'internal') {
88
- if (node.reference.startsWith('.') && base) {
89
- const resolved = resolve_relative_path(node.reference, base);
90
- imports.set('resolve', {path: '$app/paths', kind: 'named'});
91
- return `<a href={resolve('${escape_js_string(resolved)}')}>${children_markup}</a>`;
92
- }
93
- if (
94
- node.reference.startsWith('#') ||
95
- node.reference.startsWith('?') ||
96
- node.reference.startsWith('.')
97
- ) {
98
- return `<a href={'${escape_js_string(node.reference)}'}>${children_markup}</a>`;
99
- }
100
- imports.set('resolve', {path: '$app/paths', kind: 'named'});
101
- return `<a href={resolve('${escape_js_string(node.reference)}')}>${children_markup}</a>`;
102
- }
103
- // External link — matches MdzNodeView: target="_blank" rel="noopener"
104
- return `<a href={'${escape_js_string(node.reference)}'} target="_blank" rel="noopener">${children_markup}</a>`;
105
- }
106
-
107
- case 'Paragraph':
108
- return `<p>${render_nodes(node.children)}</p>`;
109
-
110
- case 'Hr':
111
- return '<hr />';
112
-
113
- case 'Heading':
114
- return `<h${node.level}>${render_nodes(node.children)}</h${node.level}>`;
115
-
116
- case 'Element': {
117
- if (!elements.has(node.name)) {
118
- has_unconfigured_tags = true;
119
- return '';
120
- }
121
- return `<${node.name}>${render_nodes(node.children)}</${node.name}>`;
122
- }
123
-
124
- case 'Component': {
125
- const import_path = components[node.name];
126
- if (!import_path) {
127
- has_unconfigured_tags = true;
128
- return '';
129
- }
130
- imports.set(node.name, {path: import_path, kind: 'default'});
131
- return `<${node.name}>${render_nodes(node.children)}</${node.name}>`;
132
- }
133
-
134
- default:
135
- throw new UnreachableError(node);
136
- }
137
- };
138
-
139
- const markup = render_nodes(nodes);
140
- return {markup, imports, has_unconfigured_tags};
141
- };