@fuzdev/mdz 0.3.0 → 0.5.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/LICENSE +10 -17
- package/README.md +6 -3
- package/dist/Mdz.svelte +8 -7
- package/dist/Mdz.svelte.d.ts.map +1 -1
- package/dist/MdzNodeView.svelte +74 -30
- package/dist/MdzNodeView.svelte.d.ts +1 -2
- package/dist/MdzNodeView.svelte.d.ts.map +1 -1
- package/dist/MdzPrecompiled.svelte +3 -3
- package/dist/MdzPrecompiled.svelte.d.ts.map +1 -1
- package/dist/MdzRoot.svelte +3 -3
- package/dist/MdzRoot.svelte.d.ts.map +1 -1
- package/dist/MdzStream.svelte +3 -3
- package/dist/MdzStream.svelte.d.ts.map +1 -1
- package/dist/MdzStreamNodeView.svelte +79 -31
- package/dist/MdzStreamNodeView.svelte.d.ts +1 -2
- package/dist/MdzStreamNodeView.svelte.d.ts.map +1 -1
- package/dist/context_helpers.js +1 -1
- package/dist/mdz.d.ts +12 -1
- package/dist/mdz.d.ts.map +1 -1
- package/dist/mdz.js +1 -1
- package/dist/mdz_contexts.d.ts +33 -6
- package/dist/mdz_contexts.d.ts.map +1 -1
- package/dist/mdz_debug_work.d.ts.map +1 -1
- package/dist/mdz_helpers.d.ts +31 -3
- package/dist/mdz_helpers.d.ts.map +1 -1
- package/dist/mdz_helpers.js +104 -12
- package/dist/mdz_lexer.d.ts +3 -1
- package/dist/mdz_lexer.d.ts.map +1 -1
- package/dist/mdz_lexer.js +115 -31
- package/dist/mdz_opcodes.d.ts +3 -1
- package/dist/mdz_opcodes.d.ts.map +1 -1
- package/dist/mdz_opcodes_to_nodes.d.ts.map +1 -1
- package/dist/mdz_opcodes_to_nodes.js +25 -17
- package/dist/mdz_stream_parser.d.ts.map +1 -1
- package/dist/mdz_stream_parser.js +8 -8
- package/dist/mdz_stream_parser_block.js +5 -5
- package/dist/mdz_stream_parser_inline.js +4 -4
- package/dist/mdz_stream_parser_link.d.ts.map +1 -1
- package/dist/mdz_stream_parser_link.js +83 -11
- package/dist/mdz_stream_parser_list.d.ts.map +1 -1
- package/dist/mdz_stream_parser_list.js +13 -7
- package/dist/mdz_stream_parser_state.d.ts.map +1 -1
- package/dist/mdz_stream_parser_state.js +7 -7
- package/dist/mdz_stream_parser_table.d.ts.map +1 -1
- package/dist/mdz_stream_parser_table.js +16 -9
- package/dist/mdz_stream_parser_text.js +2 -2
- package/dist/mdz_stream_parser_url.js +7 -7
- package/dist/mdz_stream_state.svelte.d.ts +3 -1
- package/dist/mdz_stream_state.svelte.d.ts.map +1 -1
- package/dist/mdz_stream_state.svelte.js +4 -0
- package/dist/mdz_to_svelte.d.ts +17 -4
- package/dist/mdz_to_svelte.d.ts.map +1 -1
- package/dist/mdz_to_svelte.js +22 -9
- package/dist/mdz_token_parser.d.ts.map +1 -1
- package/dist/mdz_token_parser.js +25 -11
- package/dist/svelte_preprocess_mdz.d.ts +6 -3
- package/dist/svelte_preprocess_mdz.d.ts.map +1 -1
- package/dist/svelte_preprocess_mdz.js +11 -11
- package/package.json +25 -40
- package/src/lib/context_helpers.ts +2 -2
- package/src/lib/mdz.ts +15 -3
- package/src/lib/mdz_contexts.ts +41 -11
- package/src/lib/mdz_debug_work.ts +1 -1
- package/src/lib/mdz_helpers.ts +164 -43
- package/src/lib/mdz_lexer.ts +173 -80
- package/src/lib/mdz_opcodes.ts +3 -1
- package/src/lib/mdz_opcodes_to_nodes.ts +40 -30
- package/src/lib/mdz_stream_parser.ts +17 -17
- package/src/lib/mdz_stream_parser_block.ts +14 -14
- package/src/lib/mdz_stream_parser_inline.ts +18 -18
- package/src/lib/mdz_stream_parser_link.ts +88 -18
- package/src/lib/mdz_stream_parser_list.ts +31 -25
- package/src/lib/mdz_stream_parser_state.ts +27 -27
- package/src/lib/mdz_stream_parser_table.ts +38 -31
- package/src/lib/mdz_stream_parser_text.ts +3 -3
- package/src/lib/mdz_stream_parser_url.ts +11 -11
- package/src/lib/mdz_stream_state.svelte.ts +11 -8
- package/src/lib/mdz_to_svelte.ts +71 -27
- package/src/lib/mdz_token_parser.ts +52 -38
- package/src/lib/svelte_preprocess_mdz.ts +66 -60
- package/src/lib/tsdoc_mdz.ts +1 -1
|
@@ -22,14 +22,14 @@ import type {
|
|
|
22
22
|
MdzNodeTableRow,
|
|
23
23
|
MdzNodeTableCell,
|
|
24
24
|
MdzNodeElement,
|
|
25
|
-
MdzNodeComponent
|
|
25
|
+
MdzNodeComponent
|
|
26
26
|
} from './mdz.ts';
|
|
27
27
|
import {
|
|
28
28
|
has_non_whitespace,
|
|
29
29
|
mdz_extract_single_tag,
|
|
30
30
|
mdz_heading_id,
|
|
31
31
|
mdz_merge_adjacent_text,
|
|
32
|
-
mdz_push_merging_text
|
|
32
|
+
mdz_push_merging_text
|
|
33
33
|
} from './mdz_helpers.ts';
|
|
34
34
|
import {
|
|
35
35
|
type MdzToken,
|
|
@@ -43,7 +43,7 @@ import {
|
|
|
43
43
|
type MdzTokenListItemOpen,
|
|
44
44
|
type MdzTokenTableOpen,
|
|
45
45
|
type MdzTokenTableRowOpen,
|
|
46
|
-
type MdzTokenTableCellOpen
|
|
46
|
+
type MdzTokenTableCellOpen
|
|
47
47
|
} from './mdz_lexer.ts';
|
|
48
48
|
|
|
49
49
|
/**
|
|
@@ -134,7 +134,7 @@ export class MdzTokenParser {
|
|
|
134
134
|
if (token.type === 'hr') {
|
|
135
135
|
const flushed = this.#flush_paragraph(paragraph_children, true);
|
|
136
136
|
if (flushed) nodes.push(flushed);
|
|
137
|
-
nodes.push({type: 'Hr', start: token.start, end: token.end});
|
|
137
|
+
nodes.push({ type: 'Hr', start: token.start, end: token.end });
|
|
138
138
|
this.#index++;
|
|
139
139
|
continue;
|
|
140
140
|
}
|
|
@@ -147,7 +147,7 @@ export class MdzTokenParser {
|
|
|
147
147
|
lang: token.lang,
|
|
148
148
|
content: token.content,
|
|
149
149
|
start: token.start,
|
|
150
|
-
end: token.end
|
|
150
|
+
end: token.end
|
|
151
151
|
});
|
|
152
152
|
this.#index++;
|
|
153
153
|
continue;
|
|
@@ -200,7 +200,7 @@ export class MdzTokenParser {
|
|
|
200
200
|
// children-derived end, like List — no-empty-start means children exist,
|
|
201
201
|
// with the open's position as a defensive fallback
|
|
202
202
|
const end = children.length > 0 ? children[children.length - 1]!.end : open.end;
|
|
203
|
-
return {type: 'Blockquote', children, start: open.start, end};
|
|
203
|
+
return { type: 'Blockquote', children, start: open.start, end };
|
|
204
204
|
}
|
|
205
205
|
|
|
206
206
|
#parse_table(): MdzNodeTable {
|
|
@@ -223,7 +223,7 @@ export class MdzTokenParser {
|
|
|
223
223
|
}
|
|
224
224
|
|
|
225
225
|
const end = rows.length > 0 ? rows[rows.length - 1]!.end : open.end;
|
|
226
|
-
return {type: 'Table', align: open.align, children: rows, start: open.start, end};
|
|
226
|
+
return { type: 'Table', align: open.align, children: rows, start: open.start, end };
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
#parse_table_row(): MdzNodeTableRow {
|
|
@@ -241,13 +241,13 @@ export class MdzTokenParser {
|
|
|
241
241
|
}
|
|
242
242
|
if (t.type === 'table_row_close') {
|
|
243
243
|
this.#index++;
|
|
244
|
-
return {type: 'TableRow', header, children: cells, start: open.start, end: t.end};
|
|
244
|
+
return { type: 'TableRow', header, children: cells, start: open.start, end: t.end };
|
|
245
245
|
}
|
|
246
246
|
this.#index++; // skip anything unexpected (defensive)
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
const end = cells.length > 0 ? cells[cells.length - 1]!.end : open.end;
|
|
250
|
-
return {type: 'TableRow', header, children: cells, start: open.start, end};
|
|
250
|
+
return { type: 'TableRow', header, children: cells, start: open.start, end };
|
|
251
251
|
}
|
|
252
252
|
|
|
253
253
|
#parse_table_cell(): MdzNodeTableCell {
|
|
@@ -261,14 +261,14 @@ export class MdzTokenParser {
|
|
|
261
261
|
const t = this.#tokens[this.#index]!;
|
|
262
262
|
if (t.type === 'table_cell_close') {
|
|
263
263
|
this.#index++;
|
|
264
|
-
return {type: 'TableCell', children, start, end: t.end};
|
|
264
|
+
return { type: 'TableCell', children, start, end: t.end };
|
|
265
265
|
}
|
|
266
266
|
const node = this.#parse_inline();
|
|
267
267
|
if (node) mdz_push_merging_text(children, node);
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
const end = children.length > 0 ? children[children.length - 1]!.end : open.end;
|
|
271
|
-
return {type: 'TableCell', children, start, end};
|
|
271
|
+
return { type: 'TableCell', children, start, end };
|
|
272
272
|
}
|
|
273
273
|
|
|
274
274
|
#parse_heading(): MdzNodeHeading {
|
|
@@ -293,7 +293,7 @@ export class MdzTokenParser {
|
|
|
293
293
|
const merged = mdz_merge_adjacent_text(children);
|
|
294
294
|
const end = merged.length > 0 ? merged[merged.length - 1]!.end : start + level + 1;
|
|
295
295
|
|
|
296
|
-
return {type: 'Heading', level, id: mdz_heading_id(merged), children: merged, start, end};
|
|
296
|
+
return { type: 'Heading', level, id: mdz_heading_id(merged), children: merged, start, end };
|
|
297
297
|
}
|
|
298
298
|
|
|
299
299
|
#parse_list(): MdzNodeList {
|
|
@@ -323,10 +323,10 @@ export class MdzTokenParser {
|
|
|
323
323
|
start_number: children[0]?.number ?? 1,
|
|
324
324
|
children,
|
|
325
325
|
start: open.start,
|
|
326
|
-
end
|
|
326
|
+
end
|
|
327
327
|
};
|
|
328
328
|
}
|
|
329
|
-
return {type: 'List', ordered: false, children, start: open.start, end};
|
|
329
|
+
return { type: 'List', ordered: false, children, start: open.start, end };
|
|
330
330
|
}
|
|
331
331
|
|
|
332
332
|
#parse_list_item(): MdzNodeListItem {
|
|
@@ -376,7 +376,7 @@ export class MdzTokenParser {
|
|
|
376
376
|
lang: t.lang,
|
|
377
377
|
content: t.content,
|
|
378
378
|
start: t.start,
|
|
379
|
-
end: t.end
|
|
379
|
+
end: t.end
|
|
380
380
|
});
|
|
381
381
|
this.#index++;
|
|
382
382
|
continue;
|
|
@@ -400,9 +400,9 @@ export class MdzTokenParser {
|
|
|
400
400
|
|
|
401
401
|
const end = children.length > 0 ? children[children.length - 1]!.end : open.end;
|
|
402
402
|
if (open.number !== undefined) {
|
|
403
|
-
return {type: 'ListItem', number: open.number, children, start: open.start, end};
|
|
403
|
+
return { type: 'ListItem', number: open.number, children, start: open.start, end };
|
|
404
404
|
}
|
|
405
|
-
return {type: 'ListItem', children, start: open.start, end};
|
|
405
|
+
return { type: 'ListItem', children, start: open.start, end };
|
|
406
406
|
}
|
|
407
407
|
|
|
408
408
|
#parse_inline(): MdzNode | null {
|
|
@@ -413,11 +413,11 @@ export class MdzTokenParser {
|
|
|
413
413
|
switch (token.type) {
|
|
414
414
|
case 'text':
|
|
415
415
|
this.#index++;
|
|
416
|
-
return {type: 'Text', content: token.content, start: token.start, end: token.end};
|
|
416
|
+
return { type: 'Text', content: token.content, start: token.start, end: token.end };
|
|
417
417
|
|
|
418
418
|
case 'code':
|
|
419
419
|
this.#index++;
|
|
420
|
-
return {type: 'Code', content: token.content, start: token.start, end: token.end};
|
|
420
|
+
return { type: 'Code', content: token.content, start: token.start, end: token.end };
|
|
421
421
|
|
|
422
422
|
case 'bold_open':
|
|
423
423
|
return this.#parse_delimiter_pair('bold_close', 'Bold', '**');
|
|
@@ -443,19 +443,19 @@ export class MdzTokenParser {
|
|
|
443
443
|
// Orphaned close tokens - treat as text
|
|
444
444
|
case 'bold_close':
|
|
445
445
|
this.#index++;
|
|
446
|
-
return {type: 'Text', content: '**', start: token.start, end: token.end};
|
|
446
|
+
return { type: 'Text', content: '**', start: token.start, end: token.end };
|
|
447
447
|
|
|
448
448
|
case 'italic_close':
|
|
449
449
|
this.#index++;
|
|
450
|
-
return {type: 'Text', content: '_', start: token.start, end: token.end};
|
|
450
|
+
return { type: 'Text', content: '_', start: token.start, end: token.end };
|
|
451
451
|
|
|
452
452
|
case 'strikethrough_close':
|
|
453
453
|
this.#index++;
|
|
454
|
-
return {type: 'Text', content: '~~', start: token.start, end: token.end};
|
|
454
|
+
return { type: 'Text', content: '~~', start: token.start, end: token.end };
|
|
455
455
|
|
|
456
456
|
case 'link_text_close':
|
|
457
457
|
this.#index++;
|
|
458
|
-
return {type: 'Text', content: ']', start: token.start, end: token.end};
|
|
458
|
+
return { type: 'Text', content: ']', start: token.start, end: token.end };
|
|
459
459
|
|
|
460
460
|
case 'link_ref':
|
|
461
461
|
this.#index++;
|
|
@@ -463,7 +463,7 @@ export class MdzTokenParser {
|
|
|
463
463
|
type: 'Text',
|
|
464
464
|
content: `(${token.reference})`,
|
|
465
465
|
start: token.start,
|
|
466
|
-
end: token.end
|
|
466
|
+
end: token.end
|
|
467
467
|
};
|
|
468
468
|
|
|
469
469
|
case 'tag_close':
|
|
@@ -472,7 +472,7 @@ export class MdzTokenParser {
|
|
|
472
472
|
type: 'Text',
|
|
473
473
|
content: `</${token.name}>`,
|
|
474
474
|
start: token.start,
|
|
475
|
-
end: token.end
|
|
475
|
+
end: token.end
|
|
476
476
|
};
|
|
477
477
|
|
|
478
478
|
default:
|
|
@@ -492,7 +492,7 @@ export class MdzTokenParser {
|
|
|
492
492
|
#parse_delimiter_pair(
|
|
493
493
|
close_type: 'bold_close' | 'italic_close' | 'strikethrough_close',
|
|
494
494
|
node_type: 'Bold' | 'Italic' | 'Strikethrough',
|
|
495
|
-
delimiter: '**' | '_' | '~~'
|
|
495
|
+
delimiter: '**' | '_' | '~~'
|
|
496
496
|
): MdzNodeBold | MdzNodeItalic | MdzNodeStrikethrough | MdzNodeText {
|
|
497
497
|
const start = this.#tokens[this.#index]!.start;
|
|
498
498
|
this.#index++;
|
|
@@ -503,7 +503,7 @@ export class MdzTokenParser {
|
|
|
503
503
|
const t = this.#tokens[this.#index]!;
|
|
504
504
|
if (t.type === close_type) {
|
|
505
505
|
this.#index++;
|
|
506
|
-
return {type: node_type, children, start, end: t.end};
|
|
506
|
+
return { type: node_type, children, start, end: t.end };
|
|
507
507
|
}
|
|
508
508
|
if (token_is_block_boundary(t.type)) {
|
|
509
509
|
break;
|
|
@@ -513,7 +513,7 @@ export class MdzTokenParser {
|
|
|
513
513
|
}
|
|
514
514
|
|
|
515
515
|
// Unclosed - treat as text
|
|
516
|
-
return {type: 'Text', content: delimiter, start, end: start + delimiter.length};
|
|
516
|
+
return { type: 'Text', content: delimiter, start, end: start + delimiter.length };
|
|
517
517
|
}
|
|
518
518
|
|
|
519
519
|
#parse_link(): MdzNodeLink | MdzNodeText {
|
|
@@ -538,12 +538,12 @@ export class MdzTokenParser {
|
|
|
538
538
|
children,
|
|
539
539
|
link_type: ref_token.link_type,
|
|
540
540
|
start,
|
|
541
|
-
end: ref_token.end
|
|
541
|
+
end: ref_token.end
|
|
542
542
|
};
|
|
543
543
|
}
|
|
544
544
|
|
|
545
545
|
// No link_ref - treat as text
|
|
546
|
-
return {type: 'Text', content: '[', start, end: start + 1};
|
|
546
|
+
return { type: 'Text', content: '[', start, end: start + 1 };
|
|
547
547
|
}
|
|
548
548
|
if (token_is_block_boundary(t.type)) {
|
|
549
549
|
break;
|
|
@@ -552,7 +552,7 @@ export class MdzTokenParser {
|
|
|
552
552
|
if (node) mdz_push_merging_text(children, node);
|
|
553
553
|
}
|
|
554
554
|
|
|
555
|
-
return {type: 'Text', content: '[', start, end: start + 1};
|
|
555
|
+
return { type: 'Text', content: '[', start, end: start + 1 };
|
|
556
556
|
}
|
|
557
557
|
|
|
558
558
|
#parse_autolink(): MdzNodeLink {
|
|
@@ -561,10 +561,10 @@ export class MdzTokenParser {
|
|
|
561
561
|
return {
|
|
562
562
|
type: 'Link',
|
|
563
563
|
reference: token.reference,
|
|
564
|
-
children: [{type: 'Text', content: token.reference, start: token.start, end: token.end}],
|
|
564
|
+
children: [{ type: 'Text', content: token.reference, start: token.start, end: token.end }],
|
|
565
565
|
link_type: token.link_type,
|
|
566
566
|
start: token.start,
|
|
567
|
-
end: token.end
|
|
567
|
+
end: token.end
|
|
568
568
|
};
|
|
569
569
|
}
|
|
570
570
|
|
|
@@ -581,21 +581,35 @@ export class MdzTokenParser {
|
|
|
581
581
|
const t = this.#tokens[this.#index]!;
|
|
582
582
|
if (t.type === 'tag_close' && t.name === tag_name) {
|
|
583
583
|
this.#index++;
|
|
584
|
-
return {
|
|
584
|
+
return {
|
|
585
|
+
type: node_type,
|
|
586
|
+
name: tag_name,
|
|
587
|
+
attributes: open_token.attributes,
|
|
588
|
+
children,
|
|
589
|
+
start,
|
|
590
|
+
end: t.end
|
|
591
|
+
};
|
|
585
592
|
}
|
|
586
593
|
const node = this.#parse_inline();
|
|
587
594
|
if (node) mdz_push_merging_text(children, node);
|
|
588
595
|
}
|
|
589
596
|
|
|
590
597
|
// Unclosed tag
|
|
591
|
-
return {type: 'Text', content: '<', start, end: start + 1};
|
|
598
|
+
return { type: 'Text', content: '<', start, end: start + 1 };
|
|
592
599
|
}
|
|
593
600
|
|
|
594
601
|
#parse_self_close_tag(): MdzNodeElement | MdzNodeComponent {
|
|
595
602
|
const token = this.#tokens[this.#index]! as MdzTokenTagSelfClose;
|
|
596
603
|
const node_type: 'Component' | 'Element' = token.is_component ? 'Component' : 'Element';
|
|
597
604
|
this.#index++;
|
|
598
|
-
return {
|
|
605
|
+
return {
|
|
606
|
+
type: node_type,
|
|
607
|
+
name: token.name,
|
|
608
|
+
attributes: token.attributes,
|
|
609
|
+
children: [],
|
|
610
|
+
start: token.start,
|
|
611
|
+
end: token.end
|
|
612
|
+
};
|
|
599
613
|
}
|
|
600
614
|
|
|
601
615
|
// -- Paragraph flushing --
|
|
@@ -627,7 +641,7 @@ export class MdzTokenParser {
|
|
|
627
641
|
// `has_non_whitespace_content` (Unicode whitespace like NBSP is
|
|
628
642
|
// content; `.trim()` here would silently drop it)
|
|
629
643
|
const has_content = paragraph_children.some(
|
|
630
|
-
(n) => n.type !== 'Text' || has_non_whitespace(n.content)
|
|
644
|
+
(n) => n.type !== 'Text' || has_non_whitespace(n.content)
|
|
631
645
|
);
|
|
632
646
|
if (!has_content) {
|
|
633
647
|
paragraph_children.length = 0;
|
|
@@ -652,7 +666,7 @@ export class MdzTokenParser {
|
|
|
652
666
|
type: 'Paragraph',
|
|
653
667
|
children: merged,
|
|
654
668
|
start: merged[0]!.start,
|
|
655
|
-
end: merged[merged.length - 1]!.end
|
|
669
|
+
end: merged[merged.length - 1]!.end
|
|
656
670
|
};
|
|
657
671
|
}
|
|
658
672
|
}
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
* @module
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import type {ImportDeclaration, VariableDeclaration} from 'estree';
|
|
18
|
-
import {parse, type PreprocessorGroup, type AST} from 'svelte/compiler';
|
|
17
|
+
import type { ImportDeclaration, VariableDeclaration } from 'estree';
|
|
18
|
+
import { parse, type PreprocessorGroup, type AST } from 'svelte/compiler';
|
|
19
19
|
import MagicString from 'magic-string';
|
|
20
|
-
import {walk} from 'zimmerframe';
|
|
21
|
-
import {should_exclude_path} from '@fuzdev/fuz_util/path.ts';
|
|
20
|
+
import { walk } from 'zimmerframe';
|
|
21
|
+
import { should_exclude_path } from '@fuzdev/fuz_util/path.ts';
|
|
22
22
|
import {
|
|
23
23
|
find_attribute,
|
|
24
24
|
extract_static_string,
|
|
@@ -33,11 +33,11 @@ import {
|
|
|
33
33
|
remove_import_specifier,
|
|
34
34
|
handle_preprocess_error,
|
|
35
35
|
type PreprocessImportInfo,
|
|
36
|
-
type ResolvedComponentImport
|
|
36
|
+
type ResolvedComponentImport
|
|
37
37
|
} from '@fuzdev/fuz_util/svelte_preprocess_helpers.ts';
|
|
38
38
|
|
|
39
|
-
import {mdz_parse} from './mdz.ts';
|
|
40
|
-
import {mdz_to_svelte, type MdzToSvelteResult} from './mdz_to_svelte.ts';
|
|
39
|
+
import { mdz_parse } from './mdz.ts';
|
|
40
|
+
import { mdz_to_svelte, type MdzComponentImport, type MdzToSvelteResult } from './mdz_to_svelte.ts';
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
43
|
* An estree `ImportDeclaration` augmented with Svelte's position data.
|
|
@@ -56,12 +56,14 @@ export interface SveltePreprocessMdzOptions {
|
|
|
56
56
|
/**
|
|
57
57
|
* Component import mapping for mdz content.
|
|
58
58
|
* Key: component name as used in mdz (e.g., 'Alert').
|
|
59
|
-
* Value: import path (e.g., '
|
|
59
|
+
* Value: import path (e.g., '#lib/Alert.svelte'), which receives no attributes,
|
|
60
|
+
* or `{path, props}` naming the attributes passed as props
|
|
61
|
+
* (e.g., `{path: '#lib/Alert.svelte', props: ['status']}`, or `props: 'all'`).
|
|
60
62
|
*
|
|
61
63
|
* If mdz content references a component not in this map,
|
|
62
64
|
* that `Mdz` usage is skipped (left as runtime).
|
|
63
65
|
*/
|
|
64
|
-
components?: Record<string,
|
|
66
|
+
components?: Record<string, MdzComponentImport>;
|
|
65
67
|
|
|
66
68
|
/**
|
|
67
69
|
* Allowed HTML elements in mdz content (e.g., `{aside: true, details: true}`).
|
|
@@ -116,10 +118,10 @@ const PRECOMPILED_NAME = 'MdzPrecompiled';
|
|
|
116
118
|
* Creates a Svelte preprocessor that compiles static `Mdz` content at build time.
|
|
117
119
|
*
|
|
118
120
|
* @param options - configuration for component/element resolution and file filtering
|
|
119
|
-
* @returns a Svelte `PreprocessorGroup` for
|
|
121
|
+
* @returns a Svelte `PreprocessorGroup` for a `preprocess` option, like `sveltekit({preprocess})` in `vite.config.ts`
|
|
120
122
|
*/
|
|
121
123
|
export const svelte_preprocess_mdz = (
|
|
122
|
-
options: SveltePreprocessMdzOptions = {}
|
|
124
|
+
options: SveltePreprocessMdzOptions = {}
|
|
123
125
|
): PreprocessorGroup => {
|
|
124
126
|
const {
|
|
125
127
|
exclude = [],
|
|
@@ -129,52 +131,56 @@ export const svelte_preprocess_mdz = (
|
|
|
129
131
|
compiled_component_import = '@fuzdev/mdz/MdzPrecompiled.svelte',
|
|
130
132
|
code_component_import,
|
|
131
133
|
codeblock_component_import,
|
|
132
|
-
on_error = process.env.CI === 'true' ? 'throw' : 'log'
|
|
134
|
+
on_error = process.env.CI === 'true' ? 'throw' : 'log'
|
|
133
135
|
} = options;
|
|
134
136
|
|
|
135
137
|
return {
|
|
136
138
|
name: 'fuz-mdz',
|
|
137
139
|
|
|
138
|
-
markup: ({content, filename}) => {
|
|
140
|
+
markup: ({ content, filename }) => {
|
|
139
141
|
if (should_exclude_path(filename, exclude)) {
|
|
140
|
-
return {code: content};
|
|
142
|
+
return { code: content };
|
|
141
143
|
}
|
|
142
144
|
|
|
143
145
|
// Quick bail: does file mention any known Mdz import source?
|
|
144
146
|
if (!mdz_component_imports.some((source) => content.includes(source))) {
|
|
145
|
-
return {code: content};
|
|
147
|
+
return { code: content };
|
|
146
148
|
}
|
|
147
149
|
|
|
148
|
-
const ast = parse(content, {filename, modern: true});
|
|
150
|
+
const ast = parse(content, { filename, modern: true });
|
|
149
151
|
|
|
150
152
|
// Resolve which local names map to the Mdz component
|
|
151
153
|
const mdz_names = resolve_component_names(ast, mdz_component_imports);
|
|
152
154
|
if (mdz_names.size === 0) {
|
|
153
|
-
return {code: content};
|
|
155
|
+
return { code: content };
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
// Check for MdzPrecompiled name collision
|
|
157
159
|
if (has_name_collision(ast, compiled_component_import)) {
|
|
158
|
-
return {code: content};
|
|
160
|
+
return { code: content };
|
|
159
161
|
}
|
|
160
162
|
|
|
161
163
|
const s = new MagicString(content);
|
|
162
164
|
const bindings = build_static_bindings(ast);
|
|
163
165
|
|
|
164
166
|
// Find and transform Mdz usages with static content
|
|
165
|
-
const {transformations, total_usages, transformed_usages} = find_mdz_usages(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
167
|
+
const { transformations, total_usages, transformed_usages } = find_mdz_usages(
|
|
168
|
+
ast,
|
|
169
|
+
mdz_names,
|
|
170
|
+
{
|
|
171
|
+
components,
|
|
172
|
+
elements,
|
|
173
|
+
filename,
|
|
174
|
+
source: content,
|
|
175
|
+
bindings,
|
|
176
|
+
on_error,
|
|
177
|
+
code_component_import,
|
|
178
|
+
codeblock_component_import
|
|
179
|
+
}
|
|
180
|
+
);
|
|
175
181
|
|
|
176
182
|
if (transformations.length === 0) {
|
|
177
|
-
return {code: content};
|
|
183
|
+
return { code: content };
|
|
178
184
|
}
|
|
179
185
|
|
|
180
186
|
// Apply transformations
|
|
@@ -190,7 +196,7 @@ export const svelte_preprocess_mdz = (
|
|
|
190
196
|
ast,
|
|
191
197
|
mdz_names,
|
|
192
198
|
total_usages,
|
|
193
|
-
transformed_usages
|
|
199
|
+
transformed_usages
|
|
194
200
|
);
|
|
195
201
|
|
|
196
202
|
// Add required imports and remove unused Mdz imports
|
|
@@ -200,14 +206,14 @@ export const svelte_preprocess_mdz = (
|
|
|
200
206
|
transformations,
|
|
201
207
|
removable_imports,
|
|
202
208
|
compiled_component_import,
|
|
203
|
-
content
|
|
209
|
+
content
|
|
204
210
|
);
|
|
205
211
|
|
|
206
212
|
return {
|
|
207
213
|
code: s.toString(),
|
|
208
|
-
map: s.generateMap({hires: true})
|
|
214
|
+
map: s.generateMap({ hires: true })
|
|
209
215
|
};
|
|
210
|
-
}
|
|
216
|
+
}
|
|
211
217
|
};
|
|
212
218
|
};
|
|
213
219
|
|
|
@@ -231,7 +237,7 @@ interface FindMdzUsagesResult {
|
|
|
231
237
|
}
|
|
232
238
|
|
|
233
239
|
interface FindMdzUsagesContext {
|
|
234
|
-
components: Record<string,
|
|
240
|
+
components: Record<string, MdzComponentImport>;
|
|
235
241
|
elements: Record<string, boolean>;
|
|
236
242
|
filename: string | undefined;
|
|
237
243
|
source: string;
|
|
@@ -273,11 +279,11 @@ const has_name_collision = (ast: AST.Root, compiled_component_import: string): b
|
|
|
273
279
|
*/
|
|
274
280
|
const collect_consumed_bindings = (
|
|
275
281
|
value: AST.Attribute['value'],
|
|
276
|
-
bindings: ReadonlyMap<string, string
|
|
282
|
+
bindings: ReadonlyMap<string, string>
|
|
277
283
|
): Set<string> => {
|
|
278
284
|
const consumed: Set<string> = new Set();
|
|
279
285
|
if (value === true || Array.isArray(value)) return consumed;
|
|
280
|
-
const collect_from_expr = (expr: {type: string; [key: string]: any}): void => {
|
|
286
|
+
const collect_from_expr = (expr: { type: string; [key: string]: any }): void => {
|
|
281
287
|
if (expr.type === 'Identifier' && bindings.has(expr.name)) {
|
|
282
288
|
consumed.add(expr.name);
|
|
283
289
|
} else if (expr.type === 'BinaryExpression' && expr.operator === '+') {
|
|
@@ -305,7 +311,7 @@ const collect_consumed_bindings = (
|
|
|
305
311
|
const render_static_mdz = (
|
|
306
312
|
content: string,
|
|
307
313
|
base: string | undefined,
|
|
308
|
-
context: FindMdzUsagesContext
|
|
314
|
+
context: FindMdzUsagesContext
|
|
309
315
|
): MdzToSvelteResult | null => {
|
|
310
316
|
let result: MdzToSvelteResult;
|
|
311
317
|
try {
|
|
@@ -314,7 +320,7 @@ const render_static_mdz = (
|
|
|
314
320
|
elements: context.elements,
|
|
315
321
|
base,
|
|
316
322
|
code_component_import: context.code_component_import,
|
|
317
|
-
codeblock_component_import: context.codeblock_component_import
|
|
323
|
+
codeblock_component_import: context.codeblock_component_import
|
|
318
324
|
});
|
|
319
325
|
} catch (error) {
|
|
320
326
|
handle_preprocess_error(error, '[fuz-mdz]', context.filename, context.on_error);
|
|
@@ -330,7 +336,7 @@ const render_static_mdz = (
|
|
|
330
336
|
const find_mdz_usages = (
|
|
331
337
|
ast: AST.Root,
|
|
332
338
|
mdz_names: Map<string, ResolvedComponentImport>,
|
|
333
|
-
context: FindMdzUsagesContext
|
|
339
|
+
context: FindMdzUsagesContext
|
|
334
340
|
): FindMdzUsagesResult => {
|
|
335
341
|
const transformations: Array<MdzTransformation> = [];
|
|
336
342
|
const total_usages: Map<string, number> = new Map();
|
|
@@ -339,7 +345,7 @@ const find_mdz_usages = (
|
|
|
339
345
|
// zimmerframe types visitors against {type: string}, requiring explicit annotations
|
|
340
346
|
// on the callback parameters for Svelte-specific AST types like AST.Component
|
|
341
347
|
walk(ast.fragment as any, null, {
|
|
342
|
-
Component(node: AST.Component, ctx: {next: () => void}) {
|
|
348
|
+
Component(node: AST.Component, ctx: { next: () => void }) {
|
|
343
349
|
// Always recurse into children so nested Mdz components are found
|
|
344
350
|
ctx.next();
|
|
345
351
|
|
|
@@ -385,7 +391,7 @@ const find_mdz_usages = (
|
|
|
385
391
|
replacement,
|
|
386
392
|
required_imports: result.imports,
|
|
387
393
|
consumed_bindings: consumed,
|
|
388
|
-
component_node: node
|
|
394
|
+
component_node: node
|
|
389
395
|
});
|
|
390
396
|
return;
|
|
391
397
|
}
|
|
@@ -394,17 +400,17 @@ const find_mdz_usages = (
|
|
|
394
400
|
const chain = try_extract_conditional_chain(
|
|
395
401
|
content_attr.value,
|
|
396
402
|
context.source,
|
|
397
|
-
context.bindings
|
|
403
|
+
context.bindings
|
|
398
404
|
);
|
|
399
405
|
if (chain === null) return;
|
|
400
406
|
|
|
401
407
|
// Parse and render each branch
|
|
402
|
-
const branch_results: Array<{markup: string; imports: Map<string, PreprocessImportInfo>}> =
|
|
408
|
+
const branch_results: Array<{ markup: string; imports: Map<string, PreprocessImportInfo> }> =
|
|
403
409
|
[];
|
|
404
410
|
for (const branch of chain) {
|
|
405
411
|
const result = render_static_mdz(branch.value, base, context);
|
|
406
412
|
if (result === null) return; // parse error or unconfigured tags — leave for runtime
|
|
407
|
-
branch_results.push({markup: result.markup, imports: result.imports});
|
|
413
|
+
branch_results.push({ markup: result.markup, imports: result.imports });
|
|
408
414
|
}
|
|
409
415
|
|
|
410
416
|
// Build {#if}/{:else if}/{:else} markup
|
|
@@ -440,12 +446,12 @@ const find_mdz_usages = (
|
|
|
440
446
|
replacement,
|
|
441
447
|
required_imports: merged_imports,
|
|
442
448
|
consumed_bindings: consumed,
|
|
443
|
-
component_node: node
|
|
449
|
+
component_node: node
|
|
444
450
|
});
|
|
445
|
-
}
|
|
451
|
+
}
|
|
446
452
|
});
|
|
447
453
|
|
|
448
|
-
return {transformations, total_usages, transformed_usages};
|
|
454
|
+
return { transformations, total_usages, transformed_usages };
|
|
449
455
|
};
|
|
450
456
|
|
|
451
457
|
/**
|
|
@@ -459,7 +465,7 @@ const remove_dead_const_bindings = (
|
|
|
459
465
|
s: MagicString,
|
|
460
466
|
ast: AST.Root,
|
|
461
467
|
transformations: Array<MdzTransformation>,
|
|
462
|
-
source: string
|
|
468
|
+
source: string
|
|
463
469
|
): void => {
|
|
464
470
|
// Collect all consumed binding names across transformations
|
|
465
471
|
const all_consumed: Set<string> = new Set();
|
|
@@ -483,12 +489,12 @@ const remove_dead_const_bindings = (
|
|
|
483
489
|
|
|
484
490
|
for (const name of all_consumed) {
|
|
485
491
|
// Find the VariableDeclaration containing this binding in instance script
|
|
486
|
-
let declaration_node: (VariableDeclaration & {start: number; end: number}) | null = null;
|
|
492
|
+
let declaration_node: (VariableDeclaration & { start: number; end: number }) | null = null;
|
|
487
493
|
for (const node of instance.content.body) {
|
|
488
494
|
if (node.type !== 'VariableDeclaration' || node.kind !== 'const') continue;
|
|
489
495
|
for (const decl of node.declarations) {
|
|
490
496
|
if (decl.id.type === 'Identifier' && decl.id.name === name) {
|
|
491
|
-
declaration_node = node as VariableDeclaration & {start: number; end: number};
|
|
497
|
+
declaration_node = node as VariableDeclaration & { start: number; end: number };
|
|
492
498
|
break;
|
|
493
499
|
}
|
|
494
500
|
}
|
|
@@ -529,13 +535,13 @@ const build_replacement = (
|
|
|
529
535
|
node: AST.Component,
|
|
530
536
|
exclude_attrs: ReadonlySet<AST.Attribute>,
|
|
531
537
|
children_markup: string,
|
|
532
|
-
source: string
|
|
538
|
+
source: string
|
|
533
539
|
): string => {
|
|
534
540
|
// Collect source ranges of all attributes except excluded ones (content, base when resolved)
|
|
535
|
-
const other_attr_ranges: Array<{start: number; end: number}> = [];
|
|
541
|
+
const other_attr_ranges: Array<{ start: number; end: number }> = [];
|
|
536
542
|
for (const attr of node.attributes) {
|
|
537
543
|
if (exclude_attrs.has(attr as AST.Attribute)) continue;
|
|
538
|
-
other_attr_ranges.push({start: attr.start, end: attr.end});
|
|
544
|
+
other_attr_ranges.push({ start: attr.start, end: attr.end });
|
|
539
545
|
}
|
|
540
546
|
|
|
541
547
|
// Build opening tag with MdzPrecompiled name
|
|
@@ -572,11 +578,11 @@ const find_removable_mdz_imports = (
|
|
|
572
578
|
ast: AST.Root,
|
|
573
579
|
mdz_names: Map<string, ResolvedComponentImport>,
|
|
574
580
|
total_usages: Map<string, number>,
|
|
575
|
-
transformed_usages: Map<string, number
|
|
581
|
+
transformed_usages: Map<string, number>
|
|
576
582
|
): Map<string, ImportRemovalAction> => {
|
|
577
583
|
const removable: Map<string, ImportRemovalAction> = new Map();
|
|
578
584
|
|
|
579
|
-
for (const [name, {import_node, specifier}] of mdz_names) {
|
|
585
|
+
for (const [name, { import_node, specifier }] of mdz_names) {
|
|
580
586
|
const total = total_usages.get(name) ?? 0;
|
|
581
587
|
const transformed = transformed_usages.get(name) ?? 0;
|
|
582
588
|
|
|
@@ -603,12 +609,12 @@ const find_removable_mdz_imports = (
|
|
|
603
609
|
|
|
604
610
|
const positioned_node = import_node as PositionedImportDeclaration;
|
|
605
611
|
if (import_node.specifiers.length === 1) {
|
|
606
|
-
removable.set(name, {node: positioned_node, kind: 'full'});
|
|
612
|
+
removable.set(name, { node: positioned_node, kind: 'full' });
|
|
607
613
|
} else {
|
|
608
614
|
removable.set(name, {
|
|
609
615
|
node: positioned_node,
|
|
610
616
|
kind: 'partial',
|
|
611
|
-
specifier_to_remove: specifier
|
|
617
|
+
specifier_to_remove: specifier
|
|
612
618
|
});
|
|
613
619
|
}
|
|
614
620
|
}
|
|
@@ -636,7 +642,7 @@ const manage_imports = (
|
|
|
636
642
|
transformations: Array<MdzTransformation>,
|
|
637
643
|
removable_imports: Map<string, ImportRemovalAction>,
|
|
638
644
|
compiled_component_import: string,
|
|
639
|
-
source: string
|
|
645
|
+
source: string
|
|
640
646
|
): void => {
|
|
641
647
|
// Collect all required imports across transformations
|
|
642
648
|
const required: Map<string, PreprocessImportInfo> = new Map();
|
|
@@ -647,7 +653,7 @@ const manage_imports = (
|
|
|
647
653
|
}
|
|
648
654
|
|
|
649
655
|
// Always need MdzPrecompiled when transformations occur
|
|
650
|
-
required.set(PRECOMPILED_NAME, {path: compiled_component_import, kind: 'default'});
|
|
656
|
+
required.set(PRECOMPILED_NAME, { path: compiled_component_import, kind: 'default' });
|
|
651
657
|
|
|
652
658
|
const script = ast.instance;
|
|
653
659
|
|
|
@@ -770,7 +776,7 @@ const manage_imports = (
|
|
|
770
776
|
action.node,
|
|
771
777
|
action.specifier_to_remove!,
|
|
772
778
|
source,
|
|
773
|
-
action === partial_carrier ? carrier_lines : ''
|
|
779
|
+
action === partial_carrier ? carrier_lines : ''
|
|
774
780
|
);
|
|
775
781
|
}
|
|
776
782
|
};
|
package/src/lib/tsdoc_mdz.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import {mdz_is_url} from './mdz_helpers.ts';
|
|
11
|
+
import { mdz_is_url } from './mdz_helpers.ts';
|
|
12
12
|
|
|
13
13
|
/** Format a reference as mdz: URLs pass through, identifiers get backticks. */
|
|
14
14
|
const format_reference = (ref: string): string => (mdz_is_url(ref) ? ref : `\`${ref}\``);
|