@fuzdev/mdz 0.1.0 → 0.2.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 (60) hide show
  1. package/README.md +4 -0
  2. package/dist/MdzNodeView.svelte +30 -1
  3. package/dist/MdzNodeView.svelte.d.ts.map +1 -1
  4. package/dist/MdzStreamNodeView.svelte +31 -0
  5. package/dist/MdzStreamNodeView.svelte.d.ts.map +1 -1
  6. package/dist/context_helpers.d.ts +1 -1
  7. package/dist/mdz.d.ts +53 -2
  8. package/dist/mdz.d.ts.map +1 -1
  9. package/dist/mdz.js +29 -1
  10. package/dist/mdz_helpers.d.ts +35 -1
  11. package/dist/mdz_helpers.d.ts.map +1 -1
  12. package/dist/mdz_helpers.js +120 -0
  13. package/dist/mdz_lexer.d.ts +35 -1
  14. package/dist/mdz_lexer.d.ts.map +1 -1
  15. package/dist/mdz_lexer.js +211 -2
  16. package/dist/mdz_opcodes.d.ts +6 -1
  17. package/dist/mdz_opcodes.d.ts.map +1 -1
  18. package/dist/mdz_opcodes_to_nodes.d.ts.map +1 -1
  19. package/dist/mdz_opcodes_to_nodes.js +26 -0
  20. package/dist/mdz_stream_parser.d.ts.map +1 -1
  21. package/dist/mdz_stream_parser.js +20 -1
  22. package/dist/mdz_stream_parser_list.d.ts.map +1 -1
  23. package/dist/mdz_stream_parser_list.js +22 -1
  24. package/dist/mdz_stream_parser_state.d.ts +12 -0
  25. package/dist/mdz_stream_parser_state.d.ts.map +1 -1
  26. package/dist/mdz_stream_parser_state.js +1 -0
  27. package/dist/mdz_stream_parser_table.d.ts +72 -0
  28. package/dist/mdz_stream_parser_table.d.ts.map +1 -0
  29. package/dist/mdz_stream_parser_table.js +351 -0
  30. package/dist/mdz_stream_state.svelte.d.ts +5 -0
  31. package/dist/mdz_stream_state.svelte.d.ts.map +1 -1
  32. package/dist/mdz_stream_state.svelte.js +8 -0
  33. package/dist/mdz_to_svelte.d.ts.map +1 -1
  34. package/dist/mdz_to_svelte.js +37 -3
  35. package/dist/mdz_token_parser.d.ts +16 -0
  36. package/dist/mdz_token_parser.d.ts.map +1 -1
  37. package/dist/mdz_token_parser.js +101 -2
  38. package/dist/svelte_preprocess_mdz.js +2 -2
  39. package/package.json +8 -8
  40. package/src/lib/context_helpers.ts +1 -1
  41. package/src/lib/mdz.ts +70 -3
  42. package/src/lib/mdz_contexts.ts +1 -1
  43. package/src/lib/mdz_helpers.ts +125 -1
  44. package/src/lib/mdz_lexer.ts +262 -3
  45. package/src/lib/mdz_opcodes.ts +10 -1
  46. package/src/lib/mdz_opcodes_to_nodes.ts +37 -3
  47. package/src/lib/mdz_stream_parser.ts +26 -9
  48. package/src/lib/mdz_stream_parser_block.ts +2 -2
  49. package/src/lib/mdz_stream_parser_inline.ts +4 -4
  50. package/src/lib/mdz_stream_parser_link.ts +3 -3
  51. package/src/lib/mdz_stream_parser_list.ts +24 -2
  52. package/src/lib/mdz_stream_parser_state.ts +12 -2
  53. package/src/lib/mdz_stream_parser_table.ts +409 -0
  54. package/src/lib/mdz_stream_parser_text.ts +2 -2
  55. package/src/lib/mdz_stream_parser_url.ts +2 -2
  56. package/src/lib/mdz_stream_state.svelte.ts +8 -1
  57. package/src/lib/mdz_to_svelte.ts +39 -5
  58. package/src/lib/mdz_token_parser.ts +122 -5
  59. package/src/lib/svelte_preprocess_mdz.ts +4 -4
  60. package/src/lib/tsdoc_mdz.ts +1 -1
@@ -0,0 +1,409 @@
1
+ /**
2
+ * Table handlers for the streaming mdz parser.
3
+ *
4
+ * A table is recognized at a column-0 pipe row whose next line is a valid
5
+ * delimiter row with a matching column count — a bounded one-line lookahead:
6
+ * the header row holds (`need_more`) until its delimiter line arrives, then the
7
+ * whole header emits at once. Body rows then stream one per line. Because a row
8
+ * is always fully buffered before it emits (no within-row streaming), each
9
+ * cell's inline content is parsed with the sync reference (`MdzTableCellParser`,
10
+ * one `MdzLexer`/`MdzTokenParser` shared across the row's cells) and replayed
11
+ * as opcodes — guaranteeing sync↔stream parity by construction.
12
+ *
13
+ * @module
14
+ */
15
+
16
+ import {DEV} from 'esm-env';
17
+
18
+ import {
19
+ NEWLINE,
20
+ PIPE,
21
+ is_line_whitespace,
22
+ mdz_split_table_row,
23
+ mdz_parse_table_delimiter,
24
+ } from './mdz_helpers.ts';
25
+ import {MdzTableCellParser, type MdzNode, type MdzTableAlign} from './mdz.ts';
26
+ import {
27
+ type MdzStreamParserState,
28
+ type TryResult,
29
+ alloc_id,
30
+ close_paragraph,
31
+ emit,
32
+ flush_text,
33
+ offset,
34
+ pop_stack_entry,
35
+ push_stack_entry,
36
+ } from './mdz_stream_parser_state.ts';
37
+
38
+ /**
39
+ * Try to open a table at a column-0 pipe row. Returns `need_more` until both
40
+ * the header line and its delimiter line are buffered (or, in forced mode,
41
+ * resolvable), `not_match` when the two lines aren't a valid header+delimiter
42
+ * pair, and `consumed` once the table opens (closing any open paragraph first,
43
+ * emitting the header row, and leaving `state.pos` at the first body line).
44
+ *
45
+ * @nodocs
46
+ */
47
+ export const try_table_start = (state: MdzStreamParserState, forced: boolean): TryResult => {
48
+ const start = state.pos;
49
+ const header_end = state.buffer.indexOf('\n', start);
50
+ // a table needs a header line plus a delimiter line — without the header's
51
+ // newline there's no room for a delimiter (at EOF it can never be a table)
52
+ if (header_end === -1) return forced ? 'not_match' : 'need_more';
53
+
54
+ const header_cells = mdz_split_table_row(state.buffer, start, header_end);
55
+ if (header_cells === null) return 'not_match';
56
+
57
+ const delim_start = header_end + 1;
58
+ let delim_end = state.buffer.indexOf('\n', delim_start);
59
+ if (delim_end === -1) {
60
+ if (!forced) return 'need_more'; // delimiter line not complete yet
61
+ delim_end = state.buffer.length;
62
+ }
63
+
64
+ const align = mdz_parse_table_delimiter(state.buffer, delim_start, delim_end);
65
+ if (align === null || align.length !== header_cells.length) return 'not_match';
66
+
67
+ // committed — the delimiter row is structural, never emitted
68
+ flush_text(state);
69
+ close_paragraph(state);
70
+
71
+ const table_id = alloc_id(state);
72
+ const table_start = offset(state, start);
73
+ emit(state, {type: 'open', id: table_id, node_type: 'Table', start: table_start, align});
74
+ push_stack_entry(state, table_id, 'Table', table_start);
75
+ state.table = {id: table_id, item_indent: null};
76
+
77
+ emit_table_row(state, header_cells, true, start, header_end);
78
+
79
+ // skip past the delimiter row to the first potential body line
80
+ state.pos = delim_end === state.buffer.length ? delim_end : delim_end + 1;
81
+ state.column = 0;
82
+ state.prev_char = NEWLINE;
83
+ return 'consumed';
84
+ };
85
+
86
+ /**
87
+ * Process the line at `state.pos` while a table is open: emit it as a body row,
88
+ * or close the table when it isn't a valid pipe row. Returns false (need more
89
+ * input) only when a pipe-row line is started but not yet complete. A non-pipe
90
+ * line (including a blank line) ends the table and is left for normal
91
+ * processing.
92
+ *
93
+ * @nodocs
94
+ */
95
+ export const process_table_line = (state: MdzStreamParserState, forced: boolean): boolean => {
96
+ const item_indent = state.table!.item_indent;
97
+ if (item_indent !== null) {
98
+ return process_table_line_in_item(state, forced, item_indent);
99
+ }
100
+
101
+ if (state.pos >= state.buffer.length) {
102
+ // the table may continue with more rows in a later chunk
103
+ if (forced) {
104
+ close_table(state);
105
+ return true;
106
+ }
107
+ return false;
108
+ }
109
+
110
+ const row_start = state.pos;
111
+ // a body row must start with `|` at column 0; anything else ends the table
112
+ // (decided without waiting for the line's newline)
113
+ if (state.buffer.charCodeAt(row_start) !== PIPE) {
114
+ close_table(state);
115
+ return true;
116
+ }
117
+
118
+ let row_end = state.buffer.indexOf('\n', row_start);
119
+ if (row_end === -1) {
120
+ if (!forced) return false; // pipe-row line not complete — hold
121
+ row_end = state.buffer.length;
122
+ }
123
+
124
+ const cells = mdz_split_table_row(state.buffer, row_start, row_end);
125
+ if (cells === null) {
126
+ // starts with `|` but isn't a valid row (e.g. no trailing pipe) — table ends
127
+ close_table(state);
128
+ return true;
129
+ }
130
+
131
+ emit_table_row(state, cells, false, row_start, row_end);
132
+ state.pos = row_end === state.buffer.length ? row_end : row_end + 1;
133
+ state.column = 0;
134
+ state.prev_char = NEWLINE;
135
+ return true;
136
+ };
137
+
138
+ /**
139
+ * Process body rows of an in-item table. Unlike the top-level path this keeps
140
+ * `state.pos` on the newline that ends each row — a row boundary the buffer
141
+ * never compacts past, since it's the active cursor. So when a dedent (to the
142
+ * item's `marker_indent` or shallower), a blank line, or a non-pipe line ends
143
+ * the table, `close_table` leaves the cursor on that newline and the list's
144
+ * newline handler re-classifies the following line — the same control-return
145
+ * shape as the in-item codeblock, robust across chunk boundaries.
146
+ */
147
+ const process_table_line_in_item = (
148
+ state: MdzStreamParserState,
149
+ forced: boolean,
150
+ item_indent: number,
151
+ ): boolean => {
152
+ if (state.pos >= state.buffer.length) {
153
+ if (forced) {
154
+ close_table(state);
155
+ return true;
156
+ }
157
+ return false;
158
+ }
159
+ // `state.pos` sits on the newline ending the previous row (or the delimiter);
160
+ // the candidate next row is the line after it
161
+ const line_start = state.pos + 1;
162
+ let j = line_start;
163
+ while (j < state.buffer.length && is_line_whitespace(state.buffer.charCodeAt(j))) {
164
+ j++;
165
+ }
166
+ if (j >= state.buffer.length) {
167
+ if (forced) {
168
+ close_table(state);
169
+ return true;
170
+ }
171
+ return false; // indent-only / incomplete line — hold
172
+ }
173
+ if (j - line_start <= item_indent || state.buffer.charCodeAt(j) !== PIPE) {
174
+ // a dedent to the marker (or shallower), a blank line, or a non-pipe line
175
+ // ends the table; the cursor stays on the row-boundary newline so the
176
+ // list resumes via its newline handler
177
+ close_table(state);
178
+ return true;
179
+ }
180
+ let row_end = state.buffer.indexOf('\n', j);
181
+ if (row_end === -1) {
182
+ if (!forced) return false; // pipe-row line not complete — hold
183
+ row_end = state.buffer.length;
184
+ }
185
+ const cells = mdz_split_table_row(state.buffer, j, row_end);
186
+ if (cells === null) {
187
+ close_table(state);
188
+ return true;
189
+ }
190
+ emit_table_row(state, cells, false, j, row_end);
191
+ state.pos = row_end; // stay on this row's terminating newline (or EOF)
192
+ return true;
193
+ };
194
+
195
+ /**
196
+ * Close the open table.
197
+ *
198
+ * @nodocs
199
+ */
200
+ export const close_table = (state: MdzStreamParserState): void => {
201
+ if (!state.table) return;
202
+ const {item_indent} = state.table;
203
+ const entry = pop_stack_entry(state);
204
+ emit(state, {type: 'close', id: entry.id, end: offset(state)});
205
+ state.table = null;
206
+ state.active_text_id = null;
207
+ if (item_indent === null) {
208
+ // top-level: the trailing newline + any blank lines absorb at the loop top
209
+ state.skip_blank_lines = true;
210
+ }
211
+ // in-item: the cursor is left on a row-boundary newline (or EOF). The list's
212
+ // newline handler re-classifies the following line; at EOF, `finish` closes
213
+ // the list. No skip_blank_lines — the list owns blank-line containment.
214
+ };
215
+
216
+ /** A recognized in-item table head: the header row's cells + the delimiter alignment. */
217
+ export interface TableInItemHead {
218
+ header_cells: Array<{start: number; end: number}>;
219
+ header_end: number;
220
+ align: Array<MdzTableAlign>;
221
+ delim_end: number;
222
+ }
223
+
224
+ /**
225
+ * Recognize a table opening as a block child of a list item: a pipe row at
226
+ * `header_first` (the line's indent already skipped) whose next line is a
227
+ * delimiter row, both indented past `marker_indent` (so both stay inside the
228
+ * item). Returns the parsed head, `'pending'` when the two-line lookahead isn't
229
+ * fully buffered, or `null` when it isn't a table.
230
+ *
231
+ * @nodocs
232
+ */
233
+ export const match_table_in_item_head = (
234
+ state: MdzStreamParserState,
235
+ header_first: number,
236
+ marker_indent: number,
237
+ forced: boolean,
238
+ ): TableInItemHead | 'pending' | null => {
239
+ const {buffer} = state;
240
+ const header_end = buffer.indexOf('\n', header_first);
241
+ if (header_end === -1) return forced ? null : 'pending'; // header needs its newline + a delimiter beneath
242
+ const header_cells = mdz_split_table_row(buffer, header_first, header_end);
243
+ if (header_cells === null) return null;
244
+ const delim_line_start = header_end + 1;
245
+ let dj = delim_line_start;
246
+ while (dj < buffer.length && is_line_whitespace(buffer.charCodeAt(dj))) dj++;
247
+ if (dj >= buffer.length) return forced ? null : 'pending'; // delimiter line not buffered yet
248
+ if (dj - delim_line_start <= marker_indent) return null; // delimiter dedented out of the item
249
+ let delim_end = buffer.indexOf('\n', dj);
250
+ if (delim_end === -1) {
251
+ if (!forced) return 'pending'; // delimiter line incomplete
252
+ delim_end = buffer.length;
253
+ }
254
+ const align = mdz_parse_table_delimiter(buffer, dj, delim_end);
255
+ if (align === null || align.length !== header_cells.length) return null;
256
+ return {header_cells, header_end, align, delim_end};
257
+ };
258
+
259
+ /**
260
+ * Open a table as a block child of a list item at `marker_indent`, given a
261
+ * `head` from `match_table_in_item_head`. Emits the `Table` open + header row
262
+ * and enters table mode (with `item_indent`), leaving `state.pos` at the first
263
+ * body line. The caller closes the item's run and pops to the attach level first.
264
+ *
265
+ * @nodocs
266
+ */
267
+ export const open_table_in_item = (
268
+ state: MdzStreamParserState,
269
+ header_first: number,
270
+ marker_indent: number,
271
+ head: TableInItemHead,
272
+ ): void => {
273
+ const table_id = alloc_id(state);
274
+ const table_start = offset(state, header_first);
275
+ emit(state, {
276
+ type: 'open',
277
+ id: table_id,
278
+ node_type: 'Table',
279
+ start: table_start,
280
+ align: head.align,
281
+ });
282
+ push_stack_entry(state, table_id, 'Table', table_start);
283
+ state.table = {id: table_id, item_indent: marker_indent};
284
+ emit_table_row(state, head.header_cells, true, header_first, head.header_end);
285
+ // leave the cursor on the delimiter's newline (or EOF) — the in-item body-row
286
+ // handler reads from the line after it and hands back on this same convention
287
+ state.pos = head.delim_end;
288
+ state.column = 0;
289
+ state.prev_char = NEWLINE;
290
+ };
291
+
292
+ /** Emit one table row: open, each cell, close. */
293
+ const emit_table_row = (
294
+ state: MdzStreamParserState,
295
+ cells: Array<{start: number; end: number}>,
296
+ header: boolean,
297
+ row_start: number,
298
+ row_end: number,
299
+ ): void => {
300
+ const row_id = alloc_id(state);
301
+ const row_off = offset(state, row_start);
302
+ emit(state, {type: 'open', id: row_id, node_type: 'TableRow', start: row_off, header});
303
+ push_stack_entry(state, row_id, 'TableRow', row_off);
304
+ // one lexer/parser shared across the row's cells (all index into the same
305
+ // buffer) rather than allocated per cell
306
+ const cell_parser = new MdzTableCellParser(state.buffer);
307
+ for (const cell of cells) {
308
+ emit_table_cell(state, cell_parser, cell.start, cell.end);
309
+ }
310
+ pop_stack_entry(state);
311
+ emit(state, {type: 'close', id: row_id, end: offset(state, row_end)});
312
+ state.active_text_id = null;
313
+ };
314
+
315
+ /**
316
+ * Emit one cell: open, its inline content (parsed with the sync reference over
317
+ * the fully-buffered span via the row's shared `cell_parser`), close.
318
+ */
319
+ const emit_table_cell = (
320
+ state: MdzStreamParserState,
321
+ cell_parser: MdzTableCellParser,
322
+ cell_start: number,
323
+ cell_end: number,
324
+ ): void => {
325
+ const cell_id = alloc_id(state);
326
+ const cell_off = offset(state, cell_start);
327
+ emit(state, {type: 'open', id: cell_id, node_type: 'TableCell', start: cell_off});
328
+ push_stack_entry(state, cell_id, 'TableCell', cell_off);
329
+ emit_inline_nodes(state, cell_parser.parse(cell_start, cell_end));
330
+ pop_stack_entry(state);
331
+ emit(state, {type: 'close', id: cell_id, end: offset(state, cell_end)});
332
+ state.active_text_id = null;
333
+ };
334
+
335
+ /**
336
+ * Replay sync-parsed inline nodes as opcodes. Cell content is inline-only;
337
+ * positions are local to `state.buffer` and lifted to global via `offset`.
338
+ */
339
+ const emit_inline_nodes = (state: MdzStreamParserState, nodes: Array<MdzNode>): void => {
340
+ for (const node of nodes) {
341
+ switch (node.type) {
342
+ case 'Text':
343
+ emit(state, {
344
+ type: 'text',
345
+ id: alloc_id(state),
346
+ content: node.content,
347
+ text_type: 'Text',
348
+ start: offset(state, node.start),
349
+ end: offset(state, node.end),
350
+ });
351
+ break;
352
+ case 'Code':
353
+ emit(state, {
354
+ type: 'text',
355
+ id: alloc_id(state),
356
+ content: node.content,
357
+ text_type: 'Code',
358
+ start: offset(state, node.start),
359
+ end: offset(state, node.end),
360
+ });
361
+ break;
362
+ case 'Bold':
363
+ case 'Italic':
364
+ case 'Strikethrough': {
365
+ const id = alloc_id(state);
366
+ const s = offset(state, node.start);
367
+ emit(state, {type: 'open', id, node_type: node.type, start: s});
368
+ push_stack_entry(state, id, node.type, s);
369
+ emit_inline_nodes(state, node.children);
370
+ pop_stack_entry(state);
371
+ emit(state, {type: 'close', id, end: offset(state, node.end)});
372
+ break;
373
+ }
374
+ case 'Link': {
375
+ const id = alloc_id(state);
376
+ const s = offset(state, node.start);
377
+ emit(state, {type: 'open', id, node_type: 'Link', start: s});
378
+ push_stack_entry(state, id, 'Link', s);
379
+ emit_inline_nodes(state, node.children);
380
+ pop_stack_entry(state);
381
+ emit(state, {
382
+ type: 'close',
383
+ id,
384
+ end: offset(state, node.end),
385
+ reference: node.reference,
386
+ link_type: node.link_type,
387
+ });
388
+ break;
389
+ }
390
+ case 'Element':
391
+ case 'Component': {
392
+ const id = alloc_id(state);
393
+ const s = offset(state, node.start);
394
+ emit(state, {type: 'open', id, node_type: node.type, start: s, name: node.name});
395
+ push_stack_entry(state, id, node.type, s, '', node.name);
396
+ emit_inline_nodes(state, node.children);
397
+ pop_stack_entry(state);
398
+ emit(state, {type: 'close', id, end: offset(state, node.end)});
399
+ break;
400
+ }
401
+ // cells are inline-only (lexed via `lex_table_cell`); a block node here
402
+ // would mean a sync/stream grammar drift — fail loud in dev
403
+ default:
404
+ if (DEV) {
405
+ throw new Error(`mdz table cell: unexpected inline node type '${node.type}'`);
406
+ }
407
+ }
408
+ }
409
+ };
@@ -22,13 +22,13 @@ import {
22
22
  UNDERSCORE,
23
23
  is_word_char,
24
24
  match_url_prefix_case_insensitive,
25
- } from './mdz_helpers.js';
25
+ } from './mdz_helpers.ts';
26
26
  import {
27
27
  type MdzStreamParserState,
28
28
  accumulate_text,
29
29
  ensure_paragraph,
30
30
  offset,
31
- } from './mdz_stream_parser_state.js';
31
+ } from './mdz_stream_parser_state.ts';
32
32
 
33
33
  /**
34
34
  * Consume a delimiter at the current position as literal text — the shared
@@ -24,7 +24,7 @@ import {
24
24
  is_word_char,
25
25
  match_url_prefix_case_insensitive,
26
26
  mdz_trim_url_punctuation,
27
- } from './mdz_helpers.js';
27
+ } from './mdz_helpers.ts';
28
28
  import {
29
29
  type MdzStreamParserState,
30
30
  type TryResult,
@@ -34,7 +34,7 @@ import {
34
34
  ensure_paragraph,
35
35
  flush_text,
36
36
  offset,
37
- } from './mdz_stream_parser_state.js';
37
+ } from './mdz_stream_parser_state.ts';
38
38
 
39
39
  const HTTPS_PREFIX = 'https://';
40
40
  const HTTP_PREFIX = 'http://';
@@ -10,7 +10,8 @@
10
10
 
11
11
  import {DEV} from 'esm-env';
12
12
 
13
- import type {MdzNodeId, MdzNodeType, MdzOpcode} from './mdz_opcodes.js';
13
+ import type {MdzNodeId, MdzNodeType, MdzOpcode} from './mdz_opcodes.ts';
14
+ import type {MdzTableAlign} from './mdz.ts';
14
15
 
15
16
  /**
16
17
  * A reactive node in the stream renderer tree.
@@ -31,6 +32,10 @@ export class MdzStreamNode {
31
32
  ordered?: boolean;
32
33
  start_number?: number;
33
34
  number?: number;
35
+ /** Per-column alignment (Table nodes). */
36
+ align?: Array<MdzTableAlign>;
37
+ /** Whether this is the header row (TableRow nodes). */
38
+ header?: boolean;
34
39
 
35
40
  constructor(id: MdzNodeId, type: MdzNodeType) {
36
41
  this.id = id;
@@ -104,6 +109,8 @@ export class MdzStreamState {
104
109
  if (opcode.ordered !== undefined) node.ordered = opcode.ordered;
105
110
  if (opcode.start_number !== undefined) node.start_number = opcode.start_number;
106
111
  if (opcode.number !== undefined) node.number = opcode.number;
112
+ if (opcode.align !== undefined) node.align = opcode.align;
113
+ if (opcode.header !== undefined) node.header = opcode.header;
107
114
  this.#attach(node);
108
115
  this.#stack.push(opcode.id);
109
116
  break;
@@ -8,12 +8,12 @@
8
8
  * @module
9
9
  */
10
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';
11
+ import {UnreachableError} from '@fuzdev/fuz_util/error.ts';
12
+ import {escape_svelte_text} from '@fuzdev/fuz_util/svelte_preprocess_helpers.ts';
13
+ import {escape_js_string} from '@fuzdev/fuz_util/string.ts';
14
14
 
15
- import type {MdzNode} from './mdz.js';
16
- import {mdz_resolve_relative_path, mdz_is_void_element} from './mdz_helpers.js';
15
+ import type {MdzNode, MdzNodeTableRow} from './mdz.ts';
16
+ import {mdz_resolve_relative_path, mdz_is_void_element} from './mdz_helpers.ts';
17
17
 
18
18
  /**
19
19
  * Result of converting `MdzNode` arrays to Svelte markup.
@@ -171,6 +171,40 @@ export const mdz_to_svelte = (
171
171
  case 'Blockquote':
172
172
  return `<blockquote>${render_nodes(node.children)}</blockquote>`;
173
173
 
174
+ case 'Table': {
175
+ const {align} = node;
176
+ const render_row = (row: MdzNodeTableRow, tag: 'th' | 'td'): string => {
177
+ let cells = '';
178
+ // normalize to the column count: pad short rows, ignore extra cells
179
+ for (let i = 0; i < align.length; i++) {
180
+ const a = align[i];
181
+ const style = a ? ` style="text-align:${a}"` : '';
182
+ const cell = row.children[i];
183
+ cells += `<${tag}${style}>${cell ? render_nodes(cell.children) : ''}</${tag}>`;
184
+ }
185
+ return `<tr>${cells}</tr>`;
186
+ };
187
+ let out = '<table>';
188
+ const header_rows = node.children.filter((r) => r.header);
189
+ const body_rows = node.children.filter((r) => !r.header);
190
+ if (header_rows.length > 0) {
191
+ out += '<thead>';
192
+ for (const row of header_rows) out += render_row(row, 'th');
193
+ out += '</thead>';
194
+ }
195
+ if (body_rows.length > 0) {
196
+ out += '<tbody>';
197
+ for (const row of body_rows) out += render_row(row, 'td');
198
+ out += '</tbody>';
199
+ }
200
+ return out + '</table>';
201
+ }
202
+
203
+ case 'TableRow':
204
+ case 'TableCell':
205
+ // rendered within their `Table`; never reached through `render_nodes`
206
+ throw new Error(`mdz: ${node.type} must render within a Table`);
207
+
174
208
  case 'Hr':
175
209
  return '<hr />';
176
210