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

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.
@@ -0,0 +1,43 @@
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
+ import type { NodeBuffer } from '@mdsvex/parse/utils';
20
+ /** the child of the root that holds a node, NONE for a node out of the tree */
21
+ export declare function top_block(buf: NodeBuffer, node: number): number;
22
+ export interface ComponentBlock {
23
+ /** Buffer index, use as Svelte each key. */
24
+ idx: number;
25
+ /** Version counter, bumped on each update while the block is open. */
26
+ v: number;
27
+ }
28
+ export declare class ComponentRenderer {
29
+ blocks: ComponentBlock[];
30
+ /** The current buffer reference (for passing to Node.svelte). */
31
+ buf: NodeBuffer | null;
32
+ /** The current source/text string (for passing to Node.svelte). */
33
+ source: string;
34
+ private closed;
35
+ update(buf: NodeBuffer, source: string): ComponentBlock[];
36
+ /**
37
+ * the nodes changed after their block closed, as the node of a deferred
38
+ * task that applied does, apply_ready gives them, each block that holds
39
+ * one renders again at the next update
40
+ */
41
+ invalidate(buf: NodeBuffer, nodes: readonly number[]): void;
42
+ reset(): void;
43
+ }
package/dist/component.js CHANGED
@@ -1,24 +1,45 @@
1
- class r {
1
+ function N(o, t) {
2
+ if (t === 0) return 4294967295;
3
+ for (; ; ) {
4
+ const e = o.parent_at(t);
5
+ if (e === 0) return t;
6
+ if (e === 4294967295) return 4294967295;
7
+ t = e;
8
+ }
9
+ }
10
+ class a {
2
11
  constructor() {
3
12
  this.blocks = [], this.buf = null, this.source = "", this.closed = /* @__PURE__ */ new Set();
4
13
  }
5
- update(t, l) {
6
- this.buf = t, this.source = l;
7
- let e = 0, i = !1, s = t.first_child_at(0);
14
+ update(t, e) {
15
+ this.buf = t, this.source = e;
16
+ let i = 0, l = !1, s = t.first_child_at(0);
8
17
  for (; s !== 4294967295; ) {
9
- const o = t.kind_at(s), c = t.next_at(s), h = c !== 4294967295 && t.parent_at(c) === 0;
10
- if (o !== 6) {
11
- const n = t.end_at(s) !== 4294967295;
12
- e >= this.blocks.length ? (this.blocks.push({ idx: s, v: 0 }), i = !0) : this.closed.has(s) || (this.blocks[e].v++, i = !0), n && !this.closed.has(s) && this.closed.add(s), e++;
18
+ const n = t.kind_at(s), c = t.next_at(s), h = c !== 4294967295 && t.parent_at(c) === 0;
19
+ if (n !== 6) {
20
+ const r = t.end_at(s) !== 4294967295;
21
+ i >= this.blocks.length ? (this.blocks.push({ idx: s, v: 0 }), l = !0) : this.closed.has(s) || (this.blocks[i].v++, l = !0), r && !this.closed.has(s) && this.closed.add(s), i++;
13
22
  }
14
23
  s = h ? c : 4294967295;
15
24
  }
16
- return i ? this.blocks = [...this.blocks] : this.blocks;
25
+ return l ? this.blocks = [...this.blocks] : this.blocks;
26
+ }
27
+ /**
28
+ * the nodes changed after their block closed, as the node of a deferred
29
+ * task that applied does, apply_ready gives them, each block that holds
30
+ * one renders again at the next update
31
+ */
32
+ invalidate(t, e) {
33
+ for (let i = 0; i < e.length; i++) {
34
+ const l = N(t, e[i]);
35
+ l !== 4294967295 && this.closed.delete(l);
36
+ }
17
37
  }
18
38
  reset() {
19
39
  this.blocks = [], this.buf = null, this.source = "", this.closed.clear();
20
40
  }
21
41
  }
22
42
  export {
23
- r as ComponentRenderer
43
+ a as ComponentRenderer,
44
+ N as top_block
24
45
  };
@@ -0,0 +1,2 @@
1
+ /** whether name is a named character reference */
2
+ export declare function is_entity_name(name: string): boolean;
@@ -0,0 +1,397 @@
1
+ /**
2
+ * cursor-based pfm html renderer
3
+ *
4
+ * renders html from a cursor over a NodeBuffer
5
+ * zero per node allocations, the cursor reads node words directly,
6
+ * text is lazily sliced from source only when needed.
7
+ *
8
+ * usage:
9
+ *
10
+ * const cursor = new cursor(tree.get_buffer(), source);
11
+ * const html = rendercursor(cursor);
12
+ */
13
+ import { Cursor } from '@mdsvex/parse/cursor';
14
+ import type { FootnoteIndex } from '@mdsvex/parse/buf-utils';
15
+ import type { NodeBuffer } from '@mdsvex/parse/utils';
16
+ import { MapSink } from './mappings.js';
17
+ import type { Mapping, MappingData } from './mappings.js';
18
+ import type { MapTrace, SourceMapV3 } from './sourcemap.js';
19
+ import { ComponentScope } from './scope.js';
20
+ import type { ReplaceWarning } from './scope.js';
21
+ export type { Mapping, CodeInformation, MappingData } from './mappings.js';
22
+ export { MapSink } from './mappings.js';
23
+ export { ComponentScope, component_imports } from './scope.js';
24
+ /** @internal a pass over the tree reads references as the renderer does */
25
+ export { is_entity_name } from './entities.js';
26
+ export type { ComponentImport, ComponentSource, DefaultImport, ReplaceWarning, } from './scope.js';
27
+ /**
28
+ * a fence the highlighter rendered, every string is markup svelte reads as
29
+ * static text, braces in it are entities
30
+ */
31
+ export interface HighlightedBlock {
32
+ /** markup ahead of the <pre>, a figure and its title, a pre replacement drops it */
33
+ before: string;
34
+ /** the attribute text of the <pre> open tag, null when body is the whole block */
35
+ attributes: string | null;
36
+ /** what the <pre> holds */
37
+ body: string;
38
+ /** markup after the </pre>, a pre replacement drops it */
39
+ after: string;
40
+ /** the props a pre replacement takes ahead of code, title, caption and meta props */
41
+ props: string;
42
+ /** the text a reader sees, the code prop of a pre replacement */
43
+ code: string;
44
+ /**
45
+ * a template literal holding code with its live expressions interpolated,
46
+ * the code prop in place of code, null when the block has none
47
+ */
48
+ code_template: string | null;
49
+ /**
50
+ * the live expressions svelte reads in body, triples of their offset in
51
+ * body, their offset in the fence text and their length, null for none
52
+ */
53
+ live: number[] | null;
54
+ /** the messages of meta props a pre replacement drops */
55
+ dropped: string[] | null;
56
+ }
57
+ /** a code span the highlighter rendered, braces in it are entities */
58
+ export interface HighlightedCode {
59
+ /** the attribute text of the <code> open tag, null when body is the whole span */
60
+ attributes: string | null;
61
+ body: string;
62
+ }
63
+ export type HighlightWarningCode = 'code_replacement_skipped' | 'meta_prop_ignored';
64
+ /** highlights the code of one render, start is the offset of the node in the source */
65
+ export interface CodeHighlighter {
66
+ /** null renders the fence plain */
67
+ block(code: string, lang: string, meta: string, start: number): HighlightedBlock | null;
68
+ /** a code span with a #! hint, null renders it plain */
69
+ inline(code: string, lang: string, start: number): HighlightedCode | null;
70
+ warn(code: HighlightWarningCode, message: string, start: number): void;
71
+ /**
72
+ * true when block renders a math fence as display math, the fence then
73
+ * takes the math replacement and never pre, its block has no <pre>
74
+ */
75
+ readonly math?: boolean;
76
+ /**
77
+ * inline math, a code span in dollars, asked only while math is true, the
78
+ * body is the whole span and the math replacement takes it as children
79
+ */
80
+ inline_math?(code: string, start: number): HighlightedCode;
81
+ }
82
+ /** reads the fence meta conventions for a pre replacement the highlighter left plain */
83
+ export interface PreMeta {
84
+ /**
85
+ * the title, caption and meta props of a pre replacement as props text,
86
+ * it warns about the meta props it drops, start is the offset of the fence
87
+ */
88
+ pre_props(meta: string, start: number): string;
89
+ }
90
+ /** a string is escaped as a url of the source is, '' leaves the attribute out, an expression goes in braces */
91
+ export type UrlSource = string | {
92
+ type: 'expression';
93
+ value: string;
94
+ };
95
+ /** what replaces the src of an image or the href of a link, url is never empty, null or undefined keeps it */
96
+ export type UrlLookup = (url: string, image: boolean) => UrlSource | null | undefined;
97
+ /** what a fence renders in place of what the document has */
98
+ export interface FenceSource {
99
+ /** the code, in place of the body of the fence */
100
+ text: string;
101
+ /** the info string in place of the written one, '' for none, undefined keeps it */
102
+ info?: string;
103
+ /** props text for a pre replacement, each as ' name={value}', after its own props */
104
+ props?: string;
105
+ }
106
+ /** the fences that take their code from elsewhere, by node index */
107
+ export type FenceSources = ReadonlyMap<number, FenceSource>;
108
+ /** the opening and closing double quote, then the opening and closing single quote */
109
+ export type SmartQuotes = readonly [string, string, string, string];
110
+ /** each kind is on unless it is false */
111
+ export interface SmartPunctuationOptions {
112
+ /** straight quotes to curly ones, four strings replace the english ones */
113
+ quotes?: boolean | SmartQuotes;
114
+ /** two hyphens to an en dash and three to an em dash */
115
+ dashes?: boolean;
116
+ /** three dots to an ellipsis */
117
+ ellipses?: boolean;
118
+ }
119
+ /** true is every kind with english quotes, false is none */
120
+ export type SmartPunctuation = boolean | SmartPunctuationOptions;
121
+ declare const enum Slot {
122
+ NONE = 4294967295
123
+ }
124
+ declare function escape_html(text: string): string;
125
+ /** escape_html that keeps character references, as text and link attributes have them */
126
+ declare function escape_text_html(text: string): string;
127
+ declare function escape_code(text: string): string;
128
+ /**
129
+ * @internal true once when a math fence rendered as code since the last
130
+ * call, a compile with a math renderer and no highlighter then knows to
131
+ * render again with one, no document is searched for math up front
132
+ */
133
+ export declare function take_plain_math(): boolean;
134
+ /**
135
+ * equals escape_text_html of c.text, reading source slices through the escape
136
+ * index, apart from an & a backslash escaped, which the slice no longer shows
137
+ */
138
+ declare function escape_node_text(c: Cursor): string;
139
+ /** equals escape_code of c.text, reading source slices through the escape index */
140
+ declare function escape_code_text(c: Cursor): string;
141
+ declare const enum K {
142
+ ROOT = 0,
143
+ TEXT = 1,
144
+ HTML = 2,
145
+ HEADING = 3,
146
+ CODE_FENCE = 5,
147
+ LINE_BREAK = 6,
148
+ PARAGRAPH = 7,
149
+ CODE_SPAN = 8,
150
+ EMPHASIS = 9,
151
+ STRONG = 10,
152
+ THEMATIC_BREAK = 11,
153
+ LINK = 12,
154
+ IMAGE = 13,
155
+ BLOCK_QUOTE = 14,
156
+ LIST = 15,
157
+ LIST_ITEM = 16,
158
+ HARD_BREAK = 17,
159
+ SOFT_BREAK = 18,
160
+ STRIKETHROUGH = 19,
161
+ SUPERSCRIPT = 20,
162
+ SUBSCRIPT = 21,
163
+ TABLE = 22,
164
+ TABLE_HEADER = 23,
165
+ TABLE_ROW = 24,
166
+ TABLE_CELL = 25,
167
+ HTML_COMMENT = 26,
168
+ SVELTE_TAG = 27,
169
+ SVELTE_BLOCK = 28,
170
+ SVELTE_BRANCH = 29,
171
+ MUSTACHE = 4,
172
+ DIRECTIVE_INLINE = 30,
173
+ DIRECTIVE_LEAF = 31,
174
+ DIRECTIVE_CONTAINER = 32,
175
+ FRONTMATTER = 33,
176
+ IMPORT_STATEMENT = 34,
177
+ DIRECTIVE_LABEL = 35,
178
+ FOOTNOTE_REFERENCE = 36,
179
+ FOOTNOTE_DEFINITION = 37,
180
+ DEFINITION = 38
181
+ }
182
+ export declare const K_ROOT = K.ROOT;
183
+ export declare const K_TEXT = K.TEXT;
184
+ export declare const K_HTML = K.HTML;
185
+ export declare const K_HEADING = K.HEADING;
186
+ export declare const K_CODE_FENCE = K.CODE_FENCE;
187
+ export declare const K_LINE_BREAK = K.LINE_BREAK;
188
+ export declare const K_PARAGRAPH = K.PARAGRAPH;
189
+ export declare const K_CODE_SPAN = K.CODE_SPAN;
190
+ export declare const K_EMPHASIS = K.EMPHASIS;
191
+ export declare const K_STRONG = K.STRONG;
192
+ export declare const K_THEMATIC_BREAK = K.THEMATIC_BREAK;
193
+ export declare const K_LINK = K.LINK;
194
+ export declare const K_IMAGE = K.IMAGE;
195
+ export declare const K_BLOCK_QUOTE = K.BLOCK_QUOTE;
196
+ export declare const K_LIST = K.LIST;
197
+ export declare const K_LIST_ITEM = K.LIST_ITEM;
198
+ export declare const K_HARD_BREAK = K.HARD_BREAK;
199
+ export declare const K_SOFT_BREAK = K.SOFT_BREAK;
200
+ export declare const K_STRIKETHROUGH = K.STRIKETHROUGH;
201
+ export declare const K_SUPERSCRIPT = K.SUPERSCRIPT;
202
+ export declare const K_SUBSCRIPT = K.SUBSCRIPT;
203
+ export declare const K_TABLE = K.TABLE;
204
+ export declare const K_TABLE_HEADER = K.TABLE_HEADER;
205
+ export declare const K_TABLE_ROW = K.TABLE_ROW;
206
+ export declare const K_TABLE_CELL = K.TABLE_CELL;
207
+ export declare const K_HTML_COMMENT = K.HTML_COMMENT;
208
+ export declare const K_SVELTE_TAG = K.SVELTE_TAG;
209
+ export declare const K_SVELTE_BLOCK = K.SVELTE_BLOCK;
210
+ export declare const K_SVELTE_BRANCH = K.SVELTE_BRANCH;
211
+ export declare const K_MUSTACHE = K.MUSTACHE;
212
+ export declare const K_DIRECTIVE_INLINE = K.DIRECTIVE_INLINE;
213
+ export declare const K_DIRECTIVE_LEAF = K.DIRECTIVE_LEAF;
214
+ export declare const K_DIRECTIVE_CONTAINER = K.DIRECTIVE_CONTAINER;
215
+ export declare const K_FRONTMATTER = K.FRONTMATTER;
216
+ export declare const K_IMPORT_STATEMENT = K.IMPORT_STATEMENT;
217
+ export declare const K_DIRECTIVE_LABEL = K.DIRECTIVE_LABEL;
218
+ export declare const NONE = Slot.NONE;
219
+ declare function emit_record(sink: MapSink, out_start: number, out_end: number, src_start: number, src_end: number, node_index: number, code: number): void;
220
+ declare function render_children(c: Cursor, sink?: MapSink): void;
221
+ /** render a single node at the current cursor position. */
222
+ declare function render_node(c: Cursor, sink?: MapSink): void;
223
+ /** a directive the compile cannot render, start indexes the source */
224
+ export declare class DirectiveError extends Error {
225
+ readonly directive: string;
226
+ readonly start: number;
227
+ readonly line: number;
228
+ readonly column: number;
229
+ constructor(c: Cursor, message: string);
230
+ }
231
+ /** generated offset of each out chunk, plus the end offset */
232
+ declare function out_offsets(out: string[], scratch?: Uint32Array): Uint32Array;
233
+ export declare const escape: typeof escape_html;
234
+ export declare const escape_text: typeof escape_node_text;
235
+ export declare const _escape_code: typeof escape_code;
236
+ export declare const _escape_code_text: typeof escape_code_text;
237
+ export declare const _escape_text_html: typeof escape_text_html;
238
+ export declare const _emit: typeof emit_record;
239
+ export declare const _children: typeof render_children;
240
+ export declare const _node: typeof render_node;
241
+ export declare const _out_offsets: typeof out_offsets;
242
+ /** @internal the trace of a sink filled by out chunk index */
243
+ export declare function _capture_trace(sink: MapSink, out: string[]): MapTrace;
244
+ /** @internal the mappings of a sink filled by out chunk index */
245
+ export declare function _resolve_mappings(out: string[], sink: MapSink): Mapping<MappingData>[];
246
+ /** wraps a document, the template module and whether the metadata export spreads into it */
247
+ export interface TemplateWrapper {
248
+ /** emitted verbatim as the import specifier */
249
+ specifier: string;
250
+ /** true spreads the module script metadata export as props */
251
+ metadata: boolean;
252
+ /** a module script export passed as the prop of its name, after the metadata */
253
+ data?: string;
254
+ }
255
+ /** the idx of the block that holds the footnotes section, which is no node */
256
+ export declare const FOOTNOTES_BLOCK = -1;
257
+ export interface CursorBlockEntry {
258
+ /** node buffer index, use as keyed each key, FOOTNOTES_BLOCK for the footnotes section */
259
+ idx: number;
260
+ /** rendered html string. */
261
+ html: string;
262
+ }
263
+ /**
264
+ * incremental html renderer using the cursor over node buffers
265
+ *
266
+ * same caching strategy as htmlrenderer: walks root's children,
267
+ * skips closed+cached blocks, re-renders only open blocks.
268
+ * but uses cursor traversal, zero per-node allocations per render.
269
+ *
270
+ * usage:
271
+ *
272
+ * const tree = new treebuilder(128);
273
+ * const parser = new pfmparser(tree);
274
+ * const renderer = new cursorhtmlrenderer();
275
+ *
276
+ * parser.init();
277
+ * parser.feed(chunk);
278
+ * renderer.update(tree.get_buffer(), accumulated_source);
279
+ * // renderer.blocks has stable {idx, html} entries
280
+ */
281
+ export declare class CursorHTMLRenderer {
282
+ blocks: CursorBlockEntry[];
283
+ /** full document html (available after update, whether cached or not). */
284
+ html: string;
285
+ private closed;
286
+ private cursor;
287
+ private cache;
288
+ /** unused, always empty */
289
+ private out;
290
+ /**
291
+ * element replacements for the next renders, closest scope first, the
292
+ * cached render of update ignores it as a replacement needs a document import
293
+ */
294
+ scope: ComponentScope | null;
295
+ /** also replace the lowercase elements the author typed, component_mode all */
296
+ replace_typed: boolean;
297
+ /** elements the last render with a scope kept for a directive, a render without one stores nothing */
298
+ get warnings(): readonly ReplaceWarning[];
299
+ /** directive replacements, a namespace apart from scope */
300
+ directives: ComponentScope | null;
301
+ /** highlights fences and code spans with a #! hint, null renders them plain */
302
+ highlight: CodeHighlighter | null;
303
+ /** title, caption and meta props for a pre replacement rendered plain, null gives it none */
304
+ pre_meta: PreMeta | null;
305
+ /**
306
+ * a directive no scope replaces throws a DirectiveError, otherwise it
307
+ * renders as its children, which suits a preview
308
+ */
309
+ strict_directives: boolean;
310
+ /**
311
+ * the template the next render wraps the document in, hoisting its scripts,
312
+ * styles and top level svelte elements, that render clears it, the cached
313
+ * render of update ignores it
314
+ */
315
+ template?: TemplateWrapper;
316
+ /**
317
+ * import statements the next renders add to the instance script, with or
318
+ * without a replacement, the cached render of update ignores them
319
+ */
320
+ imports?: readonly string[];
321
+ /** replaces the src of images and the href of links in the next renders, unset keeps every url */
322
+ urls?: UrlLookup;
323
+ /**
324
+ * the fences of the next renders that take their code, info string or pre
325
+ * props from elsewhere, unset reads every fence from the source
326
+ */
327
+ fences?: FenceSources;
328
+ /**
329
+ * curly quotes, dashes and ellipses in the prose of the next renders, unset
330
+ * leaves text as written, as does a text node that holds a stored string
331
+ */
332
+ smart_punctuation?: SmartPunctuation;
333
+ /**
334
+ * the footnotes of the next renders with their numbers, unset numbers the
335
+ * references of a document as its labels are first met
336
+ */
337
+ footnotes?: FootnoteRender;
338
+ /** the cached render met a footnote, so each update numbers them again */
339
+ private noted;
340
+ constructor(opts?: {
341
+ cache?: boolean;
342
+ });
343
+ /**
344
+ * @param code code the module script starts with, it goes into the top
345
+ * level module script or a new one in place of the frontmatter
346
+ */
347
+ update(buf: NodeBuffer, source: string, code?: string): CursorBlockEntry[];
348
+ /** the cached render, block by block */
349
+ private update_blocks;
350
+ /** trace renders with no syntax records, see tr_node */
351
+ private render_mapped;
352
+ /**
353
+ * a render wrapped in the template, kept apart so a plain render pays one
354
+ * check, sink null for the fold walk
355
+ */
356
+ private render_wrapped;
357
+ /**
358
+ * render with source mapping. always full render (no caching).
359
+ * collapsed moves the source offsets onto raw
360
+ */
361
+ update_mapped(buf: NodeBuffer, source: string, collapsed?: readonly number[] | null, module_code?: string): {
362
+ blocks: CursorBlockEntry[];
363
+ mappings: Mapping<MappingData>[];
364
+ };
365
+ /**
366
+ * equals mappings_to_v3 over update_mapped when raw normalizes to source
367
+ * without collapsing any crlf, but builds no Mapping objects
368
+ */
369
+ update_v3(buf: NodeBuffer, source: string, raw: string, file?: string, module_code?: string): SourceMapV3;
370
+ /** trace_to_v3 over the trace with the same arguments equals update_v3 */
371
+ update_trace(buf: NodeBuffer, source: string, module_code?: string): MapTrace;
372
+ /** update_trace filling out instead of a new trace */
373
+ update_trace_into(buf: NodeBuffer, source: string, out: MapTrace, module_code?: string): void;
374
+ /**
375
+ * the nodes changed after their block was rendered, as the node of a
376
+ * deferred task that applied does, apply_ready gives them, each block that
377
+ * holds one renders again at the next update, a renderer without a cache
378
+ * renders every block anyway
379
+ */
380
+ invalidate(buf: NodeBuffer, nodes: readonly number[]): void;
381
+ reset(): void;
382
+ /** drop every reference to the last render so a kept renderer holds no document */
383
+ release(): void;
384
+ }
385
+ /** @internal starts a mapped render that _node appends to html, record offsets index the result */
386
+ export declare function _mapped_begin(buf: NodeBuffer, source: string, html: string, urls?: UrlLookup | null): void;
387
+ /** @internal ends the mapped render started by _mapped_begin and returns its html */
388
+ export declare function _mapped_end(): string;
389
+ /** @internal the mappings of a sink whose records hold html offsets */
390
+ export declare function _resolve_offset_mappings(sink: MapSink): Mapping<MappingData>[];
391
+ /** how the footnotes of a render are numbered and what its section says */
392
+ export interface FootnoteRender extends FootnoteIndex {
393
+ /** the accessible name of the section, Footnotes when unset */
394
+ label?: string;
395
+ /** the accessible name of a link back to a reference, {n} is its number, Back to reference {n} when unset */
396
+ back_label?: string;
397
+ }