@mdsvex/render 1.0.0-next.1 → 1.0.0-next.3

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/src/Node.svelte CHANGED
@@ -1,10 +1,13 @@
1
1
  <script lang="ts">
2
+ import { is_raw_text_tag } from '@mdsvex/parse/utils';
2
3
  import type { NodeBuffer } from '@mdsvex/parse/utils';
3
4
  import {
4
5
  buf_children,
6
+ buf_code,
5
7
  buf_text,
6
8
  buf_text_content,
7
9
  } from '@mdsvex/parse/buf-utils';
10
+ import { setContext } from 'svelte';
8
11
  import type { Component } from 'svelte';
9
12
  import Node from './Node.svelte';
10
13
 
@@ -15,13 +18,25 @@
15
18
  idx,
16
19
  source,
17
20
  components,
21
+ directives,
22
+ plugin_data,
18
23
  }: {
19
24
  buf: NodeBuffer;
20
25
  idx: number;
21
26
  source: string;
27
+ /** a component for an element by its tag */
22
28
  components?: ComponentMap;
29
+ /** a component for a directive by its name, args are its props, label and children snippets */
30
+ directives?: ComponentMap;
31
+ /** what plugins wrote, a component reads it from the context, see PluginStream */
32
+ plugin_data?: { readonly current: unknown };
23
33
  } = $props();
24
34
 
35
+ // the key of @mdsvex/render/stream and of a compiled document, Symbol.for so every copy agrees
36
+ // svelte-ignore state_referenced_locally
37
+ if (plugin_data !== undefined)
38
+ setContext(Symbol.for('mdsvex.plugin_data'), plugin_data);
39
+
25
40
  const NONE = 0xffffffff;
26
41
 
27
42
  // kind constants
@@ -52,7 +67,13 @@
52
67
  // a cell extra is its grid column, merged cells leave gaps in a row
53
68
  const K_TABLE_CELL = 25;
54
69
  const K_HTML_COMMENT = 26;
70
+ const K_DIRECTIVE_INLINE = 30;
71
+ const K_DIRECTIVE_LEAF = 31;
72
+ const K_DIRECTIVE_CONTAINER = 32;
55
73
  const K_DIRECTIVE_LABEL = 35;
74
+ const K_FOOTNOTE_REFERENCE = 36;
75
+ const K_FOOTNOTE_DEFINITION = 37;
76
+ const K_DEFINITION = 38;
56
77
 
57
78
  const NEWLINE = '\n';
58
79
 
@@ -61,6 +82,7 @@
61
82
  'ordered',
62
83
  'tight',
63
84
  'start',
85
+ 'checked',
64
86
  'info',
65
87
  'info_start',
66
88
  'info_end',
@@ -109,8 +131,71 @@
109
131
  buf.pending_at(idx) === 1 &&
110
132
  buf.kind_at(buf.parent_at(idx)) === K_LIST_ITEM
111
133
  );
134
+
135
+ /** true when the checkbox of a task item goes inside its first paragraph, as github renders a loose list */
136
+ function box_in_paragraph(item: number): boolean {
137
+ const first = buf.first_child_at(item);
138
+ return (
139
+ first !== NONE &&
140
+ buf.kind_at(first) === K_PARAGRAPH &&
141
+ buf.pending_at(first) !== 1
142
+ );
143
+ }
144
+
145
+ // the checkbox state this node renders, undefined for no checkbox
146
+ let checked = $derived.by(() => {
147
+ if (kind === K_LIST_ITEM) {
148
+ return box_in_paragraph(idx) ? undefined : meta?.checked;
149
+ }
150
+ if (kind !== K_PARAGRAPH || skip_list_item_paragraph_wrapper) {
151
+ return undefined;
152
+ }
153
+ const parent = buf.parent_at(idx);
154
+ if (
155
+ parent === NONE ||
156
+ buf.kind_at(parent) !== K_LIST_ITEM ||
157
+ buf.first_child_at(parent) !== idx
158
+ ) {
159
+ return undefined;
160
+ }
161
+ return buf.metadata_at(parent)?.checked;
162
+ });
163
+
164
+ /** the directive_label of a leaf or container, its first child, or NONE */
165
+ function label_of(node: number): number {
166
+ if (kind === K_DIRECTIVE_INLINE) return NONE;
167
+ const first = buf.first_child_at(node);
168
+ return first !== NONE &&
169
+ buf.parent_at(first) === node &&
170
+ buf.kind_at(first) === K_DIRECTIVE_LABEL
171
+ ? first
172
+ : NONE;
173
+ }
174
+
175
+ /** a child other than the label and line breaks, a component gets no empty children */
176
+ function has_body(node: number): boolean {
177
+ for (const child of buf_children(buf, node)) {
178
+ const k = buf.kind_at(child);
179
+ if (k !== K_LINE_BREAK && k !== K_DIRECTIVE_LABEL) return true;
180
+ }
181
+ return false;
182
+ }
183
+
184
+ let directive = $derived(
185
+ kind === K_DIRECTIVE_INLINE ||
186
+ kind === K_DIRECTIVE_LEAF ||
187
+ kind === K_DIRECTIVE_CONTAINER
188
+ ? directives?.[meta?.name as string]
189
+ : undefined
190
+ );
112
191
  </script>
113
192
 
193
+ {#snippet task_box()}
194
+ {#if typeof checked === 'boolean'}
195
+ <input type="checkbox" disabled {checked} />{' '}
196
+ {/if}
197
+ {/snippet}
198
+
114
199
  {#snippet child_nodes(parent_idx: number)}
115
200
  {#each buf_children(buf, parent_idx) as child_idx (child_idx)}
116
201
  {@const kind = buf.kind_at(child_idx)}
@@ -119,11 +204,26 @@
119
204
  {:else if kind === K_LINE_BREAK}
120
205
  <!-- skip -->
121
206
  {:else}
122
- <Node {buf} idx={child_idx} {source} {components} />
207
+ <Node
208
+ {buf}
209
+ idx={child_idx}
210
+ {source}
211
+ {components}
212
+ {directives}
213
+ {plugin_data}
214
+ />
123
215
  {/if}
124
216
  {/each}
125
217
  {/snippet}
126
218
 
219
+ {#snippet directive_label()}
220
+ {@render child_nodes(label_of(idx))}
221
+ {/snippet}
222
+
223
+ {#snippet directive_body()}
224
+ {@render child_nodes(idx)}
225
+ {/snippet}
226
+
127
227
  {#snippet table_body(table_idx: number)}
128
228
  {@const meta = buf.metadata_at(table_idx)}
129
229
  {@const alignments = (meta?.alignments as string[]) ?? []}
@@ -187,7 +287,14 @@
187
287
  </table>
188
288
  {/snippet}
189
289
 
190
- {#if kind === K_ROOT}
290
+ {#if directive}
291
+ {@const Directive = directive}
292
+ <Directive
293
+ {...meta?.args}
294
+ label={label_of(idx) !== NONE ? directive_label : undefined}
295
+ children={has_body(idx) ? directive_body : undefined}
296
+ />
297
+ {:else if kind === K_ROOT}
191
298
  {@render child_nodes(idx)}
192
299
  {:else if kind === K_HEADING}
193
300
  <svelte:element this={'h' + extra} {...get_attrs(meta)}>
@@ -197,7 +304,7 @@
197
304
  {#if skip_list_item_paragraph_wrapper}
198
305
  {@render child_nodes(idx)}
199
306
  {:else}
200
- <p {...get_attrs(meta)}>{@render child_nodes(idx)}</p>
307
+ <p {...get_attrs(meta)}>{@render task_box()}{@render child_nodes(idx)}</p>
201
308
  {/if}
202
309
  {:else if kind === K_EMPHASIS}
203
310
  <em {...get_attrs(meta)}>{@render child_nodes(idx)}</em>
@@ -205,7 +312,7 @@
205
312
  <strong {...get_attrs(meta)}>{@render child_nodes(idx)}</strong>
206
313
  {:else if kind === K_CODE_SPAN}
207
314
  <code {...get_attrs(meta)}
208
- >{buf_text(buf, idx, source).replace(/\n/g, ' ')}</code
315
+ >{buf_code(buf, idx, source).replace(/\n/g, ' ')}</code
209
316
  >
210
317
  {:else if kind === K_CODE_FENCE}
211
318
  {@const info =
@@ -214,7 +321,7 @@
214
321
  ? source.slice(meta.info_start as number, meta.info_end as number)
215
322
  : undefined)}
216
323
  <pre><code class={info ? 'language-' + info : undefined} {...get_attrs(meta)}
217
- >{buf_text(buf, idx, source)}</code
324
+ >{buf_code(buf, idx, source)}</code
218
325
  ></pre>
219
326
  {:else if kind === K_BLOCK_QUOTE}
220
327
  <blockquote {...get_attrs(meta)}>{@render child_nodes(idx)}</blockquote>
@@ -246,7 +353,7 @@
246
353
  <ul {...get_attrs(meta)}>{@render child_nodes(idx)}</ul>
247
354
  {/if}
248
355
  {:else if kind === K_LIST_ITEM}
249
- <li {...get_attrs(meta)}>{@render child_nodes(idx)}</li>
356
+ <li {...get_attrs(meta)}>{@render task_box()}{@render child_nodes(idx)}</li>
250
357
  {:else if kind === K_THEMATIC_BREAK}
251
358
  <hr {...get_attrs(meta)} />
252
359
  {:else if kind === K_HARD_BREAK}
@@ -275,7 +382,7 @@
275
382
  )
276
383
  : {}}
277
384
  {@const CustomComponent = components?.[tag]}
278
- {@const is_raw_text = tag === 'script' || tag === 'style'}
385
+ {@const is_raw_text = is_raw_text_tag(tag)}
279
386
  {#if CustomComponent}
280
387
  <CustomComponent {...spread}>
281
388
  {#if !meta?.self_closing}
@@ -299,6 +406,16 @@
299
406
  {@render table_body(idx)}
300
407
  {:else if kind === K_DIRECTIVE_LABEL}
301
408
  <!-- a directive renders as its children, its label is not one of them -->
409
+ {:else if kind === K_FOOTNOTE_REFERENCE}
410
+ <!-- a stream has no number for it yet, so the label stands in -->
411
+ <sup class="footnote-reference">{meta?.label}</sup>
412
+ {:else if kind === K_FOOTNOTE_DEFINITION}
413
+ <div class="footnote">
414
+ <sup class="footnote-reference">{meta?.label}</sup>
415
+ {@render child_nodes(idx)}
416
+ </div>
417
+ {:else if kind === K_DEFINITION}
418
+ <!-- a link reference definition renders nothing, the links that use it carry its url -->
302
419
  {:else}
303
420
  {@render child_nodes(idx)}
304
421
  {/if}
package/src/component.ts CHANGED
@@ -22,6 +22,17 @@ import type { NodeBuffer } from '@mdsvex/parse/utils';
22
22
  const NONE = 0xffffffff;
23
23
  const K_LINE_BREAK = 6;
24
24
 
25
+ /** the child of the root that holds a node, NONE for a node out of the tree */
26
+ export function top_block(buf: NodeBuffer, node: number): number {
27
+ if (node === 0) return NONE;
28
+ for (;;) {
29
+ const parent = buf.parent_at(node);
30
+ if (parent === 0) return node;
31
+ if (parent === NONE) return NONE;
32
+ node = parent;
33
+ }
34
+ }
35
+
25
36
  // block entry
26
37
 
27
38
  export interface ComponentBlock {
@@ -79,6 +90,18 @@ export class ComponentRenderer {
79
90
  return changed ? (this.blocks = [...this.blocks]) : this.blocks;
80
91
  }
81
92
 
93
+ /**
94
+ * the nodes changed after their block closed, as the node of a deferred
95
+ * task that applied does, apply_ready gives them, each block that holds
96
+ * one renders again at the next update
97
+ */
98
+ invalidate(buf: NodeBuffer, nodes: readonly number[]): void {
99
+ for (let i = 0; i < nodes.length; i++) {
100
+ const block = top_block(buf, nodes[i]);
101
+ if (block !== NONE) this.closed.delete(block);
102
+ }
103
+ }
104
+
82
105
  reset(): void {
83
106
  this.blocks = [];
84
107
  this.buf = null;