@mdsvex/render 1.0.0-next.5 → 1.1.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.
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * {#each renderer.blocks as block (block.idx)}
10
10
  * {#key block.v}
11
- * <Node buf={renderer.buf} idx={block.idx} source={renderer.source} />
11
+ * <Node buf={renderer.buf} idx={block.idx} source={renderer.source} footnotes={renderer.footnotes} />
12
12
  * {/key}
13
13
  * {/each}
14
14
  *
@@ -16,11 +16,67 @@
16
16
  * Open (streaming) blocks get bumped, {#key} recreates the subtree
17
17
  * with fresh content.
18
18
  */
19
+ import type { FootnoteIndex } from '@mdsvex/parse/buf-utils';
19
20
  import type { NodeBuffer } from '@mdsvex/parse/utils';
21
+ /** the idx of the block that holds the footnotes section, which is no node */
22
+ export declare const FOOTNOTES_BLOCK = -1;
20
23
  /** the child of the root that holds a node, NONE for a node out of the tree */
21
24
  export declare function top_block(buf: NodeBuffer, node: number): number;
25
+ /** what a highlighter knows about the fence or the code span it renders */
26
+ export interface HighlightInfo {
27
+ /** the first word of the info string, "" when the fence names none, the #! hint of a code span */
28
+ lang: string;
29
+ /** the rest of the info string, raw, "" for a code span */
30
+ meta: string;
31
+ /** true for a code span with a #! hint, false for a fence */
32
+ inline: boolean;
33
+ /** true while the code is still being written, code is then what has arrived of it */
34
+ open: boolean;
35
+ }
36
+ /**
37
+ * html for a fence, usually a single pre, or for a code span with a #! hint,
38
+ * usually a single code, null or undefined draws it plain, it must be
39
+ * synchronous and must escape the code
40
+ */
41
+ export type Highlighter = (code: string, info: HighlightInfo) => string | null | undefined;
42
+ /** what create_highlight from mdsvex/highlight returns, as the Node component reads it */
43
+ export interface HighlightConfig {
44
+ block(code: string, lang: string, meta: string, ctx: {
45
+ readonly filename: string | undefined;
46
+ unknown_language(lang: string): void;
47
+ warn(code: any, message: string): void;
48
+ readonly open?: boolean;
49
+ }): {
50
+ before: string;
51
+ attributes: string | null;
52
+ body: string;
53
+ after: string;
54
+ } | null;
55
+ inline?(code: string, lang: string, ctx: {
56
+ readonly filename: string | undefined;
57
+ unknown_language(lang: string): void;
58
+ warn(code: any, message: string): void;
59
+ readonly open?: boolean;
60
+ }): {
61
+ attributes: string | null;
62
+ body: string;
63
+ } | null;
64
+ }
65
+ /** the highlight prop of the Node component, what the highlight option of compile takes */
66
+ export type Highlight = Highlighter | HighlightConfig;
67
+ /** what a math renderer knows about the latex it renders, as the MathInfo of mdsvex */
68
+ export interface MathInfo {
69
+ /** true for a math fence, false for inline math */
70
+ display: boolean;
71
+ /** the info string of the fence after math, '' for inline math */
72
+ meta: string;
73
+ /** undefined in a stream, it has no file */
74
+ filename?: string;
75
+ }
76
+ /** the math prop of the Node component, what the math option of compile takes */
77
+ export type MathRenderer = (latex: string, info: MathInfo) => string;
22
78
  export interface ComponentBlock {
23
- /** Buffer index, use as Svelte each key. */
79
+ /** Buffer index, use as Svelte each key, FOOTNOTES_BLOCK for the footnotes section. */
24
80
  idx: number;
25
81
  /** Version counter, bumped on each update while the block is open. */
26
82
  v: number;
@@ -31,7 +87,13 @@ export declare class ComponentRenderer {
31
87
  buf: NodeBuffer | null;
32
88
  /** The current source/text string (for passing to Node.svelte). */
33
89
  source: string;
90
+ /** numbered by first reference, null with none, a new object at each update, pass it to every Node */
91
+ footnotes: FootnoteIndex | null;
34
92
  private closed;
93
+ /** a block held a footnote, so each update numbers them again */
94
+ private noted;
95
+ private section_v;
96
+ private section_key;
35
97
  update(buf: NodeBuffer, source: string): ComponentBlock[];
36
98
  /**
37
99
  * the nodes changed after their block closed, as the node of a deferred
package/dist/component.js CHANGED
@@ -1,45 +1,86 @@
1
- function N(o, t) {
2
- if (t === 0) return 4294967295;
1
+ import { footnote_index as x } from "@mdsvex/parse/buf-utils";
2
+ const o = 4294967295, O = 6, E = 36, v = 37, p = -1;
3
+ function N(l, t) {
4
+ let s = t;
3
5
  for (; ; ) {
4
- const e = o.parent_at(t);
5
- if (e === 0) return t;
6
- if (e === 4294967295) return 4294967295;
7
- t = e;
6
+ const i = l.kind_at(s);
7
+ if (i === E || i === v)
8
+ return !0;
9
+ const n = l.first_child_at(s);
10
+ if (n !== o) {
11
+ s = n;
12
+ continue;
13
+ }
14
+ for (; ; ) {
15
+ if (s === t) return !1;
16
+ const r = l.next_at(s), c = l.parent_at(s);
17
+ if (r !== o && l.parent_at(r) === c) {
18
+ s = r;
19
+ break;
20
+ }
21
+ if (c === o) return !1;
22
+ s = c;
23
+ }
24
+ }
25
+ }
26
+ function b(l, t) {
27
+ if (t === 0) return o;
28
+ for (; ; ) {
29
+ const s = l.parent_at(t);
30
+ if (s === 0) return t;
31
+ if (s === o) return o;
32
+ t = s;
8
33
  }
9
34
  }
10
- class a {
35
+ class T {
11
36
  constructor() {
12
- this.blocks = [], this.buf = null, this.source = "", this.closed = /* @__PURE__ */ new Set();
37
+ this.blocks = [], this.buf = null, this.source = "", this.footnotes = null, this.closed = /* @__PURE__ */ new Set(), this.noted = !1, this.section_v = 0, this.section_key = "";
13
38
  }
14
- update(t, e) {
15
- this.buf = t, this.source = e;
16
- let i = 0, l = !1, s = t.first_child_at(0);
17
- for (; s !== 4294967295; ) {
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++;
39
+ update(t, s) {
40
+ this.buf = t, this.source = s;
41
+ let i = 0, n = !1;
42
+ const r = this.blocks.length - 1, c = r >= 0 && this.blocks[r].idx === p ? this.blocks.pop() : null, k = [];
43
+ let e = t.first_child_at(0);
44
+ for (; e !== o; ) {
45
+ const f = t.kind_at(e), h = t.next_at(e), d = h !== o && t.parent_at(h) === 0;
46
+ if (f !== O) {
47
+ const _ = t.end_at(e) !== o;
48
+ this.closed.has(e) || (k.push(e), this.noted || (this.noted = N(t, e))), i >= this.blocks.length ? (this.blocks.push({ idx: e, v: 0 }), n = !0) : this.blocks[i].idx !== e ? (this.blocks[i] = { idx: e, v: 0 }, n = !0) : this.closed.has(e) || (this.blocks[i].v++, n = !0), _ && !this.closed.has(e) && this.closed.add(e), i++;
49
+ }
50
+ e = d ? h : o;
51
+ }
52
+ if (i < this.blocks.length && (this.blocks.length = i, n = !0), this.noted) {
53
+ const f = this.footnotes = x(t, !0);
54
+ let h = "", d = !1;
55
+ if (f !== null) {
56
+ const _ = f.definitions;
57
+ for (let a = 0; a < _.length; a++) {
58
+ const u = _[a];
59
+ u !== o && (h += `${a} ${u} ${f.counts[a]} ${f.labels[a]}
60
+ `, k.includes(b(t, u)) && (d = !0));
61
+ }
22
62
  }
23
- s = h ? c : 4294967295;
63
+ h !== this.section_key && (this.section_key = h, d = !0), h === "" ? c !== null && (n = !0) : c !== null && !d ? this.blocks.push(c) : (c !== null && this.section_v++, this.blocks.push({ idx: p, v: this.section_v }), n = !0);
24
64
  }
25
- return l ? this.blocks = [...this.blocks] : this.blocks;
65
+ return n ? this.blocks = [...this.blocks] : this.blocks;
26
66
  }
27
67
  /**
28
68
  * the nodes changed after their block closed, as the node of a deferred
29
69
  * task that applied does, apply_ready gives them, each block that holds
30
70
  * one renders again at the next update
31
71
  */
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);
72
+ invalidate(t, s) {
73
+ for (let i = 0; i < s.length; i++) {
74
+ const n = b(t, s[i]);
75
+ n !== o && this.closed.delete(n);
36
76
  }
37
77
  }
38
78
  reset() {
39
- this.blocks = [], this.buf = null, this.source = "", this.closed.clear();
79
+ this.blocks = [], this.buf = null, this.source = "", this.closed.clear(), this.footnotes = null, this.noted = !1, this.section_v = 0, this.section_key = "";
40
80
  }
41
81
  }
42
82
  export {
43
- a as ComponentRenderer,
44
- N as top_block
83
+ T as ComponentRenderer,
84
+ p as FOOTNOTES_BLOCK,
85
+ b as top_block
45
86
  };
@@ -372,6 +372,12 @@ export declare class CursorHTMLRenderer {
372
372
  * directive with none renders live, the cached render of update ignores it
373
373
  */
374
374
  shells?: ShellTable;
375
+ /**
376
+ * false leaves the braces of plain fences and code spans as written, for
377
+ * html that svelte never reads, where a brace is text, unset or true
378
+ * writes them as entities, a highlighter escapes its own output
379
+ */
380
+ code_braces?: boolean;
375
381
  /** the cached render met a footnote, so each update numbers them again */
376
382
  private noted;
377
383
  constructor(opts?: {