@mdsvex/render 1.0.0 → 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.
package/src/component.ts CHANGED
@@ -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
  *
@@ -17,10 +17,43 @@
17
17
  * with fresh content.
18
18
  */
19
19
 
20
+ import { footnote_index } from '@mdsvex/parse/buf-utils';
21
+ import type { FootnoteIndex } from '@mdsvex/parse/buf-utils';
20
22
  import type { NodeBuffer } from '@mdsvex/parse/utils';
21
23
 
22
24
  const NONE = 0xffffffff;
23
25
  const K_LINE_BREAK = 6;
26
+ const K_FOOTNOTE_REFERENCE = 36;
27
+ const K_FOOTNOTE_DEFINITION = 37;
28
+
29
+ /** the idx of the block that holds the footnotes section, which is no node */
30
+ export const FOOTNOTES_BLOCK = -1;
31
+
32
+ /** true when block or a node inside it is a footnote reference or definition */
33
+ function has_footnote(buf: NodeBuffer, block: number): boolean {
34
+ let node = block;
35
+ for (;;) {
36
+ const kind = buf.kind_at(node) as number;
37
+ if (kind === K_FOOTNOTE_REFERENCE || kind === K_FOOTNOTE_DEFINITION)
38
+ return true;
39
+ const down = buf.first_child_at(node);
40
+ if (down !== NONE) {
41
+ node = down;
42
+ continue;
43
+ }
44
+ for (;;) {
45
+ if (node === block) return false;
46
+ const next = buf.next_at(node);
47
+ const parent = buf.parent_at(node);
48
+ if (next !== NONE && buf.parent_at(next) === parent) {
49
+ node = next;
50
+ break;
51
+ }
52
+ if (parent === NONE) return false;
53
+ node = parent;
54
+ }
55
+ }
56
+ }
24
57
 
25
58
  /** the child of the root that holds a node, NONE for a node out of the tree */
26
59
  export function top_block(buf: NodeBuffer, node: number): number {
@@ -33,10 +66,78 @@ export function top_block(buf: NodeBuffer, node: number): number {
33
66
  }
34
67
  }
35
68
 
69
+ /** what a highlighter knows about the fence or the code span it renders */
70
+ export interface HighlightInfo {
71
+ /** the first word of the info string, "" when the fence names none, the #! hint of a code span */
72
+ lang: string;
73
+ /** the rest of the info string, raw, "" for a code span */
74
+ meta: string;
75
+ /** true for a code span with a #! hint, false for a fence */
76
+ inline: boolean;
77
+ /** true while the code is still being written, code is then what has arrived of it */
78
+ open: boolean;
79
+ }
80
+
81
+ /**
82
+ * html for a fence, usually a single pre, or for a code span with a #! hint,
83
+ * usually a single code, null or undefined draws it plain, it must be
84
+ * synchronous and must escape the code
85
+ */
86
+ export type Highlighter = (
87
+ code: string,
88
+ info: HighlightInfo
89
+ ) => string | null | undefined;
90
+
91
+ /** what create_highlight from mdsvex/highlight returns, as the Node component reads it */
92
+ export interface HighlightConfig {
93
+ block(
94
+ code: string,
95
+ lang: string,
96
+ meta: string,
97
+ ctx: {
98
+ readonly filename: string | undefined;
99
+ unknown_language(lang: string): void;
100
+ warn(code: any, message: string): void;
101
+ readonly open?: boolean;
102
+ }
103
+ ): {
104
+ before: string;
105
+ attributes: string | null;
106
+ body: string;
107
+ after: string;
108
+ } | null;
109
+ inline?(
110
+ code: string,
111
+ lang: string,
112
+ ctx: {
113
+ readonly filename: string | undefined;
114
+ unknown_language(lang: string): void;
115
+ warn(code: any, message: string): void;
116
+ readonly open?: boolean;
117
+ }
118
+ ): { attributes: string | null; body: string } | null;
119
+ }
120
+
121
+ /** the highlight prop of the Node component, what the highlight option of compile takes */
122
+ export type Highlight = Highlighter | HighlightConfig;
123
+
124
+ /** what a math renderer knows about the latex it renders, as the MathInfo of mdsvex */
125
+ export interface MathInfo {
126
+ /** true for a math fence, false for inline math */
127
+ display: boolean;
128
+ /** the info string of the fence after math, '' for inline math */
129
+ meta: string;
130
+ /** undefined in a stream, it has no file */
131
+ filename?: string;
132
+ }
133
+
134
+ /** the math prop of the Node component, what the math option of compile takes */
135
+ export type MathRenderer = (latex: string, info: MathInfo) => string;
136
+
36
137
  // block entry
37
138
 
38
139
  export interface ComponentBlock {
39
- /** Buffer index, use as Svelte each key. */
140
+ /** Buffer index, use as Svelte each key, FOOTNOTES_BLOCK for the footnotes section. */
40
141
  idx: number;
41
142
  /** Version counter, bumped on each update while the block is open. */
42
143
  v: number;
@@ -50,7 +151,13 @@ export class ComponentRenderer {
50
151
  buf: NodeBuffer | null = null;
51
152
  /** The current source/text string (for passing to Node.svelte). */
52
153
  source = '';
154
+ /** numbered by first reference, null with none, a new object at each update, pass it to every Node */
155
+ footnotes: FootnoteIndex | null = null;
53
156
  private closed: Set<number> = new Set();
157
+ /** a block held a footnote, so each update numbers them again */
158
+ private noted = false;
159
+ private section_v = 0;
160
+ private section_key = '';
54
161
 
55
162
  update(buf: NodeBuffer, source: string): ComponentBlock[] {
56
163
  this.buf = buf;
@@ -59,6 +166,14 @@ export class ComponentRenderer {
59
166
  let block_idx = 0;
60
167
  let changed = false;
61
168
 
169
+ const last = this.blocks.length - 1;
170
+ const section =
171
+ last >= 0 && this.blocks[last].idx === FOOTNOTES_BLOCK
172
+ ? this.blocks.pop()!
173
+ : null;
174
+ // a definition in a block drawn again draws the section again
175
+ const drawn: number[] = [];
176
+
62
177
  // walk root's children via sibling chain
63
178
  let child = buf.first_child_at(0);
64
179
  while (child !== NONE) {
@@ -68,10 +183,19 @@ export class ComponentRenderer {
68
183
 
69
184
  if (kind !== K_LINE_BREAK) {
70
185
  const closed = buf.end_at(child) !== NONE;
186
+ if (!this.closed.has(child)) {
187
+ drawn.push(child);
188
+ if (!this.noted) this.noted = has_footnote(buf, child);
189
+ }
71
190
 
72
191
  if (block_idx >= this.blocks.length) {
73
192
  this.blocks.push({ idx: child, v: 0 });
74
193
  changed = true;
194
+ } else if (this.blocks[block_idx].idx !== child) {
195
+ // the parser took a guess back, as the paragraph it opens for a
196
+ // tag that turns out to stand alone, another node has its place
197
+ this.blocks[block_idx] = { idx: child, v: 0 };
198
+ changed = true;
75
199
  } else if (!this.closed.has(child)) {
76
200
  this.blocks[block_idx].v++;
77
201
  changed = true;
@@ -87,6 +211,40 @@ export class ComponentRenderer {
87
211
  child = is_sibling ? next : NONE;
88
212
  }
89
213
 
214
+ // a guess taken back can leave the root with fewer children
215
+ if (block_idx < this.blocks.length) {
216
+ this.blocks.length = block_idx;
217
+ changed = true;
218
+ }
219
+
220
+ if (this.noted) {
221
+ const notes = (this.footnotes = footnote_index(buf, true));
222
+ let key = '';
223
+ let stale = false;
224
+ if (notes !== null) {
225
+ const definitions = notes.definitions;
226
+ for (let i = 0; i < definitions.length; i++) {
227
+ const node = definitions[i];
228
+ if (node === NONE) continue;
229
+ key += `${i} ${node} ${notes.counts[i]} ${notes.labels[i]}\n`;
230
+ if (drawn.includes(top_block(buf, node))) stale = true;
231
+ }
232
+ }
233
+ if (key !== this.section_key) {
234
+ this.section_key = key;
235
+ stale = true;
236
+ }
237
+ if (key === '') {
238
+ if (section !== null) changed = true;
239
+ } else if (section !== null && !stale) {
240
+ this.blocks.push(section);
241
+ } else {
242
+ if (section !== null) this.section_v++;
243
+ this.blocks.push({ idx: FOOTNOTES_BLOCK, v: this.section_v });
244
+ changed = true;
245
+ }
246
+ }
247
+
90
248
  return changed ? (this.blocks = [...this.blocks]) : this.blocks;
91
249
  }
92
250
 
@@ -107,5 +265,9 @@ export class ComponentRenderer {
107
265
  this.buf = null;
108
266
  this.source = '';
109
267
  this.closed.clear();
268
+ this.footnotes = null;
269
+ this.noted = false;
270
+ this.section_v = 0;
271
+ this.section_key = '';
110
272
  }
111
273
  }
@@ -302,6 +302,11 @@ function escape_code(text: string): string {
302
302
  return text.replace(CODE_MATCH, code_replace);
303
303
  }
304
304
 
305
+ /** escape_code of fence or code span text, braces stay when code_braces is false */
306
+ function escape_code_string(text: string): string {
307
+ return esc_brace_from === -1 ? escape_code(text) : escape_html(text);
308
+ }
309
+
305
310
  const BRACE_TEST = /[{}]/;
306
311
  const BRACE_MATCH = /[{}]/g;
307
312
 
@@ -356,6 +361,9 @@ let esc_quot = -1;
356
361
  // only code reads the brace pointers, so prose never scans for braces
357
362
  let esc_lbrace = -1;
358
363
  let esc_rbrace = -1;
364
+ // a reset puts the brace pointers here, BRACES_KEPT is past the end of any source so no brace is ever met
365
+ let esc_brace_from = -1;
366
+ const BRACES_KEPT = 0x3fffffff;
359
367
  // false for a buffer with no prebuilt strings, so text skips the lookup
360
368
  let esc_prebuilt = true;
361
369
  // only nodes whose bit is set hold a prebuilt string, a late repair can turn
@@ -390,8 +398,14 @@ function esc_reset(src: string): void {
390
398
  esc_lt = -1;
391
399
  esc_gt = -1;
392
400
  esc_quot = -1;
393
- esc_lbrace = -1;
394
- esc_rbrace = -1;
401
+ esc_lbrace = esc_brace_from;
402
+ esc_rbrace = esc_brace_from;
403
+ }
404
+
405
+ function braces_kept(): void {
406
+ esc_brace_from = BRACES_KEPT;
407
+ esc_lbrace = BRACES_KEPT;
408
+ esc_rbrace = BRACES_KEPT;
395
409
  }
396
410
 
397
411
  function esc_next(ch: string, from: number): number {
@@ -424,11 +438,12 @@ function held_text(c: Cursor): string {
424
438
 
425
439
  /** equals escape_code of fence_text */
426
440
  function escape_fence_text(c: Cursor): string {
427
- if (fs !== null) return escape_code(fence_text_from(c));
428
- if (holds(c)) return escape_code(buf_code(c.buffer, c.index, c.source));
441
+ if (fs !== null) return escape_code_string(fence_text_from(c));
442
+ if (holds(c))
443
+ return escape_code_string(buf_code(c.buffer, c.index, c.source));
429
444
  // a fence in a block quote or a list item holds its code as a string, without what they put before its lines
430
445
  const text = c.prebuilt;
431
- return text === undefined ? escape_code_text(c) : escape_code(text);
446
+ return text === undefined ? escape_code_text(c) : escape_code_string(text);
432
447
  }
433
448
 
434
449
  /** equals escape_code of c.text, reading source slices through the escape index */
@@ -438,7 +453,8 @@ function escape_code_text(c: Cursor): string {
438
453
 
439
454
  /** equals escape_code of the held_text of a code span */
440
455
  function escape_span_text(c: Cursor): string {
441
- if (holds(c)) return escape_code(buf_code(c.buffer, c.index, c.source));
456
+ if (holds(c))
457
+ return escape_code_string(buf_code(c.buffer, c.index, c.source));
442
458
  return escape_code_text(c);
443
459
  }
444
460
 
@@ -542,8 +558,9 @@ function escape_text_at(
542
558
 
543
559
  /** a stored string, apart so escape_text_at stays small enough for v8 to inline */
544
560
  function stored_text(c: Cursor, i: number, s: string, code: boolean): string {
561
+ if (code) return escape_code_string(s);
545
562
  // text a plugin wrote is all characters, a brace or a reference too
546
- return code || (c.words[i * W.stride + W.pending] & 2) !== 0
563
+ return (c.words[i * W.stride + W.pending] & 2) !== 0
547
564
  ? escape_code(s)
548
565
  : escape_text_html(s);
549
566
  }
@@ -5888,6 +5905,12 @@ export class CursorHTMLRenderer {
5888
5905
  * directive with none renders live, the cached render of update ignores it
5889
5906
  */
5890
5907
  declare shells?: ShellTable;
5908
+ /**
5909
+ * false leaves the braces of plain fences and code spans as written, for
5910
+ * html that svelte never reads, where a brace is text, unset or true
5911
+ * writes them as entities, a highlighter escapes its own output
5912
+ */
5913
+ declare code_braces?: boolean;
5891
5914
  /** the cached render met a footnote, so each update numbers them again */
5892
5915
  private noted = false;
5893
5916
 
@@ -5913,6 +5936,7 @@ export class CursorHTMLRenderer {
5913
5936
  // ahead of the rest, it throws for an option it can not read
5914
5937
  const smart = this.smart_punctuation;
5915
5938
  if (smart !== undefined) sp_begin(smart);
5939
+ if (this.code_braces === false) braces_kept();
5916
5940
  hl = this.highlight;
5917
5941
  pm = this.pre_meta;
5918
5942
  const urls = this.urls;
@@ -5962,6 +5986,7 @@ export class CursorHTMLRenderer {
5962
5986
  if (smart !== undefined) sp_end();
5963
5987
  esc_prebuilt = true;
5964
5988
  esc_bits = null;
5989
+ esc_brace_from = -1;
5965
5990
  if (dir_strict) dir_strict = false;
5966
5991
  if (ws_on) ws_on = false;
5967
5992
  if (comp_scope !== null) comp_end();
@@ -5983,6 +6008,7 @@ export class CursorHTMLRenderer {
5983
6008
  if (urls !== undefined) ul = null;
5984
6009
  if (fences !== undefined) fs = null;
5985
6010
  if (smart !== undefined) sp_end();
6011
+ esc_brace_from = -1;
5986
6012
  if (code) module_end();
5987
6013
  }
5988
6014
  return this.blocks;
@@ -6052,6 +6078,7 @@ export class CursorHTMLRenderer {
6052
6078
  // ahead of the rest, it throws for an option it can not read
6053
6079
  const smart = this.smart_punctuation;
6054
6080
  if (smart !== undefined) sp_begin(smart);
6081
+ if (this.code_braces === false) braces_kept();
6055
6082
  hl = this.highlight;
6056
6083
  pm = this.pre_meta;
6057
6084
  const urls = this.urls;
@@ -6107,6 +6134,7 @@ export class CursorHTMLRenderer {
6107
6134
  if (smart !== undefined) sp_end();
6108
6135
  esc_prebuilt = true;
6109
6136
  esc_bits = null;
6137
+ esc_brace_from = -1;
6110
6138
  if (dir_strict) dir_strict = false;
6111
6139
  if (ws_on) ws_on = false;
6112
6140
  if (comp_scope !== null) comp_end();
@@ -6186,6 +6214,7 @@ export class CursorHTMLRenderer {
6186
6214
  if (sp_on) sp_end();
6187
6215
  esc_prebuilt = true;
6188
6216
  esc_bits = null;
6217
+ esc_brace_from = -1;
6189
6218
  if (dir_strict) dir_strict = false;
6190
6219
  if (ws_on) ws_on = false;
6191
6220
  if (comp_scope !== null) comp_end();
@@ -6996,7 +7025,7 @@ function escape_past(
6996
7025
  ): string {
6997
7026
  if (sp_on)
6998
7027
  return code
6999
- ? escape_code(c.source.slice(vs, ve))
7028
+ ? escape_code_string(c.source.slice(vs, ve))
7000
7029
  : smart_text(c, i, vs, ve);
7001
7030
  // a value cut short by the source, as a fed document has
7002
7031
  return esc_len <= vs ? '' : escape_text_at(c, i, vs, esc_len, code);
@@ -7910,6 +7939,7 @@ function ws_static(s: string): boolean {
7910
7939
  i = lt + 2;
7911
7940
  continue;
7912
7941
  }
7942
+ const first = ch;
7913
7943
  // a capital, a dot or a colon in the name is a component or a svelte element
7914
7944
  for (; j < len; j++) {
7915
7945
  ch = s.charCodeAt(j);
@@ -7918,8 +7948,32 @@ function ws_static(s: string): boolean {
7918
7948
  break;
7919
7949
  }
7920
7950
  if (j === lt + 1) return false;
7921
- const name = s.slice(lt + 1, j);
7922
- if (name === 'script' || name === 'style' || name === 'slot') return false;
7951
+ // matching script, style and slot by code avoids slicing every opening tag
7952
+ if (first === 115) {
7953
+ const tag_len = j - lt - 1;
7954
+ if (
7955
+ (tag_len === 4 &&
7956
+ s.charCodeAt(lt + 2) === 108 &&
7957
+ s.charCodeAt(lt + 3) === 111 &&
7958
+ s.charCodeAt(lt + 4) === 116) ||
7959
+ (tag_len === 5 &&
7960
+ s.charCodeAt(lt + 2) === 116 &&
7961
+ s.charCodeAt(lt + 3) === 121 &&
7962
+ s.charCodeAt(lt + 4) === 108 &&
7963
+ s.charCodeAt(lt + 5) === 101) ||
7964
+ (tag_len === 6 &&
7965
+ s.charCodeAt(lt + 2) === 99 &&
7966
+ s.charCodeAt(lt + 3) === 114 &&
7967
+ s.charCodeAt(lt + 4) === 105 &&
7968
+ s.charCodeAt(lt + 5) === 112 &&
7969
+ s.charCodeAt(lt + 6) === 116)
7970
+ )
7971
+ return false;
7972
+ }
7973
+ if (ch === 62) {
7974
+ i = j + 1;
7975
+ continue;
7976
+ }
7923
7977
  // the attributes, a colon outside a value is a directive, a brace anywhere an expression
7924
7978
  let quote = 0;
7925
7979
  for (; j < len; j++) {
@@ -7929,7 +7983,10 @@ function ws_static(s: string): boolean {
7929
7983
  if (ch === quote) quote = 0;
7930
7984
  } else if (ch === 34 || ch === 39) quote = ch;
7931
7985
  else if (ch === 58) return false;
7932
- else if (ch === 62) break;
7986
+ else if (ch === 62) {
7987
+ j++;
7988
+ break;
7989
+ }
7933
7990
  }
7934
7991
  i = j;
7935
7992
  }
package/src/index.ts CHANGED
@@ -28,7 +28,13 @@ export {
28
28
  K_IMPORT_STATEMENT,
29
29
  } from './html_cursor.js';
30
30
  export { ComponentRenderer } from './component.js';
31
- export type { ComponentBlock } from './component.js';
31
+ export type {
32
+ ComponentBlock,
33
+ Highlight,
34
+ HighlightConfig,
35
+ Highlighter,
36
+ HighlightInfo,
37
+ } from './component.js';
32
38
  export { CI_TEXT, CI_CODE, CI_SVELTE, CI_STRUCTURE } from './mappings.js';
33
39
  export type {
34
40
  Mapping,
package/src/sourcemap.ts CHANGED
@@ -617,15 +617,21 @@ function encode_mappings(
617
617
  source: string,
618
618
  generated: string
619
619
  ): string {
620
- fill_line_starts(src_table, source, PAST_END, PAST_END);
621
- fill_line_starts(gen_table, generated, PAST_END, PAST_END);
622
-
623
620
  collect_spans(mappings);
621
+ if (span_count === 0) return '';
624
622
  sort_spans();
625
623
  if (runs_overlap()) {
626
624
  collect_char_spans(mappings);
627
625
  sort_spans();
628
626
  }
627
+ const last = span_order[span_count - 1];
628
+ fill_line_starts(src_table, source, PAST_END, PAST_END);
629
+ fill_line_starts(
630
+ gen_table,
631
+ generated,
632
+ PAST_END,
633
+ span_gen[last] + span_len[last] - 1
634
+ );
629
635
  return encode_spans();
630
636
  }
631
637
 
@@ -637,15 +643,21 @@ function encode_records(
637
643
  source: string,
638
644
  generated: string
639
645
  ): string {
640
- fill_line_starts(src_table, source, PAST_END, PAST_END);
641
- fill_line_starts(gen_table, generated, PAST_END, PAST_END);
642
-
643
646
  collect_record_spans(rec, start, end);
647
+ if (span_count === 0) return '';
644
648
  sort_spans();
645
649
  if (runs_overlap()) {
646
650
  collect_record_char_spans(rec, start, end);
647
651
  sort_spans();
648
652
  }
653
+ const last = span_order[span_count - 1];
654
+ fill_line_starts(src_table, source, PAST_END, PAST_END);
655
+ fill_line_starts(
656
+ gen_table,
657
+ generated,
658
+ PAST_END,
659
+ span_gen[last] + span_len[last] - 1
660
+ );
649
661
  return encode_spans();
650
662
  }
651
663
 
@@ -656,15 +668,21 @@ function encode_trace(
656
668
  source: string,
657
669
  generated: string
658
670
  ): string {
659
- fill_line_starts(src_table, source, PAST_END, PAST_END);
660
- fill_line_starts(gen_table, generated, PAST_END, PAST_END);
661
-
662
671
  collect_trace_spans(rec, start, end);
672
+ if (span_count === 0) return '';
663
673
  sort_spans();
664
674
  if (runs_overlap()) {
665
675
  collect_trace_char_spans(rec, start, end);
666
676
  sort_spans();
667
677
  }
678
+ const last = span_order[span_count - 1];
679
+ fill_line_starts(src_table, source, PAST_END, PAST_END);
680
+ fill_line_starts(
681
+ gen_table,
682
+ generated,
683
+ PAST_END,
684
+ span_gen[last] + span_len[last] - 1
685
+ );
668
686
  return encode_spans();
669
687
  }
670
688