@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.
@@ -12,14 +12,22 @@
12
12
  */
13
13
 
14
14
  import { Cursor } from '@mdsvex/parse/cursor';
15
+ import { is_raw_text_tag } from '@mdsvex/parse/utils';
16
+ import {
17
+ buf_code,
18
+ footnote_id,
19
+ footnote_index,
20
+ footnote_ref_id,
21
+ } from '@mdsvex/parse/buf-utils';
22
+ import type { FootnoteIndex } from '@mdsvex/parse/buf-utils';
15
23
  import type { NodeBuffer } from '@mdsvex/parse/utils';
16
24
  import {
17
25
  MapSink,
18
26
  RecordMapping,
19
27
  record_data,
20
28
  record_mappings,
21
- } from './mappings';
22
- import type { Mapping, MappingData } from './mappings';
29
+ } from './mappings.js';
30
+ import type { Mapping, MappingData } from './mappings.js';
23
31
  import {
24
32
  records_by_offset,
25
33
  reserve_trace,
@@ -28,21 +36,24 @@ import {
28
36
  trace_free_at,
29
37
  trace_room,
30
38
  trace_take,
31
- } from './sourcemap';
32
- import type { MapTrace, SourceMapV3 } from './sourcemap';
33
- import { ComponentScope, component_imports } from './scope';
34
- import type { ComponentImport, ReplaceWarning } from './scope';
35
- import { is_entity_name } from './entities';
36
-
37
- export type { Mapping, CodeInformation, MappingData } from './mappings';
38
- export { MapSink } from './mappings';
39
- export { ComponentScope, component_imports } from './scope';
39
+ } from './sourcemap.js';
40
+ import type { MapTrace, SourceMapV3 } from './sourcemap.js';
41
+ import { ComponentScope, component_imports } from './scope.js';
42
+ import type { ComponentImport, ReplaceWarning } from './scope.js';
43
+ import { is_entity_name } from './entities.js';
44
+ import { top_block } from './component.js';
45
+
46
+ export type { Mapping, CodeInformation, MappingData } from './mappings.js';
47
+ export { MapSink } from './mappings.js';
48
+ export { ComponentScope, component_imports } from './scope.js';
49
+ /** @internal a pass over the tree reads references as the renderer does */
50
+ export { is_entity_name } from './entities.js';
40
51
  export type {
41
52
  ComponentImport,
42
53
  ComponentSource,
43
54
  DefaultImport,
44
55
  ReplaceWarning,
45
- } from './scope';
56
+ } from './scope.js';
46
57
 
47
58
  /**
48
59
  * a fence the highlighter rendered, every string is markup svelte reads as
@@ -98,6 +109,16 @@ export interface CodeHighlighter {
98
109
  /** a code span with a #! hint, null renders it plain */
99
110
  inline(code: string, lang: string, start: number): HighlightedCode | null;
100
111
  warn(code: HighlightWarningCode, message: string, start: number): void;
112
+ /**
113
+ * true when block renders a math fence as display math, the fence then
114
+ * takes the math replacement and never pre, its block has no <pre>
115
+ */
116
+ readonly math?: boolean;
117
+ /**
118
+ * inline math, a code span in dollars, asked only while math is true, the
119
+ * body is the whole span and the math replacement takes it as children
120
+ */
121
+ inline_math?(code: string, start: number): HighlightedCode;
101
122
  }
102
123
 
103
124
  /** reads the fence meta conventions for a pre replacement the highlighter left plain */
@@ -109,6 +130,44 @@ export interface PreMeta {
109
130
  pre_props(meta: string, start: number): string;
110
131
  }
111
132
 
133
+ /** a string is escaped as a url of the source is, '' leaves the attribute out, an expression goes in braces */
134
+ export type UrlSource = string | { type: 'expression'; value: string };
135
+
136
+ /** what replaces the src of an image or the href of a link, url is never empty, null or undefined keeps it */
137
+ export type UrlLookup = (
138
+ url: string,
139
+ image: boolean
140
+ ) => UrlSource | null | undefined;
141
+
142
+ /** what a fence renders in place of what the document has */
143
+ export interface FenceSource {
144
+ /** the code, in place of the body of the fence */
145
+ text: string;
146
+ /** the info string in place of the written one, '' for none, undefined keeps it */
147
+ info?: string;
148
+ /** props text for a pre replacement, each as ' name={value}', after its own props */
149
+ props?: string;
150
+ }
151
+
152
+ /** the fences that take their code from elsewhere, by node index */
153
+ export type FenceSources = ReadonlyMap<number, FenceSource>;
154
+
155
+ /** the opening and closing double quote, then the opening and closing single quote */
156
+ export type SmartQuotes = readonly [string, string, string, string];
157
+
158
+ /** each kind is on unless it is false */
159
+ export interface SmartPunctuationOptions {
160
+ /** straight quotes to curly ones, four strings replace the english ones */
161
+ quotes?: boolean | SmartQuotes;
162
+ /** two hyphens to an en dash and three to an em dash */
163
+ dashes?: boolean;
164
+ /** three dots to an ellipsis */
165
+ ellipses?: boolean;
166
+ }
167
+
168
+ /** true is every kind with english quotes, false is none */
169
+ export type SmartPunctuation = boolean | SmartPunctuationOptions;
170
+
112
171
  // must equal NONE in @mdsvex/parse
113
172
  const enum Slot {
114
173
  NONE = 0xffffffff,
@@ -220,8 +279,25 @@ function escape_code(text: string): string {
220
279
  const BRACE_TEST = /[{}]/;
221
280
  const BRACE_MATCH = /[{}]/g;
222
281
 
223
- /** an info string is an attribute, braces as code has them */
282
+ let plain_math = false;
283
+
284
+ /**
285
+ * @internal true once when a math fence rendered as code since the last
286
+ * call, a compile with a math renderer and no highlighter then knows to
287
+ * render again with one, no document is searched for math up front
288
+ */
289
+ export function take_plain_math(): boolean {
290
+ if (!plain_math) return false;
291
+ plain_math = false;
292
+ return true;
293
+ }
294
+
295
+ /**
296
+ * the language of a fence that renders plain, as an attribute, braces as
297
+ * code has them, every such fence passes here so it notes a math one
298
+ */
224
299
  function escape_info(info: string): string {
300
+ if (info === 'math') plain_math = true;
225
301
  if (!CODE_TEST.test(info)) return info;
226
302
  const s = escape_html(info);
227
303
  if (!BRACE_TEST.test(s)) return s;
@@ -305,65 +381,28 @@ function escape_node_text(c: Cursor): string {
305
381
  return escape_text_at(c, c.index, c.value_start, c.value_end, false);
306
382
  }
307
383
 
308
- function bq_depth(n: Uint32Array, i: number): number {
309
- let depth = 0;
310
- for (
311
- let p = n[i * W.stride + W.parent];
312
- p !== Slot.NONE;
313
- p = n[p * W.stride + W.parent]
314
- )
315
- if ((n[p * W.stride] & 0xff) === K.BLOCK_QUOTE) depth++;
316
- return depth;
384
+ function fence_text(c: Cursor): string {
385
+ if (fs !== null) return fence_text_from(c);
386
+ return held_text(c);
317
387
  }
318
388
 
319
- /** each value line keeps its quote markers, skipped as skip_bq_markers does */
320
- function strip_bq(text: string, depth: number): string {
321
- const len = text.length;
322
- let out = '';
323
- let pos = 0;
324
- for (;;) {
325
- let mark = pos;
326
- for (let i = 0; i < depth; i++) {
327
- while (
328
- pos < len &&
329
- (text.charCodeAt(pos) === 32 || text.charCodeAt(pos) === 9)
330
- )
331
- pos++;
332
- if (pos >= len || text.charCodeAt(pos) !== 62) {
333
- // not a marker, keep the indent
334
- pos = mark;
335
- break;
336
- }
337
- pos++;
338
- if (pos < len && text.charCodeAt(pos) === 32) pos++;
339
- mark = pos;
340
- }
341
- let eol = pos;
342
- while (eol < len) {
343
- const ch = text.charCodeAt(eol);
344
- if (ch === 10) break;
345
- if (ch === 13) {
346
- if (eol + 1 < len && text.charCodeAt(eol + 1) === 10) eol++;
347
- break;
348
- }
349
- eol++;
350
- }
351
- if (eol >= len) return out + text.slice(pos);
352
- out += text.slice(pos, eol + 1);
353
- pos = eol + 1;
354
- }
389
+ /** true for a fence, a code span or a comment a plugin put nodes in, their text is its text too */
390
+ function holds(c: Cursor): boolean {
391
+ return c.words[c.index * W.stride + W.first_child] !== Slot.NONE;
355
392
  }
356
393
 
357
- function fence_text(c: Cursor): string {
358
- const depth = bq_depth(c.words, c.index);
359
- return depth === 0 ? c.text() : strip_bq(c.text(), depth);
394
+ /** c.text of a fence, a code span or a comment, and then the text of the nodes a plugin put in it */
395
+ function held_text(c: Cursor): string {
396
+ return holds(c) ? buf_code(c.buffer, c.index, c.source) : c.text();
360
397
  }
361
398
 
362
399
  /** equals escape_code of fence_text */
363
400
  function escape_fence_text(c: Cursor): string {
364
- const depth = bq_depth(c.words, c.index);
365
- if (depth === 0) return escape_code_text(c);
366
- return escape_code(strip_bq(c.text(), depth));
401
+ if (fs !== null) return escape_code(fence_text_from(c));
402
+ if (holds(c)) return escape_code(buf_code(c.buffer, c.index, c.source));
403
+ // a fence in a block quote or a list item holds its code as a string, without what they put before its lines
404
+ const text = c.prebuilt;
405
+ return text === undefined ? escape_code_text(c) : escape_code(text);
367
406
  }
368
407
 
369
408
  /** equals escape_code of c.text, reading source slices through the escape index */
@@ -371,6 +410,12 @@ function escape_code_text(c: Cursor): string {
371
410
  return escape_text_at(c, c.index, c.value_start, c.value_end, true);
372
411
  }
373
412
 
413
+ /** equals escape_code of the held_text of a code span */
414
+ function escape_span_text(c: Cursor): string {
415
+ if (holds(c)) return escape_code(buf_code(c.buffer, c.index, c.source));
416
+ return escape_code_text(c);
417
+ }
418
+
374
419
  /** m is the first of & < > " at or after vs */
375
420
  function escape_code_slice(
376
421
  src: string,
@@ -442,17 +487,15 @@ function escape_text_at(
442
487
  const bits = esc_bits;
443
488
  if (bits === null || (bits[i >>> 3] & (1 << (i & 7))) !== 0) {
444
489
  const s = c.prebuilt_at(i);
445
- if (s !== undefined) return code ? escape_code(s) : escape_text_html(s);
490
+ if (s !== undefined) return stored_text(c, i, s, code);
446
491
  }
447
492
  }
448
493
  // empty cases must match Cursor.text
449
494
  if (vs === Slot.NONE || ve === Slot.NONE || ve <= vs) return '';
450
495
  const src = c.source;
451
496
  if (src !== esc_src) esc_reset(src);
452
- if (ve > esc_len) {
453
- ve = esc_len;
454
- if (ve <= vs) return '';
455
- }
497
+ // one call, a statement more here and v8 no longer inlines this function
498
+ if (ve > esc_len) return escape_past(c, i, vs, ve, code);
456
499
  if (vs < esc_lo) esc_reset(src);
457
500
  if (esc_amp < vs) esc_amp = esc_next('&', vs);
458
501
  if (esc_lt < vs) esc_lt = esc_next('<', vs);
@@ -464,10 +507,45 @@ function escape_text_at(
464
507
  if (esc_gt < m) m = esc_gt;
465
508
  if (esc_quot < m) m = esc_quot;
466
509
  if (code) return escape_code_slice(src, vs, ve, m);
510
+ const first = src.charCodeAt(vs);
511
+ if ((first === 123 || first === 125) && brace_escaped(src, vs))
512
+ return brace_text(c, i, vs, ve, first);
467
513
  if (m >= ve) return src.slice(vs, ve);
468
514
  return escape_hits(src, vs, ve, m);
469
515
  }
470
516
 
517
+ /** a stored string, apart so escape_text_at stays small enough for v8 to inline */
518
+ function stored_text(c: Cursor, i: number, s: string, code: boolean): string {
519
+ // text a plugin wrote is all characters, a brace or a reference too
520
+ return code || (c.words[i * W.stride + W.pending] & 2) !== 0
521
+ ? escape_code(s)
522
+ : escape_text_html(s);
523
+ }
524
+
525
+ /** the text of a value that opens at a brace a backslash escaped */
526
+ function brace_text(
527
+ c: Cursor,
528
+ i: number,
529
+ vs: number,
530
+ ve: number,
531
+ first: number
532
+ ): string {
533
+ return (
534
+ (first === 123 ? '&#123;' : '&#125;') +
535
+ escape_text_at(c, i, vs + 1, ve, false)
536
+ );
537
+ }
538
+
539
+ /**
540
+ * the brace at pos follows an odd run of backslashes, an escape opens its
541
+ * text node at the escaped char, and svelte would read the brace as an expression
542
+ */
543
+ function brace_escaped(src: string, pos: number): boolean {
544
+ let p = pos;
545
+ while (p > 0 && src.charCodeAt(p - 1) === 92) p--;
546
+ return ((pos - p) & 1) === 1;
547
+ }
548
+
471
549
  /** a reference in text stays as written, unless a backslash escaped its & */
472
550
  function text_reference(
473
551
  src: string,
@@ -554,6 +632,9 @@ const enum K {
554
632
  FRONTMATTER = 33,
555
633
  IMPORT_STATEMENT = 34,
556
634
  DIRECTIVE_LABEL = 35,
635
+ FOOTNOTE_REFERENCE = 36,
636
+ FOOTNOTE_DEFINITION = 37,
637
+ DEFINITION = 38,
557
638
  }
558
639
 
559
640
  export const K_ROOT = K.ROOT;
@@ -649,7 +730,8 @@ function emit_record(
649
730
 
650
731
  /**
651
732
  * offsets in html characters, every span the render records covers at least
652
- * one chunk, so none is dropped for its width
733
+ * one chunk, so none is dropped for its width, a span with no end is a node
734
+ * a parse plugin added and gets no record
653
735
  */
654
736
  function put_record(
655
737
  sink: MapSink,
@@ -660,7 +742,7 @@ function put_record(
660
742
  node_index: number,
661
743
  code: number
662
744
  ): void {
663
- if (src_start !== Slot.NONE) {
745
+ if (src_start !== Slot.NONE && src_end !== Slot.NONE) {
664
746
  let rec = sink.rec;
665
747
  const p = sink.n;
666
748
  if (p + Rec.SIZE > rec.length) rec = sink.grow();
@@ -735,6 +817,8 @@ function _spans_some(
735
817
  const idx = c.index;
736
818
  const s = c.start,
737
819
  e = c.end;
820
+ // a node a parse plugin added has no end and no place in the source
821
+ if (e === Slot.NONE) return;
738
822
  put_record(sink, pre, post, s, e, idx, preset << 2);
739
823
  if (!sink.syntax) return;
740
824
  const vs = c.value_start,
@@ -794,6 +878,8 @@ const INTERNAL_KEYS = new Set([
794
878
  'ordered',
795
879
  'tight',
796
880
  'start',
881
+ // a task item, rendered as a checkbox or passed to a replacement as a prop
882
+ 'checked',
797
883
  // code fence info (transformed into class="language-X")
798
884
  'info',
799
885
  'info_start',
@@ -835,6 +921,20 @@ function expr_attr(k: string, value: string): string {
835
921
  : ' ' + k + '={' + value + '}';
836
922
  }
837
923
 
924
+ /** the later values of the names a tag writes again, written so that svelte reports them */
925
+ function repeated_attrs(repeated: [string, unknown][], typed: boolean): string {
926
+ let s = '';
927
+ for (let i = 0; i < repeated.length; i++) {
928
+ const k = repeated[i][0];
929
+ const v = repeated[i][1];
930
+ if (v === true) s += ' ' + k;
931
+ else if (typeof v === 'object' && v !== null)
932
+ s += expr_attr(k, meta_str((v as any).value));
933
+ else s += ' ' + k + '="' + escape_attr(meta_str(v), typed) + '"';
934
+ }
935
+ return s;
936
+ }
937
+
838
938
  function _attrs(c: Cursor, skip?: Set<string>): string {
839
939
  const meta = c.meta();
840
940
  if (!meta) return '';
@@ -876,6 +976,7 @@ const enum W {
876
976
  parent = 5,
877
977
  next = 6,
878
978
  first_child = 8,
979
+ pending = 10,
879
980
  meta = 11,
880
981
  stride = 12,
881
982
  }
@@ -906,7 +1007,7 @@ const comp_warnings: ReplaceWarning[] = [];
906
1007
  const NO_WARNINGS: readonly ReplaceWarning[] = Object.freeze([]);
907
1008
  /** what the last comp_begin handed out */
908
1009
  let comp_last = NO_WARNINGS;
909
- /** import lines of the used replacements, they join the instance script */
1010
+ /** import lines of the used replacements and the extra lines, they join the instance script */
910
1011
  let comp_lines = '';
911
1012
  /** a script of comp_lines when the document has no script and no import to start one */
912
1013
  let comp_prefix = '';
@@ -928,52 +1029,29 @@ const hoist_nodes: number[] = [];
928
1029
  let module_code = '';
929
1030
  /** the module script the module code goes into, -1 renders a new one in place of the frontmatter */
930
1031
  let hoist_module = -1;
1032
+ /** the open tag of an instance script the render starts, ts for type imports or a ts module script */
1033
+ let hoist_open = '<script>\n';
931
1034
 
932
1035
  function hoist_reset(): void {
933
1036
  hoist_script = -1;
934
1037
  hoist_at = -1;
1038
+ hoist_open = '<script>\n';
935
1039
  if (hoist_nodes.length !== 0) hoist_nodes.length = 0;
936
1040
  }
937
1041
 
938
1042
  /**
939
- * find the import statements and, when there are some or replacements need
940
- * importing, the instance script
1043
+ * find the import statements and, when there are some or the render has
1044
+ * replacements or extra lines to import, the instance script, extra is passed
1045
+ * so a render with none reads no state for them
941
1046
  */
942
- function hoist_begin(buf: NodeBuffer): void {
943
- hoist_reset();
944
- const n = buf._n;
945
- let child = n[W.first_child];
946
- // imports come first, after any frontmatter and blank lines
947
- while (child !== Slot.NONE) {
948
- const b = child * W.stride;
949
- if (n[b + W.parent] !== 0) break;
950
- const kind = n[b] & 0xff;
951
- if (kind === K.IMPORT_STATEMENT) hoist_nodes.push(child);
952
- else if (kind !== K.LINE_BREAK && kind !== K.FRONTMATTER) break;
953
- child = n[b + W.next];
954
- }
955
- if (hoist_nodes.length === 0 && !has_components) return;
956
- while (child !== Slot.NONE) {
957
- const b = child * W.stride;
958
- if (n[b + W.parent] !== 0) break;
959
- if ((n[b] & 0xff) === K.HTML) {
960
- const slot = n[b + W.meta];
961
- const meta = slot === 0 ? undefined : buf._meta[slot - 1];
962
- if (
963
- meta !== undefined &&
964
- meta.tag === 'script' &&
965
- !meta.self_closing &&
966
- !is_other_script(meta.attributes as Record<string, unknown> | undefined)
967
- ) {
968
- hoist_script = child;
969
- return;
970
- }
971
- }
972
- child = n[b + W.next];
973
- }
974
- if (hoist_nodes.length !== 0) hoist_at = hoist_nodes[0];
1047
+ function hoist_begin(c: Cursor, buf: NodeBuffer, extra: boolean): void {
1048
+ const child = hoist_imports(buf);
1049
+ if (hoist_nodes.length === 0 && !has_components && !extra) return;
1050
+ if (extra) comp_lines += extra_lines;
1051
+ hoist_script_from(c, buf, child);
975
1052
  // no script and no import to start one, the render starts with one
976
- else comp_prefix = '<script>\n' + comp_lines + '</script>';
1053
+ if (hoist_script === -1 && hoist_at === -1)
1054
+ comp_prefix = hoist_open + comp_lines + '</script>';
977
1055
  }
978
1056
 
979
1057
  /** the import statements, returns the root child after them */
@@ -994,35 +1072,45 @@ function hoist_imports(buf: NodeBuffer): number {
994
1072
  }
995
1073
 
996
1074
  /** the instance script from child on, or the first import that starts one */
997
- function hoist_script_from(buf: NodeBuffer, child: number): void {
1075
+ function hoist_script_from(c: Cursor, buf: NodeBuffer, child: number): void {
998
1076
  const n = buf._n;
1077
+ let ts = false;
999
1078
  while (child !== Slot.NONE) {
1000
1079
  const b = child * W.stride;
1001
1080
  if (n[b + W.parent] !== 0) break;
1002
1081
  if ((n[b] & 0xff) === K.HTML) {
1003
1082
  const slot = n[b + W.meta];
1004
1083
  const meta = slot === 0 ? undefined : buf._meta[slot - 1];
1005
- if (
1006
- meta !== undefined &&
1007
- meta.tag === 'script' &&
1008
- !meta.self_closing &&
1009
- !is_other_script(meta.attributes as Record<string, unknown> | undefined)
1010
- ) {
1011
- hoist_script = child;
1012
- return;
1084
+ if (meta !== undefined && meta.tag === 'script' && !meta.self_closing) {
1085
+ const attrs = meta.attributes as Record<string, unknown> | undefined;
1086
+ if (!is_other_script(attrs)) {
1087
+ hoist_script = child;
1088
+ return;
1089
+ }
1090
+ if (attrs!.src === undefined && attrs!.lang === 'ts') ts = true;
1013
1091
  }
1014
1092
  }
1015
1093
  child = n[b + W.next];
1016
1094
  }
1017
1095
  if (hoist_nodes.length !== 0) hoist_at = hoist_nodes[0];
1096
+ for (let i = 0; !ts && i < hoist_nodes.length; i++) {
1097
+ const b = hoist_nodes[i] * W.stride;
1098
+ ts = TYPE_IMPORT.test(c.slice(n[b + W.value_start], n[b + W.value_end]));
1099
+ }
1100
+ if (ts) hoist_open = '<script lang="ts">\n';
1018
1101
  }
1019
1102
 
1103
+ /** a type import or a type specifier, svelte only parses them in a ts component */
1104
+ const TYPE_IMPORT =
1105
+ /^import\s+type(?:\s*[{*]|\s+(?!from\b)[\w$])|[{,]\s*type\s+(?!as\b)[\w$]/;
1106
+
1020
1107
  /**
1021
1108
  * hoist_begin for a wrapped render, the instance script starts with the
1022
- * replacement imports, the template import and the props it forwards
1109
+ * replacement imports, the template import, the extra lines and the props it
1110
+ * forwards
1023
1111
  */
1024
1112
  function wrap_hoist_begin(c: Cursor, buf: NodeBuffer): void {
1025
- hoist_script_from(buf, hoist_imports(buf));
1113
+ hoist_script_from(c, buf, hoist_imports(buf));
1026
1114
  let props = WRAP_PROPS;
1027
1115
  let declare = true;
1028
1116
  if (hoist_script !== -1) {
@@ -1036,20 +1124,22 @@ function wrap_hoist_begin(c: Cursor, buf: NodeBuffer): void {
1036
1124
  props = named === null ? '' : named[1];
1037
1125
  }
1038
1126
  }
1039
- comp_lines = component_imports(comp_used, {
1040
- specifier: wrap_spec,
1041
- local: WRAP_LOCAL,
1042
- });
1127
+ comp_lines =
1128
+ component_imports(comp_used, {
1129
+ specifier: wrap_spec,
1130
+ local: WRAP_LOCAL,
1131
+ }) + extra_lines;
1043
1132
  if (declare) comp_lines += 'let ' + WRAP_PROPS + ' = $props();\n';
1044
1133
  wrap_open =
1045
1134
  '<' +
1046
1135
  WRAP_LOCAL +
1047
1136
  (wrap_metadata ? ' {...metadata}' : '') +
1137
+ (wrap_data === '' ? '' : ' {' + wrap_data + '}') +
1048
1138
  (props === '' ? '' : ' {...' + props + '}') +
1049
1139
  '>';
1050
1140
  // no script and no import to start one, the render starts with one
1051
1141
  if (hoist_script === -1 && hoist_at === -1)
1052
- comp_prefix = '<script>\n' + comp_lines + '</script>';
1142
+ comp_prefix = hoist_open + comp_lines + '</script>';
1053
1143
  }
1054
1144
 
1055
1145
  /**
@@ -1083,9 +1173,24 @@ function module_begin(buf: NodeBuffer, code: string): void {
1083
1173
  }
1084
1174
  }
1085
1175
 
1176
+ /**
1177
+ * module_begin for a fold render, which meets the module code at the
1178
+ * frontmatter, a document with neither that nor a module script starts with it
1179
+ */
1180
+ function module_begin_fold(buf: NodeBuffer, code: string): void {
1181
+ module_begin(buf, code);
1182
+ if (hoist_module !== -1) return;
1183
+ const n = buf._n;
1184
+ const first = n[W.first_child];
1185
+ if (first === Slot.NONE || (n[first * W.stride] & 0xff) !== K.FRONTMATTER)
1186
+ comp_prefix += module_script();
1187
+ }
1188
+
1086
1189
  function module_end(): void {
1087
1190
  module_code = '';
1088
1191
  hoist_module = -1;
1192
+ // module_begin_fold may have left the script there
1193
+ comp_prefix = '';
1089
1194
  }
1090
1195
 
1091
1196
  /** the module script with the module code, for a document with none */
@@ -1120,7 +1225,7 @@ function is_embed_script(c: Cursor, tag: string): boolean {
1120
1225
  function embed_html(c: Cursor): string {
1121
1226
  let s = '<svelte:element this={"script"}';
1122
1227
  const attrs = c.meta()!.attributes as Record<string, string | boolean>;
1123
- const typed = c.end !== Slot.NONE;
1228
+ const typed = !c.synthetic;
1124
1229
  for (const k in attrs) {
1125
1230
  const v = attrs[k];
1126
1231
  if (v === true) s += ' ' + k;
@@ -1183,6 +1288,65 @@ const LINK_HANDLED = new Set(['href', 'title']);
1183
1288
  const IMAGE_HANDLED = new Set(['title']);
1184
1289
 
1185
1290
  /** render children of the current cursor position, collecting escaped text and recursive node output. */
1291
+ // task items
1292
+
1293
+ const TASK_OFF = '<input type="checkbox" disabled /> ';
1294
+ const TASK_ON = '<input type="checkbox" checked disabled /> ';
1295
+
1296
+ /** the replaced paragraph that takes a checkbox as its first child, -1 for none */
1297
+ let task_at = -1;
1298
+ let task_box = '';
1299
+
1300
+ /** the first paragraph of a loose task item, which takes the checkbox as on github, else NONE */
1301
+ function task_para(c: Cursor): number {
1302
+ const n = c.words;
1303
+ const item = c.index;
1304
+ const first = n[item * W.stride + W.first_child];
1305
+ if (first === Slot.NONE) return Slot.NONE;
1306
+ const b = first * W.stride;
1307
+ // a pending wrapper renders as its children
1308
+ if ((n[b] & 0xff) !== K.PARAGRAPH || n[b + W.pending] === 1) return Slot.NONE;
1309
+ // the walks render text only through its parent, so a text sibling sends the checkbox to the item start
1310
+ for (
1311
+ let next = n[b + W.next];
1312
+ next !== Slot.NONE && n[next * W.stride + W.parent] === item;
1313
+ next = n[next * W.stride + W.next]
1314
+ ) {
1315
+ if ((n[next * W.stride] & 0xff) === K.TEXT) return Slot.NONE;
1316
+ }
1317
+ return first;
1318
+ }
1319
+
1320
+ /** the children of a task item with its checkbox, c stays on the item */
1321
+ function render_task(c: Cursor, sink: MapSink | undefined, on: boolean): void {
1322
+ const box = on ? TASK_ON : TASK_OFF;
1323
+ const para = task_para(c);
1324
+ if (para === Slot.NONE) {
1325
+ mo += box;
1326
+ render_children(c, sink);
1327
+ return;
1328
+ }
1329
+ const n = c.words;
1330
+ const item = c.index;
1331
+ c.move_to(para);
1332
+ const pre = mo.length;
1333
+ _open(c, '<p', '<p>', '>');
1334
+ const ao = mo.length;
1335
+ mo += box;
1336
+ render_children(c, sink);
1337
+ const bc = mo.length;
1338
+ mo += '</p>';
1339
+ if (sink) _spans(sink, pre, ao, bc, mo.length, c, Preset.TEXT);
1340
+ let next = n[para * W.stride + W.next];
1341
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === item) {
1342
+ const after = n[next * W.stride + W.next];
1343
+ c.move_to(next);
1344
+ render_node(c, sink);
1345
+ next = after;
1346
+ }
1347
+ c.move_to(item);
1348
+ }
1349
+
1186
1350
  function render_children(c: Cursor, sink?: MapSink): void {
1187
1351
  const n = c.words;
1188
1352
  let child = n[c.index * W.stride + W.first_child];
@@ -1221,7 +1385,18 @@ function _children_raw(c: Cursor): string {
1221
1385
  let text = '';
1222
1386
  do {
1223
1387
  if (c.kind === K.TEXT) {
1224
- text += c.text();
1388
+ const t = c.text();
1389
+ const vs = c.value_start;
1390
+ const first = t.charCodeAt(0);
1391
+ // alt is an attribute, svelte reads a brace there as an expression too
1392
+ if (
1393
+ (first === 123 || first === 125) &&
1394
+ vs !== Slot.NONE &&
1395
+ c.source.charCodeAt(vs) === first &&
1396
+ brace_escaped(c.source, vs)
1397
+ )
1398
+ text += (first === 123 ? '&#123;' : '&#125;') + t.slice(1);
1399
+ else text += t;
1225
1400
  } else {
1226
1401
  text += _children_raw(c);
1227
1402
  }
@@ -1249,6 +1424,90 @@ function content_record(
1249
1424
  );
1250
1425
  }
1251
1426
 
1427
+ /**
1428
+ * a content record per line of fence code the parser built, false for other code
1429
+ * each such line is the end of its source line once indent and quote markers go
1430
+ */
1431
+ function fence_lines(
1432
+ c: Cursor,
1433
+ sink: MapSink,
1434
+ at: number,
1435
+ text: string,
1436
+ trace: boolean
1437
+ ): boolean {
1438
+ const code = c.prebuilt;
1439
+ if (code === undefined || (fs !== null && fs.has(c.index)) || holds(c))
1440
+ return false;
1441
+ const src = c.source;
1442
+ const ve = c.value_end;
1443
+ let line = c.value_start;
1444
+ let g = 0;
1445
+ let r = 0;
1446
+ for (;;) {
1447
+ // escaping keeps the linefeeds, so the text and the code have the same lines
1448
+ let g_end = string_index_of.call(text, '\n', g);
1449
+ let r_end = string_index_of.call(code, '\n', r);
1450
+ let s_end = string_index_of.call(src, '\n', line);
1451
+ if (s_end === -1 || s_end > ve) s_end = ve;
1452
+ const last = g_end === -1;
1453
+ if (last) {
1454
+ g_end = text.length;
1455
+ r_end = code.length;
1456
+ } else {
1457
+ g_end++;
1458
+ r_end++;
1459
+ }
1460
+ const s_to = last ? s_end : s_end + 1;
1461
+ let s_from = s_to - (r_end - r);
1462
+ // a tab the indent cut leaves spaces the source line has no room for
1463
+ if (s_from < line) s_from = line;
1464
+ if (g_end > g) {
1465
+ if (trace) tr_run(sink, at + g, at + g_end, s_from, s_to);
1466
+ else
1467
+ put_record(
1468
+ sink,
1469
+ at + g,
1470
+ at + g_end,
1471
+ s_from,
1472
+ s_to,
1473
+ c.index,
1474
+ Code.CODE_CONTENT
1475
+ );
1476
+ }
1477
+ if (last) return true;
1478
+ g = g_end;
1479
+ r = r_end;
1480
+ line = s_end + 1;
1481
+ }
1482
+ }
1483
+
1484
+ /** the escaped code of a fence and its content records, after flushing the register */
1485
+ function fence_content(
1486
+ c: Cursor,
1487
+ sink: MapSink,
1488
+ p: number,
1489
+ text: string,
1490
+ trace: boolean
1491
+ ): number {
1492
+ if (p !== 0) mo += FOLD_STR[p];
1493
+ const at = mo.length;
1494
+ if (!fence_lines(c, sink, at, text, trace)) {
1495
+ if (trace) tr_run(sink, at, at + text.length, c.value_start, c.value_end);
1496
+ else
1497
+ put_record(
1498
+ sink,
1499
+ at,
1500
+ at + text.length,
1501
+ c.value_start,
1502
+ c.value_end,
1503
+ c.index,
1504
+ Code.CODE_CONTENT
1505
+ );
1506
+ }
1507
+ mo += text;
1508
+ return 0;
1509
+ }
1510
+
1252
1511
  /** render a single node at the current cursor position. */
1253
1512
  function render_node(c: Cursor, sink?: MapSink): void {
1254
1513
  // as number, or the case labels narrow c.kind for the reads inside a case
@@ -1321,7 +1580,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1321
1580
  const pre = mo.length;
1322
1581
  _open(c, '<code', '<code>', '>');
1323
1582
  const ao = mo.length;
1324
- let code = escape_code_text(c);
1583
+ let code = escape_span_text(c);
1325
1584
  if (string_index_of.call(code, '\n') !== -1)
1326
1585
  code = code.replace(/\n/g, ' ');
1327
1586
  if (sink) content_record(sink, c, code, Code.CODE_CONTENT);
@@ -1350,7 +1609,8 @@ function render_node(c: Cursor, sink?: MapSink): void {
1350
1609
  }
1351
1610
  const ao = mo.length;
1352
1611
  const text = escape_fence_text(c);
1353
- if (sink) content_record(sink, c, text, Code.CODE_CONTENT);
1612
+ if (sink && !fence_lines(c, sink, mo.length, text, false))
1613
+ content_record(sink, c, text, Code.CODE_CONTENT);
1354
1614
  mo += text;
1355
1615
  const bc = mo.length;
1356
1616
  mo += '</code></pre>';
@@ -1373,8 +1633,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1373
1633
  const pre = mo.length;
1374
1634
  const meta = c.meta();
1375
1635
  let s = '<a';
1376
- if (meta?.href)
1377
- s += ' href="' + escape_text_html(meta.href as string) + '"';
1636
+ if (meta?.href) s += url_attr(meta.href as string, false);
1378
1637
  if (meta?.title)
1379
1638
  s += ' title="' + escape_text_html(meta.title as string) + '"';
1380
1639
  mo = mo + s + _attrs(c, LINK_HANDLED) + '>';
@@ -1390,7 +1649,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1390
1649
  const pre = mo.length;
1391
1650
  const meta = c.meta();
1392
1651
  let s = '<img';
1393
- if (meta?.src) s += ' src="' + escape_text_html(meta.src as string) + '"';
1652
+ if (meta?.src) s += url_attr(meta.src as string, true);
1394
1653
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
1395
1654
  if (meta?.title)
1396
1655
  s += ' title="' + escape_text_html(meta.title as string) + '"';
@@ -1425,7 +1684,9 @@ function render_node(c: Cursor, sink?: MapSink): void {
1425
1684
  const pre = mo.length;
1426
1685
  _open(c, '<li', '<li>', '>');
1427
1686
  const ao = mo.length;
1428
- render_children(c, sink);
1687
+ const checked = c.meta()?.checked;
1688
+ if (typeof checked === 'boolean') render_task(c, sink, checked);
1689
+ else render_children(c, sink);
1429
1690
  const bc = mo.length;
1430
1691
  mo += '</li>\n';
1431
1692
  if (sink) _spans(sink, pre, ao, bc, mo.length, c, Preset.TEXT);
@@ -1436,7 +1697,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1436
1697
  const pre = mo.length;
1437
1698
  mo += '<hr />';
1438
1699
  // node and open syntax, the close syntax span is empty
1439
- if (sink) {
1700
+ if (sink && c.end !== Slot.NONE) {
1440
1701
  const post = mo.length;
1441
1702
  put_record(
1442
1703
  sink,
@@ -1534,7 +1795,9 @@ function render_node(c: Cursor, sink?: MapSink): void {
1534
1795
  // (e.g. wire/streaming renderer with empty source).
1535
1796
  const self_closing = !!meta?.self_closing;
1536
1797
  const passthrough =
1537
- self_closing && c.end > c.start ? c.slice(c.start, c.end) : '';
1798
+ self_closing && !c.synthetic && c.end > c.start
1799
+ ? c.slice(c.start, c.end)
1800
+ : '';
1538
1801
  if (passthrough) {
1539
1802
  mo += passthrough;
1540
1803
  } else {
@@ -1543,7 +1806,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1543
1806
  | Record<string, string | boolean>
1544
1807
  | undefined;
1545
1808
  if (html_attrs) {
1546
- const typed = c.end !== Slot.NONE;
1809
+ const typed = !c.synthetic;
1547
1810
  for (const k in html_attrs) {
1548
1811
  const v = html_attrs[k];
1549
1812
  if (v === true) {
@@ -1557,6 +1820,8 @@ function render_node(c: Cursor, sink?: MapSink): void {
1557
1820
  s += ' ' + k + '="' + escape_attr(v as string, typed) + '"';
1558
1821
  }
1559
1822
  }
1823
+ if (meta!.repeated !== undefined)
1824
+ s += repeated_attrs(meta!.repeated as [string, unknown][], typed);
1560
1825
  }
1561
1826
  mo += self_closing ? s + ' />' : s + '>';
1562
1827
  }
@@ -1574,10 +1839,9 @@ function render_node(c: Cursor, sink?: MapSink): void {
1574
1839
  }
1575
1840
  } else {
1576
1841
  const ao = mo.length;
1577
- // raw-text elements: parser stores content as value range on
1578
- // the html node itself (no child nodes). emit unescaped, the
1579
- // browser does not parse script/style bodies as html.
1580
- if (tag === 'script' || tag === 'style') {
1842
+ // raw text elements hold their content as the value range of the
1843
+ // node, with no children, it is written as it is in the source
1844
+ if (is_raw_text_tag(tag)) {
1581
1845
  if (c.index === hoist_script) {
1582
1846
  mo += '\n';
1583
1847
  mo_imports(c, sink, false);
@@ -1601,7 +1865,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1601
1865
  const pre = mo.length;
1602
1866
  mo += '<!--';
1603
1867
  const ao = mo.length;
1604
- const text = c.text();
1868
+ const text = held_text(c);
1605
1869
  if (sink) content_record(sink, c, text, Code.TEXT_CONTENT);
1606
1870
  mo += text;
1607
1871
  const bc = mo.length;
@@ -1703,7 +1967,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1703
1967
 
1704
1968
  case K.IMPORT_STATEMENT:
1705
1969
  if (c.index === hoist_at) {
1706
- mo += '<script>\n';
1970
+ mo += hoist_open;
1707
1971
  mo_imports(c, sink, false);
1708
1972
  mo += '</script>';
1709
1973
  }
@@ -1714,8 +1978,15 @@ function render_node(c: Cursor, sink?: MapSink): void {
1714
1978
  break;
1715
1979
 
1716
1980
  default:
1717
- // a label renders only as the snippet of a replaced directive
1718
- if (c.kind !== K.DIRECTIVE_LABEL) render_children(c, sink);
1981
+ // a label renders only as the snippet of a replaced directive, a
1982
+ // footnote definition in the section at the end and a link reference
1983
+ // definition not at all
1984
+ if (c.kind < K.DIRECTIVE_LABEL) render_children(c, sink);
1985
+ else if (c.kind === K.FOOTNOTE_REFERENCE) {
1986
+ const pre = mo.length;
1987
+ mo += fn_reference(c);
1988
+ if (sink) put_record(sink, pre, mo.length, c.start, c.end, c.index, 0);
1989
+ }
1719
1990
  break;
1720
1991
  }
1721
1992
  }
@@ -1940,6 +2211,14 @@ const S_TR_OPEN = fold_base('<tr>\n');
1940
2211
  const S_TR_CLOSE = fold_base('</tr>\n');
1941
2212
  const S_TH_CLOSE = fold_base('</th>\n');
1942
2213
  const S_TD_CLOSE = fold_base('</td>\n');
2214
+ const S_P = fold_base('<p');
2215
+ const S_P_OPEN = fold_base('<p>');
2216
+ const S_P_CLOSE = fold_base('</p>');
2217
+ const S_LI = fold_base('<li');
2218
+ const S_LI_OPEN = fold_base('<li>');
2219
+ const S_LI_CLOSE = fold_base('</li>\n');
2220
+ const S_TASK_OFF = fold_base(TASK_OFF);
2221
+ const S_TASK_ON = fold_base(TASK_ON);
1943
2222
 
1944
2223
  /** indexed by alignment none, left, center, right */
1945
2224
  function fold_cell_opens(tag: string): Uint8Array {
@@ -1981,7 +2260,6 @@ wrap_row(K.STRONG, '<strong', '<strong>', '>', '</strong>');
1981
2260
  wrap_row(K.STRIKETHROUGH, '<del', '<del>', '>', '</del>');
1982
2261
  wrap_row(K.SUPERSCRIPT, '<sup', '<sup>', '>', '</sup>');
1983
2262
  wrap_row(K.SUBSCRIPT, '<sub', '<sub>', '>', '</sub>');
1984
- wrap_row(K.LIST_ITEM, '<li', '<li>', '>', '</li>\n');
1985
2263
  wrap_row(
1986
2264
  K.BLOCK_QUOTE,
1987
2265
  '<blockquote',
@@ -2222,7 +2500,6 @@ function fold_node(c: Cursor, p: number): number {
2222
2500
  case K.EMPHASIS:
2223
2501
  case K.STRONG:
2224
2502
  case K.BLOCK_QUOTE:
2225
- case K.LIST_ITEM:
2226
2503
  case K.STRIKETHROUGH:
2227
2504
  case K.SUPERSCRIPT:
2228
2505
  case K.SUBSCRIPT: {
@@ -2235,6 +2512,18 @@ function fold_node(c: Cursor, p: number): number {
2235
2512
  return push_static(p, WRAP_CLOSE[row]);
2236
2513
  }
2237
2514
 
2515
+ case K.LIST_ITEM: {
2516
+ const meta = c.meta();
2517
+ if (meta === undefined) p = push_static(p, S_LI_OPEN);
2518
+ else {
2519
+ p = fold_open(c, p, S_LI, S_LI_OPEN, S_GT);
2520
+ if (typeof meta.checked === 'boolean')
2521
+ return fold_task(c, p, meta.checked);
2522
+ }
2523
+ p = fold_children(c, p);
2524
+ return push_static(p, S_LI_CLOSE);
2525
+ }
2526
+
2238
2527
  case K.CODE_SPAN: {
2239
2528
  if (hl !== null) {
2240
2529
  const h = hl_span(c);
@@ -2242,7 +2531,7 @@ function fold_node(c: Cursor, p: number): number {
2242
2531
  return fold_hl(p, hl_span_head(c, h), h.body, hl_span_tail(h));
2243
2532
  }
2244
2533
  p = fold_open(c, p, S_CODE, S_CODE_OPEN, S_GT);
2245
- const code = escape_code_text(c);
2534
+ const code = escape_span_text(c);
2246
2535
  p = push_dyn(
2247
2536
  p,
2248
2537
  code.indexOf('\n') === -1 ? code : code.replace(/\n/g, ' ')
@@ -2276,7 +2565,7 @@ function fold_node(c: Cursor, p: number): number {
2276
2565
 
2277
2566
  case K.HTML_COMMENT:
2278
2567
  p = push_static(p, S_COMMENT_OPEN);
2279
- p = push_dyn(p, c.text());
2568
+ p = push_dyn(p, held_text(c));
2280
2569
  return push_static(p, S_COMMENT_CLOSE);
2281
2570
 
2282
2571
  case K.MUSTACHE:
@@ -2307,21 +2596,60 @@ function fold_node(c: Cursor, p: number): number {
2307
2596
 
2308
2597
  case K.IMPORT_STATEMENT:
2309
2598
  if (c.index !== hoist_at) return p;
2310
- return push_dyn(p, '<script>\n' + import_lines(c) + '</script>');
2599
+ return push_dyn(p, hoist_open + import_lines(c) + '</script>');
2311
2600
 
2312
2601
  case K.FRONTMATTER:
2313
2602
  if (module_code === '') return p;
2314
2603
  return push_dyn(p, module_script());
2315
2604
 
2316
2605
  default:
2317
- // a label renders only as the snippet of a replaced directive
2318
- if (c.kind === K.DIRECTIVE_LABEL) return p;
2606
+ // a label renders only as the snippet of a replaced directive, a
2607
+ // footnote definition in the section at the end and a link reference
2608
+ // definition not at all
2609
+ if (c.kind >= K.DIRECTIVE_LABEL)
2610
+ return c.kind === K.FOOTNOTE_REFERENCE
2611
+ ? push_dyn(p, fn_reference(c))
2612
+ : p;
2319
2613
  // a replaced directive never gets here, see comp_ref
2320
2614
  if (dir_strict) dir_check(c);
2321
2615
  return fold_children(c, p);
2322
2616
  }
2323
2617
  }
2324
2618
 
2619
+ /** render_task for the fold walk, after the open tag */
2620
+ function fold_task(c: Cursor, p: number, on: boolean): number {
2621
+ const box = on ? S_TASK_ON : S_TASK_OFF;
2622
+ const para = task_para(c);
2623
+ if (para === Slot.NONE) {
2624
+ p = push_static(p, box);
2625
+ p = fold_children(c, p);
2626
+ return push_static(p, S_LI_CLOSE);
2627
+ }
2628
+ const n = c.words;
2629
+ const item = c.index;
2630
+ c.move_to(para);
2631
+ if (has_components && comp_ref(c) !== null) {
2632
+ task_at = para;
2633
+ task_box = FOLD_STR[box];
2634
+ p = fold_node(c, p);
2635
+ task_at = -1;
2636
+ } else {
2637
+ p = fold_open(c, p, S_P, S_P_OPEN, S_GT);
2638
+ p = push_static(p, box);
2639
+ p = fold_children(c, p);
2640
+ p = push_static(p, S_P_CLOSE);
2641
+ }
2642
+ let next = n[para * W.stride + W.next];
2643
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === item) {
2644
+ const after = n[next * W.stride + W.next];
2645
+ c.move_to(next);
2646
+ p = fold_node(c, p);
2647
+ next = after;
2648
+ }
2649
+ c.move_to(item);
2650
+ return push_static(p, S_LI_CLOSE);
2651
+ }
2652
+
2325
2653
  function fold_code_fence(c: Cursor, p: number): number {
2326
2654
  const info = fence_info(c, c.meta());
2327
2655
  if (hl !== null) {
@@ -2342,11 +2670,7 @@ function fold_code_fence(c: Cursor, p: number): number {
2342
2670
  function fold_link(c: Cursor, p: number): number {
2343
2671
  const meta = c.meta();
2344
2672
  p = push_static(p, S_A);
2345
- if (meta?.href) {
2346
- p = push_static(p, S_HREF);
2347
- p = push_dyn(p, escape_text_html(meta.href as string));
2348
- p = push_static(p, S_QUOTE);
2349
- }
2673
+ if (meta?.href) p = fold_url(p, meta.href as string, false);
2350
2674
  if (meta?.title) {
2351
2675
  p = push_static(p, S_TITLE);
2352
2676
  p = push_dyn(p, escape_text_html(meta.title as string));
@@ -2361,11 +2685,7 @@ function fold_link(c: Cursor, p: number): number {
2361
2685
  function fold_image(c: Cursor, p: number): number {
2362
2686
  const meta = c.meta();
2363
2687
  p = push_static(p, S_IMG);
2364
- if (meta?.src) {
2365
- p = push_static(p, S_SRC);
2366
- p = push_dyn(p, escape_text_html(meta.src as string));
2367
- p = push_static(p, S_QUOTE);
2368
- }
2688
+ if (meta?.src) p = fold_url(p, meta.src as string, true);
2369
2689
  p = push_static(p, S_ALT);
2370
2690
  p = push_dyn(p, escape_text_html(_children_raw(c)));
2371
2691
  p = push_static(p, S_QUOTE);
@@ -2433,20 +2753,28 @@ function fold_html(c: Cursor, p: number): number {
2433
2753
  if (meta?.self_closing) {
2434
2754
  // source passthrough as in _node, decided before any push because a
2435
2755
  // pending register cannot be truncated back
2436
- const passthrough = c.end > c.start ? c.slice(c.start, c.end) : '';
2756
+ const passthrough =
2757
+ !c.synthetic && c.end > c.start ? c.slice(c.start, c.end) : '';
2437
2758
  if (passthrough) return push_dyn(p, passthrough);
2438
2759
  p = push_static(p, S_LT);
2439
2760
  p = push_dyn(p, tag);
2440
- if (html_attrs) p = fold_html_attrs(html_attrs, c.end !== Slot.NONE, p);
2761
+ if (html_attrs) p = fold_html_attrs(html_attrs, !c.synthetic, p);
2441
2762
  return push_static(p, S_SELF_CLOSE);
2442
2763
  }
2443
2764
 
2444
2765
  p = push_static(p, S_LT);
2445
2766
  p = push_dyn(p, tag);
2446
- if (html_attrs) p = fold_html_attrs(html_attrs, c.end !== Slot.NONE, p);
2767
+ if (html_attrs) {
2768
+ p = fold_html_attrs(html_attrs, !c.synthetic, p);
2769
+ if (meta!.repeated !== undefined)
2770
+ p = push_dyn(
2771
+ p,
2772
+ repeated_attrs(meta!.repeated as [string, unknown][], !c.synthetic)
2773
+ );
2774
+ }
2447
2775
  p = push_static(p, S_GT);
2448
2776
  // raw text elements keep their content as the node value range, see _node
2449
- if (tag === 'script' || tag === 'style') {
2777
+ if (is_raw_text_tag(tag)) {
2450
2778
  if (c.index === hoist_script) p = push_dyn(p, '\n' + import_lines(c));
2451
2779
  else if (c.index === hoist_module)
2452
2780
  p = push_dyn(p, '\n' + module_code + '\n');
@@ -2615,6 +2943,11 @@ function tr_point(sink: MapSink, gen: number, src: number): void {
2615
2943
  }
2616
2944
  }
2617
2945
 
2946
+ /** the point of a node, a node a parse plugin added has no end and no point */
2947
+ function tr_at(sink: MapSink, gen: number, c: Cursor): void {
2948
+ if (c.end !== Slot.NONE) tr_point(sink, gen, c.start);
2949
+ }
2950
+
2618
2951
  /** a content span, a run when it copies the source, empty spans map nothing */
2619
2952
  function tr_run(
2620
2953
  sink: MapSink,
@@ -2772,7 +3105,6 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2772
3105
  case K.EMPHASIS:
2773
3106
  case K.STRONG:
2774
3107
  case K.BLOCK_QUOTE:
2775
- case K.LIST_ITEM:
2776
3108
  case K.STRIKETHROUGH:
2777
3109
  case K.SUPERSCRIPT:
2778
3110
  case K.SUBSCRIPT: {
@@ -2784,7 +3116,20 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2784
3116
  p = tr_open(c, p, WRAP_HEAD[row], WRAP_FOLDED[row], WRAP_END[row]);
2785
3117
  p = tr_children(c, sink, p);
2786
3118
  p = tr_push(p, WRAP_CLOSE[row]);
2787
- tr_point(sink, pre, c.start);
3119
+ tr_at(sink, pre, c);
3120
+ return p;
3121
+ }
3122
+
3123
+ case K.LIST_ITEM: {
3124
+ const pre = mo.length + FOLD_LEN[p];
3125
+ const meta = c.meta();
3126
+ if (meta === undefined) p = tr_push(p, S_LI_OPEN);
3127
+ else p = tr_open(c, p, S_LI, S_LI_OPEN, S_GT);
3128
+ if (meta !== undefined && typeof meta.checked === 'boolean')
3129
+ p = tr_task(c, sink, p, meta.checked);
3130
+ else p = tr_children(c, sink, p);
3131
+ p = tr_push(p, S_LI_CLOSE);
3132
+ tr_at(sink, pre, c);
2788
3133
  return p;
2789
3134
  }
2790
3135
 
@@ -2804,11 +3149,11 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2804
3149
  }
2805
3150
  const pre = mo.length + FOLD_LEN[p];
2806
3151
  p = tr_open(c, p, S_CODE, S_CODE_OPEN, S_GT);
2807
- let code = escape_code_text(c);
3152
+ let code = escape_span_text(c);
2808
3153
  if (string_index_of.call(code, '\n') !== -1)
2809
3154
  code = code.replace(/\n/g, ' ');
2810
3155
  tr_text(c, sink, p, code);
2811
- tr_point(sink, pre, c.start);
3156
+ tr_at(sink, pre, c);
2812
3157
  return S_CODE_CLOSE;
2813
3158
  }
2814
3159
 
@@ -2828,14 +3173,14 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2828
3173
  }
2829
3174
  p = tr_children(c, sink, p);
2830
3175
  p = tr_push(p, ordered ? S_OL_CLOSE : S_UL_CLOSE);
2831
- tr_point(sink, pre, c.start);
3176
+ tr_at(sink, pre, c);
2832
3177
  return p;
2833
3178
  }
2834
3179
 
2835
3180
  case K.THEMATIC_BREAK: {
2836
3181
  const pre = mo.length + FOLD_LEN[p];
2837
3182
  p = tr_push(p, S_HR);
2838
- tr_point(sink, pre, c.start);
3183
+ tr_at(sink, pre, c);
2839
3184
  return p;
2840
3185
  }
2841
3186
 
@@ -2850,7 +3195,7 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2850
3195
  p = tr_open(c, p, S_TABLE, S_TABLE_OPEN, S_GT_LF);
2851
3196
  p = tr_table_content(c, sink, p);
2852
3197
  p = tr_push(p, S_TABLE_CLOSE);
2853
- tr_point(sink, pre, c.start);
3198
+ tr_at(sink, pre, c);
2854
3199
  return p;
2855
3200
  }
2856
3201
 
@@ -2871,14 +3216,13 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2871
3216
  if (p !== 0) mo += FOLD_STR[p];
2872
3217
  const meta = c.meta();
2873
3218
  let s = '<a';
2874
- if (meta?.href)
2875
- s += ' href="' + escape_text_html(meta.href as string) + '"';
3219
+ if (meta?.href) s += url_attr(meta.href as string, false);
2876
3220
  if (meta?.title)
2877
3221
  s += ' title="' + escape_text_html(meta.title as string) + '"';
2878
3222
  mo = mo + s + _attrs(c, LINK_HANDLED);
2879
3223
  p = tr_children(c, sink, S_GT);
2880
3224
  p = tr_push(p, S_A_CLOSE);
2881
- tr_point(sink, pre, c.start);
3225
+ tr_at(sink, pre, c);
2882
3226
  return p;
2883
3227
  }
2884
3228
 
@@ -2887,21 +3231,21 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2887
3231
  if (p !== 0) mo += FOLD_STR[p];
2888
3232
  const meta = c.meta();
2889
3233
  let s = '<img';
2890
- if (meta?.src) s += ' src="' + escape_text_html(meta.src as string) + '"';
3234
+ if (meta?.src) s += url_attr(meta.src as string, true);
2891
3235
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
2892
3236
  if (meta?.title)
2893
3237
  s += ' title="' + escape_text_html(meta.title as string) + '"';
2894
3238
  mo = mo + s + _attrs(c, IMAGE_HANDLED);
2895
3239
  // the syntax spans are empty, only the node is recorded
2896
- tr_point(sink, pre, c.start);
3240
+ tr_at(sink, pre, c);
2897
3241
  return S_SELF_CLOSE;
2898
3242
  }
2899
3243
 
2900
3244
  case K.HTML_COMMENT: {
2901
3245
  const pre = mo.length + FOLD_LEN[p];
2902
3246
  p = tr_push(p, S_COMMENT_OPEN);
2903
- tr_text(c, sink, p, c.text());
2904
- tr_point(sink, pre, c.start);
3247
+ tr_text(c, sink, p, held_text(c));
3248
+ tr_at(sink, pre, c);
2905
3249
  return S_COMMENT_CLOSE;
2906
3250
  }
2907
3251
 
@@ -2909,7 +3253,7 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2909
3253
  const pre = mo.length + FOLD_LEN[p];
2910
3254
  p = tr_push(p, S_BRACE_OPEN);
2911
3255
  tr_text(c, sink, p, c.text());
2912
- tr_point(sink, pre, c.start);
3256
+ tr_at(sink, pre, c);
2913
3257
  return S_BRACE_CLOSE;
2914
3258
  }
2915
3259
 
@@ -2921,27 +3265,64 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2921
3265
  const text = c.text();
2922
3266
  mo = mo + '{@' + meta_str(tag);
2923
3267
  if (text) tr_text(c, sink, S_SPACE, text);
2924
- tr_point(sink, pre, c.start);
3268
+ tr_at(sink, pre, c);
2925
3269
  return S_BRACE_CLOSE;
2926
3270
  }
2927
3271
 
2928
3272
  case K.IMPORT_STATEMENT:
2929
3273
  if (c.index !== hoist_at) return p;
2930
3274
  if (p !== 0) mo += FOLD_STR[p];
2931
- mo += '<script>\n';
3275
+ mo += hoist_open;
2932
3276
  mo_imports(c, sink, true);
2933
3277
  mo += '</script>';
2934
3278
  return 0;
2935
3279
 
2936
3280
  default:
2937
- // a label renders only as the snippet of a replaced directive
2938
- if (c.kind === K.DIRECTIVE_LABEL) return p;
3281
+ // a label renders only as the snippet of a replaced directive, a
3282
+ // footnote definition in the section at the end and a link reference
3283
+ // definition not at all
3284
+ if (c.kind >= K.DIRECTIVE_LABEL)
3285
+ return c.kind === K.FOOTNOTE_REFERENCE
3286
+ ? fn_reference_mapped(c, sink, p, true)
3287
+ : p;
2939
3288
  // a replaced directive never gets here, see comp_ref
2940
3289
  if (dir_strict) dir_check(c);
2941
3290
  return tr_children(c, sink, p);
2942
3291
  }
2943
3292
  }
2944
3293
 
3294
+ /** render_task for the trace walk */
3295
+ function tr_task(c: Cursor, sink: MapSink, p: number, on: boolean): number {
3296
+ const box = on ? S_TASK_ON : S_TASK_OFF;
3297
+ const para = task_para(c);
3298
+ if (para === Slot.NONE) return tr_children(c, sink, tr_push(p, box));
3299
+ const n = c.words;
3300
+ const item = c.index;
3301
+ c.move_to(para);
3302
+ if (has_components && comp_ref(c) !== null) {
3303
+ task_at = para;
3304
+ task_box = FOLD_STR[box];
3305
+ p = tr_node(c, sink, p);
3306
+ task_at = -1;
3307
+ } else {
3308
+ const pre = mo.length + FOLD_LEN[p];
3309
+ p = tr_open(c, p, S_P, S_P_OPEN, S_GT);
3310
+ p = tr_push(p, box);
3311
+ p = tr_children(c, sink, p);
3312
+ p = tr_push(p, S_P_CLOSE);
3313
+ tr_at(sink, pre, c);
3314
+ }
3315
+ let next = n[para * W.stride + W.next];
3316
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === item) {
3317
+ const after = n[next * W.stride + W.next];
3318
+ c.move_to(next);
3319
+ p = tr_node(c, sink, p);
3320
+ next = after;
3321
+ }
3322
+ c.move_to(item);
3323
+ return p;
3324
+ }
3325
+
2945
3326
  function tr_code_fence(c: Cursor, sink: MapSink, p: number): number {
2946
3327
  const info = fence_info(c, c.meta());
2947
3328
  if (hl !== null) {
@@ -2957,8 +3338,8 @@ function tr_code_fence(c: Cursor, sink: MapSink, p: number): number {
2957
3338
  } else {
2958
3339
  p = tr_open(c, p, S_PRE_CODE, S_PRE_CODE_OPEN, S_GT);
2959
3340
  }
2960
- tr_text(c, sink, p, escape_fence_text(c));
2961
- tr_point(sink, pre, c.start);
3341
+ fence_content(c, sink, p, escape_fence_text(c), true);
3342
+ tr_at(sink, pre, c);
2962
3343
  return S_PRE_CODE_CLOSE;
2963
3344
  }
2964
3345
 
@@ -2976,7 +3357,9 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
2976
3357
  // source passthrough and reconstruction exactly as render_node
2977
3358
  const self_closing = !!meta?.self_closing;
2978
3359
  const passthrough =
2979
- self_closing && c.end > c.start ? c.slice(c.start, c.end) : '';
3360
+ self_closing && !c.synthetic && c.end > c.start
3361
+ ? c.slice(c.start, c.end)
3362
+ : '';
2980
3363
  if (passthrough) {
2981
3364
  mo += passthrough;
2982
3365
  } else {
@@ -2985,7 +3368,7 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
2985
3368
  | Record<string, string | boolean>
2986
3369
  | undefined;
2987
3370
  if (html_attrs) {
2988
- const typed = c.end !== Slot.NONE;
3371
+ const typed = !c.synthetic;
2989
3372
  for (const k in html_attrs) {
2990
3373
  const v = html_attrs[k];
2991
3374
  if (v === true) {
@@ -2996,6 +3379,8 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
2996
3379
  s += ' ' + k + '="' + escape_attr(v as string, typed) + '"';
2997
3380
  }
2998
3381
  }
3382
+ if (meta!.repeated !== undefined)
3383
+ s += repeated_attrs(meta!.repeated as [string, unknown][], typed);
2999
3384
  }
3000
3385
  if (self_closing) {
3001
3386
  mo += s;
@@ -3008,7 +3393,7 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
3008
3393
  tr_run(sink, pre, mo.length, c.start, c.end);
3009
3394
  return 0;
3010
3395
  }
3011
- if (tag === 'script' || tag === 'style') {
3396
+ if (is_raw_text_tag(tag)) {
3012
3397
  if (c.index === hoist_script) {
3013
3398
  mo = mo + FOLD_STR[S_GT] + '\n';
3014
3399
  mo_imports(c, sink, true);
@@ -3022,7 +3407,7 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
3022
3407
  if (q !== 0) mo += FOLD_STR[q];
3023
3408
  }
3024
3409
  mo = mo + '</' + meta_str(tag);
3025
- tr_point(sink, pre, c.start);
3410
+ tr_at(sink, pre, c);
3026
3411
  return S_GT;
3027
3412
  }
3028
3413
 
@@ -3067,7 +3452,7 @@ function tr_svelte_block(c: Cursor, sink: MapSink, p: number): number {
3067
3452
  if (p !== 0) mo += FOLD_STR[p];
3068
3453
  mo = mo + '{/' + block_tag;
3069
3454
  // node span for the whole block, use the block node (goto_parent already called)
3070
- tr_point(sink, pre, c.start);
3455
+ tr_at(sink, pre, c);
3071
3456
  return S_BRACE_CLOSE;
3072
3457
  }
3073
3458
 
@@ -3224,7 +3609,6 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3224
3609
  case K.EMPHASIS:
3225
3610
  case K.STRONG:
3226
3611
  case K.BLOCK_QUOTE:
3227
- case K.LIST_ITEM:
3228
3612
  case K.STRIKETHROUGH:
3229
3613
  case K.SUPERSCRIPT:
3230
3614
  case K.SUBSCRIPT: {
@@ -3242,6 +3626,21 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3242
3626
  return p;
3243
3627
  }
3244
3628
 
3629
+ case K.LIST_ITEM: {
3630
+ const pre = mo.length + FOLD_LEN[p];
3631
+ const meta = c.meta();
3632
+ if (meta === undefined) p = tr_push(p, S_LI_OPEN);
3633
+ else p = tr_open(c, p, S_LI, S_LI_OPEN, S_GT);
3634
+ const ao = mo.length + FOLD_LEN[p];
3635
+ if (meta !== undefined && typeof meta.checked === 'boolean')
3636
+ p = mp_task(c, sink, p, meta.checked);
3637
+ else p = mp_children(c, sink, p);
3638
+ const bc = mo.length + FOLD_LEN[p];
3639
+ p = tr_push(p, S_LI_CLOSE);
3640
+ _spans(sink, pre, ao, bc, mo.length + FOLD_LEN[p], c, Preset.TEXT);
3641
+ return p;
3642
+ }
3643
+
3245
3644
  case K.CODE_SPAN: {
3246
3645
  if (hl !== null) {
3247
3646
  const h = hl_span(c);
@@ -3259,7 +3658,7 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3259
3658
  const pre = mo.length + FOLD_LEN[p];
3260
3659
  p = tr_open(c, p, S_CODE, S_CODE_OPEN, S_GT);
3261
3660
  const ao = mo.length + FOLD_LEN[p];
3262
- let code = escape_code_text(c);
3661
+ let code = escape_span_text(c);
3263
3662
  if (string_index_of.call(code, '\n') !== -1)
3264
3663
  code = code.replace(/\n/g, ' ');
3265
3664
  tr_content(c, sink, p, code, Code.CODE_CONTENT);
@@ -3304,7 +3703,7 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3304
3703
  c.index,
3305
3704
  Preset.STRUCTURE << 2
3306
3705
  );
3307
- if (sink.syntax) {
3706
+ if (sink.syntax && c.end !== Slot.NONE) {
3308
3707
  const vs = c.value_start;
3309
3708
  put_record(
3310
3709
  sink,
@@ -3353,8 +3752,7 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3353
3752
  if (p !== 0) mo += FOLD_STR[p];
3354
3753
  const meta = c.meta();
3355
3754
  let s = '<a';
3356
- if (meta?.href)
3357
- s += ' href="' + escape_text_html(meta.href as string) + '"';
3755
+ if (meta?.href) s += url_attr(meta.href as string, false);
3358
3756
  if (meta?.title)
3359
3757
  s += ' title="' + escape_text_html(meta.title as string) + '"';
3360
3758
  mo = mo + s + _attrs(c, LINK_HANDLED);
@@ -3371,7 +3769,7 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3371
3769
  if (p !== 0) mo += FOLD_STR[p];
3372
3770
  const meta = c.meta();
3373
3771
  let s = '<img';
3374
- if (meta?.src) s += ' src="' + escape_text_html(meta.src as string) + '"';
3772
+ if (meta?.src) s += url_attr(meta.src as string, true);
3375
3773
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
3376
3774
  if (meta?.title)
3377
3775
  s += ' title="' + escape_text_html(meta.title as string) + '"';
@@ -3393,7 +3791,7 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3393
3791
  const pre = mo.length + FOLD_LEN[p];
3394
3792
  p = tr_push(p, S_COMMENT_OPEN);
3395
3793
  const ao = mo.length + FOLD_LEN[p];
3396
- tr_content(c, sink, p, c.text(), Code.TEXT_CONTENT);
3794
+ tr_content(c, sink, p, held_text(c), Code.TEXT_CONTENT);
3397
3795
  const bc = mo.length;
3398
3796
  _spans(sink, pre, ao, bc, bc + FOLD_LEN[S_COMMENT_CLOSE], c, Preset.TEXT);
3399
3797
  return S_COMMENT_CLOSE;
@@ -3429,20 +3827,59 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3429
3827
  case K.IMPORT_STATEMENT:
3430
3828
  if (c.index !== hoist_at) return p;
3431
3829
  if (p !== 0) mo += FOLD_STR[p];
3432
- mo += '<script>\n';
3830
+ mo += hoist_open;
3433
3831
  mo_imports(c, sink, false);
3434
3832
  mo += '</script>';
3435
3833
  return 0;
3436
3834
 
3437
3835
  default:
3438
- // a label renders only as the snippet of a replaced directive
3439
- if (c.kind === K.DIRECTIVE_LABEL) return p;
3836
+ // a label renders only as the snippet of a replaced directive, a
3837
+ // footnote definition in the section at the end and a link reference
3838
+ // definition not at all
3839
+ if (c.kind >= K.DIRECTIVE_LABEL)
3840
+ return c.kind === K.FOOTNOTE_REFERENCE
3841
+ ? fn_reference_mapped(c, sink, p, false)
3842
+ : p;
3440
3843
  // a replaced directive never gets here, see comp_ref
3441
3844
  if (dir_strict) dir_check(c);
3442
3845
  return mp_children(c, sink, p);
3443
3846
  }
3444
3847
  }
3445
3848
 
3849
+ /** render_task for the mapped walk */
3850
+ function mp_task(c: Cursor, sink: MapSink, p: number, on: boolean): number {
3851
+ const box = on ? S_TASK_ON : S_TASK_OFF;
3852
+ const para = task_para(c);
3853
+ if (para === Slot.NONE) return mp_children(c, sink, tr_push(p, box));
3854
+ const n = c.words;
3855
+ const item = c.index;
3856
+ c.move_to(para);
3857
+ if (has_components && comp_ref(c) !== null) {
3858
+ task_at = para;
3859
+ task_box = FOLD_STR[box];
3860
+ p = mp_node(c, sink, p);
3861
+ task_at = -1;
3862
+ } else {
3863
+ const pre = mo.length + FOLD_LEN[p];
3864
+ p = tr_open(c, p, S_P, S_P_OPEN, S_GT);
3865
+ const ao = mo.length + FOLD_LEN[p];
3866
+ p = tr_push(p, box);
3867
+ p = mp_children(c, sink, p);
3868
+ const bc = mo.length + FOLD_LEN[p];
3869
+ p = tr_push(p, S_P_CLOSE);
3870
+ _spans(sink, pre, ao, bc, mo.length + FOLD_LEN[p], c, Preset.TEXT);
3871
+ }
3872
+ let next = n[para * W.stride + W.next];
3873
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === item) {
3874
+ const after = n[next * W.stride + W.next];
3875
+ c.move_to(next);
3876
+ p = mp_node(c, sink, p);
3877
+ next = after;
3878
+ }
3879
+ c.move_to(item);
3880
+ return p;
3881
+ }
3882
+
3446
3883
  function mp_code_fence(c: Cursor, sink: MapSink, p: number): number {
3447
3884
  const info = fence_info(c, c.meta());
3448
3885
  if (hl !== null) {
@@ -3459,7 +3896,7 @@ function mp_code_fence(c: Cursor, sink: MapSink, p: number): number {
3459
3896
  p = tr_open(c, p, S_PRE_CODE, S_PRE_CODE_OPEN, S_GT);
3460
3897
  }
3461
3898
  const ao = mo.length + FOLD_LEN[p];
3462
- tr_content(c, sink, p, escape_fence_text(c), Code.CODE_CONTENT);
3899
+ fence_content(c, sink, p, escape_fence_text(c), false);
3463
3900
  const bc = mo.length;
3464
3901
  _spans(sink, pre, ao, bc, bc + FOLD_LEN[S_PRE_CODE_CLOSE], c, Preset.CODE);
3465
3902
  return S_PRE_CODE_CLOSE;
@@ -3487,7 +3924,9 @@ function mp_html(c: Cursor, sink: MapSink, p: number): number {
3487
3924
  // source passthrough and reconstruction exactly as render_node
3488
3925
  const self_closing = !!meta?.self_closing;
3489
3926
  const passthrough =
3490
- self_closing && c.end > c.start ? c.slice(c.start, c.end) : '';
3927
+ self_closing && !c.synthetic && c.end > c.start
3928
+ ? c.slice(c.start, c.end)
3929
+ : '';
3491
3930
  if (passthrough) {
3492
3931
  mo += passthrough;
3493
3932
  } else {
@@ -3496,7 +3935,7 @@ function mp_html(c: Cursor, sink: MapSink, p: number): number {
3496
3935
  | Record<string, string | boolean>
3497
3936
  | undefined;
3498
3937
  if (html_attrs) {
3499
- const typed = c.end !== Slot.NONE;
3938
+ const typed = !c.synthetic;
3500
3939
  for (const k in html_attrs) {
3501
3940
  const v = html_attrs[k];
3502
3941
  if (v === true) {
@@ -3507,6 +3946,8 @@ function mp_html(c: Cursor, sink: MapSink, p: number): number {
3507
3946
  s += ' ' + k + '="' + escape_attr(v as string, typed) + '"';
3508
3947
  }
3509
3948
  }
3949
+ if (meta!.repeated !== undefined)
3950
+ s += repeated_attrs(meta!.repeated as [string, unknown][], typed);
3510
3951
  }
3511
3952
  mo += s;
3512
3953
  if (self_closing) {
@@ -3535,7 +3976,7 @@ function mp_html(c: Cursor, sink: MapSink, p: number): number {
3535
3976
  return 0;
3536
3977
  }
3537
3978
  const ao = mo.length + FOLD_LEN[S_GT];
3538
- if (tag === 'script' || tag === 'style') {
3979
+ if (is_raw_text_tag(tag)) {
3539
3980
  if (c.index === hoist_script) {
3540
3981
  mo = mo + FOLD_STR[S_GT] + '\n';
3541
3982
  mo_imports(c, sink, false);
@@ -3717,7 +4158,6 @@ function render_folded(c: Cursor): string {
3717
4158
  // one walker below serves the fold, trace and mapped renders
3718
4159
 
3719
4160
  const H_NAME = ['', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
3720
- const CHECKED = new Set(['checked']);
3721
4161
  const TABLE_PARTS = ['thead', 'tbody', 'tr', 'th', 'td'];
3722
4162
 
3723
4163
  /** the element a markdown node renders as, empty when it is not replaceable */
@@ -3747,9 +4187,9 @@ function comp_name(c: Cursor): string {
3747
4187
  case K.LIST_ITEM:
3748
4188
  return 'li';
3749
4189
  case K.CODE_SPAN:
3750
- return 'code';
4190
+ return c.extra === 1 && hl !== null && hl.math === true ? 'math' : 'code';
3751
4191
  case K.CODE_FENCE:
3752
- return 'pre';
4192
+ return hl !== null && hl.math === true && is_math(c) ? 'math' : 'pre';
3753
4193
  case K.LINK:
3754
4194
  return 'a';
3755
4195
  case K.IMAGE:
@@ -3758,6 +4198,8 @@ function comp_name(c: Cursor): string {
3758
4198
  return 'hr';
3759
4199
  case K.HARD_BREAK:
3760
4200
  return 'br';
4201
+ case K.FOOTNOTE_REFERENCE:
4202
+ return 'footnote';
3761
4203
  case K.TABLE:
3762
4204
  return 'table';
3763
4205
  case K.HTML:
@@ -3770,10 +4212,9 @@ function comp_name(c: Cursor): string {
3770
4212
  /** lowercase elements only, never svelte tags or components */
3771
4213
  const ELEMENT_NAME = /^[a-z][a-z0-9-]*$/;
3772
4214
 
3773
- /** the tag of an element a parse plugin created, empty for typed html, which has a source span */
4215
+ /** the tag of an element a parse plugin created, empty for typed html */
3774
4216
  function plugin_tag(c: Cursor): string {
3775
- const e = c.end;
3776
- return e !== Slot.NONE && e > c.start ? '' : html_tag(c);
4217
+ return c.synthetic ? html_tag(c) : '';
3777
4218
  }
3778
4219
 
3779
4220
  /** the tag of an element, typed or from a plugin */
@@ -3859,6 +4300,9 @@ function comp_scan(c: Cursor): void {
3859
4300
  if (k >= K.DIRECTIVE_INLINE && k <= K.DIRECTIVE_CONTAINER) {
3860
4301
  const ref = dir_scope!.get(dir_name(c));
3861
4302
  if (ref !== undefined) note_use(ref);
4303
+ } else if (k === K.FOOTNOTE_DEFINITION) {
4304
+ // a definition is in the tree where it was written, the section holds it
4305
+ comp_use('footnotes');
3862
4306
  } else {
3863
4307
  const name = comp_name(c);
3864
4308
  if (name !== '') {
@@ -3986,7 +4430,8 @@ function cm_text(
3986
4430
  c: Cursor,
3987
4431
  sink: MapSink | undefined,
3988
4432
  p: number,
3989
- text: string
4433
+ text: string,
4434
+ code: number = Code.CODE_CONTENT
3990
4435
  ): number {
3991
4436
  if (comp_mode === CM.FOLD) return push_dyn(p, text);
3992
4437
  if (p !== 0) mo += FOLD_STR[p];
@@ -4001,7 +4446,7 @@ function cm_text(
4001
4446
  c.value_start,
4002
4447
  c.value_end,
4003
4448
  c.index,
4004
- Code.CODE_CONTENT
4449
+ code
4005
4450
  );
4006
4451
  mo += text;
4007
4452
  return 0;
@@ -4016,7 +4461,7 @@ function cm_spans(
4016
4461
  post: number,
4017
4462
  preset: number
4018
4463
  ): void {
4019
- if (comp_mode === CM.TRACE) tr_point(sink!, pre, c.start);
4464
+ if (comp_mode === CM.TRACE) tr_at(sink!, pre, c);
4020
4465
  else if (comp_mode === CM.MAPPED) _spans(sink!, pre, ao, bc, post, c, preset);
4021
4466
  }
4022
4467
 
@@ -4027,11 +4472,11 @@ function cm_void(
4027
4472
  pre: number,
4028
4473
  preset: number
4029
4474
  ): void {
4030
- if (comp_mode === CM.TRACE) tr_point(sink!, pre, c.start);
4475
+ if (comp_mode === CM.TRACE) tr_at(sink!, pre, c);
4031
4476
  else if (comp_mode === CM.MAPPED) {
4032
4477
  const post = mo.length;
4033
4478
  put_record(sink!, pre, post, c.start, c.end, c.index, preset << 2);
4034
- if (preset === Preset.STRUCTURE && sink!.syntax) {
4479
+ if (preset === Preset.STRUCTURE && sink!.syntax && c.end !== Slot.NONE) {
4035
4480
  const vs = c.value_start;
4036
4481
  put_record(
4037
4482
  sink!,
@@ -4175,6 +4620,7 @@ function js_prop(key: string, value: string): string {
4175
4620
  * resolves it, the tree builder path keeps byte offsets
4176
4621
  */
4177
4622
  function fence_info(c: Cursor, meta: Record<string, unknown> | undefined) {
4623
+ if (fs !== null) return fence_info_from(c, meta);
4178
4624
  let info = meta?.info as string | undefined;
4179
4625
  if (!info) {
4180
4626
  const info_start = meta?.info_start as number | undefined;
@@ -4209,6 +4655,7 @@ function comp_node(
4209
4655
  let lead = '';
4210
4656
  let close = '</' + local + '>';
4211
4657
  let preset: number = Preset.TEXT;
4658
+ let raw = false;
4212
4659
 
4213
4660
  switch (kind) {
4214
4661
  case K.HEADING:
@@ -4217,8 +4664,7 @@ function comp_node(
4217
4664
 
4218
4665
  case K.LINK: {
4219
4666
  const meta = c.meta();
4220
- if (meta?.href)
4221
- open += ' href="' + escape_text_html(meta.href as string) + '"';
4667
+ if (meta?.href) open += url_attr(meta.href as string, false);
4222
4668
  if (meta?.title)
4223
4669
  open += ' title="' + escape_text_html(meta.title as string) + '"';
4224
4670
  open += _attrs(c, LINK_HANDLED);
@@ -4227,8 +4673,7 @@ function comp_node(
4227
4673
 
4228
4674
  case K.IMAGE: {
4229
4675
  const meta = c.meta();
4230
- if (meta?.src)
4231
- open += ' src="' + escape_text_html(meta.src as string) + '"';
4676
+ if (meta?.src) open += url_attr(meta.src as string, true);
4232
4677
  open += ' alt="' + escape_text_html(_children_raw(c)) + '"';
4233
4678
  if (meta?.title)
4234
4679
  open += ' title="' + escape_text_html(meta.title as string) + '"';
@@ -4245,6 +4690,15 @@ function comp_node(
4245
4690
  case K.HARD_BREAK:
4246
4691
  return cm_put(p, open + _attrs(c) + ' />\n');
4247
4692
 
4693
+ case K.FOOTNOTE_REFERENCE:
4694
+ return fn_component(c, sink, p, pre, open, close);
4695
+
4696
+ case K.PARAGRAPH:
4697
+ open += _attrs(c);
4698
+ // the first paragraph of a task item, see task_para
4699
+ if (c.index === task_at) lead = task_box;
4700
+ break;
4701
+
4248
4702
  case K.LIST: {
4249
4703
  const meta = c.meta();
4250
4704
  open += _attrs(c);
@@ -4260,9 +4714,9 @@ function comp_node(
4260
4714
 
4261
4715
  case K.LIST_ITEM: {
4262
4716
  const checked = c.meta()?.checked;
4263
- if (typeof checked === 'boolean')
4264
- open += _attrs(c, CHECKED) + ' checked={' + checked + '}';
4265
- else open += _attrs(c);
4717
+ open += _attrs(c);
4718
+ // the replacement draws its own checkbox
4719
+ if (typeof checked === 'boolean') open += ' checked={' + checked + '}';
4266
4720
  close += '\n';
4267
4721
  break;
4268
4722
  }
@@ -4283,11 +4737,15 @@ function comp_node(
4283
4737
  case K.CODE_SPAN: {
4284
4738
  if (hl !== null) {
4285
4739
  const h = hl_span(c);
4286
- if (h !== null) return comp_hl_code(c, sink, p, h, open, close);
4740
+ if (h !== null)
4741
+ // comp_name picked the replacement, math or code
4742
+ return ref.name === 'math'
4743
+ ? comp_hl_span_math(c, sink, p, h, open, close)
4744
+ : comp_hl_code(c, sink, p, h, open, close);
4287
4745
  }
4288
4746
  p = cm_put(p, open + _attrs(c) + '>');
4289
4747
  const ao = mo.length;
4290
- let code = escape_code_text(c);
4748
+ let code = escape_span_text(c);
4291
4749
  if (string_index_of.call(code, '\n') !== -1)
4292
4750
  code = code.replace(/\n/g, ' ');
4293
4751
  p = cm_text(c, sink, p, code);
@@ -4301,7 +4759,11 @@ function comp_node(
4301
4759
  const info = fence_info(c, c.meta());
4302
4760
  if (hl !== null) {
4303
4761
  const b = hl_block(c, info);
4304
- if (b !== null) return comp_hl_pre(c, sink, p, b, info, open, close);
4762
+ if (b !== null)
4763
+ // comp_name picked the replacement, math or pre
4764
+ return ref.name === 'math'
4765
+ ? comp_hl_math(c, sink, p, b, info!, open, close)
4766
+ : comp_hl_pre(c, sink, p, b, info, open, close);
4305
4767
  }
4306
4768
  open += _attrs(c);
4307
4769
  let inner = '<code>';
@@ -4317,9 +4779,14 @@ function comp_node(
4317
4779
  }
4318
4780
  // children are the element, svelte keeps whitespace only inside a <pre>
4319
4781
  // it can see, code is the raw text
4782
+ if (fs !== null) open += fence_props(c);
4320
4783
  p = cm_put(p, open + js_prop('code', fence_text(c)) + '><pre>' + inner);
4321
4784
  const ao = mo.length;
4322
- p = cm_text(c, sink, p, escape_fence_text(c));
4785
+ const text = escape_fence_text(c);
4786
+ p =
4787
+ comp_mode === CM.FOLD
4788
+ ? push_dyn(p, text)
4789
+ : fence_content(c, sink!, p, text, comp_mode === CM.TRACE);
4323
4790
  const bc = mo.length;
4324
4791
  p = cm_put(p, '</code></pre>' + close);
4325
4792
  cm_spans(c, sink, pre, ao, bc, mo.length, Preset.CODE);
@@ -4330,7 +4797,7 @@ function comp_node(
4330
4797
  const meta = c.meta()!;
4331
4798
  const tag = meta.tag as string;
4332
4799
  const attrs = meta.attributes as Record<string, unknown> | undefined;
4333
- const typed = c.end !== Slot.NONE && c.end > c.start;
4800
+ const typed = !c.synthetic;
4334
4801
  const end = typed ? open_tag_end(c, tag, attrs) : -1;
4335
4802
  // a typed tag keeps its attributes as written, they map as identity
4336
4803
  const from = c.start + 1 + tag.length;
@@ -4344,6 +4811,8 @@ function comp_node(
4344
4811
  open += expr_attr(k, meta_str((v as any).value));
4345
4812
  else open += ' ' + k + '="' + escape_attr(meta_str(v), typed) + '"';
4346
4813
  }
4814
+ if (meta.repeated !== undefined)
4815
+ open += repeated_attrs(meta.repeated as [string, unknown][], typed);
4347
4816
  }
4348
4817
  if (typed) open += typed_extras(tag, attrs);
4349
4818
  const shut = meta.self_closing ? ' />' : '>';
@@ -4362,6 +4831,7 @@ function comp_node(
4362
4831
  cm_void(c, sink, pre, Preset.TEXT);
4363
4832
  return p;
4364
4833
  }
4834
+ raw = is_raw_text_tag(tag);
4365
4835
  break;
4366
4836
  }
4367
4837
 
@@ -4371,7 +4841,8 @@ function comp_node(
4371
4841
 
4372
4842
  p = cm_put(p, open + '>' + lead);
4373
4843
  const ao = mo.length;
4374
- p = kind === K.TABLE ? cm_table(c, sink, p) : cm_children(c, sink, p);
4844
+ if (raw) p = cm_text(c, sink, p, c.text(), Code.SVELTE_CONTENT);
4845
+ else p = kind === K.TABLE ? cm_table(c, sink, p) : cm_children(c, sink, p);
4375
4846
  const bc = mo.length;
4376
4847
  p = cm_put(p, close);
4377
4848
  cm_spans(c, sink, pre, ao, bc, mo.length, preset);
@@ -4909,6 +5380,8 @@ export interface TemplateWrapper {
4909
5380
  specifier: string;
4910
5381
  /** true spreads the module script metadata export as props */
4911
5382
  metadata: boolean;
5383
+ /** a module script export passed as the prop of its name, after the metadata */
5384
+ data?: string;
4912
5385
  }
4913
5386
 
4914
5387
  const WRAP_LOCAL = 'Template_MDSVEX';
@@ -4930,18 +5403,22 @@ const WRAP_HOISTED = new Set([
4930
5403
  // set by wrap_begin, '' when the render wraps nothing
4931
5404
  let wrap_spec = '';
4932
5405
  let wrap_metadata = false;
5406
+ let wrap_data = '';
4933
5407
  let wrap_open = '';
4934
5408
  let wrap_mode: number = CM.FOLD;
4935
5409
 
4936
5410
  function wrap_begin(wrapper: TemplateWrapper, mode: number): void {
4937
5411
  wrap_spec = wrapper.specifier;
4938
5412
  wrap_metadata = wrapper.metadata;
5413
+ const data = wrapper.data;
5414
+ if (data !== undefined) wrap_data = data;
4939
5415
  wrap_mode = mode;
4940
5416
  }
4941
5417
 
4942
5418
  function wrap_end(): void {
4943
5419
  wrap_spec = '';
4944
5420
  wrap_metadata = false;
5421
+ wrap_data = '';
4945
5422
  wrap_open = '';
4946
5423
  wrap_mode = CM.FOLD;
4947
5424
  comp_lines = '';
@@ -4976,34 +5453,8 @@ function wrap_pass(
4976
5453
  const k = n[b] & 0xff;
4977
5454
  if (k !== K.LINE_BREAK) {
4978
5455
  c.move_to(child);
4979
- if (wrap_hoists(c, k) === hoisted) {
4980
- if (wrap_mode === CM.FOLD) p = fold_node(c, p);
4981
- else if (k === K.TEXT) {
4982
- // the mapped walks render text in their children loops
4983
- const vs = n[b + W.value_start];
4984
- const ve = n[b + W.value_end];
4985
- const t = escape_text_at(c, child, vs, ve, false);
4986
- if (p !== 0) mo += FOLD_STR[p];
4987
- p = 0;
4988
- if (vs !== Slot.NONE && ve > vs) {
4989
- const at = mo.length;
4990
- if (wrap_mode === CM.TRACE)
4991
- tr_run(sink!, at, at + t.length, vs, ve);
4992
- else
4993
- put_record(
4994
- sink!,
4995
- at,
4996
- at + t.length,
4997
- vs,
4998
- ve,
4999
- child,
5000
- Code.TEXT_CONTENT
5001
- );
5002
- }
5003
- mo += t;
5004
- } else if (wrap_mode === CM.TRACE) p = tr_node(c, sink!, p);
5005
- else p = mp_node(c, sink!, p);
5006
- }
5456
+ if (wrap_hoists(c, k) === hoisted)
5457
+ p = walk_child(c, sink, p, wrap_mode, child, k);
5007
5458
  }
5008
5459
  child = n[b + W.next];
5009
5460
  }
@@ -5011,15 +5462,46 @@ function wrap_pass(
5011
5462
  return p;
5012
5463
  }
5013
5464
 
5014
- /** c is at the root, the hoisted children then the rest inside the wrapper */
5015
- function wrap_root(c: Cursor, sink: MapSink | undefined, p: number): number {
5016
- p = wrap_pass(c, sink, p, true);
5017
- p = cm_wrap(p, wrap_open);
5018
- p = wrap_pass(c, sink, p, false);
5019
- return cm_wrap(p, '</' + WRAP_LOCAL + '>');
5020
- }
5021
-
5022
- function cm_wrap(p: number, s: string): number {
5465
+ /** the child of kind k the cursor is at, through the walk of mode */
5466
+ function walk_child(
5467
+ c: Cursor,
5468
+ sink: MapSink | undefined,
5469
+ p: number,
5470
+ mode: number,
5471
+ child: number,
5472
+ k: number
5473
+ ): number {
5474
+ if (mode === CM.FOLD) return fold_node(c, p);
5475
+ if (k === K.TEXT) {
5476
+ // the mapped walks render text in their children loops
5477
+ const n = c.words;
5478
+ const b = child * W.stride;
5479
+ const vs = n[b + W.value_start];
5480
+ const ve = n[b + W.value_end];
5481
+ const t = escape_text_at(c, child, vs, ve, false);
5482
+ if (p !== 0) mo += FOLD_STR[p];
5483
+ if (vs !== Slot.NONE && ve > vs) {
5484
+ const at = mo.length;
5485
+ if (mode === CM.TRACE) tr_run(sink!, at, at + t.length, vs, ve);
5486
+ else
5487
+ put_record(sink!, at, at + t.length, vs, ve, child, Code.TEXT_CONTENT);
5488
+ }
5489
+ mo += t;
5490
+ return 0;
5491
+ }
5492
+ return mode === CM.TRACE ? tr_node(c, sink!, p) : mp_node(c, sink!, p);
5493
+ }
5494
+
5495
+ /** c is at the root, the hoisted children then the rest inside the wrapper */
5496
+ function wrap_root(c: Cursor, sink: MapSink | undefined, p: number): number {
5497
+ p = wrap_pass(c, sink, p, true);
5498
+ p = cm_wrap(p, wrap_open);
5499
+ p = wrap_pass(c, sink, p, false);
5500
+ if (fn !== null) p = fn_section(c, sink, p, wrap_mode);
5501
+ return cm_wrap(p, '</' + WRAP_LOCAL + '>');
5502
+ }
5503
+
5504
+ function cm_wrap(p: number, s: string): number {
5023
5505
  if (wrap_mode === CM.FOLD) return push_dyn(p, s);
5024
5506
  if (p !== 0) mo += FOLD_STR[p];
5025
5507
  mo += s;
@@ -5035,8 +5517,11 @@ function _render_block(cursor: Cursor): string {
5035
5517
 
5036
5518
  // block entry
5037
5519
 
5520
+ /** the idx of the block that holds the footnotes section, which is no node */
5521
+ export const FOOTNOTES_BLOCK = -1;
5522
+
5038
5523
  export interface CursorBlockEntry {
5039
- /** node buffer index, use as keyed each key. */
5524
+ /** node buffer index, use as keyed each key, FOOTNOTES_BLOCK for the footnotes section */
5040
5525
  idx: number;
5041
5526
  /** rendered html string. */
5042
5527
  html: string;
@@ -5100,6 +5585,30 @@ export class CursorHTMLRenderer {
5100
5585
  */
5101
5586
  // declared, so a renderer that never wraps builds and keeps no field
5102
5587
  declare template?: TemplateWrapper;
5588
+ /**
5589
+ * import statements the next renders add to the instance script, with or
5590
+ * without a replacement, the cached render of update ignores them
5591
+ */
5592
+ declare imports?: readonly string[];
5593
+ /** replaces the src of images and the href of links in the next renders, unset keeps every url */
5594
+ declare urls?: UrlLookup;
5595
+ /**
5596
+ * the fences of the next renders that take their code, info string or pre
5597
+ * props from elsewhere, unset reads every fence from the source
5598
+ */
5599
+ declare fences?: FenceSources;
5600
+ /**
5601
+ * curly quotes, dashes and ellipses in the prose of the next renders, unset
5602
+ * leaves text as written, as does a text node that holds a stored string
5603
+ */
5604
+ declare smart_punctuation?: SmartPunctuation;
5605
+ /**
5606
+ * the footnotes of the next renders with their numbers, unset numbers the
5607
+ * references of a document as its labels are first met
5608
+ */
5609
+ declare footnotes?: FootnoteRender;
5610
+ /** the cached render met a footnote, so each update numbers them again */
5611
+ private noted = false;
5103
5612
 
5104
5613
  constructor(opts?: { cache?: boolean }) {
5105
5614
  this.cache = opts?.cache ?? true;
@@ -5120,8 +5629,17 @@ export class CursorHTMLRenderer {
5120
5629
  const c = this.cursor;
5121
5630
  c.reset();
5122
5631
  esc_reset(source);
5632
+ // ahead of the rest, it throws for an option it can not read
5633
+ const smart = this.smart_punctuation;
5634
+ if (smart !== undefined) sp_begin(smart);
5123
5635
  hl = this.highlight;
5124
5636
  pm = this.pre_meta;
5637
+ const urls = this.urls;
5638
+ if (urls !== undefined) ul = urls;
5639
+ const fences = this.fences;
5640
+ if (fences !== undefined) fs = fences;
5641
+ const notes = this.footnotes;
5642
+ if (notes !== undefined) fn = notes;
5125
5643
 
5126
5644
  // no caching, single-pass full render
5127
5645
  if (!this.cache) {
@@ -5131,6 +5649,8 @@ export class CursorHTMLRenderer {
5131
5649
  this.html = this.render_wrapped(c, buf, null, false, code);
5132
5650
  return this.blocks;
5133
5651
  }
5652
+ const imports = this.imports;
5653
+ const extra = imports !== undefined && imports.length !== 0;
5134
5654
  try {
5135
5655
  if (
5136
5656
  (scope !== null && scope.size !== 0) ||
@@ -5143,30 +5663,42 @@ export class CursorHTMLRenderer {
5143
5663
  CM.FOLD,
5144
5664
  this.replace_typed
5145
5665
  );
5666
+ if (extra) extra_begin(imports);
5146
5667
  if (this.strict_directives) dir_strict = true;
5147
- hoist_begin(buf);
5148
- if (code) module_begin(buf, code);
5668
+ hoist_begin(c, buf, extra);
5669
+ if (code) module_begin_fold(buf, code);
5149
5670
  prebuilt_begin(buf);
5150
5671
  this.html = render_folded(c);
5672
+ if (fn !== null) this.html += fn_section_folded(c);
5151
5673
  } finally {
5674
+ fn = null;
5152
5675
  hl = null;
5153
5676
  pm = null;
5677
+ if (urls !== undefined) ul = null;
5678
+ if (fences !== undefined) fs = null;
5679
+ if (smart !== undefined) sp_end();
5154
5680
  esc_prebuilt = true;
5155
5681
  esc_bits = null;
5156
5682
  if (dir_strict) dir_strict = false;
5157
5683
  if (comp_scope !== null) comp_end();
5684
+ if (extra) extra_end();
5158
5685
  if (code) module_end();
5159
5686
  }
5160
5687
  return this.blocks;
5161
5688
  }
5162
5689
 
5163
5690
  try {
5164
- hoist_begin(buf);
5691
+ hoist_begin(c, buf, false);
5165
5692
  if (code) module_begin(buf, code);
5693
+ if (this.noted && fn === null) fn = footnote_index(buf, true);
5166
5694
  this.update_blocks(c);
5167
5695
  } finally {
5696
+ fn = null;
5168
5697
  hl = null;
5169
5698
  pm = null;
5699
+ if (urls !== undefined) ul = null;
5700
+ if (fences !== undefined) fs = null;
5701
+ if (smart !== undefined) sp_end();
5170
5702
  if (code) module_end();
5171
5703
  }
5172
5704
  return this.blocks;
@@ -5176,6 +5708,13 @@ export class CursorHTMLRenderer {
5176
5708
  private update_blocks(c: Cursor): void {
5177
5709
  if (!c.goto_first_child()) return;
5178
5710
 
5711
+ // the section of the last update is no child of the root
5712
+ const blocks = this.blocks;
5713
+ if (
5714
+ blocks.length !== 0 &&
5715
+ blocks[blocks.length - 1].idx === FOOTNOTES_BLOCK
5716
+ )
5717
+ blocks.pop();
5179
5718
  let block_idx = 0;
5180
5719
  do {
5181
5720
  if (c.kind === K.LINE_BREAK) continue;
@@ -5201,6 +5740,12 @@ export class CursorHTMLRenderer {
5201
5740
  } while (c.goto_next_sibling());
5202
5741
 
5203
5742
  c.goto_parent();
5743
+ // a block may have met the first footnote of the document
5744
+ if (fn !== null) {
5745
+ this.noted = true;
5746
+ const html = fn_section_folded(c);
5747
+ if (html !== '') blocks.push({ idx: FOOTNOTES_BLOCK, html });
5748
+ }
5204
5749
  this.html = this.blocks.map((b) => b.html).join('');
5205
5750
  }
5206
5751
 
@@ -5220,8 +5765,17 @@ export class CursorHTMLRenderer {
5220
5765
  const c = this.cursor;
5221
5766
  c.reset();
5222
5767
  esc_reset(source);
5768
+ // ahead of the rest, it throws for an option it can not read
5769
+ const smart = this.smart_punctuation;
5770
+ if (smart !== undefined) sp_begin(smart);
5223
5771
  hl = this.highlight;
5224
5772
  pm = this.pre_meta;
5773
+ const urls = this.urls;
5774
+ if (urls !== undefined) ul = urls;
5775
+ const fences = this.fences;
5776
+ if (fences !== undefined) fs = fences;
5777
+ const notes = this.footnotes;
5778
+ if (notes !== undefined) fn = notes;
5225
5779
 
5226
5780
  if (this.template !== undefined) {
5227
5781
  this.html = this.render_wrapped(c, buf, sink, trace, code);
@@ -5229,6 +5783,8 @@ export class CursorHTMLRenderer {
5229
5783
  }
5230
5784
  const scope = this.scope;
5231
5785
  const directives = this.directives;
5786
+ const imports = this.imports;
5787
+ const extra = imports !== undefined && imports.length !== 0;
5232
5788
  let p = 0;
5233
5789
  try {
5234
5790
  if (
@@ -5242,8 +5798,9 @@ export class CursorHTMLRenderer {
5242
5798
  trace ? CM.TRACE : CM.MAPPED,
5243
5799
  this.replace_typed
5244
5800
  );
5801
+ if (extra) extra_begin(imports);
5245
5802
  if (this.strict_directives) dir_strict = true;
5246
- hoist_begin(buf);
5803
+ hoist_begin(c, buf, extra);
5247
5804
  mo = comp_prefix;
5248
5805
  if (code) {
5249
5806
  module_begin(buf, code);
@@ -5253,13 +5810,19 @@ export class CursorHTMLRenderer {
5253
5810
  prebuilt_begin(buf);
5254
5811
  if (trace) p = tr_node(c, sink, 0);
5255
5812
  else p = mp_node(c, sink, 0);
5813
+ if (fn !== null) p = fn_section(c, sink, p, trace ? CM.TRACE : CM.MAPPED);
5256
5814
  } finally {
5815
+ fn = null;
5257
5816
  hl = null;
5258
5817
  pm = null;
5818
+ if (urls !== undefined) ul = null;
5819
+ if (fences !== undefined) fs = null;
5820
+ if (smart !== undefined) sp_end();
5259
5821
  esc_prebuilt = true;
5260
5822
  esc_bits = null;
5261
5823
  if (dir_strict) dir_strict = false;
5262
5824
  if (comp_scope !== null) comp_end();
5825
+ if (extra) extra_end();
5263
5826
  if (code) module_end();
5264
5827
  }
5265
5828
  // the module string would keep the document alive
@@ -5284,6 +5847,8 @@ export class CursorHTMLRenderer {
5284
5847
  ): string {
5285
5848
  const scope = this.scope;
5286
5849
  const directives = this.directives;
5850
+ const imports = this.imports;
5851
+ const extra = imports !== undefined && imports.length !== 0;
5287
5852
  const mode = sink === null ? CM.FOLD : trace ? CM.TRACE : CM.MAPPED;
5288
5853
  let html: string;
5289
5854
  try {
@@ -5292,10 +5857,14 @@ export class CursorHTMLRenderer {
5292
5857
  (directives !== null && directives.size !== 0)
5293
5858
  )
5294
5859
  comp_last = comp_begin(c, scope, directives, mode, this.replace_typed);
5860
+ if (extra) extra_begin(imports);
5295
5861
  if (this.strict_directives) dir_strict = true;
5296
5862
  wrap_begin(this.template!, mode);
5297
5863
  wrap_hoist_begin(c, buf);
5298
- if (code) module_begin(buf, code);
5864
+ if (code) {
5865
+ if (sink === null) module_begin_fold(buf, code);
5866
+ else module_begin(buf, code);
5867
+ }
5299
5868
  prebuilt_begin(buf);
5300
5869
  if (sink === null) {
5301
5870
  fold_out = comp_prefix;
@@ -5313,12 +5882,17 @@ export class CursorHTMLRenderer {
5313
5882
  if (p !== 0) html += FOLD_STR[p];
5314
5883
  }
5315
5884
  } finally {
5885
+ fn = null;
5316
5886
  hl = null;
5317
5887
  pm = null;
5888
+ ul = null;
5889
+ fs = null;
5890
+ if (sp_on) sp_end();
5318
5891
  esc_prebuilt = true;
5319
5892
  esc_bits = null;
5320
5893
  if (dir_strict) dir_strict = false;
5321
5894
  if (comp_scope !== null) comp_end();
5895
+ if (extra) extra_end();
5322
5896
  wrap_end();
5323
5897
  if (code) module_end();
5324
5898
  this.template = undefined;
@@ -5416,10 +5990,26 @@ export class CursorHTMLRenderer {
5416
5990
  sink.release();
5417
5991
  }
5418
5992
 
5993
+ /**
5994
+ * the nodes changed after their block was rendered, as the node of a
5995
+ * deferred task that applied does, apply_ready gives them, each block that
5996
+ * holds one renders again at the next update, a renderer without a cache
5997
+ * renders every block anyway
5998
+ */
5999
+ invalidate(buf: NodeBuffer, nodes: readonly number[]): void {
6000
+ const closed = this.closed;
6001
+ if (closed === null) return;
6002
+ for (let i = 0; i < nodes.length; i++) {
6003
+ const block = top_block(buf, nodes[i]);
6004
+ if (block !== Slot.NONE) closed.delete(block);
6005
+ }
6006
+ }
6007
+
5419
6008
  reset(): void {
5420
6009
  this.blocks.length = 0;
5421
6010
  this.closed?.clear();
5422
6011
  this.html = '';
6012
+ this.noted = false;
5423
6013
  }
5424
6014
 
5425
6015
  /** drop every reference to the last render so a kept renderer holds no document */
@@ -5438,6 +6028,13 @@ export class CursorHTMLRenderer {
5438
6028
  // a tag is a slice that would keep the source alive
5439
6029
  comp_last = NO_WARNINGS;
5440
6030
  if (this.template !== undefined) this.template = undefined;
6031
+ if (this.imports !== undefined) this.imports = undefined;
6032
+ if (this.urls !== undefined) this.urls = undefined;
6033
+ if (this.fences !== undefined) this.fences = undefined;
6034
+ if (this.smart_punctuation !== undefined)
6035
+ this.smart_punctuation = undefined;
6036
+ if (this.footnotes !== undefined) this.footnotes = undefined;
6037
+ this.noted = false;
5441
6038
  // the escape index is module state and would keep the source alive,
5442
6039
  // every render resets it, a zero length clamps any text of '' to empty
5443
6040
  esc_src = '';
@@ -5451,12 +6048,14 @@ export class CursorHTMLRenderer {
5451
6048
  export function _mapped_begin(
5452
6049
  buf: NodeBuffer,
5453
6050
  source: string,
5454
- html: string
6051
+ html: string,
6052
+ urls: UrlLookup | null = null
5455
6053
  ): void {
5456
6054
  esc_reset(source);
5457
6055
  prebuilt_begin(buf);
5458
6056
  // callers render parts, they place imports and scripts themselves
5459
6057
  hoist_reset();
6058
+ ul = urls;
5460
6059
  mo = html;
5461
6060
  }
5462
6061
 
@@ -5464,6 +6063,7 @@ export function _mapped_begin(
5464
6063
  export function _mapped_end(): string {
5465
6064
  esc_prebuilt = true;
5466
6065
  esc_bits = null;
6066
+ ul = null;
5467
6067
  const html = mo;
5468
6068
  mo = '';
5469
6069
  return html;
@@ -5483,6 +6083,84 @@ let hl: CodeHighlighter | null = null;
5483
6083
  /** the meta conventions of the render in progress for a plain pre replacement */
5484
6084
  let pm: PreMeta | null = null;
5485
6085
 
6086
+ /** the extra import lines of the render in progress, '' for none, hoist_begin adds them to comp_lines */
6087
+ let extra_lines = '';
6088
+
6089
+ /** extra_end follows it, in a finally */
6090
+ function extra_begin(lines: readonly string[]): void {
6091
+ let s = '';
6092
+ for (let i = 0; i < lines.length; i++) s = s + lines[i] + '\n';
6093
+ extra_lines = s;
6094
+ }
6095
+
6096
+ function extra_end(): void {
6097
+ extra_lines = '';
6098
+ // a render with no replacement never reaches comp_end
6099
+ comp_lines = '';
6100
+ comp_prefix = '';
6101
+ }
6102
+
6103
+ /** the url lookup of the render in progress, null keeps every url */
6104
+ let ul: UrlLookup | null = null;
6105
+
6106
+ /** the src or href attribute of an image or link, every walk writes it here or in the fold twin */
6107
+ function url_attr(url: string, image: boolean): string {
6108
+ if (ul === null)
6109
+ return (image ? ' src="' : ' href="') + escape_text_html(url) + '"';
6110
+ return url_swap(url, image);
6111
+ }
6112
+
6113
+ /** fold twin of url_attr, without a lookup the statics stay in the register */
6114
+ function fold_url(p: number, url: string, image: boolean): number {
6115
+ if (ul !== null) return push_dyn(p, url_swap(url, image));
6116
+ p = push_static(p, image ? S_SRC : S_HREF);
6117
+ p = push_dyn(p, escape_text_html(url));
6118
+ return push_static(p, S_QUOTE);
6119
+ }
6120
+
6121
+ /** url_attr of a render with a lookup */
6122
+ function url_swap(url: string, image: boolean): string {
6123
+ const name = image ? ' src=' : ' href=';
6124
+ let to = ul!(url, image);
6125
+ if (to == null) to = url;
6126
+ if (typeof to !== 'string') return name + '{' + to.value + '}';
6127
+ // an empty url of the source has no attribute either
6128
+ return to === '' ? '' : name + '"' + escape_text_html(to) + '"';
6129
+ }
6130
+
6131
+ /** the fences of the render in progress that take their code from elsewhere, null reads every one from the source */
6132
+ let fs: FenceSources | null = null;
6133
+
6134
+ /** fence_text of a render with fence sources */
6135
+ function fence_text_from(c: Cursor): string {
6136
+ const from = fs!.get(c.index);
6137
+ if (from !== undefined) return from.text;
6138
+ return held_text(c);
6139
+ }
6140
+
6141
+ /** fence_info of a render with fence sources */
6142
+ function fence_info_from(
6143
+ c: Cursor,
6144
+ meta: Record<string, unknown> | undefined
6145
+ ): string | undefined {
6146
+ const given = fs!.get(c.index)?.info;
6147
+ if (given !== undefined) return given;
6148
+ let info = meta?.info as string | undefined;
6149
+ if (!info) {
6150
+ const info_start = meta?.info_start as number | undefined;
6151
+ const info_end = meta?.info_end as number | undefined;
6152
+ if (info_start != null && info_end != null)
6153
+ info = c.slice(info_start, info_end);
6154
+ }
6155
+ return info;
6156
+ }
6157
+
6158
+ /** the extra props of the pre replacement of the fence at c */
6159
+ function fence_props(c: Cursor): string {
6160
+ const props = fs!.get(c.index)?.props;
6161
+ return props === undefined ? '' : props;
6162
+ }
6163
+
5486
6164
  /** the first space or tab of an info string, -1 for none */
5487
6165
  function info_space(info: string): number {
5488
6166
  for (let i = 0; i < info.length; i++) {
@@ -5504,6 +6182,12 @@ function info_meta(info: string): string {
5504
6182
  return space === -1 ? '' : info.slice(space).trim();
5505
6183
  }
5506
6184
 
6185
+ /** true for a fence whose language is math */
6186
+ function is_math(c: Cursor): boolean {
6187
+ const info = fence_info(c, c.meta());
6188
+ return info !== undefined && info_lang(info) === 'math';
6189
+ }
6190
+
5507
6191
  /** the fence at c through the highlighter, null renders it plain */
5508
6192
  function hl_block(
5509
6193
  c: Cursor,
@@ -5517,11 +6201,16 @@ function hl_block(
5517
6201
  );
5518
6202
  }
5519
6203
 
5520
- /** the code span at c through the highlighter when it has a #! hint */
6204
+ /**
6205
+ * the code span at c through the highlighter when it has a #! hint, or as
6206
+ * inline math when it is one, without a math renderer that is plain code
6207
+ */
5521
6208
  function hl_span(c: Cursor): HighlightedCode | null {
6209
+ if (c.extra === 1)
6210
+ return hl!.math === true ? hl!.inline_math!(held_text(c), c.start) : null;
5522
6211
  const lang = fence_info(c, c.meta());
5523
6212
  if (!lang) return null;
5524
- let code = c.text();
6213
+ let code = held_text(c);
5525
6214
  if (string_index_of.call(code, '\n') !== -1) code = code.replace(/\n/g, ' ');
5526
6215
  return hl!.inline(code, lang, c.start);
5527
6216
  }
@@ -5590,7 +6279,7 @@ function tr_hl(
5590
6279
  if (live !== null) hl_live_trace(sink, c, mo.length, body, live);
5591
6280
  else if (body.length !== 0) tr_point(sink, mo.length, c.value_start);
5592
6281
  mo += body + tail;
5593
- tr_point(sink, pre, c.start);
6282
+ tr_at(sink, pre, c);
5594
6283
  return 0;
5595
6284
  }
5596
6285
 
@@ -5659,6 +6348,7 @@ function comp_hl_pre(
5659
6348
  if (dropped !== null)
5660
6349
  for (let i = 0; i < dropped.length; i++)
5661
6350
  hl!.warn('meta_prop_ignored', dropped[i], c.start);
6351
+ if (fs !== null) open += fence_props(c);
5662
6352
  open +=
5663
6353
  b.props +
5664
6354
  (b.code_template === null
@@ -5680,6 +6370,40 @@ function comp_hl_pre(
5680
6370
  );
5681
6371
  }
5682
6372
 
6373
+ /**
6374
+ * display math as the math replacement, the children are what renders
6375
+ * without one, the block gives the props that say which math it is
6376
+ */
6377
+ function comp_hl_math(
6378
+ c: Cursor,
6379
+ sink: MapSink | undefined,
6380
+ p: number,
6381
+ b: HighlightedBlock,
6382
+ info: string,
6383
+ open: string,
6384
+ close: string
6385
+ ): number {
6386
+ const pre = mo.length;
6387
+ const meta = info_meta(info);
6388
+ open += b.props + js_prop('code', b.code);
6389
+ if (meta) open += js_prop('meta', meta);
6390
+ return cm_hl(c, sink, p, pre, open + _attrs(c) + '>', b.body, close, null);
6391
+ }
6392
+
6393
+ /** inline math as the math replacement, which takes code and no display */
6394
+ function comp_hl_span_math(
6395
+ c: Cursor,
6396
+ sink: MapSink | undefined,
6397
+ p: number,
6398
+ h: HighlightedCode,
6399
+ open: string,
6400
+ close: string
6401
+ ): number {
6402
+ const pre = mo.length;
6403
+ open += js_prop('code', c.text()) + _attrs(c) + '>';
6404
+ return cm_hl(c, sink, p, pre, open, h.body, close, null);
6405
+ }
6406
+
5683
6407
  /** a highlighted code span as the code replacement, which also takes lang */
5684
6408
  function comp_hl_code(
5685
6409
  c: Cursor,
@@ -5710,8 +6434,7 @@ function comp_hl_code(
5710
6434
  */
5711
6435
  function hl_source(c: Cursor, text: string, at: number, len: number): number {
5712
6436
  const vs = c.value_start;
5713
- if (c.prebuilt === undefined && bq_depth(c.words, c.index) === 0)
5714
- return vs + at;
6437
+ if (c.prebuilt === undefined) return vs + at;
5715
6438
  const src = c.source;
5716
6439
  const ve = c.value_end;
5717
6440
  let line_start = vs;
@@ -5744,8 +6467,21 @@ function hl_live_records(
5744
6467
  body: string,
5745
6468
  live: number[]
5746
6469
  ): void {
5747
- const text = fence_text(c);
5748
6470
  const idx = c.index;
6471
+ // code from elsewhere has no source to map an expression to
6472
+ if (fs !== null && fs.has(idx)) {
6473
+ put_record(
6474
+ sink,
6475
+ at,
6476
+ at + body.length,
6477
+ c.value_start,
6478
+ c.value_end,
6479
+ idx,
6480
+ Code.CODE_CONTENT
6481
+ );
6482
+ return;
6483
+ }
6484
+ const text = fence_text(c);
5749
6485
  let gen = at;
5750
6486
  let src = c.value_start;
5751
6487
  for (let i = 0; i < live.length; i += 3) {
@@ -5771,6 +6507,10 @@ function hl_live_trace(
5771
6507
  body: string,
5772
6508
  live: number[]
5773
6509
  ): void {
6510
+ if (fs !== null && fs.has(c.index)) {
6511
+ if (body.length !== 0) tr_point(sink, at, c.value_start);
6512
+ return;
6513
+ }
5774
6514
  const text = fence_text(c);
5775
6515
  let gen = at;
5776
6516
  let src = c.value_start;
@@ -5787,3 +6527,902 @@ function hl_live_trace(
5787
6527
  const end = at + body.length;
5788
6528
  if (end > gen) tr_run(sink, gen, end, src, c.value_end);
5789
6529
  }
6530
+
6531
+ // smart punctuation is off for most renders, so its bindings sit last too
6532
+
6533
+ /** true while a render with smart punctuation runs, its text goes through smart_text */
6534
+ let sp_on = false;
6535
+ /** the option the fields below were read from */
6536
+ let sp_opt: SmartPunctuation | undefined;
6537
+ let sp_quotes = false;
6538
+ let sp_dashes = false;
6539
+ let sp_dots = false;
6540
+ let sp_open2 = '“';
6541
+ let sp_close2 = '”';
6542
+ let sp_open1 = '‘';
6543
+ let sp_close1 = '’';
6544
+
6545
+ // an escape index of its own, as esc_src and its pointers, so a render
6546
+ // without the option reads and writes none of it
6547
+ let sp_src = '';
6548
+ let sp_len = 0;
6549
+ let sp_lo = 0;
6550
+ let sp_amp = -1;
6551
+ let sp_lt = -1;
6552
+ let sp_gt = -1;
6553
+ let sp_quot = -1;
6554
+ let sp_apos = -1;
6555
+ let sp_dash = -1;
6556
+ let sp_dot = -1;
6557
+ /** the least of the seven when they were last moved, a slice that ends by it has no hit */
6558
+ let sp_min = -1;
6559
+
6560
+ /** the parent whose text sp_plain answered last, text nodes arrive in runs of one parent */
6561
+ let sp_parent = -1;
6562
+ let sp_parent_plain = false;
6563
+
6564
+ // a child list has no back links, the walk moves forward, so the pair the
6565
+ // last search found is where the next one starts
6566
+ let sp_trail: number = Slot.NONE;
6567
+ let sp_trail_prev: number = Slot.NONE;
6568
+
6569
+ /** how a neighbour of a quote reads */
6570
+ const enum Side {
6571
+ /** renders nothing, the neighbour is further on */
6572
+ NOTHING = -1,
6573
+ /** whitespace, a break, or the edge of the block */
6574
+ SPACE = 0,
6575
+ /** an opening bracket or a dash, a quote after one opens */
6576
+ OPENER = 1,
6577
+ /** a letter or a digit, or an inline node that is not text */
6578
+ WORD = 2,
6579
+ OTHER = 3,
6580
+ }
6581
+
6582
+ /** what a quote becomes */
6583
+ const enum Quote {
6584
+ STRAIGHT = 0,
6585
+ OPEN = 1,
6586
+ CLOSE = 2,
6587
+ /** a closing quote with a word on both sides */
6588
+ APOSTROPHE = 3,
6589
+ }
6590
+
6591
+ /** typed html that starts and ends a block, so no text reads on across its tags */
6592
+ const SP_BLOCK_TAGS = new Set([
6593
+ 'address',
6594
+ 'article',
6595
+ 'aside',
6596
+ 'blockquote',
6597
+ 'br',
6598
+ 'caption',
6599
+ 'dd',
6600
+ 'details',
6601
+ 'dialog',
6602
+ 'div',
6603
+ 'dl',
6604
+ 'dt',
6605
+ 'fieldset',
6606
+ 'figcaption',
6607
+ 'figure',
6608
+ 'footer',
6609
+ 'form',
6610
+ 'h1',
6611
+ 'h2',
6612
+ 'h3',
6613
+ 'h4',
6614
+ 'h5',
6615
+ 'h6',
6616
+ 'header',
6617
+ 'hr',
6618
+ 'li',
6619
+ 'main',
6620
+ 'nav',
6621
+ 'ol',
6622
+ 'p',
6623
+ 'pre',
6624
+ 'section',
6625
+ 'summary',
6626
+ 'table',
6627
+ 'tbody',
6628
+ 'td',
6629
+ 'tfoot',
6630
+ 'th',
6631
+ 'thead',
6632
+ 'tr',
6633
+ 'ul',
6634
+ ]);
6635
+
6636
+ /** typed html whose text is code, or is read as written */
6637
+ const SP_PLAIN_TAGS = new Set([
6638
+ 'code',
6639
+ 'kbd',
6640
+ 'pre',
6641
+ 'samp',
6642
+ 'var',
6643
+ 'tt',
6644
+ 'textarea',
6645
+ 'script',
6646
+ 'style',
6647
+ ]);
6648
+
6649
+ /**
6650
+ * escape_text_at of a value that ends past esc_len, which is zero for a
6651
+ * render with smart punctuation, so all of its text comes here and a render
6652
+ * without the option runs no check for it
6653
+ */
6654
+ function escape_past(
6655
+ c: Cursor,
6656
+ i: number,
6657
+ vs: number,
6658
+ ve: number,
6659
+ code: boolean
6660
+ ): string {
6661
+ if (sp_on)
6662
+ return code
6663
+ ? escape_code(c.source.slice(vs, ve))
6664
+ : smart_text(c, i, vs, ve);
6665
+ // a value cut short by the source, as a fed document has
6666
+ return esc_len <= vs ? '' : escape_text_at(c, i, vs, esc_len, code);
6667
+ }
6668
+
6669
+ /** after esc_reset, sp_end follows it, in a finally */
6670
+ function sp_begin(opt: SmartPunctuation): void {
6671
+ if (opt !== sp_opt) sp_read(opt);
6672
+ if (!(sp_quotes || sp_dashes || sp_dots)) return;
6673
+ sp_on = true;
6674
+ sp_parent = -1;
6675
+ sp_trail = Slot.NONE;
6676
+ // every value is then past the end and goes to escape_past
6677
+ esc_len = 0;
6678
+ }
6679
+
6680
+ function sp_end(): void {
6681
+ sp_on = false;
6682
+ // the indexes would keep the source alive, and the next text resets esc_len
6683
+ sp_src = '';
6684
+ sp_len = 0;
6685
+ esc_src = '';
6686
+ }
6687
+
6688
+ /** a quote string is text, a brace in it would start an expression */
6689
+ function sp_quote(s: unknown): string {
6690
+ if (typeof s !== 'string')
6691
+ throw new TypeError('smart_punctuation.quotes takes four strings');
6692
+ const text = escape_text_html(s);
6693
+ return BRACE_TEST.test(text) ? text.replace(BRACE_MATCH, code_replace) : text;
6694
+ }
6695
+
6696
+ function sp_read(opt: SmartPunctuation): void {
6697
+ // a throw below leaves no half read option behind
6698
+ sp_opt = undefined;
6699
+ sp_open2 = '“';
6700
+ sp_close2 = '”';
6701
+ sp_open1 = '‘';
6702
+ sp_close1 = '’';
6703
+ if (typeof opt === 'boolean') {
6704
+ sp_quotes = sp_dashes = sp_dots = opt;
6705
+ } else {
6706
+ const quotes = opt.quotes;
6707
+ sp_quotes = quotes !== false;
6708
+ sp_dashes = opt.dashes !== false;
6709
+ sp_dots = opt.ellipses !== false;
6710
+ if (typeof quotes === 'object') {
6711
+ if (!Array.isArray(quotes) || quotes.length !== 4)
6712
+ throw new TypeError('smart_punctuation.quotes takes four strings');
6713
+ sp_open2 = sp_quote(quotes[0]);
6714
+ sp_close2 = sp_quote(quotes[1]);
6715
+ sp_open1 = sp_quote(quotes[2]);
6716
+ sp_close1 = sp_quote(quotes[3]);
6717
+ }
6718
+ }
6719
+ sp_opt = opt;
6720
+ }
6721
+
6722
+ function sp_reset(src: string): void {
6723
+ sp_src = src;
6724
+ const len = (sp_len = src.length);
6725
+ sp_lo = 0;
6726
+ sp_amp = -1;
6727
+ sp_lt = -1;
6728
+ sp_gt = -1;
6729
+ sp_quot = -1;
6730
+ // a kind that is off never hits
6731
+ sp_apos = sp_quotes ? -1 : len;
6732
+ sp_dash = sp_dashes ? -1 : len;
6733
+ sp_dot = sp_dots ? -1 : len;
6734
+ sp_min = -1;
6735
+ }
6736
+
6737
+ function sp_next(needle: string, from: number): number {
6738
+ const i = sp_src.indexOf(needle, from);
6739
+ return i === -1 ? sp_len : i;
6740
+ }
6741
+
6742
+ /**
6743
+ * escape_text_at for the prose of a render with smart punctuation, the slice
6744
+ * is node i or a run of text and soft breaks from it, as fold_children joins
6745
+ * them, a quote at either end of the slice asks the tree
6746
+ */
6747
+ function smart_text(c: Cursor, i: number, vs: number, ve: number): string {
6748
+ const src = c.source;
6749
+ if (src !== sp_src || vs < sp_lo) sp_reset(src);
6750
+ if (ve > sp_len) {
6751
+ ve = sp_len;
6752
+ if (ve <= vs) return '';
6753
+ }
6754
+ let m = sp_min;
6755
+ // with the least pointer past the slice none is behind it and none hits
6756
+ if (m < ve) m = sp_seek(vs);
6757
+ const first = src.charCodeAt(vs);
6758
+ if ((first === 123 || first === 125) && brace_escaped(src, vs)) {
6759
+ const brace = first === 123 ? '&#123;' : '&#125;';
6760
+ if (m >= ve) return brace + src.slice(vs + 1, ve);
6761
+ return brace + smart_hits(c, i, src, vs, ve, m, vs + 1);
6762
+ }
6763
+ if (m >= ve) return src.slice(vs, ve);
6764
+ return smart_hits(c, i, src, vs, ve, m, vs);
6765
+ }
6766
+
6767
+ /** moves every pointer to its first match at or after vs, returns the least */
6768
+ function sp_seek(vs: number): number {
6769
+ if (sp_amp < vs) sp_amp = sp_next('&', vs);
6770
+ if (sp_lt < vs) sp_lt = sp_next('<', vs);
6771
+ if (sp_gt < vs) sp_gt = sp_next('>', vs);
6772
+ if (sp_quot < vs) sp_quot = sp_next('"', vs);
6773
+ if (sp_apos < vs) sp_apos = sp_next("'", vs);
6774
+ if (sp_dash < vs) sp_dash = sp_next('--', vs);
6775
+ if (sp_dot < vs) sp_dot = sp_next('...', vs);
6776
+ sp_lo = vs;
6777
+ let m = sp_amp;
6778
+ if (sp_lt < m) m = sp_lt;
6779
+ if (sp_gt < m) m = sp_gt;
6780
+ if (sp_quot < m) m = sp_quot;
6781
+ if (sp_apos < m) m = sp_apos;
6782
+ if (sp_dash < m) m = sp_dash;
6783
+ if (sp_dot < m) m = sp_dot;
6784
+ return (sp_min = m);
6785
+ }
6786
+
6787
+ /** m is the first hit at or after vs, the text is copied from pos */
6788
+ function smart_hits(
6789
+ c: Cursor,
6790
+ i: number,
6791
+ src: string,
6792
+ vs: number,
6793
+ ve: number,
6794
+ m: number,
6795
+ pos: number
6796
+ ): string {
6797
+ // asked once a node has a hit, a node with none never walks up
6798
+ const smart = !sp_plain(c, i);
6799
+ // an escape opens its text node at the escaped char
6800
+ const escaped = brace_escaped(src, vs);
6801
+ let text = '';
6802
+ while (m < ve) {
6803
+ const ch = src.charCodeAt(m);
6804
+ text += src.slice(pos, m);
6805
+ pos = m + 1;
6806
+ if (ch === 38) {
6807
+ sp_amp = sp_next('&', m + 1);
6808
+ if (text_reference(src, vs, ve, m)) pos = m;
6809
+ else text += '&amp;';
6810
+ } else if (ch === 60) {
6811
+ text += '&lt;';
6812
+ sp_lt = sp_next('<', m + 1);
6813
+ } else if (ch === 62) {
6814
+ text += '&gt;';
6815
+ sp_gt = sp_next('>', m + 1);
6816
+ } else if (ch === 34 || ch === 39) {
6817
+ if (ch === 34) sp_quot = sp_next('"', m + 1);
6818
+ else sp_apos = sp_next("'", m + 1);
6819
+ const side =
6820
+ smart && sp_quotes && !(escaped && m === vs)
6821
+ ? quote_side(c, i, src, vs, ve, m)
6822
+ : Quote.STRAIGHT;
6823
+ if (side === Quote.STRAIGHT) {
6824
+ if (ch === 34) text += '&quot;';
6825
+ else pos = m;
6826
+ } else if (side === Quote.OPEN) text += ch === 34 ? sp_open2 : sp_open1;
6827
+ else if (ch === 34) text += sp_close2;
6828
+ else text += side === Quote.APOSTROPHE ? '’' : sp_close1;
6829
+ } else {
6830
+ // a run of hyphens or dots, only one of an exact length is replaced
6831
+ let end = m + 1;
6832
+ while (end < ve && src.charCodeAt(end) === ch) end++;
6833
+ const dash = ch === 45;
6834
+ if (dash) sp_dash = sp_next('--', end);
6835
+ else sp_dot = sp_next('...', end);
6836
+ const run = end - m;
6837
+ if (
6838
+ smart &&
6839
+ (run === 3 || (run === 2 && dash)) &&
6840
+ !(escaped && m === vs) &&
6841
+ // the run goes on in the next text node, behind a backslash
6842
+ !(
6843
+ end === ve &&
6844
+ src.charCodeAt(ve) === 92 &&
6845
+ src.charCodeAt(ve + 1) === ch
6846
+ ) &&
6847
+ !(run === 2 && is_flag(c, i, src, vs, ve, m))
6848
+ )
6849
+ text += dash ? (run === 2 ? '–' : '—') : '…';
6850
+ else text += src.slice(m, end);
6851
+ pos = end;
6852
+ }
6853
+ m = sp_amp;
6854
+ if (sp_lt < m) m = sp_lt;
6855
+ if (sp_gt < m) m = sp_gt;
6856
+ if (sp_quot < m) m = sp_quot;
6857
+ if (sp_apos < m) m = sp_apos;
6858
+ if (sp_dash < m) m = sp_dash;
6859
+ if (sp_dot < m) m = sp_dot;
6860
+ }
6861
+ // every pointer is now at or past ve
6862
+ sp_lo = ve;
6863
+ sp_min = m;
6864
+ return text + src.slice(pos, ve);
6865
+ }
6866
+
6867
+ /** two hyphens after a space and before a letter are a command line flag */
6868
+ function is_flag(
6869
+ c: Cursor,
6870
+ i: number,
6871
+ src: string,
6872
+ vs: number,
6873
+ ve: number,
6874
+ m: number
6875
+ ): boolean {
6876
+ if (m + 2 >= ve) return false;
6877
+ const after = src.charCodeAt(m + 2) | 32;
6878
+ if (after < 97 || after > 122) return false;
6879
+ const before = m > vs ? sp_side(src.charCodeAt(m - 1)) : sp_left(c, i);
6880
+ return before === Side.SPACE;
6881
+ }
6882
+
6883
+ /**
6884
+ * which way the quote at m faces, from the character before it and the one
6885
+ * after, in an unbroken run of quotes each takes the side of the first
6886
+ */
6887
+ function quote_side(
6888
+ c: Cursor,
6889
+ i: number,
6890
+ src: string,
6891
+ vs: number,
6892
+ ve: number,
6893
+ m: number
6894
+ ): number {
6895
+ let q = m;
6896
+ while (q > vs) {
6897
+ const ch = src.charCodeAt(q - 1);
6898
+ if (ch !== 34 && ch !== 39) break;
6899
+ q--;
6900
+ }
6901
+ let before: number;
6902
+ if (q > vs) before = sp_side(src.charCodeAt(q - 1));
6903
+ else if (q < m && brace_escaped(src, vs)) {
6904
+ // the run starts at a quote a backslash escaped, which stays as written
6905
+ before = Side.OTHER;
6906
+ q = m;
6907
+ } else before = sp_left(c, i);
6908
+ const after =
6909
+ m + 1 < ve
6910
+ ? sp_side(src.charCodeAt(m + 1))
6911
+ : sp_right(c, sp_last(c, i, ve));
6912
+ if (q < m)
6913
+ return (before === Side.SPACE || before === Side.OPENER) &&
6914
+ after !== Side.SPACE
6915
+ ? Quote.OPEN
6916
+ : Quote.CLOSE;
6917
+ if (before === Side.SPACE)
6918
+ return after === Side.SPACE ? Quote.STRAIGHT : Quote.OPEN;
6919
+ if (before === Side.OPENER && after !== Side.SPACE) return Quote.OPEN;
6920
+ return before === Side.WORD && after === Side.WORD
6921
+ ? Quote.APOSTROPHE
6922
+ : Quote.CLOSE;
6923
+ }
6924
+
6925
+ /** how a character reads beside a quote, only the apostrophe asks for a word */
6926
+ function sp_side(ch: number): number {
6927
+ if (ch <= 32 || ch === 0xa0) return Side.SPACE;
6928
+ if (ch < 128) {
6929
+ if (ch === 40 || ch === 91 || ch === 123 || ch === 45) return Side.OPENER;
6930
+ if (ch >= 48 && ch <= 57) return Side.WORD;
6931
+ const lower = ch | 32;
6932
+ return lower >= 97 && lower <= 122 ? Side.WORD : Side.OTHER;
6933
+ }
6934
+ if (ch === 0x2013 || ch === 0x2014) return Side.OPENER;
6935
+ if (ch < 0xc0) return Side.OTHER;
6936
+ if (ch >= 0x2000 && ch <= 0x206f)
6937
+ return ch <= 0x200a ||
6938
+ ch === 0x2028 ||
6939
+ ch === 0x2029 ||
6940
+ ch === 0x202f ||
6941
+ ch === 0x205f
6942
+ ? Side.SPACE
6943
+ : Side.OTHER;
6944
+ if (ch === 0x1680 || ch === 0x3000) return Side.SPACE;
6945
+ if (ch > 0x3000 && ch <= 0x303f) return Side.OTHER;
6946
+ // a letter of any script, no table says which of the rest are not
6947
+ return Side.WORD;
6948
+ }
6949
+
6950
+ /** an inline node whose children read on from what is beside it */
6951
+ function sp_inline(c: Cursor, node: number): boolean {
6952
+ const kind = c.words[node * W.stride] & 0xff;
6953
+ if (kind === K.HTML) {
6954
+ const at = c.index;
6955
+ c.move_to(node);
6956
+ const tag = c.meta()?.tag;
6957
+ c.move_to(at);
6958
+ return typeof tag !== 'string' || !SP_BLOCK_TAGS.has(tag);
6959
+ }
6960
+ return (
6961
+ kind === K.EMPHASIS ||
6962
+ kind === K.STRONG ||
6963
+ kind === K.STRIKETHROUGH ||
6964
+ kind === K.SUPERSCRIPT ||
6965
+ kind === K.SUBSCRIPT ||
6966
+ kind === K.LINK ||
6967
+ kind === K.DIRECTIVE_INLINE
6968
+ );
6969
+ }
6970
+
6971
+ /** the sibling before node i, or NONE */
6972
+ function sp_before(n: Uint32Array, i: number): number {
6973
+ const parent = n[i * W.stride + W.parent];
6974
+ if (parent === Slot.NONE) return Slot.NONE;
6975
+ const first = n[parent * W.stride + W.first_child];
6976
+ let at = sp_trail;
6977
+ let prev = sp_trail_prev;
6978
+ if (at === Slot.NONE || n[at * W.stride + W.parent] !== parent) {
6979
+ at = first;
6980
+ prev = Slot.NONE;
6981
+ }
6982
+ // the second pass is for a node before the trail
6983
+ for (let pass = 0; pass < 2; pass++) {
6984
+ while (at !== Slot.NONE && n[at * W.stride + W.parent] === parent) {
6985
+ if (at === i) {
6986
+ sp_trail = at;
6987
+ sp_trail_prev = prev;
6988
+ return prev;
6989
+ }
6990
+ prev = at;
6991
+ at = n[at * W.stride + W.next];
6992
+ }
6993
+ at = first;
6994
+ prev = Slot.NONE;
6995
+ }
6996
+ return Slot.NONE;
6997
+ }
6998
+
6999
+ /** how the text before node i ends, in the sibling before it or before its inline parent */
7000
+ function sp_left(c: Cursor, i: number): number {
7001
+ const n = c.words;
7002
+ for (;;) {
7003
+ let prev = sp_before(n, i);
7004
+ while (prev !== Slot.NONE) {
7005
+ const side = sp_edge(c, prev, true);
7006
+ if (side !== Side.NOTHING) return side;
7007
+ prev = sp_before(n, prev);
7008
+ }
7009
+ const parent = n[i * W.stride + W.parent];
7010
+ if (parent === Slot.NONE || !sp_inline(c, parent)) return Side.SPACE;
7011
+ i = parent;
7012
+ }
7013
+ }
7014
+
7015
+ /** the text node of a run from i whose value ends at ve */
7016
+ function sp_last(c: Cursor, i: number, ve: number): number {
7017
+ const n = c.words;
7018
+ const parent = n[i * W.stride + W.parent];
7019
+ for (;;) {
7020
+ const b = i * W.stride;
7021
+ const kind = n[b] & 0xff;
7022
+ if (kind === K.TEXT) {
7023
+ if (n[b + W.value_end] >= ve) return i;
7024
+ } else if (kind !== K.SOFT_BREAK) return i;
7025
+ const next = n[b + W.next];
7026
+ if (next === Slot.NONE || n[next * W.stride + W.parent] !== parent)
7027
+ return i;
7028
+ i = next;
7029
+ }
7030
+ }
7031
+
7032
+ /** how the text after node i starts, in the sibling after it or after its inline parent */
7033
+ function sp_right(c: Cursor, i: number): number {
7034
+ const n = c.words;
7035
+ for (;;) {
7036
+ const parent = n[i * W.stride + W.parent];
7037
+ let next = n[i * W.stride + W.next];
7038
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === parent) {
7039
+ const side = sp_edge(c, next, false);
7040
+ if (side !== Side.NOTHING) return side;
7041
+ next = n[next * W.stride + W.next];
7042
+ }
7043
+ if (parent === Slot.NONE || !sp_inline(c, parent)) return Side.SPACE;
7044
+ i = parent;
7045
+ }
7046
+ }
7047
+
7048
+ /** how a node reads from outside, at its end when last is set, at its start otherwise */
7049
+ function sp_edge(c: Cursor, node: number, last: boolean): number {
7050
+ const n = c.words;
7051
+ const b = node * W.stride;
7052
+ const kind = n[b] & 0xff;
7053
+ if (kind === K.TEXT) {
7054
+ const s = c.prebuilt_at(node);
7055
+ if (s !== undefined) {
7056
+ if (s.length === 0) return Side.NOTHING;
7057
+ return sp_side(s.charCodeAt(last ? s.length - 1 : 0));
7058
+ }
7059
+ const vs = n[b + W.value_start];
7060
+ let ve = n[b + W.value_end];
7061
+ const src = c.source;
7062
+ if (ve !== Slot.NONE && ve > src.length) ve = src.length;
7063
+ if (vs === Slot.NONE || ve === Slot.NONE || ve <= vs) return Side.NOTHING;
7064
+ if (!last) return sp_side(src.charCodeAt(vs));
7065
+ const ch = src.charCodeAt(ve - 1);
7066
+ // a quote that opened is an opener for the quote after it
7067
+ if (
7068
+ (ch === 34 || ch === 39) &&
7069
+ sp_quotes &&
7070
+ !(ve - 1 === vs && brace_escaped(src, vs)) &&
7071
+ !sp_plain(c, node)
7072
+ )
7073
+ return quote_side(c, node, src, vs, ve, ve - 1) === Quote.OPEN
7074
+ ? Side.OPENER
7075
+ : Side.OTHER;
7076
+ return sp_side(ch);
7077
+ }
7078
+ if (kind === K.SOFT_BREAK || kind === K.HARD_BREAK || kind === K.LINE_BREAK)
7079
+ return Side.SPACE;
7080
+ if (kind === K.HTML_COMMENT) return Side.NOTHING;
7081
+ if (
7082
+ kind === K.CODE_SPAN ||
7083
+ kind === K.MUSTACHE ||
7084
+ kind === K.IMAGE ||
7085
+ kind === K.SVELTE_TAG
7086
+ )
7087
+ return Side.WORD;
7088
+ if (!sp_inline(c, node)) return Side.SPACE;
7089
+ let child = n[b + W.first_child];
7090
+ if (child === Slot.NONE) return Side.WORD;
7091
+ if (last)
7092
+ for (;;) {
7093
+ const next = n[child * W.stride + W.next];
7094
+ if (next === Slot.NONE || n[next * W.stride + W.parent] !== node) break;
7095
+ child = next;
7096
+ }
7097
+ const side = sp_edge(c, child, last);
7098
+ // an empty text or a comment at the edge, what is past it is not asked
7099
+ return side === Side.NOTHING ? Side.WORD : side;
7100
+ }
7101
+
7102
+ /**
7103
+ * true when the text of node i is not prose, the url of an autolink or the
7104
+ * text of typed html that holds code, at any depth
7105
+ */
7106
+ function sp_plain(c: Cursor, i: number): boolean {
7107
+ const n = c.words;
7108
+ let p = n[i * W.stride + W.parent];
7109
+ if (p === sp_parent) return sp_parent_plain;
7110
+ sp_parent = p;
7111
+ let plain = false;
7112
+ if (
7113
+ p !== Slot.NONE &&
7114
+ (n[p * W.stride] & 0xff) === K.LINK &&
7115
+ c.source.charCodeAt(n[p * W.stride + W.start]) === 60
7116
+ )
7117
+ plain = true;
7118
+ const at = c.index;
7119
+ while (!plain && p !== Slot.NONE) {
7120
+ if ((n[p * W.stride] & 0xff) === K.HTML) {
7121
+ c.move_to(p);
7122
+ const tag = c.meta()?.tag;
7123
+ if (typeof tag === 'string' && SP_PLAIN_TAGS.has(tag)) plain = true;
7124
+ }
7125
+ p = n[p * W.stride + W.parent];
7126
+ }
7127
+ c.move_to(at);
7128
+ return (sp_parent_plain = plain);
7129
+ }
7130
+
7131
+ // footnotes are cold beside the walks too, so their bindings follow
7132
+
7133
+ /** how the footnotes of a render are numbered and what its section says */
7134
+ export interface FootnoteRender extends FootnoteIndex {
7135
+ /** the accessible name of the section, Footnotes when unset */
7136
+ label?: string;
7137
+ /** the accessible name of a link back to a reference, {n} is its number, Back to reference {n} when unset */
7138
+ back_label?: string;
7139
+ }
7140
+
7141
+ /**
7142
+ * the footnotes of the render in progress, null until a walk meets one, a
7143
+ * render that was given none then numbers them itself, every reference as
7144
+ * its label is first met, which suits a document that is still arriving
7145
+ */
7146
+ let fn: FootnoteRender | null = null;
7147
+
7148
+ function fn_get(c: Cursor): FootnoteRender {
7149
+ if (fn !== null) return fn;
7150
+ const made: FootnoteRender = footnote_index(c.buffer, true)!;
7151
+ fn = made;
7152
+ return made;
7153
+ }
7154
+
7155
+ /** an id as the fragment of a url, a label with half a surrogate pair has no encoding and goes as it is */
7156
+ function fn_fragment(id: string): string {
7157
+ try {
7158
+ return encodeURIComponent(id);
7159
+ } catch {
7160
+ return id;
7161
+ }
7162
+ }
7163
+
7164
+ function fn_href(id: string): string {
7165
+ return escape_code(fn_fragment(id));
7166
+ }
7167
+
7168
+ /** the id a footnote id is given, another when a heading was given it first */
7169
+ function fn_id(notes: FootnoteRender, id: string): string {
7170
+ const moved = notes.moved;
7171
+ return moved === undefined || moved === null ? id : (moved.get(id) ?? id);
7172
+ }
7173
+
7174
+ /** the link a reference renders as, '' for one in a definition that is not rendered */
7175
+ function fn_reference(c: Cursor): string {
7176
+ const notes = fn_get(c);
7177
+ const at = notes.references.get(c.index);
7178
+ if (at === undefined) return '';
7179
+ const number = Math.floor(at / 65536);
7180
+ return (
7181
+ '<sup><a href="#' +
7182
+ fn_href(fn_id(notes, footnote_id(notes.labels[number - 1]))) +
7183
+ '" id="' +
7184
+ escape_code(fn_id(notes, footnote_ref_id(number, at % 65536))) +
7185
+ '" role="doc-noteref">' +
7186
+ number +
7187
+ '</a></sup>'
7188
+ );
7189
+ }
7190
+
7191
+ /** fn_reference for the trace and the mapped walk, the node is one record */
7192
+ function fn_reference_mapped(
7193
+ c: Cursor,
7194
+ sink: MapSink,
7195
+ p: number,
7196
+ trace: boolean
7197
+ ): number {
7198
+ const s = fn_reference(c);
7199
+ if (p !== 0) mo += FOLD_STR[p];
7200
+ const pre = mo.length;
7201
+ mo += s;
7202
+ if (trace) tr_at(sink, pre, c);
7203
+ else put_record(sink, pre, mo.length, c.start, c.end, c.index, 0);
7204
+ return 0;
7205
+ }
7206
+
7207
+ /** appends s in the walk of mode, returns the register */
7208
+ function fn_put(p: number, s: string, mode: number): number {
7209
+ if (mode === CM.FOLD) return push_dyn(p, s);
7210
+ if (p !== 0) mo += FOLD_STR[p];
7211
+ mo += s;
7212
+ return 0;
7213
+ }
7214
+
7215
+ /** the links from footnote number back to each of its count references */
7216
+ function fn_back(notes: FootnoteRender, number: number, count: number): string {
7217
+ const label = escape_code(notes.back_label ?? 'Back to reference {n}');
7218
+ let s = '';
7219
+ for (let nth = 1; nth <= count; nth++) {
7220
+ const name = nth === 1 ? '' + number : number + '-' + nth;
7221
+ s +=
7222
+ ' <a href="#' +
7223
+ fn_href(fn_id(notes, footnote_ref_id(number, nth))) +
7224
+ '" role="doc-backlink" aria-label="' +
7225
+ label.replace('&#123;n&#125;', name) +
7226
+ // the selector keeps the arrow from being drawn as an emoji
7227
+ '">\u21a9\ufe0e' +
7228
+ (nth === 1 ? '' : '<sup>' + nth + '</sup>') +
7229
+ '</a>';
7230
+ }
7231
+ return s;
7232
+ }
7233
+
7234
+ /**
7235
+ * the paragraph the cursor is at with links before its close tag, what the
7236
+ * paragraph case of each walk writes for one outside a tight list
7237
+ */
7238
+ function fn_paragraph(
7239
+ c: Cursor,
7240
+ sink: MapSink | undefined,
7241
+ p: number,
7242
+ mode: number,
7243
+ links: string
7244
+ ): number {
7245
+ if (mode === CM.FOLD) {
7246
+ p = fold_open(c, p, S_P, S_P_OPEN, S_GT);
7247
+ p = fold_children(c, p);
7248
+ return push_static(push_dyn(p, links), S_P_CLOSE);
7249
+ }
7250
+ const pre = mo.length + FOLD_LEN[p];
7251
+ const start = c.start;
7252
+ p = tr_open(c, p, S_P, S_P_OPEN, S_GT);
7253
+ if (mode === CM.TRACE) {
7254
+ p = tr_children(c, sink!, p);
7255
+ if (p !== 0) mo += FOLD_STR[p];
7256
+ mo += links;
7257
+ tr_point(sink!, pre, start);
7258
+ return S_P_CLOSE;
7259
+ }
7260
+ const ao = mo.length + FOLD_LEN[p];
7261
+ p = mp_children(c, sink!, p);
7262
+ if (p !== 0) mo += FOLD_STR[p];
7263
+ mo += links;
7264
+ const bc = mo.length;
7265
+ _spans(sink!, pre, ao, bc, bc + FOLD_LEN[S_P_CLOSE], c, Preset.TEXT);
7266
+ return S_P_CLOSE;
7267
+ }
7268
+
7269
+ /**
7270
+ * the blocks of the definition node with its back links, inside its last
7271
+ * paragraph when the walk writes that one itself, after its last block otherwise
7272
+ */
7273
+ function fn_definition(
7274
+ c: Cursor,
7275
+ sink: MapSink | undefined,
7276
+ p: number,
7277
+ mode: number,
7278
+ node: number,
7279
+ links: string
7280
+ ): number {
7281
+ const n = c.words;
7282
+ const first = n[node * W.stride + W.first_child];
7283
+ let last: number = Slot.NONE;
7284
+ for (
7285
+ let child = first;
7286
+ child !== Slot.NONE && n[child * W.stride + W.parent] === node;
7287
+ child = n[child * W.stride + W.next]
7288
+ ) {
7289
+ if ((n[child * W.stride] & 0xff) !== K.LINE_BREAK) last = child;
7290
+ }
7291
+ let placed = false;
7292
+ for (
7293
+ let child = first;
7294
+ child !== Slot.NONE && n[child * W.stride + W.parent] === node;
7295
+ child = n[child * W.stride + W.next]
7296
+ ) {
7297
+ const k = n[child * W.stride] & 0xff;
7298
+ if (k === K.LINE_BREAK) continue;
7299
+ c.move_to(child);
7300
+ if (
7301
+ child === last &&
7302
+ k === K.PARAGRAPH &&
7303
+ !(has_components && comp_ref(c) !== null)
7304
+ ) {
7305
+ p = fn_paragraph(c, sink, p, mode, links);
7306
+ placed = true;
7307
+ } else p = walk_child(c, sink, p, mode, child, k);
7308
+ }
7309
+ return placed ? p : fn_put(p, links, mode);
7310
+ }
7311
+
7312
+ /**
7313
+ * the section that holds every footnote a reference reached, in number
7314
+ * order, after the content of the root, nothing when no definition renders
7315
+ */
7316
+ function fn_section(
7317
+ c: Cursor,
7318
+ sink: MapSink | undefined,
7319
+ p: number,
7320
+ mode: number
7321
+ ): number {
7322
+ const notes = fn!;
7323
+ const definitions = notes.definitions;
7324
+ // a footnotes replacement is the section, and gets the list
7325
+ const ref = has_components ? comp_scope!.get('footnotes') : undefined;
7326
+ let open =
7327
+ ref !== undefined
7328
+ ? '<' + ref.local + '>\n<ol>\n'
7329
+ : '<section class="footnotes" role="doc-endnotes" aria-label="' +
7330
+ escape_code(notes.label ?? 'Footnotes') +
7331
+ '">\n<ol>\n';
7332
+ // the number an item gets with no value of its own
7333
+ let next = 1;
7334
+ for (let i = 0; i < definitions.length; i++) {
7335
+ const node = definitions[i];
7336
+ if (node === Slot.NONE) continue;
7337
+ let item =
7338
+ open +
7339
+ '<li id="' +
7340
+ escape_code(fn_id(notes, footnote_id(notes.labels[i]))) +
7341
+ '"';
7342
+ open = '';
7343
+ // a reference with no definition took a number, so the list skips it
7344
+ if (i + 1 !== next) item += ' value="' + (i + 1) + '"';
7345
+ next = i + 2;
7346
+ p = fn_put(p, item + '>', mode);
7347
+ p = fn_definition(
7348
+ c,
7349
+ sink,
7350
+ p,
7351
+ mode,
7352
+ node,
7353
+ fn_back(notes, i + 1, notes.counts[i])
7354
+ );
7355
+ p = fn_put(p, '</li>\n', mode);
7356
+ }
7357
+ c.move_to(0);
7358
+ if (open !== '') return p;
7359
+ return fn_put(
7360
+ p,
7361
+ ref !== undefined ? '</ol>\n</' + ref.local + '>' : '</ol>\n</section>',
7362
+ mode
7363
+ );
7364
+ }
7365
+
7366
+ const FOOTNOTE_HANDLED = new Set(['label']);
7367
+
7368
+ /** true while a note snippet is written, a reference in it gets none of its own */
7369
+ let fn_in_note = false;
7370
+
7371
+ /**
7372
+ * a reference as the footnote replacement, the link it renders as without
7373
+ * one is its children, and what its definition holds is the note snippet
7374
+ */
7375
+ function fn_component(
7376
+ c: Cursor,
7377
+ sink: MapSink | undefined,
7378
+ p: number,
7379
+ pre: number,
7380
+ open: string,
7381
+ close: string
7382
+ ): number {
7383
+ const notes = fn_get(c);
7384
+ const at = notes.references.get(c.index);
7385
+ // one in a definition that is not rendered
7386
+ if (at === undefined) return p;
7387
+ const number = Math.floor(at / 65536);
7388
+ const label = notes.labels[number - 1];
7389
+ const node = c.index;
7390
+ p = cm_put(
7391
+ p,
7392
+ open +
7393
+ js_prop('label', label) +
7394
+ ' number={' +
7395
+ number +
7396
+ '}' +
7397
+ js_prop('href', '#' + fn_fragment(fn_id(notes, footnote_id(label)))) +
7398
+ js_prop('id', fn_id(notes, footnote_ref_id(number, at % 65536))) +
7399
+ _attrs(c, FOOTNOTE_HANDLED) +
7400
+ '>'
7401
+ );
7402
+ const definition = notes.definitions[number - 1];
7403
+ // a note that refers to itself would never end
7404
+ if (definition !== Slot.NONE && !fn_in_note) {
7405
+ fn_in_note = true;
7406
+ try {
7407
+ p = cm_put(p, '{#snippet note()}');
7408
+ p = fn_definition(c, sink, p, comp_mode, definition, '');
7409
+ p = cm_put(p, '{/snippet}');
7410
+ } finally {
7411
+ fn_in_note = false;
7412
+ }
7413
+ c.move_to(node);
7414
+ }
7415
+ p = cm_put(p, fn_reference(c) + close);
7416
+ cm_void(c, sink, pre, Preset.TEXT);
7417
+ return p;
7418
+ }
7419
+
7420
+ /** fn_section as the html of the fold walk */
7421
+ function fn_section_folded(c: Cursor): string {
7422
+ fold_out = '';
7423
+ const p = fn_section(c, undefined, 0, CM.FOLD);
7424
+ let html = fold_out;
7425
+ fold_out = '';
7426
+ if (p !== 0) html += FOLD_STR[p];
7427
+ return html;
7428
+ }