@mdsvex/render 0.0.0 → 1.0.0-next.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/package.json CHANGED
@@ -1,15 +1,56 @@
1
1
  {
2
2
  "name": "@mdsvex/render",
3
- "version": "0.0.0",
4
- "description": "Placeholder release so npm trusted publishing can be configured.",
5
- "license": "MIT",
6
- "homepage": "https://mdsvex.pngwn.io",
3
+ "version": "1.0.0-next.0",
7
4
  "repository": {
8
5
  "type": "git",
9
- "url": "https://github.com/pngwn/MDsveX",
6
+ "url": "git+https://github.com/pngwn/MDsveX.git",
10
7
  "directory": "packages/render"
11
8
  },
12
- "publishConfig": {
13
- "access": "public"
9
+ "description": "Renderers for the PFM parser wire format",
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "types": "./src/index.ts"
15
+ },
16
+ "./html-cursor": {
17
+ "import": "./dist/html_cursor.js",
18
+ "types": "./src/html_cursor.ts"
19
+ },
20
+ "./component": {
21
+ "import": "./dist/component.js",
22
+ "types": "./src/component.ts"
23
+ },
24
+ "./mappings": {
25
+ "import": "./dist/mappings.js",
26
+ "types": "./src/mappings.ts"
27
+ },
28
+ "./sourcemap": {
29
+ "import": "./dist/sourcemap.js",
30
+ "types": "./src/sourcemap.ts"
31
+ },
32
+ "./Node.svelte": {
33
+ "types": "./src/Node.svelte",
34
+ "svelte": "./src/Node.svelte",
35
+ "import": "./src/Node.svelte"
36
+ }
37
+ },
38
+ "peerDependencies": {
39
+ "svelte": "^5.0.0"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "svelte": {
43
+ "optional": true
44
+ }
45
+ },
46
+ "dependencies": {
47
+ "@mdsvex/parse": "1.0.0-next.0"
48
+ },
49
+ "author": "pngwn <hello@pngwn.io>",
50
+ "license": "MIT",
51
+ "scripts": {
52
+ "test": "vitest --disableConsoleIntercept",
53
+ "test:run": "vitest run --silent",
54
+ "build": "vite build --config vite.config.build.ts"
14
55
  }
15
56
  }
@@ -0,0 +1,245 @@
1
+ <script lang="ts">
2
+ import type { NodeBuffer } from "@mdsvex/parse/utils";
3
+ import {
4
+ buf_children,
5
+ buf_text,
6
+ buf_text_content,
7
+ } from "@mdsvex/parse/buf-utils";
8
+ import type { Component } from "svelte";
9
+ import Node from "./Node.svelte";
10
+
11
+ type ComponentMap = Record<string, Component<any>>;
12
+
13
+ let {
14
+ buf,
15
+ idx,
16
+ source,
17
+ components,
18
+ }: {
19
+ buf: NodeBuffer;
20
+ idx: number;
21
+ source: string;
22
+ components?: ComponentMap;
23
+ } = $props();
24
+
25
+ const NONE = 0xffffffff;
26
+
27
+ // kind constants
28
+ const K_ROOT = 0;
29
+ const K_TEXT = 1;
30
+ const K_HTML = 2;
31
+ const K_HEADING = 3;
32
+ const K_CODE_FENCE = 5;
33
+ const K_LINE_BREAK = 6;
34
+ const K_PARAGRAPH = 7;
35
+ const K_CODE_SPAN = 8;
36
+ const K_EMPHASIS = 9;
37
+ const K_STRONG = 10;
38
+ const K_THEMATIC_BREAK = 11;
39
+ const K_LINK = 12;
40
+ const K_IMAGE = 13;
41
+ const K_BLOCK_QUOTE = 14;
42
+ const K_LIST = 15;
43
+ const K_LIST_ITEM = 16;
44
+ const K_HARD_BREAK = 17;
45
+ const K_SOFT_BREAK = 18;
46
+ const K_STRIKETHROUGH = 19;
47
+ const K_SUPERSCRIPT = 20;
48
+ const K_SUBSCRIPT = 21;
49
+ const K_TABLE = 22;
50
+ const K_TABLE_HEADER = 23;
51
+ const K_TABLE_ROW = 24;
52
+ const K_TABLE_CELL = 25;
53
+ const K_HTML_COMMENT = 26;
54
+
55
+ const NEWLINE = "\n";
56
+
57
+ /** metadata keys that are structural and should not become html attributes. */
58
+ const INTERNAL_KEYS = new Set([
59
+ "ordered", "tight", "start",
60
+ "info", "info_start", "info_end",
61
+ "tag", "attributes", "self_closing",
62
+ "alignments", "col_count",
63
+ "src",
64
+ ]);
65
+ const LINK_SKIP = new Set(["href", "title"]);
66
+ const IMAGE_SKIP = new Set(["title"]);
67
+
68
+ /** extract plugin-set attrs from metadata as a spreadable object. */
69
+ function get_attrs(meta: Record<string, unknown> | undefined, skip?: Set<string>): Record<string, any> {
70
+ if (!meta) return {};
71
+ const result: Record<string, any> = {};
72
+ for (const key in meta) {
73
+ if (INTERNAL_KEYS.has(key)) continue;
74
+ if (skip !== undefined && skip.has(key)) continue;
75
+ const val = meta[key];
76
+ if (val === true) {
77
+ result[key] = true;
78
+ } else if (val !== false && val != null) {
79
+ result[key] = String(val);
80
+ }
81
+ }
82
+ return result;
83
+ }
84
+
85
+ let kind = $derived(buf._kinds[idx]);
86
+ let extra = $derived(buf._extras[idx]);
87
+ let meta = $derived(buf.metadata_at(idx));
88
+
89
+ // a pending paragraph inside a list_item is a speculative wrapper for
90
+ // a list that may still become loose. render its children transparently
91
+ // so the tight-list default matches the common case and avoids a
92
+ // <p> flash before the list closes.
93
+ let skip_list_item_paragraph_wrapper = $derived(
94
+ kind === K_PARAGRAPH &&
95
+ buf._pending_nodes[idx] === 1 &&
96
+ buf._kinds[buf._parents[idx]] === K_LIST_ITEM,
97
+ );
98
+ </script>
99
+
100
+ {#snippet child_nodes(parent_idx: number)}
101
+ {#each buf_children(buf, parent_idx) as child_idx (child_idx)}
102
+ {@const kind = buf._kinds[child_idx]}
103
+ {#if kind === K_TEXT}
104
+ {buf_text(buf, child_idx, source)}
105
+ {:else if kind === K_LINE_BREAK}
106
+ <!-- skip -->
107
+ {:else}
108
+ <Node {buf} idx={child_idx} {source} {components} />
109
+ {/if}
110
+ {/each}
111
+ {/snippet}
112
+
113
+ {#snippet table_body(table_idx: number)}
114
+ {@const meta = buf.metadata_at(table_idx)}
115
+ {@const alignments = (meta?.alignments as string[]) ?? []}
116
+ {@const rows = buf_children(buf, table_idx)}
117
+ <table>
118
+ {#each rows as row_idx (row_idx)}
119
+ {#if buf._kinds[row_idx] === K_TABLE_HEADER}
120
+ <thead>
121
+ <tr>
122
+ {#each buf_children(buf, row_idx).filter((c) => buf._kinds[c] === K_TABLE_CELL) as cell_idx, col (cell_idx)}
123
+ {@const align = alignments[col]}
124
+ <th align={align && align !== 'none' ? align : undefined}>
125
+ {@render child_nodes(cell_idx)}
126
+ </th>
127
+ {/each}
128
+ </tr>
129
+ </thead>
130
+ {/if}
131
+ {/each}
132
+ <tbody>
133
+ {#each rows as row_idx (row_idx)}
134
+ {#if buf._kinds[row_idx] === K_TABLE_ROW}
135
+ <tr>
136
+ {#each buf_children(buf, row_idx).filter((c) => buf._kinds[c] === K_TABLE_CELL) as cell_idx, col (cell_idx)}
137
+ {@const align = alignments[col]}
138
+ <td align={align && align !== 'none' ? align : undefined}>
139
+ {@render child_nodes(cell_idx)}
140
+ </td>
141
+ {/each}
142
+ </tr>
143
+ {/if}
144
+ {/each}
145
+ </tbody>
146
+ </table>
147
+ {/snippet}
148
+
149
+ {#if kind === K_ROOT}
150
+ {@render child_nodes(idx)}
151
+ {:else if kind === K_HEADING}
152
+ <svelte:element this={'h' + extra} {...get_attrs(meta)}>
153
+ {@render child_nodes(idx)}
154
+ </svelte:element>
155
+ {:else if kind === K_PARAGRAPH}
156
+ {#if skip_list_item_paragraph_wrapper}
157
+ {@render child_nodes(idx)}
158
+ {:else}
159
+ <p {...get_attrs(meta)}>{@render child_nodes(idx)}</p>
160
+ {/if}
161
+ {:else if kind === K_EMPHASIS}
162
+ <em {...get_attrs(meta)}>{@render child_nodes(idx)}</em>
163
+ {:else if kind === K_STRONG}
164
+ <strong {...get_attrs(meta)}>{@render child_nodes(idx)}</strong>
165
+ {:else if kind === K_CODE_SPAN}
166
+ <code {...get_attrs(meta)}>{buf_text(buf, idx, source).replace(/\n/g, ' ')}</code>
167
+ {:else if kind === K_CODE_FENCE}
168
+ {@const info =
169
+ (meta?.info as string | undefined) ??
170
+ (meta?.info_start != null
171
+ ? source.slice(meta.info_start as number, meta.info_end as number)
172
+ : undefined)}
173
+ <pre><code class={info ? 'language-' + info : undefined} {...get_attrs(meta)}
174
+ >{buf_text(buf, idx, source)}</code
175
+ ></pre>
176
+ {:else if kind === K_BLOCK_QUOTE}
177
+ <blockquote {...get_attrs(meta)}>{@render child_nodes(idx)}</blockquote>
178
+ {:else if kind === K_LINK}
179
+ <a href={meta?.href as string} title={(meta?.title as string) || undefined} {...get_attrs(meta, LINK_SKIP)}>
180
+ {@render child_nodes(idx)}
181
+ </a>
182
+ {:else if kind === K_IMAGE}
183
+ <img
184
+ src={meta?.src as string}
185
+ alt={buf_text_content(buf, idx, source)}
186
+ title={(meta?.title as string) || undefined}
187
+ {...get_attrs(meta, IMAGE_SKIP)}
188
+ />
189
+ {:else if kind === K_LIST}
190
+ {#if meta?.ordered}
191
+ {@const start = meta?.start as number | undefined}
192
+ <ol start={start != null && start !== 1 ? start : undefined} {...get_attrs(meta)}>
193
+ {@render child_nodes(idx)}
194
+ </ol>
195
+ {:else}
196
+ <ul {...get_attrs(meta)}>{@render child_nodes(idx)}</ul>
197
+ {/if}
198
+ {:else if kind === K_LIST_ITEM}
199
+ <li {...get_attrs(meta)}>{@render child_nodes(idx)}</li>
200
+ {:else if kind === K_THEMATIC_BREAK}
201
+ <hr {...get_attrs(meta)} />
202
+ {:else if kind === K_HARD_BREAK}
203
+ <br />
204
+ {:else if kind === K_SOFT_BREAK}
205
+ {NEWLINE}
206
+ {:else if kind === K_STRIKETHROUGH}
207
+ <del {...get_attrs(meta)}>{@render child_nodes(idx)}</del>
208
+ {:else if kind === K_SUPERSCRIPT}
209
+ <sup {...get_attrs(meta)}>{@render child_nodes(idx)}</sup>
210
+ {:else if kind === K_SUBSCRIPT}
211
+ <sub {...get_attrs(meta)}>{@render child_nodes(idx)}</sub>
212
+ {:else if kind === K_HTML}
213
+ {@const tag = meta?.tag as string}
214
+ {@const attrs = meta?.attributes as Record | undefined}
215
+ {@const spread = attrs
216
+ ? Object.fromEntries(
217
+ Object.entries(attrs).map(([k, v]) => [k, v === true ? true : typeof v === 'object' && v?.type === 'expression' ? v.value : v])
218
+ )
219
+ : {}}
220
+ {@const CustomComponent = components?.[tag]}
221
+ {@const is_raw_text = tag === 'script' || tag === 'style'}
222
+ {#if CustomComponent}
223
+ <CustomComponent {...spread}>
224
+ {#if !meta?.self_closing}
225
+ {@render child_nodes(idx)}
226
+ {/if}
227
+ </CustomComponent>
228
+ {:else}
229
+ <svelte:element this={tag} {...spread}>
230
+ {#if meta?.self_closing}
231
+ <!-- void element -->
232
+ {:else if is_raw_text}
233
+ {buf_text(buf, idx, source)}
234
+ {:else}
235
+ {@render child_nodes(idx)}
236
+ {/if}
237
+ </svelte:element>
238
+ {/if}
239
+ {:else if kind === K_HTML_COMMENT}
240
+ <!-- {buf_text(buf, idx, source)} -->
241
+ {:else if kind === K_TABLE}
242
+ {@render table_body(idx)}
243
+ {:else}
244
+ {@render child_nodes(idx)}
245
+ {/if}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * PFM Component Renderer
3
+ *
4
+ * Maintains a stable list of block-level entries for a NodeBuffer,
5
+ * each holding a buffer index and a version counter.
6
+ *
7
+ * Designed for use with the <Node> Svelte component:
8
+ *
9
+ * {#each renderer.blocks as block (block.idx)}
10
+ * {#key block.v}
11
+ * <Node buf={renderer.buf} idx={block.idx} source={renderer.source} />
12
+ * {/key}
13
+ * {/each}
14
+ *
15
+ * Closed blocks have a frozen version, {#key} keeps them stable.
16
+ * Open (streaming) blocks get bumped, {#key} recreates the subtree
17
+ * with fresh content.
18
+ */
19
+
20
+ import type { NodeBuffer } from "@mdsvex/parse/utils";
21
+
22
+ const NONE = 0xffffffff;
23
+ const K_LINE_BREAK = 6;
24
+
25
+ // block entry
26
+
27
+ export interface ComponentBlock {
28
+ /** Buffer index, use as Svelte each key. */
29
+ idx: number;
30
+ /** Version counter, bumped on each update while the block is open. */
31
+ v: number;
32
+ }
33
+
34
+ // componentrenderer
35
+
36
+ export class ComponentRenderer {
37
+ blocks: ComponentBlock[] = [];
38
+ /** The current buffer reference (for passing to Node.svelte). */
39
+ buf: NodeBuffer | null = null;
40
+ /** The current source/text string (for passing to Node.svelte). */
41
+ source = "";
42
+ private closed: Set<number> = new Set();
43
+
44
+ update(buf: NodeBuffer, source: string): ComponentBlock[] {
45
+ this.buf = buf;
46
+ this.source = source;
47
+
48
+ let block_idx = 0;
49
+ let changed = false;
50
+
51
+ // walk root's children via sibling chain
52
+ let child = buf._children_starts[0]; // root is index 0
53
+ while (child !== NONE) {
54
+ const kind = buf._kinds[child];
55
+ const next = buf._next_siblings[child];
56
+ const is_sibling = next !== NONE && buf._parents[next] === 0;
57
+
58
+ if (kind !== K_LINE_BREAK) {
59
+ const closed = buf._ends[child] !== NONE;
60
+
61
+ if (block_idx >= this.blocks.length) {
62
+ this.blocks.push({ idx: child, v: 0 });
63
+ changed = true;
64
+ } else if (!this.closed.has(child)) {
65
+ this.blocks[block_idx].v++;
66
+ changed = true;
67
+ }
68
+
69
+ if (closed && !this.closed.has(child)) {
70
+ this.closed.add(child);
71
+ }
72
+
73
+ block_idx++;
74
+ }
75
+
76
+ child = is_sibling ? next : NONE;
77
+ }
78
+
79
+ return changed ? (this.blocks = [...this.blocks]) : this.blocks;
80
+ }
81
+
82
+ reset(): void {
83
+ this.blocks = [];
84
+ this.buf = null;
85
+ this.source = "";
86
+ this.closed.clear();
87
+ }
88
+ }