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

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,28 @@
12
12
  */
13
13
 
14
14
  import { Cursor } from '@mdsvex/parse/cursor';
15
+ import {
16
+ is_raw_text_tag,
17
+ spans_have,
18
+ spans_text,
19
+ unread_spans,
20
+ } from '@mdsvex/parse/utils';
21
+ import type { AttrSpans } from '@mdsvex/parse/utils';
22
+ import {
23
+ buf_code,
24
+ footnote_id,
25
+ footnote_index,
26
+ footnote_ref_id,
27
+ } from '@mdsvex/parse/buf-utils';
28
+ import type { FootnoteIndex } from '@mdsvex/parse/buf-utils';
15
29
  import type { NodeBuffer } from '@mdsvex/parse/utils';
16
30
  import {
17
31
  MapSink,
18
32
  RecordMapping,
19
33
  record_data,
20
34
  record_mappings,
21
- } from './mappings';
22
- import type { Mapping, MappingData } from './mappings';
35
+ } from './mappings.js';
36
+ import type { Mapping, MappingData } from './mappings.js';
23
37
  import {
24
38
  records_by_offset,
25
39
  reserve_trace,
@@ -28,21 +42,24 @@ import {
28
42
  trace_free_at,
29
43
  trace_room,
30
44
  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';
45
+ } from './sourcemap.js';
46
+ import type { MapTrace, SourceMapV3 } from './sourcemap.js';
47
+ import { ComponentScope, component_imports } from './scope.js';
48
+ import type { ComponentImport, ReplaceWarning } from './scope.js';
49
+ import { is_entity_name } from './entities.js';
50
+ import { top_block } from './component.js';
51
+
52
+ export type { Mapping, CodeInformation, MappingData } from './mappings.js';
53
+ export { MapSink } from './mappings.js';
54
+ export { ComponentScope, component_imports } from './scope.js';
55
+ /** @internal a pass over the tree reads references as the renderer does */
56
+ export { is_entity_name } from './entities.js';
40
57
  export type {
41
58
  ComponentImport,
42
59
  ComponentSource,
43
60
  DefaultImport,
44
61
  ReplaceWarning,
45
- } from './scope';
62
+ } from './scope.js';
46
63
 
47
64
  /**
48
65
  * a fence the highlighter rendered, every string is markup svelte reads as
@@ -98,6 +115,16 @@ export interface CodeHighlighter {
98
115
  /** a code span with a #! hint, null renders it plain */
99
116
  inline(code: string, lang: string, start: number): HighlightedCode | null;
100
117
  warn(code: HighlightWarningCode, message: string, start: number): void;
118
+ /**
119
+ * true when block renders a math fence as display math, the fence then
120
+ * takes the math replacement and never pre, its block has no <pre>
121
+ */
122
+ readonly math?: boolean;
123
+ /**
124
+ * inline math, a code span in dollars, asked only while math is true, the
125
+ * body is the whole span and the math replacement takes it as children
126
+ */
127
+ inline_math?(code: string, start: number): HighlightedCode;
101
128
  }
102
129
 
103
130
  /** reads the fence meta conventions for a pre replacement the highlighter left plain */
@@ -109,6 +136,52 @@ export interface PreMeta {
109
136
  pre_props(meta: string, start: number): string;
110
137
  }
111
138
 
139
+ /**
140
+ * a string is escaped as a url of the source is, '' leaves the attribute out,
141
+ * an expression goes in braces, a picture names an imagetools Picture, a
142
+ * markdown image renders it as a picture element and a replacement gets it
143
+ * as its src
144
+ */
145
+ export type UrlSource =
146
+ | string
147
+ | { type: 'expression'; value: string }
148
+ | { type: 'picture'; value: string };
149
+
150
+ /** what replaces the src of an image or the href of a link, url is never empty, null or undefined keeps it */
151
+ export type UrlLookup = (
152
+ url: string,
153
+ image: boolean
154
+ ) => UrlSource | null | undefined;
155
+
156
+ /** what a fence renders in place of what the document has */
157
+ export interface FenceSource {
158
+ /** the code, in place of the body of the fence */
159
+ text: string;
160
+ /** the info string in place of the written one, '' for none, undefined keeps it */
161
+ info?: string;
162
+ /** props text for a pre replacement, each as ' name={value}', after its own props */
163
+ props?: string;
164
+ }
165
+
166
+ /** the fences that take their code from elsewhere, by node index */
167
+ export type FenceSources = ReadonlyMap<number, FenceSource>;
168
+
169
+ /** the opening and closing double quote, then the opening and closing single quote */
170
+ export type SmartQuotes = readonly [string, string, string, string];
171
+
172
+ /** each kind is on unless it is false */
173
+ export interface SmartPunctuationOptions {
174
+ /** straight quotes to curly ones, four strings replace the english ones */
175
+ quotes?: boolean | SmartQuotes;
176
+ /** two hyphens to an en dash and three to an em dash */
177
+ dashes?: boolean;
178
+ /** three dots to an ellipsis */
179
+ ellipses?: boolean;
180
+ }
181
+
182
+ /** true is every kind with english quotes, false is none */
183
+ export type SmartPunctuation = boolean | SmartPunctuationOptions;
184
+
112
185
  // must equal NONE in @mdsvex/parse
113
186
  const enum Slot {
114
187
  NONE = 0xffffffff,
@@ -220,8 +293,25 @@ function escape_code(text: string): string {
220
293
  const BRACE_TEST = /[{}]/;
221
294
  const BRACE_MATCH = /[{}]/g;
222
295
 
223
- /** an info string is an attribute, braces as code has them */
296
+ let plain_math = false;
297
+
298
+ /**
299
+ * @internal true once when a math fence rendered as code since the last
300
+ * call, a compile with a math renderer and no highlighter then knows to
301
+ * render again with one, no document is searched for math up front
302
+ */
303
+ export function take_plain_math(): boolean {
304
+ if (!plain_math) return false;
305
+ plain_math = false;
306
+ return true;
307
+ }
308
+
309
+ /**
310
+ * the language of a fence that renders plain, as an attribute, braces as
311
+ * code has them, every such fence passes here so it notes a math one
312
+ */
224
313
  function escape_info(info: string): string {
314
+ if (info === 'math') plain_math = true;
225
315
  if (!CODE_TEST.test(info)) return info;
226
316
  const s = escape_html(info);
227
317
  if (!BRACE_TEST.test(s)) return s;
@@ -305,65 +395,28 @@ function escape_node_text(c: Cursor): string {
305
395
  return escape_text_at(c, c.index, c.value_start, c.value_end, false);
306
396
  }
307
397
 
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;
398
+ function fence_text(c: Cursor): string {
399
+ if (fs !== null) return fence_text_from(c);
400
+ return held_text(c);
317
401
  }
318
402
 
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
- }
403
+ /** true for a fence, a code span or a comment a plugin put nodes in, their text is its text too */
404
+ function holds(c: Cursor): boolean {
405
+ return c.words[c.index * W.stride + W.first_child] !== Slot.NONE;
355
406
  }
356
407
 
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);
408
+ /** c.text of a fence, a code span or a comment, and then the text of the nodes a plugin put in it */
409
+ function held_text(c: Cursor): string {
410
+ return holds(c) ? buf_code(c.buffer, c.index, c.source) : c.text();
360
411
  }
361
412
 
362
413
  /** equals escape_code of fence_text */
363
414
  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));
415
+ if (fs !== null) return escape_code(fence_text_from(c));
416
+ if (holds(c)) return escape_code(buf_code(c.buffer, c.index, c.source));
417
+ // a fence in a block quote or a list item holds its code as a string, without what they put before its lines
418
+ const text = c.prebuilt;
419
+ return text === undefined ? escape_code_text(c) : escape_code(text);
367
420
  }
368
421
 
369
422
  /** equals escape_code of c.text, reading source slices through the escape index */
@@ -371,6 +424,12 @@ function escape_code_text(c: Cursor): string {
371
424
  return escape_text_at(c, c.index, c.value_start, c.value_end, true);
372
425
  }
373
426
 
427
+ /** equals escape_code of the held_text of a code span */
428
+ function escape_span_text(c: Cursor): string {
429
+ if (holds(c)) return escape_code(buf_code(c.buffer, c.index, c.source));
430
+ return escape_code_text(c);
431
+ }
432
+
374
433
  /** m is the first of & < > " at or after vs */
375
434
  function escape_code_slice(
376
435
  src: string,
@@ -442,17 +501,15 @@ function escape_text_at(
442
501
  const bits = esc_bits;
443
502
  if (bits === null || (bits[i >>> 3] & (1 << (i & 7))) !== 0) {
444
503
  const s = c.prebuilt_at(i);
445
- if (s !== undefined) return code ? escape_code(s) : escape_text_html(s);
504
+ if (s !== undefined) return stored_text(c, i, s, code);
446
505
  }
447
506
  }
448
507
  // empty cases must match Cursor.text
449
508
  if (vs === Slot.NONE || ve === Slot.NONE || ve <= vs) return '';
450
509
  const src = c.source;
451
510
  if (src !== esc_src) esc_reset(src);
452
- if (ve > esc_len) {
453
- ve = esc_len;
454
- if (ve <= vs) return '';
455
- }
511
+ // one call, a statement more here and v8 no longer inlines this function
512
+ if (ve > esc_len) return escape_past(c, i, vs, ve, code);
456
513
  if (vs < esc_lo) esc_reset(src);
457
514
  if (esc_amp < vs) esc_amp = esc_next('&', vs);
458
515
  if (esc_lt < vs) esc_lt = esc_next('<', vs);
@@ -464,10 +521,45 @@ function escape_text_at(
464
521
  if (esc_gt < m) m = esc_gt;
465
522
  if (esc_quot < m) m = esc_quot;
466
523
  if (code) return escape_code_slice(src, vs, ve, m);
524
+ const first = src.charCodeAt(vs);
525
+ if ((first === 123 || first === 125) && brace_escaped(src, vs))
526
+ return brace_text(c, i, vs, ve, first);
467
527
  if (m >= ve) return src.slice(vs, ve);
468
528
  return escape_hits(src, vs, ve, m);
469
529
  }
470
530
 
531
+ /** a stored string, apart so escape_text_at stays small enough for v8 to inline */
532
+ function stored_text(c: Cursor, i: number, s: string, code: boolean): string {
533
+ // text a plugin wrote is all characters, a brace or a reference too
534
+ return code || (c.words[i * W.stride + W.pending] & 2) !== 0
535
+ ? escape_code(s)
536
+ : escape_text_html(s);
537
+ }
538
+
539
+ /** the text of a value that opens at a brace a backslash escaped */
540
+ function brace_text(
541
+ c: Cursor,
542
+ i: number,
543
+ vs: number,
544
+ ve: number,
545
+ first: number
546
+ ): string {
547
+ return (
548
+ (first === 123 ? '&#123;' : '&#125;') +
549
+ escape_text_at(c, i, vs + 1, ve, false)
550
+ );
551
+ }
552
+
553
+ /**
554
+ * the brace at pos follows an odd run of backslashes, an escape opens its
555
+ * text node at the escaped char, and svelte would read the brace as an expression
556
+ */
557
+ function brace_escaped(src: string, pos: number): boolean {
558
+ let p = pos;
559
+ while (p > 0 && src.charCodeAt(p - 1) === 92) p--;
560
+ return ((pos - p) & 1) === 1;
561
+ }
562
+
471
563
  /** a reference in text stays as written, unless a backslash escaped its & */
472
564
  function text_reference(
473
565
  src: string,
@@ -554,6 +646,9 @@ const enum K {
554
646
  FRONTMATTER = 33,
555
647
  IMPORT_STATEMENT = 34,
556
648
  DIRECTIVE_LABEL = 35,
649
+ FOOTNOTE_REFERENCE = 36,
650
+ FOOTNOTE_DEFINITION = 37,
651
+ DEFINITION = 38,
557
652
  }
558
653
 
559
654
  export const K_ROOT = K.ROOT;
@@ -649,7 +744,8 @@ function emit_record(
649
744
 
650
745
  /**
651
746
  * offsets in html characters, every span the render records covers at least
652
- * one chunk, so none is dropped for its width
747
+ * one chunk, so none is dropped for its width, a span with no end is a node
748
+ * a parse plugin added and gets no record
653
749
  */
654
750
  function put_record(
655
751
  sink: MapSink,
@@ -660,7 +756,7 @@ function put_record(
660
756
  node_index: number,
661
757
  code: number
662
758
  ): void {
663
- if (src_start !== Slot.NONE) {
759
+ if (src_start !== Slot.NONE && src_end !== Slot.NONE) {
664
760
  let rec = sink.rec;
665
761
  const p = sink.n;
666
762
  if (p + Rec.SIZE > rec.length) rec = sink.grow();
@@ -735,6 +831,8 @@ function _spans_some(
735
831
  const idx = c.index;
736
832
  const s = c.start,
737
833
  e = c.end;
834
+ // a node a parse plugin added has no end and no place in the source
835
+ if (e === Slot.NONE) return;
738
836
  put_record(sink, pre, post, s, e, idx, preset << 2);
739
837
  if (!sink.syntax) return;
740
838
  const vs = c.value_start,
@@ -794,6 +892,8 @@ const INTERNAL_KEYS = new Set([
794
892
  'ordered',
795
893
  'tight',
796
894
  'start',
895
+ // a task item, rendered as a checkbox or passed to a replacement as a prop
896
+ 'checked',
797
897
  // code fence info (transformed into class="language-X")
798
898
  'info',
799
899
  'info_start',
@@ -835,6 +935,20 @@ function expr_attr(k: string, value: string): string {
835
935
  : ' ' + k + '={' + value + '}';
836
936
  }
837
937
 
938
+ /** the later values of the names a tag writes again, written so that svelte reports them */
939
+ function repeated_attrs(repeated: [string, unknown][], typed: boolean): string {
940
+ let s = '';
941
+ for (let i = 0; i < repeated.length; i++) {
942
+ const k = repeated[i][0];
943
+ const v = repeated[i][1];
944
+ if (v === true) s += ' ' + k;
945
+ else if (typeof v === 'object' && v !== null)
946
+ s += expr_attr(k, meta_str((v as any).value));
947
+ else s += ' ' + k + '="' + escape_attr(meta_str(v), typed) + '"';
948
+ }
949
+ return s;
950
+ }
951
+
838
952
  function _attrs(c: Cursor, skip?: Set<string>): string {
839
953
  const meta = c.meta();
840
954
  if (!meta) return '';
@@ -876,6 +990,7 @@ const enum W {
876
990
  parent = 5,
877
991
  next = 6,
878
992
  first_child = 8,
993
+ pending = 10,
879
994
  meta = 11,
880
995
  stride = 12,
881
996
  }
@@ -906,7 +1021,7 @@ const comp_warnings: ReplaceWarning[] = [];
906
1021
  const NO_WARNINGS: readonly ReplaceWarning[] = Object.freeze([]);
907
1022
  /** what the last comp_begin handed out */
908
1023
  let comp_last = NO_WARNINGS;
909
- /** import lines of the used replacements, they join the instance script */
1024
+ /** import lines of the used replacements and the extra lines, they join the instance script */
910
1025
  let comp_lines = '';
911
1026
  /** a script of comp_lines when the document has no script and no import to start one */
912
1027
  let comp_prefix = '';
@@ -928,52 +1043,29 @@ const hoist_nodes: number[] = [];
928
1043
  let module_code = '';
929
1044
  /** the module script the module code goes into, -1 renders a new one in place of the frontmatter */
930
1045
  let hoist_module = -1;
1046
+ /** the open tag of an instance script the render starts, ts for type imports or a ts module script */
1047
+ let hoist_open = '<script>\n';
931
1048
 
932
1049
  function hoist_reset(): void {
933
1050
  hoist_script = -1;
934
1051
  hoist_at = -1;
1052
+ hoist_open = '<script>\n';
935
1053
  if (hoist_nodes.length !== 0) hoist_nodes.length = 0;
936
1054
  }
937
1055
 
938
1056
  /**
939
- * find the import statements and, when there are some or replacements need
940
- * importing, the instance script
1057
+ * find the import statements and, when there are some or the render has
1058
+ * replacements or extra lines to import, the instance script, extra is passed
1059
+ * so a render with none reads no state for them
941
1060
  */
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];
1061
+ function hoist_begin(c: Cursor, buf: NodeBuffer, extra: boolean): void {
1062
+ const child = hoist_imports(buf);
1063
+ if (hoist_nodes.length === 0 && !has_components && !extra) return;
1064
+ if (extra) comp_lines += extra_lines;
1065
+ hoist_script_from(c, buf, child);
975
1066
  // no script and no import to start one, the render starts with one
976
- else comp_prefix = '<script>\n' + comp_lines + '</script>';
1067
+ if (hoist_script === -1 && hoist_at === -1)
1068
+ comp_prefix = hoist_open + comp_lines + '</script>';
977
1069
  }
978
1070
 
979
1071
  /** the import statements, returns the root child after them */
@@ -994,35 +1086,45 @@ function hoist_imports(buf: NodeBuffer): number {
994
1086
  }
995
1087
 
996
1088
  /** the instance script from child on, or the first import that starts one */
997
- function hoist_script_from(buf: NodeBuffer, child: number): void {
1089
+ function hoist_script_from(c: Cursor, buf: NodeBuffer, child: number): void {
998
1090
  const n = buf._n;
1091
+ let ts = false;
999
1092
  while (child !== Slot.NONE) {
1000
1093
  const b = child * W.stride;
1001
1094
  if (n[b + W.parent] !== 0) break;
1002
1095
  if ((n[b] & 0xff) === K.HTML) {
1003
1096
  const slot = n[b + W.meta];
1004
1097
  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;
1098
+ if (meta !== undefined && meta.tag === 'script' && !meta.self_closing) {
1099
+ const attrs = meta.attributes as Record<string, unknown> | undefined;
1100
+ if (!is_other_script(attrs)) {
1101
+ hoist_script = child;
1102
+ return;
1103
+ }
1104
+ if (attrs!.src === undefined && attrs!.lang === 'ts') ts = true;
1013
1105
  }
1014
1106
  }
1015
1107
  child = n[b + W.next];
1016
1108
  }
1017
1109
  if (hoist_nodes.length !== 0) hoist_at = hoist_nodes[0];
1110
+ for (let i = 0; !ts && i < hoist_nodes.length; i++) {
1111
+ const b = hoist_nodes[i] * W.stride;
1112
+ ts = TYPE_IMPORT.test(c.slice(n[b + W.value_start], n[b + W.value_end]));
1113
+ }
1114
+ if (ts) hoist_open = '<script lang="ts">\n';
1018
1115
  }
1019
1116
 
1117
+ /** a type import or a type specifier, svelte only parses them in a ts component */
1118
+ const TYPE_IMPORT =
1119
+ /^import\s+type(?:\s*[{*]|\s+(?!from\b)[\w$])|[{,]\s*type\s+(?!as\b)[\w$]/;
1120
+
1020
1121
  /**
1021
1122
  * hoist_begin for a wrapped render, the instance script starts with the
1022
- * replacement imports, the template import and the props it forwards
1123
+ * replacement imports, the template import, the extra lines and the props it
1124
+ * forwards
1023
1125
  */
1024
1126
  function wrap_hoist_begin(c: Cursor, buf: NodeBuffer): void {
1025
- hoist_script_from(buf, hoist_imports(buf));
1127
+ hoist_script_from(c, buf, hoist_imports(buf));
1026
1128
  let props = WRAP_PROPS;
1027
1129
  let declare = true;
1028
1130
  if (hoist_script !== -1) {
@@ -1036,20 +1138,22 @@ function wrap_hoist_begin(c: Cursor, buf: NodeBuffer): void {
1036
1138
  props = named === null ? '' : named[1];
1037
1139
  }
1038
1140
  }
1039
- comp_lines = component_imports(comp_used, {
1040
- specifier: wrap_spec,
1041
- local: WRAP_LOCAL,
1042
- });
1141
+ comp_lines =
1142
+ component_imports(comp_used, {
1143
+ specifier: wrap_spec,
1144
+ local: WRAP_LOCAL,
1145
+ }) + extra_lines;
1043
1146
  if (declare) comp_lines += 'let ' + WRAP_PROPS + ' = $props();\n';
1044
1147
  wrap_open =
1045
1148
  '<' +
1046
1149
  WRAP_LOCAL +
1047
1150
  (wrap_metadata ? ' {...metadata}' : '') +
1151
+ (wrap_data === '' ? '' : ' {' + wrap_data + '}') +
1048
1152
  (props === '' ? '' : ' {...' + props + '}') +
1049
1153
  '>';
1050
1154
  // no script and no import to start one, the render starts with one
1051
1155
  if (hoist_script === -1 && hoist_at === -1)
1052
- comp_prefix = '<script>\n' + comp_lines + '</script>';
1156
+ comp_prefix = hoist_open + comp_lines + '</script>';
1053
1157
  }
1054
1158
 
1055
1159
  /**
@@ -1066,11 +1170,13 @@ function module_begin(buf: NodeBuffer, code: string): void {
1066
1170
  if ((n[b] & 0xff) === K.HTML) {
1067
1171
  const slot = n[b + W.meta];
1068
1172
  const meta = slot === 0 ? undefined : buf._meta[slot - 1];
1069
- const attrs = meta?.attributes as Record<string, unknown> | undefined;
1173
+ const script =
1174
+ meta !== undefined && meta.tag === 'script' && !meta.self_closing;
1175
+ const attrs = script
1176
+ ? (meta.attributes as Record<string, unknown> | undefined)
1177
+ : undefined;
1070
1178
  if (
1071
- meta !== undefined &&
1072
- meta.tag === 'script' &&
1073
- !meta.self_closing &&
1179
+ script &&
1074
1180
  attrs !== undefined &&
1075
1181
  attrs.src === undefined &&
1076
1182
  (attrs.module !== undefined || attrs.context === 'module')
@@ -1083,9 +1189,24 @@ function module_begin(buf: NodeBuffer, code: string): void {
1083
1189
  }
1084
1190
  }
1085
1191
 
1192
+ /**
1193
+ * module_begin for a fold render, which meets the module code at the
1194
+ * frontmatter, a document with neither that nor a module script starts with it
1195
+ */
1196
+ function module_begin_fold(buf: NodeBuffer, code: string): void {
1197
+ module_begin(buf, code);
1198
+ if (hoist_module !== -1) return;
1199
+ const n = buf._n;
1200
+ const first = n[W.first_child];
1201
+ if (first === Slot.NONE || (n[first * W.stride] & 0xff) !== K.FRONTMATTER)
1202
+ comp_prefix += module_script();
1203
+ }
1204
+
1086
1205
  function module_end(): void {
1087
1206
  module_code = '';
1088
1207
  hoist_module = -1;
1208
+ // module_begin_fold may have left the script there
1209
+ comp_prefix = '';
1089
1210
  }
1090
1211
 
1091
1212
  /** the module script with the module code, for a document with none */
@@ -1120,7 +1241,7 @@ function is_embed_script(c: Cursor, tag: string): boolean {
1120
1241
  function embed_html(c: Cursor): string {
1121
1242
  let s = '<svelte:element this={"script"}';
1122
1243
  const attrs = c.meta()!.attributes as Record<string, string | boolean>;
1123
- const typed = c.end !== Slot.NONE;
1244
+ const typed = !c.synthetic;
1124
1245
  for (const k in attrs) {
1125
1246
  const v = attrs[k];
1126
1247
  if (v === true) s += ' ' + k;
@@ -1183,6 +1304,65 @@ const LINK_HANDLED = new Set(['href', 'title']);
1183
1304
  const IMAGE_HANDLED = new Set(['title']);
1184
1305
 
1185
1306
  /** render children of the current cursor position, collecting escaped text and recursive node output. */
1307
+ // task items
1308
+
1309
+ const TASK_OFF = '<input type="checkbox" disabled /> ';
1310
+ const TASK_ON = '<input type="checkbox" checked disabled /> ';
1311
+
1312
+ /** the replaced paragraph that takes a checkbox as its first child, -1 for none */
1313
+ let task_at = -1;
1314
+ let task_box = '';
1315
+
1316
+ /** the first paragraph of a loose task item, which takes the checkbox as on github, else NONE */
1317
+ function task_para(c: Cursor): number {
1318
+ const n = c.words;
1319
+ const item = c.index;
1320
+ const first = n[item * W.stride + W.first_child];
1321
+ if (first === Slot.NONE) return Slot.NONE;
1322
+ const b = first * W.stride;
1323
+ // a pending wrapper renders as its children
1324
+ if ((n[b] & 0xff) !== K.PARAGRAPH || n[b + W.pending] === 1) return Slot.NONE;
1325
+ // the walks render text only through its parent, so a text sibling sends the checkbox to the item start
1326
+ for (
1327
+ let next = n[b + W.next];
1328
+ next !== Slot.NONE && n[next * W.stride + W.parent] === item;
1329
+ next = n[next * W.stride + W.next]
1330
+ ) {
1331
+ if ((n[next * W.stride] & 0xff) === K.TEXT) return Slot.NONE;
1332
+ }
1333
+ return first;
1334
+ }
1335
+
1336
+ /** the children of a task item with its checkbox, c stays on the item */
1337
+ function render_task(c: Cursor, sink: MapSink | undefined, on: boolean): void {
1338
+ const box = on ? TASK_ON : TASK_OFF;
1339
+ const para = task_para(c);
1340
+ if (para === Slot.NONE) {
1341
+ mo += box;
1342
+ render_children(c, sink);
1343
+ return;
1344
+ }
1345
+ const n = c.words;
1346
+ const item = c.index;
1347
+ c.move_to(para);
1348
+ const pre = mo.length;
1349
+ _open(c, '<p', '<p>', '>');
1350
+ const ao = mo.length;
1351
+ mo += box;
1352
+ render_children(c, sink);
1353
+ const bc = mo.length;
1354
+ mo += '</p>';
1355
+ if (sink) _spans(sink, pre, ao, bc, mo.length, c, Preset.TEXT);
1356
+ let next = n[para * W.stride + W.next];
1357
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === item) {
1358
+ const after = n[next * W.stride + W.next];
1359
+ c.move_to(next);
1360
+ render_node(c, sink);
1361
+ next = after;
1362
+ }
1363
+ c.move_to(item);
1364
+ }
1365
+
1186
1366
  function render_children(c: Cursor, sink?: MapSink): void {
1187
1367
  const n = c.words;
1188
1368
  let child = n[c.index * W.stride + W.first_child];
@@ -1221,7 +1401,18 @@ function _children_raw(c: Cursor): string {
1221
1401
  let text = '';
1222
1402
  do {
1223
1403
  if (c.kind === K.TEXT) {
1224
- text += c.text();
1404
+ const t = c.text();
1405
+ const vs = c.value_start;
1406
+ const first = t.charCodeAt(0);
1407
+ // alt is an attribute, svelte reads a brace there as an expression too
1408
+ if (
1409
+ (first === 123 || first === 125) &&
1410
+ vs !== Slot.NONE &&
1411
+ c.source.charCodeAt(vs) === first &&
1412
+ brace_escaped(c.source, vs)
1413
+ )
1414
+ text += (first === 123 ? '&#123;' : '&#125;') + t.slice(1);
1415
+ else text += t;
1225
1416
  } else {
1226
1417
  text += _children_raw(c);
1227
1418
  }
@@ -1249,6 +1440,90 @@ function content_record(
1249
1440
  );
1250
1441
  }
1251
1442
 
1443
+ /**
1444
+ * a content record per line of fence code the parser built, false for other code
1445
+ * each such line is the end of its source line once indent and quote markers go
1446
+ */
1447
+ function fence_lines(
1448
+ c: Cursor,
1449
+ sink: MapSink,
1450
+ at: number,
1451
+ text: string,
1452
+ trace: boolean
1453
+ ): boolean {
1454
+ const code = c.prebuilt;
1455
+ if (code === undefined || (fs !== null && fs.has(c.index)) || holds(c))
1456
+ return false;
1457
+ const src = c.source;
1458
+ const ve = c.value_end;
1459
+ let line = c.value_start;
1460
+ let g = 0;
1461
+ let r = 0;
1462
+ for (;;) {
1463
+ // escaping keeps the linefeeds, so the text and the code have the same lines
1464
+ let g_end = string_index_of.call(text, '\n', g);
1465
+ let r_end = string_index_of.call(code, '\n', r);
1466
+ let s_end = string_index_of.call(src, '\n', line);
1467
+ if (s_end === -1 || s_end > ve) s_end = ve;
1468
+ const last = g_end === -1;
1469
+ if (last) {
1470
+ g_end = text.length;
1471
+ r_end = code.length;
1472
+ } else {
1473
+ g_end++;
1474
+ r_end++;
1475
+ }
1476
+ const s_to = last ? s_end : s_end + 1;
1477
+ let s_from = s_to - (r_end - r);
1478
+ // a tab the indent cut leaves spaces the source line has no room for
1479
+ if (s_from < line) s_from = line;
1480
+ if (g_end > g) {
1481
+ if (trace) tr_run(sink, at + g, at + g_end, s_from, s_to);
1482
+ else
1483
+ put_record(
1484
+ sink,
1485
+ at + g,
1486
+ at + g_end,
1487
+ s_from,
1488
+ s_to,
1489
+ c.index,
1490
+ Code.CODE_CONTENT
1491
+ );
1492
+ }
1493
+ if (last) return true;
1494
+ g = g_end;
1495
+ r = r_end;
1496
+ line = s_end + 1;
1497
+ }
1498
+ }
1499
+
1500
+ /** the escaped code of a fence and its content records, after flushing the register */
1501
+ function fence_content(
1502
+ c: Cursor,
1503
+ sink: MapSink,
1504
+ p: number,
1505
+ text: string,
1506
+ trace: boolean
1507
+ ): number {
1508
+ if (p !== 0) mo += FOLD_STR[p];
1509
+ const at = mo.length;
1510
+ if (!fence_lines(c, sink, at, text, trace)) {
1511
+ if (trace) tr_run(sink, at, at + text.length, c.value_start, c.value_end);
1512
+ else
1513
+ put_record(
1514
+ sink,
1515
+ at,
1516
+ at + text.length,
1517
+ c.value_start,
1518
+ c.value_end,
1519
+ c.index,
1520
+ Code.CODE_CONTENT
1521
+ );
1522
+ }
1523
+ mo += text;
1524
+ return 0;
1525
+ }
1526
+
1252
1527
  /** render a single node at the current cursor position. */
1253
1528
  function render_node(c: Cursor, sink?: MapSink): void {
1254
1529
  // as number, or the case labels narrow c.kind for the reads inside a case
@@ -1321,7 +1596,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1321
1596
  const pre = mo.length;
1322
1597
  _open(c, '<code', '<code>', '>');
1323
1598
  const ao = mo.length;
1324
- let code = escape_code_text(c);
1599
+ let code = escape_span_text(c);
1325
1600
  if (string_index_of.call(code, '\n') !== -1)
1326
1601
  code = code.replace(/\n/g, ' ');
1327
1602
  if (sink) content_record(sink, c, code, Code.CODE_CONTENT);
@@ -1350,7 +1625,8 @@ function render_node(c: Cursor, sink?: MapSink): void {
1350
1625
  }
1351
1626
  const ao = mo.length;
1352
1627
  const text = escape_fence_text(c);
1353
- if (sink) content_record(sink, c, text, Code.CODE_CONTENT);
1628
+ if (sink && !fence_lines(c, sink, mo.length, text, false))
1629
+ content_record(sink, c, text, Code.CODE_CONTENT);
1354
1630
  mo += text;
1355
1631
  const bc = mo.length;
1356
1632
  mo += '</code></pre>';
@@ -1373,8 +1649,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1373
1649
  const pre = mo.length;
1374
1650
  const meta = c.meta();
1375
1651
  let s = '<a';
1376
- if (meta?.href)
1377
- s += ' href="' + escape_text_html(meta.href as string) + '"';
1652
+ if (meta?.href) s += url_attr(meta.href as string, false);
1378
1653
  if (meta?.title)
1379
1654
  s += ' title="' + escape_text_html(meta.title as string) + '"';
1380
1655
  mo = mo + s + _attrs(c, LINK_HANDLED) + '>';
@@ -1389,12 +1664,11 @@ function render_node(c: Cursor, sink?: MapSink): void {
1389
1664
  case K.IMAGE: {
1390
1665
  const pre = mo.length;
1391
1666
  const meta = c.meta();
1392
- let s = '<img';
1393
- if (meta?.src) s += ' src="' + escape_text_html(meta.src as string) + '"';
1667
+ let s = img_head(meta?.src);
1394
1668
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
1395
1669
  if (meta?.title)
1396
1670
  s += ' title="' + escape_text_html(meta.title as string) + '"';
1397
- mo = mo + s + _attrs(c, IMAGE_HANDLED) + ' />';
1671
+ mo = mo + s + _attrs(c, IMAGE_HANDLED) + FOLD_STR[img_close];
1398
1672
  // the syntax spans are empty, only the node is recorded
1399
1673
  if (sink) put_record(sink, pre, mo.length, c.start, c.end, c.index, 0);
1400
1674
  break;
@@ -1425,7 +1699,9 @@ function render_node(c: Cursor, sink?: MapSink): void {
1425
1699
  const pre = mo.length;
1426
1700
  _open(c, '<li', '<li>', '>');
1427
1701
  const ao = mo.length;
1428
- render_children(c, sink);
1702
+ const checked = c.meta()?.checked;
1703
+ if (typeof checked === 'boolean') render_task(c, sink, checked);
1704
+ else render_children(c, sink);
1429
1705
  const bc = mo.length;
1430
1706
  mo += '</li>\n';
1431
1707
  if (sink) _spans(sink, pre, ao, bc, mo.length, c, Preset.TEXT);
@@ -1436,7 +1712,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1436
1712
  const pre = mo.length;
1437
1713
  mo += '<hr />';
1438
1714
  // node and open syntax, the close syntax span is empty
1439
- if (sink) {
1715
+ if (sink && c.end !== Slot.NONE) {
1440
1716
  const post = mo.length;
1441
1717
  put_record(
1442
1718
  sink,
@@ -1534,7 +1810,9 @@ function render_node(c: Cursor, sink?: MapSink): void {
1534
1810
  // (e.g. wire/streaming renderer with empty source).
1535
1811
  const self_closing = !!meta?.self_closing;
1536
1812
  const passthrough =
1537
- self_closing && c.end > c.start ? c.slice(c.start, c.end) : '';
1813
+ self_closing && !c.synthetic && c.end > c.start
1814
+ ? c.slice(c.start, c.end)
1815
+ : '';
1538
1816
  if (passthrough) {
1539
1817
  mo += passthrough;
1540
1818
  } else {
@@ -1543,7 +1821,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1543
1821
  | Record<string, string | boolean>
1544
1822
  | undefined;
1545
1823
  if (html_attrs) {
1546
- const typed = c.end !== Slot.NONE;
1824
+ const typed = !c.synthetic;
1547
1825
  for (const k in html_attrs) {
1548
1826
  const v = html_attrs[k];
1549
1827
  if (v === true) {
@@ -1557,6 +1835,8 @@ function render_node(c: Cursor, sink?: MapSink): void {
1557
1835
  s += ' ' + k + '="' + escape_attr(v as string, typed) + '"';
1558
1836
  }
1559
1837
  }
1838
+ if (meta!.repeated !== undefined)
1839
+ s += repeated_attrs(meta!.repeated as [string, unknown][], typed);
1560
1840
  }
1561
1841
  mo += self_closing ? s + ' />' : s + '>';
1562
1842
  }
@@ -1574,10 +1854,9 @@ function render_node(c: Cursor, sink?: MapSink): void {
1574
1854
  }
1575
1855
  } else {
1576
1856
  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') {
1857
+ // raw text elements hold their content as the value range of the
1858
+ // node, with no children, it is written as it is in the source
1859
+ if (is_raw_text_tag(tag)) {
1581
1860
  if (c.index === hoist_script) {
1582
1861
  mo += '\n';
1583
1862
  mo_imports(c, sink, false);
@@ -1601,7 +1880,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1601
1880
  const pre = mo.length;
1602
1881
  mo += '<!--';
1603
1882
  const ao = mo.length;
1604
- const text = c.text();
1883
+ const text = held_text(c);
1605
1884
  if (sink) content_record(sink, c, text, Code.TEXT_CONTENT);
1606
1885
  mo += text;
1607
1886
  const bc = mo.length;
@@ -1703,7 +1982,7 @@ function render_node(c: Cursor, sink?: MapSink): void {
1703
1982
 
1704
1983
  case K.IMPORT_STATEMENT:
1705
1984
  if (c.index === hoist_at) {
1706
- mo += '<script>\n';
1985
+ mo += hoist_open;
1707
1986
  mo_imports(c, sink, false);
1708
1987
  mo += '</script>';
1709
1988
  }
@@ -1714,8 +1993,15 @@ function render_node(c: Cursor, sink?: MapSink): void {
1714
1993
  break;
1715
1994
 
1716
1995
  default:
1717
- // a label renders only as the snippet of a replaced directive
1718
- if (c.kind !== K.DIRECTIVE_LABEL) render_children(c, sink);
1996
+ // a label renders only as the snippet of a replaced directive, a
1997
+ // footnote definition in the section at the end and a link reference
1998
+ // definition not at all
1999
+ if (c.kind < K.DIRECTIVE_LABEL) render_children(c, sink);
2000
+ else if (c.kind === K.FOOTNOTE_REFERENCE) {
2001
+ const pre = mo.length;
2002
+ mo += fn_reference(c);
2003
+ if (sink) put_record(sink, pre, mo.length, c.start, c.end, c.index, 0);
2004
+ }
1719
2005
  break;
1720
2006
  }
1721
2007
  }
@@ -1911,6 +2197,7 @@ const S_IMG = fold_base('<img');
1911
2197
  const S_SRC = fold_base(' src="');
1912
2198
  const S_ALT = fold_base(' alt="');
1913
2199
  const S_SELF_CLOSE = fold_base(' />');
2200
+ const S_PICTURE_CLOSE = fold_base(' /></picture>');
1914
2201
  const S_OL_START = fold_base('<ol start="');
1915
2202
  const S_OL = fold_base('<ol');
1916
2203
  const S_OL_OPEN = fold_base('<ol>\n');
@@ -1940,6 +2227,14 @@ const S_TR_OPEN = fold_base('<tr>\n');
1940
2227
  const S_TR_CLOSE = fold_base('</tr>\n');
1941
2228
  const S_TH_CLOSE = fold_base('</th>\n');
1942
2229
  const S_TD_CLOSE = fold_base('</td>\n');
2230
+ const S_P = fold_base('<p');
2231
+ const S_P_OPEN = fold_base('<p>');
2232
+ const S_P_CLOSE = fold_base('</p>');
2233
+ const S_LI = fold_base('<li');
2234
+ const S_LI_OPEN = fold_base('<li>');
2235
+ const S_LI_CLOSE = fold_base('</li>\n');
2236
+ const S_TASK_OFF = fold_base(TASK_OFF);
2237
+ const S_TASK_ON = fold_base(TASK_ON);
1943
2238
 
1944
2239
  /** indexed by alignment none, left, center, right */
1945
2240
  function fold_cell_opens(tag: string): Uint8Array {
@@ -1981,7 +2276,6 @@ wrap_row(K.STRONG, '<strong', '<strong>', '>', '</strong>');
1981
2276
  wrap_row(K.STRIKETHROUGH, '<del', '<del>', '>', '</del>');
1982
2277
  wrap_row(K.SUPERSCRIPT, '<sup', '<sup>', '>', '</sup>');
1983
2278
  wrap_row(K.SUBSCRIPT, '<sub', '<sub>', '>', '</sub>');
1984
- wrap_row(K.LIST_ITEM, '<li', '<li>', '>', '</li>\n');
1985
2279
  wrap_row(
1986
2280
  K.BLOCK_QUOTE,
1987
2281
  '<blockquote',
@@ -2222,7 +2516,6 @@ function fold_node(c: Cursor, p: number): number {
2222
2516
  case K.EMPHASIS:
2223
2517
  case K.STRONG:
2224
2518
  case K.BLOCK_QUOTE:
2225
- case K.LIST_ITEM:
2226
2519
  case K.STRIKETHROUGH:
2227
2520
  case K.SUPERSCRIPT:
2228
2521
  case K.SUBSCRIPT: {
@@ -2235,6 +2528,18 @@ function fold_node(c: Cursor, p: number): number {
2235
2528
  return push_static(p, WRAP_CLOSE[row]);
2236
2529
  }
2237
2530
 
2531
+ case K.LIST_ITEM: {
2532
+ const meta = c.meta();
2533
+ if (meta === undefined) p = push_static(p, S_LI_OPEN);
2534
+ else {
2535
+ p = fold_open(c, p, S_LI, S_LI_OPEN, S_GT);
2536
+ if (typeof meta.checked === 'boolean')
2537
+ return fold_task(c, p, meta.checked);
2538
+ }
2539
+ p = fold_children(c, p);
2540
+ return push_static(p, S_LI_CLOSE);
2541
+ }
2542
+
2238
2543
  case K.CODE_SPAN: {
2239
2544
  if (hl !== null) {
2240
2545
  const h = hl_span(c);
@@ -2242,7 +2547,7 @@ function fold_node(c: Cursor, p: number): number {
2242
2547
  return fold_hl(p, hl_span_head(c, h), h.body, hl_span_tail(h));
2243
2548
  }
2244
2549
  p = fold_open(c, p, S_CODE, S_CODE_OPEN, S_GT);
2245
- const code = escape_code_text(c);
2550
+ const code = escape_span_text(c);
2246
2551
  p = push_dyn(
2247
2552
  p,
2248
2553
  code.indexOf('\n') === -1 ? code : code.replace(/\n/g, ' ')
@@ -2276,7 +2581,7 @@ function fold_node(c: Cursor, p: number): number {
2276
2581
 
2277
2582
  case K.HTML_COMMENT:
2278
2583
  p = push_static(p, S_COMMENT_OPEN);
2279
- p = push_dyn(p, c.text());
2584
+ p = push_dyn(p, held_text(c));
2280
2585
  return push_static(p, S_COMMENT_CLOSE);
2281
2586
 
2282
2587
  case K.MUSTACHE:
@@ -2307,21 +2612,60 @@ function fold_node(c: Cursor, p: number): number {
2307
2612
 
2308
2613
  case K.IMPORT_STATEMENT:
2309
2614
  if (c.index !== hoist_at) return p;
2310
- return push_dyn(p, '<script>\n' + import_lines(c) + '</script>');
2615
+ return push_dyn(p, hoist_open + import_lines(c) + '</script>');
2311
2616
 
2312
2617
  case K.FRONTMATTER:
2313
2618
  if (module_code === '') return p;
2314
2619
  return push_dyn(p, module_script());
2315
2620
 
2316
2621
  default:
2317
- // a label renders only as the snippet of a replaced directive
2318
- if (c.kind === K.DIRECTIVE_LABEL) return p;
2622
+ // a label renders only as the snippet of a replaced directive, a
2623
+ // footnote definition in the section at the end and a link reference
2624
+ // definition not at all
2625
+ if (c.kind >= K.DIRECTIVE_LABEL)
2626
+ return c.kind === K.FOOTNOTE_REFERENCE
2627
+ ? push_dyn(p, fn_reference(c))
2628
+ : p;
2319
2629
  // a replaced directive never gets here, see comp_ref
2320
2630
  if (dir_strict) dir_check(c);
2321
2631
  return fold_children(c, p);
2322
2632
  }
2323
2633
  }
2324
2634
 
2635
+ /** render_task for the fold walk, after the open tag */
2636
+ function fold_task(c: Cursor, p: number, on: boolean): number {
2637
+ const box = on ? S_TASK_ON : S_TASK_OFF;
2638
+ const para = task_para(c);
2639
+ if (para === Slot.NONE) {
2640
+ p = push_static(p, box);
2641
+ p = fold_children(c, p);
2642
+ return push_static(p, S_LI_CLOSE);
2643
+ }
2644
+ const n = c.words;
2645
+ const item = c.index;
2646
+ c.move_to(para);
2647
+ if (has_components && comp_ref(c) !== null) {
2648
+ task_at = para;
2649
+ task_box = FOLD_STR[box];
2650
+ p = fold_node(c, p);
2651
+ task_at = -1;
2652
+ } else {
2653
+ p = fold_open(c, p, S_P, S_P_OPEN, S_GT);
2654
+ p = push_static(p, box);
2655
+ p = fold_children(c, p);
2656
+ p = push_static(p, S_P_CLOSE);
2657
+ }
2658
+ let next = n[para * W.stride + W.next];
2659
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === item) {
2660
+ const after = n[next * W.stride + W.next];
2661
+ c.move_to(next);
2662
+ p = fold_node(c, p);
2663
+ next = after;
2664
+ }
2665
+ c.move_to(item);
2666
+ return push_static(p, S_LI_CLOSE);
2667
+ }
2668
+
2325
2669
  function fold_code_fence(c: Cursor, p: number): number {
2326
2670
  const info = fence_info(c, c.meta());
2327
2671
  if (hl !== null) {
@@ -2342,11 +2686,7 @@ function fold_code_fence(c: Cursor, p: number): number {
2342
2686
  function fold_link(c: Cursor, p: number): number {
2343
2687
  const meta = c.meta();
2344
2688
  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
- }
2689
+ if (meta?.href) p = fold_url(p, meta.href as string, false);
2350
2690
  if (meta?.title) {
2351
2691
  p = push_static(p, S_TITLE);
2352
2692
  p = push_dyn(p, escape_text_html(meta.title as string));
@@ -2360,11 +2700,13 @@ function fold_link(c: Cursor, p: number): number {
2360
2700
 
2361
2701
  function fold_image(c: Cursor, p: number): number {
2362
2702
  const meta = c.meta();
2363
- 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);
2703
+ let close = S_SELF_CLOSE;
2704
+ if (ul === null) {
2705
+ p = push_static(p, S_IMG);
2706
+ if (meta?.src) p = fold_url(p, meta.src as string, true);
2707
+ } else {
2708
+ p = push_dyn(p, img_head(meta?.src));
2709
+ close = img_close;
2368
2710
  }
2369
2711
  p = push_static(p, S_ALT);
2370
2712
  p = push_dyn(p, escape_text_html(_children_raw(c)));
@@ -2375,7 +2717,7 @@ function fold_image(c: Cursor, p: number): number {
2375
2717
  p = push_static(p, S_QUOTE);
2376
2718
  }
2377
2719
  p = fold_attrs(meta, p, IMAGE_HANDLED);
2378
- return push_static(p, S_SELF_CLOSE);
2720
+ return push_static(p, close);
2379
2721
  }
2380
2722
 
2381
2723
  function fold_list(c: Cursor, p: number): number {
@@ -2426,27 +2768,42 @@ function fold_html(c: Cursor, p: number): number {
2426
2768
  const meta = c.meta();
2427
2769
  const tag = meta?.tag as string;
2428
2770
  if (is_embed_script(c, tag)) return push_dyn(p, embed_html(c));
2429
- const html_attrs = meta?.attributes as
2430
- | Record<string, string | boolean>
2431
- | undefined;
2771
+ // an unread tag written as fold writes it is its own attribute text
2772
+ const spans = c.synthetic ? undefined : unread_spans(meta);
2773
+ const text =
2774
+ spans !== undefined && spans.canonical ? spans_text(spans) : undefined;
2775
+ const html_attrs =
2776
+ text === undefined
2777
+ ? (meta?.attributes as Record<string, string | boolean> | undefined)
2778
+ : undefined;
2432
2779
 
2433
2780
  if (meta?.self_closing) {
2434
2781
  // source passthrough as in _node, decided before any push because a
2435
2782
  // pending register cannot be truncated back
2436
- const passthrough = c.end > c.start ? c.slice(c.start, c.end) : '';
2783
+ const passthrough =
2784
+ !c.synthetic && c.end > c.start ? c.slice(c.start, c.end) : '';
2437
2785
  if (passthrough) return push_dyn(p, passthrough);
2438
2786
  p = push_static(p, S_LT);
2439
2787
  p = push_dyn(p, tag);
2440
- if (html_attrs) p = fold_html_attrs(html_attrs, c.end !== Slot.NONE, p);
2788
+ if (text !== undefined) p = push_dyn(p, text);
2789
+ else if (html_attrs) p = fold_html_attrs(html_attrs, !c.synthetic, p);
2441
2790
  return push_static(p, S_SELF_CLOSE);
2442
2791
  }
2443
2792
 
2444
2793
  p = push_static(p, S_LT);
2445
2794
  p = push_dyn(p, tag);
2446
- if (html_attrs) p = fold_html_attrs(html_attrs, c.end !== Slot.NONE, p);
2795
+ if (text !== undefined) p = push_dyn(p, text);
2796
+ else if (html_attrs) {
2797
+ p = fold_html_attrs(html_attrs, !c.synthetic, p);
2798
+ if (meta!.repeated !== undefined)
2799
+ p = push_dyn(
2800
+ p,
2801
+ repeated_attrs(meta!.repeated as [string, unknown][], !c.synthetic)
2802
+ );
2803
+ }
2447
2804
  p = push_static(p, S_GT);
2448
2805
  // raw text elements keep their content as the node value range, see _node
2449
- if (tag === 'script' || tag === 'style') {
2806
+ if (is_raw_text_tag(tag)) {
2450
2807
  if (c.index === hoist_script) p = push_dyn(p, '\n' + import_lines(c));
2451
2808
  else if (c.index === hoist_module)
2452
2809
  p = push_dyn(p, '\n' + module_code + '\n');
@@ -2615,6 +2972,11 @@ function tr_point(sink: MapSink, gen: number, src: number): void {
2615
2972
  }
2616
2973
  }
2617
2974
 
2975
+ /** the point of a node, a node a parse plugin added has no end and no point */
2976
+ function tr_at(sink: MapSink, gen: number, c: Cursor): void {
2977
+ if (c.end !== Slot.NONE) tr_point(sink, gen, c.start);
2978
+ }
2979
+
2618
2980
  /** a content span, a run when it copies the source, empty spans map nothing */
2619
2981
  function tr_run(
2620
2982
  sink: MapSink,
@@ -2772,7 +3134,6 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2772
3134
  case K.EMPHASIS:
2773
3135
  case K.STRONG:
2774
3136
  case K.BLOCK_QUOTE:
2775
- case K.LIST_ITEM:
2776
3137
  case K.STRIKETHROUGH:
2777
3138
  case K.SUPERSCRIPT:
2778
3139
  case K.SUBSCRIPT: {
@@ -2784,7 +3145,20 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2784
3145
  p = tr_open(c, p, WRAP_HEAD[row], WRAP_FOLDED[row], WRAP_END[row]);
2785
3146
  p = tr_children(c, sink, p);
2786
3147
  p = tr_push(p, WRAP_CLOSE[row]);
2787
- tr_point(sink, pre, c.start);
3148
+ tr_at(sink, pre, c);
3149
+ return p;
3150
+ }
3151
+
3152
+ case K.LIST_ITEM: {
3153
+ const pre = mo.length + FOLD_LEN[p];
3154
+ const meta = c.meta();
3155
+ if (meta === undefined) p = tr_push(p, S_LI_OPEN);
3156
+ else p = tr_open(c, p, S_LI, S_LI_OPEN, S_GT);
3157
+ if (meta !== undefined && typeof meta.checked === 'boolean')
3158
+ p = tr_task(c, sink, p, meta.checked);
3159
+ else p = tr_children(c, sink, p);
3160
+ p = tr_push(p, S_LI_CLOSE);
3161
+ tr_at(sink, pre, c);
2788
3162
  return p;
2789
3163
  }
2790
3164
 
@@ -2804,11 +3178,11 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2804
3178
  }
2805
3179
  const pre = mo.length + FOLD_LEN[p];
2806
3180
  p = tr_open(c, p, S_CODE, S_CODE_OPEN, S_GT);
2807
- let code = escape_code_text(c);
3181
+ let code = escape_span_text(c);
2808
3182
  if (string_index_of.call(code, '\n') !== -1)
2809
3183
  code = code.replace(/\n/g, ' ');
2810
3184
  tr_text(c, sink, p, code);
2811
- tr_point(sink, pre, c.start);
3185
+ tr_at(sink, pre, c);
2812
3186
  return S_CODE_CLOSE;
2813
3187
  }
2814
3188
 
@@ -2828,14 +3202,14 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2828
3202
  }
2829
3203
  p = tr_children(c, sink, p);
2830
3204
  p = tr_push(p, ordered ? S_OL_CLOSE : S_UL_CLOSE);
2831
- tr_point(sink, pre, c.start);
3205
+ tr_at(sink, pre, c);
2832
3206
  return p;
2833
3207
  }
2834
3208
 
2835
3209
  case K.THEMATIC_BREAK: {
2836
3210
  const pre = mo.length + FOLD_LEN[p];
2837
3211
  p = tr_push(p, S_HR);
2838
- tr_point(sink, pre, c.start);
3212
+ tr_at(sink, pre, c);
2839
3213
  return p;
2840
3214
  }
2841
3215
 
@@ -2850,7 +3224,7 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2850
3224
  p = tr_open(c, p, S_TABLE, S_TABLE_OPEN, S_GT_LF);
2851
3225
  p = tr_table_content(c, sink, p);
2852
3226
  p = tr_push(p, S_TABLE_CLOSE);
2853
- tr_point(sink, pre, c.start);
3227
+ tr_at(sink, pre, c);
2854
3228
  return p;
2855
3229
  }
2856
3230
 
@@ -2871,14 +3245,13 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2871
3245
  if (p !== 0) mo += FOLD_STR[p];
2872
3246
  const meta = c.meta();
2873
3247
  let s = '<a';
2874
- if (meta?.href)
2875
- s += ' href="' + escape_text_html(meta.href as string) + '"';
3248
+ if (meta?.href) s += url_attr(meta.href as string, false);
2876
3249
  if (meta?.title)
2877
3250
  s += ' title="' + escape_text_html(meta.title as string) + '"';
2878
3251
  mo = mo + s + _attrs(c, LINK_HANDLED);
2879
3252
  p = tr_children(c, sink, S_GT);
2880
3253
  p = tr_push(p, S_A_CLOSE);
2881
- tr_point(sink, pre, c.start);
3254
+ tr_at(sink, pre, c);
2882
3255
  return p;
2883
3256
  }
2884
3257
 
@@ -2886,22 +3259,21 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2886
3259
  const pre = mo.length + FOLD_LEN[p];
2887
3260
  if (p !== 0) mo += FOLD_STR[p];
2888
3261
  const meta = c.meta();
2889
- let s = '<img';
2890
- if (meta?.src) s += ' src="' + escape_text_html(meta.src as string) + '"';
3262
+ let s = img_head(meta?.src);
2891
3263
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
2892
3264
  if (meta?.title)
2893
3265
  s += ' title="' + escape_text_html(meta.title as string) + '"';
2894
3266
  mo = mo + s + _attrs(c, IMAGE_HANDLED);
2895
3267
  // the syntax spans are empty, only the node is recorded
2896
- tr_point(sink, pre, c.start);
2897
- return S_SELF_CLOSE;
3268
+ tr_at(sink, pre, c);
3269
+ return img_close;
2898
3270
  }
2899
3271
 
2900
3272
  case K.HTML_COMMENT: {
2901
3273
  const pre = mo.length + FOLD_LEN[p];
2902
3274
  p = tr_push(p, S_COMMENT_OPEN);
2903
- tr_text(c, sink, p, c.text());
2904
- tr_point(sink, pre, c.start);
3275
+ tr_text(c, sink, p, held_text(c));
3276
+ tr_at(sink, pre, c);
2905
3277
  return S_COMMENT_CLOSE;
2906
3278
  }
2907
3279
 
@@ -2909,7 +3281,7 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2909
3281
  const pre = mo.length + FOLD_LEN[p];
2910
3282
  p = tr_push(p, S_BRACE_OPEN);
2911
3283
  tr_text(c, sink, p, c.text());
2912
- tr_point(sink, pre, c.start);
3284
+ tr_at(sink, pre, c);
2913
3285
  return S_BRACE_CLOSE;
2914
3286
  }
2915
3287
 
@@ -2921,27 +3293,64 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
2921
3293
  const text = c.text();
2922
3294
  mo = mo + '{@' + meta_str(tag);
2923
3295
  if (text) tr_text(c, sink, S_SPACE, text);
2924
- tr_point(sink, pre, c.start);
3296
+ tr_at(sink, pre, c);
2925
3297
  return S_BRACE_CLOSE;
2926
3298
  }
2927
3299
 
2928
3300
  case K.IMPORT_STATEMENT:
2929
3301
  if (c.index !== hoist_at) return p;
2930
3302
  if (p !== 0) mo += FOLD_STR[p];
2931
- mo += '<script>\n';
3303
+ mo += hoist_open;
2932
3304
  mo_imports(c, sink, true);
2933
3305
  mo += '</script>';
2934
3306
  return 0;
2935
3307
 
2936
3308
  default:
2937
- // a label renders only as the snippet of a replaced directive
2938
- if (c.kind === K.DIRECTIVE_LABEL) return p;
3309
+ // a label renders only as the snippet of a replaced directive, a
3310
+ // footnote definition in the section at the end and a link reference
3311
+ // definition not at all
3312
+ if (c.kind >= K.DIRECTIVE_LABEL)
3313
+ return c.kind === K.FOOTNOTE_REFERENCE
3314
+ ? fn_reference_mapped(c, sink, p, true)
3315
+ : p;
2939
3316
  // a replaced directive never gets here, see comp_ref
2940
3317
  if (dir_strict) dir_check(c);
2941
3318
  return tr_children(c, sink, p);
2942
3319
  }
2943
3320
  }
2944
3321
 
3322
+ /** render_task for the trace walk */
3323
+ function tr_task(c: Cursor, sink: MapSink, p: number, on: boolean): number {
3324
+ const box = on ? S_TASK_ON : S_TASK_OFF;
3325
+ const para = task_para(c);
3326
+ if (para === Slot.NONE) return tr_children(c, sink, tr_push(p, box));
3327
+ const n = c.words;
3328
+ const item = c.index;
3329
+ c.move_to(para);
3330
+ if (has_components && comp_ref(c) !== null) {
3331
+ task_at = para;
3332
+ task_box = FOLD_STR[box];
3333
+ p = tr_node(c, sink, p);
3334
+ task_at = -1;
3335
+ } else {
3336
+ const pre = mo.length + FOLD_LEN[p];
3337
+ p = tr_open(c, p, S_P, S_P_OPEN, S_GT);
3338
+ p = tr_push(p, box);
3339
+ p = tr_children(c, sink, p);
3340
+ p = tr_push(p, S_P_CLOSE);
3341
+ tr_at(sink, pre, c);
3342
+ }
3343
+ let next = n[para * W.stride + W.next];
3344
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === item) {
3345
+ const after = n[next * W.stride + W.next];
3346
+ c.move_to(next);
3347
+ p = tr_node(c, sink, p);
3348
+ next = after;
3349
+ }
3350
+ c.move_to(item);
3351
+ return p;
3352
+ }
3353
+
2945
3354
  function tr_code_fence(c: Cursor, sink: MapSink, p: number): number {
2946
3355
  const info = fence_info(c, c.meta());
2947
3356
  if (hl !== null) {
@@ -2957,8 +3366,8 @@ function tr_code_fence(c: Cursor, sink: MapSink, p: number): number {
2957
3366
  } else {
2958
3367
  p = tr_open(c, p, S_PRE_CODE, S_PRE_CODE_OPEN, S_GT);
2959
3368
  }
2960
- tr_text(c, sink, p, escape_fence_text(c));
2961
- tr_point(sink, pre, c.start);
3369
+ fence_content(c, sink, p, escape_fence_text(c), true);
3370
+ tr_at(sink, pre, c);
2962
3371
  return S_PRE_CODE_CLOSE;
2963
3372
  }
2964
3373
 
@@ -2976,7 +3385,9 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
2976
3385
  // source passthrough and reconstruction exactly as render_node
2977
3386
  const self_closing = !!meta?.self_closing;
2978
3387
  const passthrough =
2979
- self_closing && c.end > c.start ? c.slice(c.start, c.end) : '';
3388
+ self_closing && !c.synthetic && c.end > c.start
3389
+ ? c.slice(c.start, c.end)
3390
+ : '';
2980
3391
  if (passthrough) {
2981
3392
  mo += passthrough;
2982
3393
  } else {
@@ -2985,7 +3396,7 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
2985
3396
  | Record<string, string | boolean>
2986
3397
  | undefined;
2987
3398
  if (html_attrs) {
2988
- const typed = c.end !== Slot.NONE;
3399
+ const typed = !c.synthetic;
2989
3400
  for (const k in html_attrs) {
2990
3401
  const v = html_attrs[k];
2991
3402
  if (v === true) {
@@ -2996,6 +3407,8 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
2996
3407
  s += ' ' + k + '="' + escape_attr(v as string, typed) + '"';
2997
3408
  }
2998
3409
  }
3410
+ if (meta!.repeated !== undefined)
3411
+ s += repeated_attrs(meta!.repeated as [string, unknown][], typed);
2999
3412
  }
3000
3413
  if (self_closing) {
3001
3414
  mo += s;
@@ -3008,7 +3421,7 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
3008
3421
  tr_run(sink, pre, mo.length, c.start, c.end);
3009
3422
  return 0;
3010
3423
  }
3011
- if (tag === 'script' || tag === 'style') {
3424
+ if (is_raw_text_tag(tag)) {
3012
3425
  if (c.index === hoist_script) {
3013
3426
  mo = mo + FOLD_STR[S_GT] + '\n';
3014
3427
  mo_imports(c, sink, true);
@@ -3022,7 +3435,7 @@ function tr_html(c: Cursor, sink: MapSink, p: number): number {
3022
3435
  if (q !== 0) mo += FOLD_STR[q];
3023
3436
  }
3024
3437
  mo = mo + '</' + meta_str(tag);
3025
- tr_point(sink, pre, c.start);
3438
+ tr_at(sink, pre, c);
3026
3439
  return S_GT;
3027
3440
  }
3028
3441
 
@@ -3067,7 +3480,7 @@ function tr_svelte_block(c: Cursor, sink: MapSink, p: number): number {
3067
3480
  if (p !== 0) mo += FOLD_STR[p];
3068
3481
  mo = mo + '{/' + block_tag;
3069
3482
  // node span for the whole block, use the block node (goto_parent already called)
3070
- tr_point(sink, pre, c.start);
3483
+ tr_at(sink, pre, c);
3071
3484
  return S_BRACE_CLOSE;
3072
3485
  }
3073
3486
 
@@ -3224,7 +3637,6 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3224
3637
  case K.EMPHASIS:
3225
3638
  case K.STRONG:
3226
3639
  case K.BLOCK_QUOTE:
3227
- case K.LIST_ITEM:
3228
3640
  case K.STRIKETHROUGH:
3229
3641
  case K.SUPERSCRIPT:
3230
3642
  case K.SUBSCRIPT: {
@@ -3242,6 +3654,21 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3242
3654
  return p;
3243
3655
  }
3244
3656
 
3657
+ case K.LIST_ITEM: {
3658
+ const pre = mo.length + FOLD_LEN[p];
3659
+ const meta = c.meta();
3660
+ if (meta === undefined) p = tr_push(p, S_LI_OPEN);
3661
+ else p = tr_open(c, p, S_LI, S_LI_OPEN, S_GT);
3662
+ const ao = mo.length + FOLD_LEN[p];
3663
+ if (meta !== undefined && typeof meta.checked === 'boolean')
3664
+ p = mp_task(c, sink, p, meta.checked);
3665
+ else p = mp_children(c, sink, p);
3666
+ const bc = mo.length + FOLD_LEN[p];
3667
+ p = tr_push(p, S_LI_CLOSE);
3668
+ _spans(sink, pre, ao, bc, mo.length + FOLD_LEN[p], c, Preset.TEXT);
3669
+ return p;
3670
+ }
3671
+
3245
3672
  case K.CODE_SPAN: {
3246
3673
  if (hl !== null) {
3247
3674
  const h = hl_span(c);
@@ -3259,7 +3686,7 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3259
3686
  const pre = mo.length + FOLD_LEN[p];
3260
3687
  p = tr_open(c, p, S_CODE, S_CODE_OPEN, S_GT);
3261
3688
  const ao = mo.length + FOLD_LEN[p];
3262
- let code = escape_code_text(c);
3689
+ let code = escape_span_text(c);
3263
3690
  if (string_index_of.call(code, '\n') !== -1)
3264
3691
  code = code.replace(/\n/g, ' ');
3265
3692
  tr_content(c, sink, p, code, Code.CODE_CONTENT);
@@ -3304,7 +3731,7 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3304
3731
  c.index,
3305
3732
  Preset.STRUCTURE << 2
3306
3733
  );
3307
- if (sink.syntax) {
3734
+ if (sink.syntax && c.end !== Slot.NONE) {
3308
3735
  const vs = c.value_start;
3309
3736
  put_record(
3310
3737
  sink,
@@ -3353,8 +3780,7 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3353
3780
  if (p !== 0) mo += FOLD_STR[p];
3354
3781
  const meta = c.meta();
3355
3782
  let s = '<a';
3356
- if (meta?.href)
3357
- s += ' href="' + escape_text_html(meta.href as string) + '"';
3783
+ if (meta?.href) s += url_attr(meta.href as string, false);
3358
3784
  if (meta?.title)
3359
3785
  s += ' title="' + escape_text_html(meta.title as string) + '"';
3360
3786
  mo = mo + s + _attrs(c, LINK_HANDLED);
@@ -3370,30 +3796,30 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3370
3796
  const pre = mo.length + FOLD_LEN[p];
3371
3797
  if (p !== 0) mo += FOLD_STR[p];
3372
3798
  const meta = c.meta();
3373
- let s = '<img';
3374
- if (meta?.src) s += ' src="' + escape_text_html(meta.src as string) + '"';
3799
+ let s = img_head(meta?.src);
3375
3800
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
3376
3801
  if (meta?.title)
3377
3802
  s += ' title="' + escape_text_html(meta.title as string) + '"';
3378
3803
  mo = mo + s + _attrs(c, IMAGE_HANDLED);
3804
+ const close = img_close;
3379
3805
  // the syntax spans are empty, only the node is recorded
3380
3806
  put_record(
3381
3807
  sink,
3382
3808
  pre,
3383
- mo.length + FOLD_LEN[S_SELF_CLOSE],
3809
+ mo.length + FOLD_LEN[close],
3384
3810
  c.start,
3385
3811
  c.end,
3386
3812
  c.index,
3387
3813
  0
3388
3814
  );
3389
- return S_SELF_CLOSE;
3815
+ return close;
3390
3816
  }
3391
3817
 
3392
3818
  case K.HTML_COMMENT: {
3393
3819
  const pre = mo.length + FOLD_LEN[p];
3394
3820
  p = tr_push(p, S_COMMENT_OPEN);
3395
3821
  const ao = mo.length + FOLD_LEN[p];
3396
- tr_content(c, sink, p, c.text(), Code.TEXT_CONTENT);
3822
+ tr_content(c, sink, p, held_text(c), Code.TEXT_CONTENT);
3397
3823
  const bc = mo.length;
3398
3824
  _spans(sink, pre, ao, bc, bc + FOLD_LEN[S_COMMENT_CLOSE], c, Preset.TEXT);
3399
3825
  return S_COMMENT_CLOSE;
@@ -3429,20 +3855,59 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3429
3855
  case K.IMPORT_STATEMENT:
3430
3856
  if (c.index !== hoist_at) return p;
3431
3857
  if (p !== 0) mo += FOLD_STR[p];
3432
- mo += '<script>\n';
3858
+ mo += hoist_open;
3433
3859
  mo_imports(c, sink, false);
3434
3860
  mo += '</script>';
3435
3861
  return 0;
3436
3862
 
3437
3863
  default:
3438
- // a label renders only as the snippet of a replaced directive
3439
- if (c.kind === K.DIRECTIVE_LABEL) return p;
3864
+ // a label renders only as the snippet of a replaced directive, a
3865
+ // footnote definition in the section at the end and a link reference
3866
+ // definition not at all
3867
+ if (c.kind >= K.DIRECTIVE_LABEL)
3868
+ return c.kind === K.FOOTNOTE_REFERENCE
3869
+ ? fn_reference_mapped(c, sink, p, false)
3870
+ : p;
3440
3871
  // a replaced directive never gets here, see comp_ref
3441
3872
  if (dir_strict) dir_check(c);
3442
3873
  return mp_children(c, sink, p);
3443
3874
  }
3444
3875
  }
3445
3876
 
3877
+ /** render_task for the mapped walk */
3878
+ function mp_task(c: Cursor, sink: MapSink, p: number, on: boolean): number {
3879
+ const box = on ? S_TASK_ON : S_TASK_OFF;
3880
+ const para = task_para(c);
3881
+ if (para === Slot.NONE) return mp_children(c, sink, tr_push(p, box));
3882
+ const n = c.words;
3883
+ const item = c.index;
3884
+ c.move_to(para);
3885
+ if (has_components && comp_ref(c) !== null) {
3886
+ task_at = para;
3887
+ task_box = FOLD_STR[box];
3888
+ p = mp_node(c, sink, p);
3889
+ task_at = -1;
3890
+ } else {
3891
+ const pre = mo.length + FOLD_LEN[p];
3892
+ p = tr_open(c, p, S_P, S_P_OPEN, S_GT);
3893
+ const ao = mo.length + FOLD_LEN[p];
3894
+ p = tr_push(p, box);
3895
+ p = mp_children(c, sink, p);
3896
+ const bc = mo.length + FOLD_LEN[p];
3897
+ p = tr_push(p, S_P_CLOSE);
3898
+ _spans(sink, pre, ao, bc, mo.length + FOLD_LEN[p], c, Preset.TEXT);
3899
+ }
3900
+ let next = n[para * W.stride + W.next];
3901
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === item) {
3902
+ const after = n[next * W.stride + W.next];
3903
+ c.move_to(next);
3904
+ p = mp_node(c, sink, p);
3905
+ next = after;
3906
+ }
3907
+ c.move_to(item);
3908
+ return p;
3909
+ }
3910
+
3446
3911
  function mp_code_fence(c: Cursor, sink: MapSink, p: number): number {
3447
3912
  const info = fence_info(c, c.meta());
3448
3913
  if (hl !== null) {
@@ -3459,7 +3924,7 @@ function mp_code_fence(c: Cursor, sink: MapSink, p: number): number {
3459
3924
  p = tr_open(c, p, S_PRE_CODE, S_PRE_CODE_OPEN, S_GT);
3460
3925
  }
3461
3926
  const ao = mo.length + FOLD_LEN[p];
3462
- tr_content(c, sink, p, escape_fence_text(c), Code.CODE_CONTENT);
3927
+ fence_content(c, sink, p, escape_fence_text(c), false);
3463
3928
  const bc = mo.length;
3464
3929
  _spans(sink, pre, ao, bc, bc + FOLD_LEN[S_PRE_CODE_CLOSE], c, Preset.CODE);
3465
3930
  return S_PRE_CODE_CLOSE;
@@ -3487,7 +3952,9 @@ function mp_html(c: Cursor, sink: MapSink, p: number): number {
3487
3952
  // source passthrough and reconstruction exactly as render_node
3488
3953
  const self_closing = !!meta?.self_closing;
3489
3954
  const passthrough =
3490
- self_closing && c.end > c.start ? c.slice(c.start, c.end) : '';
3955
+ self_closing && !c.synthetic && c.end > c.start
3956
+ ? c.slice(c.start, c.end)
3957
+ : '';
3491
3958
  if (passthrough) {
3492
3959
  mo += passthrough;
3493
3960
  } else {
@@ -3496,7 +3963,7 @@ function mp_html(c: Cursor, sink: MapSink, p: number): number {
3496
3963
  | Record<string, string | boolean>
3497
3964
  | undefined;
3498
3965
  if (html_attrs) {
3499
- const typed = c.end !== Slot.NONE;
3966
+ const typed = !c.synthetic;
3500
3967
  for (const k in html_attrs) {
3501
3968
  const v = html_attrs[k];
3502
3969
  if (v === true) {
@@ -3507,6 +3974,8 @@ function mp_html(c: Cursor, sink: MapSink, p: number): number {
3507
3974
  s += ' ' + k + '="' + escape_attr(v as string, typed) + '"';
3508
3975
  }
3509
3976
  }
3977
+ if (meta!.repeated !== undefined)
3978
+ s += repeated_attrs(meta!.repeated as [string, unknown][], typed);
3510
3979
  }
3511
3980
  mo += s;
3512
3981
  if (self_closing) {
@@ -3535,7 +4004,7 @@ function mp_html(c: Cursor, sink: MapSink, p: number): number {
3535
4004
  return 0;
3536
4005
  }
3537
4006
  const ao = mo.length + FOLD_LEN[S_GT];
3538
- if (tag === 'script' || tag === 'style') {
4007
+ if (is_raw_text_tag(tag)) {
3539
4008
  if (c.index === hoist_script) {
3540
4009
  mo = mo + FOLD_STR[S_GT] + '\n';
3541
4010
  mo_imports(c, sink, false);
@@ -3703,7 +4172,7 @@ function mp_table_cells_ext(
3703
4172
  /** flat so the caller never pays for a rope */
3704
4173
  function render_folded(c: Cursor): string {
3705
4174
  fold_out = comp_prefix;
3706
- const p = fold_node(c, 0);
4175
+ const p = ws_on ? ws_pass(c, undefined, 0, CM.FOLD, false) : fold_node(c, 0);
3707
4176
  let html = fold_out;
3708
4177
  fold_out = '';
3709
4178
  if (p !== 0) html += FOLD_STR[p];
@@ -3717,7 +4186,6 @@ function render_folded(c: Cursor): string {
3717
4186
  // one walker below serves the fold, trace and mapped renders
3718
4187
 
3719
4188
  const H_NAME = ['', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
3720
- const CHECKED = new Set(['checked']);
3721
4189
  const TABLE_PARTS = ['thead', 'tbody', 'tr', 'th', 'td'];
3722
4190
 
3723
4191
  /** the element a markdown node renders as, empty when it is not replaceable */
@@ -3747,9 +4215,9 @@ function comp_name(c: Cursor): string {
3747
4215
  case K.LIST_ITEM:
3748
4216
  return 'li';
3749
4217
  case K.CODE_SPAN:
3750
- return 'code';
4218
+ return c.extra === 1 && hl !== null && hl.math === true ? 'math' : 'code';
3751
4219
  case K.CODE_FENCE:
3752
- return 'pre';
4220
+ return hl !== null && hl.math === true && is_math(c) ? 'math' : 'pre';
3753
4221
  case K.LINK:
3754
4222
  return 'a';
3755
4223
  case K.IMAGE:
@@ -3758,6 +4226,8 @@ function comp_name(c: Cursor): string {
3758
4226
  return 'hr';
3759
4227
  case K.HARD_BREAK:
3760
4228
  return 'br';
4229
+ case K.FOOTNOTE_REFERENCE:
4230
+ return 'footnote';
3761
4231
  case K.TABLE:
3762
4232
  return 'table';
3763
4233
  case K.HTML:
@@ -3770,10 +4240,9 @@ function comp_name(c: Cursor): string {
3770
4240
  /** lowercase elements only, never svelte tags or components */
3771
4241
  const ELEMENT_NAME = /^[a-z][a-z0-9-]*$/;
3772
4242
 
3773
- /** the tag of an element a parse plugin created, empty for typed html, which has a source span */
4243
+ /** the tag of an element a parse plugin created, empty for typed html */
3774
4244
  function plugin_tag(c: Cursor): string {
3775
- const e = c.end;
3776
- return e !== Slot.NONE && e > c.start ? '' : html_tag(c);
4245
+ return c.synthetic ? html_tag(c) : '';
3777
4246
  }
3778
4247
 
3779
4248
  /** the tag of an element, typed or from a plugin */
@@ -3859,6 +4328,9 @@ function comp_scan(c: Cursor): void {
3859
4328
  if (k >= K.DIRECTIVE_INLINE && k <= K.DIRECTIVE_CONTAINER) {
3860
4329
  const ref = dir_scope!.get(dir_name(c));
3861
4330
  if (ref !== undefined) note_use(ref);
4331
+ } else if (k === K.FOOTNOTE_DEFINITION) {
4332
+ // a definition is in the tree where it was written, the section holds it
4333
+ comp_use('footnotes');
3862
4334
  } else {
3863
4335
  const name = comp_name(c);
3864
4336
  if (name !== '') {
@@ -3986,7 +4458,8 @@ function cm_text(
3986
4458
  c: Cursor,
3987
4459
  sink: MapSink | undefined,
3988
4460
  p: number,
3989
- text: string
4461
+ text: string,
4462
+ code: number = Code.CODE_CONTENT
3990
4463
  ): number {
3991
4464
  if (comp_mode === CM.FOLD) return push_dyn(p, text);
3992
4465
  if (p !== 0) mo += FOLD_STR[p];
@@ -4001,7 +4474,7 @@ function cm_text(
4001
4474
  c.value_start,
4002
4475
  c.value_end,
4003
4476
  c.index,
4004
- Code.CODE_CONTENT
4477
+ code
4005
4478
  );
4006
4479
  mo += text;
4007
4480
  return 0;
@@ -4016,7 +4489,7 @@ function cm_spans(
4016
4489
  post: number,
4017
4490
  preset: number
4018
4491
  ): void {
4019
- if (comp_mode === CM.TRACE) tr_point(sink!, pre, c.start);
4492
+ if (comp_mode === CM.TRACE) tr_at(sink!, pre, c);
4020
4493
  else if (comp_mode === CM.MAPPED) _spans(sink!, pre, ao, bc, post, c, preset);
4021
4494
  }
4022
4495
 
@@ -4027,11 +4500,11 @@ function cm_void(
4027
4500
  pre: number,
4028
4501
  preset: number
4029
4502
  ): void {
4030
- if (comp_mode === CM.TRACE) tr_point(sink!, pre, c.start);
4503
+ if (comp_mode === CM.TRACE) tr_at(sink!, pre, c);
4031
4504
  else if (comp_mode === CM.MAPPED) {
4032
4505
  const post = mo.length;
4033
4506
  put_record(sink!, pre, post, c.start, c.end, c.index, preset << 2);
4034
- if (preset === Preset.STRUCTURE && sink!.syntax) {
4507
+ if (preset === Preset.STRUCTURE && sink!.syntax && c.end !== Slot.NONE) {
4035
4508
  const vs = c.value_start;
4036
4509
  put_record(
4037
4510
  sink!,
@@ -4129,17 +4602,27 @@ function open_tag_end(
4129
4602
  /** the per kind extras of a typed element, those the author wrote are kept */
4130
4603
  function typed_extras(
4131
4604
  tag: string,
4132
- attrs: Record<string, unknown> | undefined
4605
+ attrs: Record<string, unknown> | undefined,
4606
+ spans: AttrSpans | undefined
4133
4607
  ): string {
4134
4608
  if (tag.length === 2 && tag.charCodeAt(0) === 104) {
4135
4609
  const d = tag.charCodeAt(1) - 48;
4136
- if (d >= 1 && d <= 6 && (attrs === undefined || !('level' in attrs)))
4610
+ if (d >= 1 && d <= 6 && !writes(attrs, spans, 'level'))
4137
4611
  return ' level={' + d + '}';
4138
- } else if (tag === 'ol' && (attrs === undefined || !('start' in attrs)))
4612
+ } else if (tag === 'ol' && !writes(attrs, spans, 'start'))
4139
4613
  return ' start={1}';
4140
4614
  return '';
4141
4615
  }
4142
4616
 
4617
+ function writes(
4618
+ attrs: Record<string, unknown> | undefined,
4619
+ spans: AttrSpans | undefined,
4620
+ name: string
4621
+ ): boolean {
4622
+ if (spans !== undefined) return spans_have(spans, name);
4623
+ return attrs !== undefined && name in attrs;
4624
+ }
4625
+
4143
4626
  /**
4144
4627
  * the attributes of a typed tag, copied from source, and what follows them up
4145
4628
  * to > or />, which maps as identity only when it is as long
@@ -4175,6 +4658,7 @@ function js_prop(key: string, value: string): string {
4175
4658
  * resolves it, the tree builder path keeps byte offsets
4176
4659
  */
4177
4660
  function fence_info(c: Cursor, meta: Record<string, unknown> | undefined) {
4661
+ if (fs !== null) return fence_info_from(c, meta);
4178
4662
  let info = meta?.info as string | undefined;
4179
4663
  if (!info) {
4180
4664
  const info_start = meta?.info_start as number | undefined;
@@ -4209,6 +4693,7 @@ function comp_node(
4209
4693
  let lead = '';
4210
4694
  let close = '</' + local + '>';
4211
4695
  let preset: number = Preset.TEXT;
4696
+ let raw = false;
4212
4697
 
4213
4698
  switch (kind) {
4214
4699
  case K.HEADING:
@@ -4217,8 +4702,7 @@ function comp_node(
4217
4702
 
4218
4703
  case K.LINK: {
4219
4704
  const meta = c.meta();
4220
- if (meta?.href)
4221
- open += ' href="' + escape_text_html(meta.href as string) + '"';
4705
+ if (meta?.href) open += url_attr(meta.href as string, false);
4222
4706
  if (meta?.title)
4223
4707
  open += ' title="' + escape_text_html(meta.title as string) + '"';
4224
4708
  open += _attrs(c, LINK_HANDLED);
@@ -4227,8 +4711,7 @@ function comp_node(
4227
4711
 
4228
4712
  case K.IMAGE: {
4229
4713
  const meta = c.meta();
4230
- if (meta?.src)
4231
- open += ' src="' + escape_text_html(meta.src as string) + '"';
4714
+ if (meta?.src) open += url_attr(meta.src as string, true);
4232
4715
  open += ' alt="' + escape_text_html(_children_raw(c)) + '"';
4233
4716
  if (meta?.title)
4234
4717
  open += ' title="' + escape_text_html(meta.title as string) + '"';
@@ -4245,6 +4728,15 @@ function comp_node(
4245
4728
  case K.HARD_BREAK:
4246
4729
  return cm_put(p, open + _attrs(c) + ' />\n');
4247
4730
 
4731
+ case K.FOOTNOTE_REFERENCE:
4732
+ return fn_component(c, sink, p, pre, open, close);
4733
+
4734
+ case K.PARAGRAPH:
4735
+ open += _attrs(c);
4736
+ // the first paragraph of a task item, see task_para
4737
+ if (c.index === task_at) lead = task_box;
4738
+ break;
4739
+
4248
4740
  case K.LIST: {
4249
4741
  const meta = c.meta();
4250
4742
  open += _attrs(c);
@@ -4260,9 +4752,9 @@ function comp_node(
4260
4752
 
4261
4753
  case K.LIST_ITEM: {
4262
4754
  const checked = c.meta()?.checked;
4263
- if (typeof checked === 'boolean')
4264
- open += _attrs(c, CHECKED) + ' checked={' + checked + '}';
4265
- else open += _attrs(c);
4755
+ open += _attrs(c);
4756
+ // the replacement draws its own checkbox
4757
+ if (typeof checked === 'boolean') open += ' checked={' + checked + '}';
4266
4758
  close += '\n';
4267
4759
  break;
4268
4760
  }
@@ -4283,11 +4775,15 @@ function comp_node(
4283
4775
  case K.CODE_SPAN: {
4284
4776
  if (hl !== null) {
4285
4777
  const h = hl_span(c);
4286
- if (h !== null) return comp_hl_code(c, sink, p, h, open, close);
4778
+ if (h !== null)
4779
+ // comp_name picked the replacement, math or code
4780
+ return ref.name === 'math'
4781
+ ? comp_hl_span_math(c, sink, p, h, open, close)
4782
+ : comp_hl_code(c, sink, p, h, open, close);
4287
4783
  }
4288
4784
  p = cm_put(p, open + _attrs(c) + '>');
4289
4785
  const ao = mo.length;
4290
- let code = escape_code_text(c);
4786
+ let code = escape_span_text(c);
4291
4787
  if (string_index_of.call(code, '\n') !== -1)
4292
4788
  code = code.replace(/\n/g, ' ');
4293
4789
  p = cm_text(c, sink, p, code);
@@ -4301,7 +4797,11 @@ function comp_node(
4301
4797
  const info = fence_info(c, c.meta());
4302
4798
  if (hl !== null) {
4303
4799
  const b = hl_block(c, info);
4304
- if (b !== null) return comp_hl_pre(c, sink, p, b, info, open, close);
4800
+ if (b !== null)
4801
+ // comp_name picked the replacement, math or pre
4802
+ return ref.name === 'math'
4803
+ ? comp_hl_math(c, sink, p, b, info!, open, close)
4804
+ : comp_hl_pre(c, sink, p, b, info, open, close);
4305
4805
  }
4306
4806
  open += _attrs(c);
4307
4807
  let inner = '<code>';
@@ -4317,9 +4817,14 @@ function comp_node(
4317
4817
  }
4318
4818
  // children are the element, svelte keeps whitespace only inside a <pre>
4319
4819
  // it can see, code is the raw text
4820
+ if (fs !== null) open += fence_props(c);
4320
4821
  p = cm_put(p, open + js_prop('code', fence_text(c)) + '><pre>' + inner);
4321
4822
  const ao = mo.length;
4322
- p = cm_text(c, sink, p, escape_fence_text(c));
4823
+ const text = escape_fence_text(c);
4824
+ p =
4825
+ comp_mode === CM.FOLD
4826
+ ? push_dyn(p, text)
4827
+ : fence_content(c, sink!, p, text, comp_mode === CM.TRACE);
4323
4828
  const bc = mo.length;
4324
4829
  p = cm_put(p, '</code></pre>' + close);
4325
4830
  cm_spans(c, sink, pre, ao, bc, mo.length, Preset.CODE);
@@ -4329,9 +4834,18 @@ function comp_node(
4329
4834
  case K.HTML: {
4330
4835
  const meta = c.meta()!;
4331
4836
  const tag = meta.tag as string;
4332
- const attrs = meta.attributes as Record<string, unknown> | undefined;
4333
- const typed = c.end !== Slot.NONE && c.end > c.start;
4334
- const end = typed ? open_tag_end(c, tag, attrs) : -1;
4837
+ const typed = !c.synthetic;
4838
+ // attributes nobody read spell the source, so they need no check
4839
+ const spans = typed ? unread_spans(meta) : undefined;
4840
+ const unread = spans !== undefined && spans.name_start === c.start + 1;
4841
+ const attrs = unread
4842
+ ? undefined
4843
+ : (meta.attributes as Record<string, unknown> | undefined);
4844
+ let end = -1;
4845
+ if (unread) {
4846
+ end = spans!.open_end;
4847
+ tag_attrs_end = spans!.attrs_end;
4848
+ } else if (typed) end = open_tag_end(c, tag, attrs);
4335
4849
  // a typed tag keeps its attributes as written, they map as identity
4336
4850
  const from = c.start + 1 + tag.length;
4337
4851
  const at = pre + open.length;
@@ -4344,8 +4858,10 @@ function comp_node(
4344
4858
  open += expr_attr(k, meta_str((v as any).value));
4345
4859
  else open += ' ' + k + '="' + escape_attr(meta_str(v), typed) + '"';
4346
4860
  }
4861
+ if (meta.repeated !== undefined)
4862
+ open += repeated_attrs(meta.repeated as [string, unknown][], typed);
4347
4863
  }
4348
- if (typed) open += typed_extras(tag, attrs);
4864
+ if (typed) open += typed_extras(tag, attrs, unread ? spans : undefined);
4349
4865
  const shut = meta.self_closing ? ' />' : '>';
4350
4866
  if (end !== -1)
4351
4867
  cm_tag(
@@ -4362,6 +4878,7 @@ function comp_node(
4362
4878
  cm_void(c, sink, pre, Preset.TEXT);
4363
4879
  return p;
4364
4880
  }
4881
+ raw = is_raw_text_tag(tag);
4365
4882
  break;
4366
4883
  }
4367
4884
 
@@ -4371,7 +4888,8 @@ function comp_node(
4371
4888
 
4372
4889
  p = cm_put(p, open + '>' + lead);
4373
4890
  const ao = mo.length;
4374
- p = kind === K.TABLE ? cm_table(c, sink, p) : cm_children(c, sink, p);
4891
+ if (raw) p = cm_text(c, sink, p, c.text(), Code.SVELTE_CONTENT);
4892
+ else p = kind === K.TABLE ? cm_table(c, sink, p) : cm_children(c, sink, p);
4375
4893
  const bc = mo.length;
4376
4894
  p = cm_put(p, close);
4377
4895
  cm_spans(c, sink, pre, ao, bc, mo.length, preset);
@@ -4909,6 +5427,8 @@ export interface TemplateWrapper {
4909
5427
  specifier: string;
4910
5428
  /** true spreads the module script metadata export as props */
4911
5429
  metadata: boolean;
5430
+ /** a module script export passed as the prop of its name, after the metadata */
5431
+ data?: string;
4912
5432
  }
4913
5433
 
4914
5434
  const WRAP_LOCAL = 'Template_MDSVEX';
@@ -4930,18 +5450,22 @@ const WRAP_HOISTED = new Set([
4930
5450
  // set by wrap_begin, '' when the render wraps nothing
4931
5451
  let wrap_spec = '';
4932
5452
  let wrap_metadata = false;
5453
+ let wrap_data = '';
4933
5454
  let wrap_open = '';
4934
5455
  let wrap_mode: number = CM.FOLD;
4935
5456
 
4936
5457
  function wrap_begin(wrapper: TemplateWrapper, mode: number): void {
4937
5458
  wrap_spec = wrapper.specifier;
4938
5459
  wrap_metadata = wrapper.metadata;
5460
+ const data = wrapper.data;
5461
+ if (data !== undefined) wrap_data = data;
4939
5462
  wrap_mode = mode;
4940
5463
  }
4941
5464
 
4942
5465
  function wrap_end(): void {
4943
5466
  wrap_spec = '';
4944
5467
  wrap_metadata = false;
5468
+ wrap_data = '';
4945
5469
  wrap_open = '';
4946
5470
  wrap_mode = CM.FOLD;
4947
5471
  comp_lines = '';
@@ -4976,34 +5500,8 @@ function wrap_pass(
4976
5500
  const k = n[b] & 0xff;
4977
5501
  if (k !== K.LINE_BREAK) {
4978
5502
  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
- }
5503
+ if (wrap_hoists(c, k) === hoisted)
5504
+ p = walk_child(c, sink, p, wrap_mode, child, k);
5007
5505
  }
5008
5506
  child = n[b + W.next];
5009
5507
  }
@@ -5011,20 +5509,53 @@ function wrap_pass(
5011
5509
  return p;
5012
5510
  }
5013
5511
 
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 {
5023
- if (wrap_mode === CM.FOLD) return push_dyn(p, s);
5024
- if (p !== 0) mo += FOLD_STR[p];
5025
- mo += s;
5026
- return 0;
5027
- }
5512
+ /** the child of kind k the cursor is at, through the walk of mode */
5513
+ function walk_child(
5514
+ c: Cursor,
5515
+ sink: MapSink | undefined,
5516
+ p: number,
5517
+ mode: number,
5518
+ child: number,
5519
+ k: number
5520
+ ): number {
5521
+ if (mode === CM.FOLD) return fold_node(c, p);
5522
+ if (k === K.TEXT) {
5523
+ // the mapped walks render text in their children loops
5524
+ const n = c.words;
5525
+ const b = child * W.stride;
5526
+ const vs = n[b + W.value_start];
5527
+ const ve = n[b + W.value_end];
5528
+ const t = escape_text_at(c, child, vs, ve, false);
5529
+ if (p !== 0) mo += FOLD_STR[p];
5530
+ if (vs !== Slot.NONE && ve > vs) {
5531
+ const at = mo.length;
5532
+ if (mode === CM.TRACE) tr_run(sink!, at, at + t.length, vs, ve);
5533
+ else
5534
+ put_record(sink!, at, at + t.length, vs, ve, child, Code.TEXT_CONTENT);
5535
+ }
5536
+ mo += t;
5537
+ return 0;
5538
+ }
5539
+ return mode === CM.TRACE ? tr_node(c, sink!, p) : mp_node(c, sink!, p);
5540
+ }
5541
+
5542
+ /** c is at the root, the hoisted children then the rest inside the wrapper */
5543
+ function wrap_root(c: Cursor, sink: MapSink | undefined, p: number): number {
5544
+ p = wrap_pass(c, sink, p, true);
5545
+ p = cm_wrap(p, wrap_open);
5546
+ p = ws_on
5547
+ ? ws_pass(c, sink, p, wrap_mode, true)
5548
+ : wrap_pass(c, sink, p, false);
5549
+ if (fn !== null) p = fn_section(c, sink, p, wrap_mode);
5550
+ return cm_wrap(p, '</' + WRAP_LOCAL + '>');
5551
+ }
5552
+
5553
+ function cm_wrap(p: number, s: string): number {
5554
+ if (wrap_mode === CM.FOLD) return push_dyn(p, s);
5555
+ if (p !== 0) mo += FOLD_STR[p];
5556
+ mo += s;
5557
+ return 0;
5558
+ }
5028
5559
 
5029
5560
  // internal helpers
5030
5561
 
@@ -5035,8 +5566,11 @@ function _render_block(cursor: Cursor): string {
5035
5566
 
5036
5567
  // block entry
5037
5568
 
5569
+ /** the idx of the block that holds the footnotes section, which is no node */
5570
+ export const FOOTNOTES_BLOCK = -1;
5571
+
5038
5572
  export interface CursorBlockEntry {
5039
- /** node buffer index, use as keyed each key. */
5573
+ /** node buffer index, use as keyed each key, FOOTNOTES_BLOCK for the footnotes section */
5040
5574
  idx: number;
5041
5575
  /** rendered html string. */
5042
5576
  html: string;
@@ -5100,6 +5634,35 @@ export class CursorHTMLRenderer {
5100
5634
  */
5101
5635
  // declared, so a renderer that never wraps builds and keeps no field
5102
5636
  declare template?: TemplateWrapper;
5637
+ /**
5638
+ * import statements the next renders add to the instance script, with or
5639
+ * without a replacement, the cached render of update ignores them
5640
+ */
5641
+ declare imports?: readonly string[];
5642
+ /** replaces the src of images and the href of links in the next renders, unset keeps every url */
5643
+ declare urls?: UrlLookup;
5644
+ /**
5645
+ * the fences of the next renders that take their code, info string or pre
5646
+ * props from elsewhere, unset reads every fence from the source
5647
+ */
5648
+ declare fences?: FenceSources;
5649
+ /**
5650
+ * curly quotes, dashes and ellipses in the prose of the next renders, unset
5651
+ * leaves text as written, as does a text node that holds a stored string
5652
+ */
5653
+ declare smart_punctuation?: SmartPunctuation;
5654
+ /**
5655
+ * the footnotes of the next renders with their numbers, unset numbers the
5656
+ * references of a document as its labels are first met
5657
+ */
5658
+ declare footnotes?: FootnoteRender;
5659
+ /**
5660
+ * true wraps each run of two or more static root blocks in a display
5661
+ * contents div, the cached render of update ignores it
5662
+ */
5663
+ declare wrap_static?: boolean;
5664
+ /** the cached render met a footnote, so each update numbers them again */
5665
+ private noted = false;
5103
5666
 
5104
5667
  constructor(opts?: { cache?: boolean }) {
5105
5668
  this.cache = opts?.cache ?? true;
@@ -5120,8 +5683,17 @@ export class CursorHTMLRenderer {
5120
5683
  const c = this.cursor;
5121
5684
  c.reset();
5122
5685
  esc_reset(source);
5686
+ // ahead of the rest, it throws for an option it can not read
5687
+ const smart = this.smart_punctuation;
5688
+ if (smart !== undefined) sp_begin(smart);
5123
5689
  hl = this.highlight;
5124
5690
  pm = this.pre_meta;
5691
+ const urls = this.urls;
5692
+ if (urls !== undefined) ul = urls;
5693
+ const fences = this.fences;
5694
+ if (fences !== undefined) fs = fences;
5695
+ const notes = this.footnotes;
5696
+ if (notes !== undefined) fn = notes;
5125
5697
 
5126
5698
  // no caching, single-pass full render
5127
5699
  if (!this.cache) {
@@ -5131,6 +5703,8 @@ export class CursorHTMLRenderer {
5131
5703
  this.html = this.render_wrapped(c, buf, null, false, code);
5132
5704
  return this.blocks;
5133
5705
  }
5706
+ const imports = this.imports;
5707
+ const extra = imports !== undefined && imports.length !== 0;
5134
5708
  try {
5135
5709
  if (
5136
5710
  (scope !== null && scope.size !== 0) ||
@@ -5143,30 +5717,44 @@ export class CursorHTMLRenderer {
5143
5717
  CM.FOLD,
5144
5718
  this.replace_typed
5145
5719
  );
5720
+ if (extra) extra_begin(imports);
5146
5721
  if (this.strict_directives) dir_strict = true;
5147
- hoist_begin(buf);
5148
- if (code) module_begin(buf, code);
5722
+ if (this.wrap_static === true) ws_on = true;
5723
+ hoist_begin(c, buf, extra);
5724
+ if (code) module_begin_fold(buf, code);
5149
5725
  prebuilt_begin(buf);
5150
5726
  this.html = render_folded(c);
5727
+ if (fn !== null) this.html += fn_section_folded(c);
5151
5728
  } finally {
5729
+ fn = null;
5152
5730
  hl = null;
5153
5731
  pm = null;
5732
+ if (urls !== undefined) ul = null;
5733
+ if (fences !== undefined) fs = null;
5734
+ if (smart !== undefined) sp_end();
5154
5735
  esc_prebuilt = true;
5155
5736
  esc_bits = null;
5156
5737
  if (dir_strict) dir_strict = false;
5738
+ if (ws_on) ws_on = false;
5157
5739
  if (comp_scope !== null) comp_end();
5740
+ if (extra) extra_end();
5158
5741
  if (code) module_end();
5159
5742
  }
5160
5743
  return this.blocks;
5161
5744
  }
5162
5745
 
5163
5746
  try {
5164
- hoist_begin(buf);
5747
+ hoist_begin(c, buf, false);
5165
5748
  if (code) module_begin(buf, code);
5749
+ if (this.noted && fn === null) fn = footnote_index(buf, true);
5166
5750
  this.update_blocks(c);
5167
5751
  } finally {
5752
+ fn = null;
5168
5753
  hl = null;
5169
5754
  pm = null;
5755
+ if (urls !== undefined) ul = null;
5756
+ if (fences !== undefined) fs = null;
5757
+ if (smart !== undefined) sp_end();
5170
5758
  if (code) module_end();
5171
5759
  }
5172
5760
  return this.blocks;
@@ -5176,6 +5764,13 @@ export class CursorHTMLRenderer {
5176
5764
  private update_blocks(c: Cursor): void {
5177
5765
  if (!c.goto_first_child()) return;
5178
5766
 
5767
+ // the section of the last update is no child of the root
5768
+ const blocks = this.blocks;
5769
+ if (
5770
+ blocks.length !== 0 &&
5771
+ blocks[blocks.length - 1].idx === FOOTNOTES_BLOCK
5772
+ )
5773
+ blocks.pop();
5179
5774
  let block_idx = 0;
5180
5775
  do {
5181
5776
  if (c.kind === K.LINE_BREAK) continue;
@@ -5201,6 +5796,12 @@ export class CursorHTMLRenderer {
5201
5796
  } while (c.goto_next_sibling());
5202
5797
 
5203
5798
  c.goto_parent();
5799
+ // a block may have met the first footnote of the document
5800
+ if (fn !== null) {
5801
+ this.noted = true;
5802
+ const html = fn_section_folded(c);
5803
+ if (html !== '') blocks.push({ idx: FOOTNOTES_BLOCK, html });
5804
+ }
5204
5805
  this.html = this.blocks.map((b) => b.html).join('');
5205
5806
  }
5206
5807
 
@@ -5220,8 +5821,17 @@ export class CursorHTMLRenderer {
5220
5821
  const c = this.cursor;
5221
5822
  c.reset();
5222
5823
  esc_reset(source);
5824
+ // ahead of the rest, it throws for an option it can not read
5825
+ const smart = this.smart_punctuation;
5826
+ if (smart !== undefined) sp_begin(smart);
5223
5827
  hl = this.highlight;
5224
5828
  pm = this.pre_meta;
5829
+ const urls = this.urls;
5830
+ if (urls !== undefined) ul = urls;
5831
+ const fences = this.fences;
5832
+ if (fences !== undefined) fs = fences;
5833
+ const notes = this.footnotes;
5834
+ if (notes !== undefined) fn = notes;
5225
5835
 
5226
5836
  if (this.template !== undefined) {
5227
5837
  this.html = this.render_wrapped(c, buf, sink, trace, code);
@@ -5229,6 +5839,8 @@ export class CursorHTMLRenderer {
5229
5839
  }
5230
5840
  const scope = this.scope;
5231
5841
  const directives = this.directives;
5842
+ const imports = this.imports;
5843
+ const extra = imports !== undefined && imports.length !== 0;
5232
5844
  let p = 0;
5233
5845
  try {
5234
5846
  if (
@@ -5242,8 +5854,10 @@ export class CursorHTMLRenderer {
5242
5854
  trace ? CM.TRACE : CM.MAPPED,
5243
5855
  this.replace_typed
5244
5856
  );
5857
+ if (extra) extra_begin(imports);
5245
5858
  if (this.strict_directives) dir_strict = true;
5246
- hoist_begin(buf);
5859
+ if (this.wrap_static === true) ws_on = true;
5860
+ hoist_begin(c, buf, extra);
5247
5861
  mo = comp_prefix;
5248
5862
  if (code) {
5249
5863
  module_begin(buf, code);
@@ -5251,15 +5865,23 @@ export class CursorHTMLRenderer {
5251
5865
  mo += module_script();
5252
5866
  }
5253
5867
  prebuilt_begin(buf);
5254
- if (trace) p = tr_node(c, sink, 0);
5868
+ if (ws_on) p = ws_pass(c, sink, 0, trace ? CM.TRACE : CM.MAPPED, false);
5869
+ else if (trace) p = tr_node(c, sink, 0);
5255
5870
  else p = mp_node(c, sink, 0);
5871
+ if (fn !== null) p = fn_section(c, sink, p, trace ? CM.TRACE : CM.MAPPED);
5256
5872
  } finally {
5873
+ fn = null;
5257
5874
  hl = null;
5258
5875
  pm = null;
5876
+ if (urls !== undefined) ul = null;
5877
+ if (fences !== undefined) fs = null;
5878
+ if (smart !== undefined) sp_end();
5259
5879
  esc_prebuilt = true;
5260
5880
  esc_bits = null;
5261
5881
  if (dir_strict) dir_strict = false;
5882
+ if (ws_on) ws_on = false;
5262
5883
  if (comp_scope !== null) comp_end();
5884
+ if (extra) extra_end();
5263
5885
  if (code) module_end();
5264
5886
  }
5265
5887
  // the module string would keep the document alive
@@ -5284,6 +5906,8 @@ export class CursorHTMLRenderer {
5284
5906
  ): string {
5285
5907
  const scope = this.scope;
5286
5908
  const directives = this.directives;
5909
+ const imports = this.imports;
5910
+ const extra = imports !== undefined && imports.length !== 0;
5287
5911
  const mode = sink === null ? CM.FOLD : trace ? CM.TRACE : CM.MAPPED;
5288
5912
  let html: string;
5289
5913
  try {
@@ -5292,10 +5916,15 @@ export class CursorHTMLRenderer {
5292
5916
  (directives !== null && directives.size !== 0)
5293
5917
  )
5294
5918
  comp_last = comp_begin(c, scope, directives, mode, this.replace_typed);
5919
+ if (extra) extra_begin(imports);
5295
5920
  if (this.strict_directives) dir_strict = true;
5921
+ if (this.wrap_static === true) ws_on = true;
5296
5922
  wrap_begin(this.template!, mode);
5297
5923
  wrap_hoist_begin(c, buf);
5298
- if (code) module_begin(buf, code);
5924
+ if (code) {
5925
+ if (sink === null) module_begin_fold(buf, code);
5926
+ else module_begin(buf, code);
5927
+ }
5299
5928
  prebuilt_begin(buf);
5300
5929
  if (sink === null) {
5301
5930
  fold_out = comp_prefix;
@@ -5313,12 +5942,18 @@ export class CursorHTMLRenderer {
5313
5942
  if (p !== 0) html += FOLD_STR[p];
5314
5943
  }
5315
5944
  } finally {
5945
+ fn = null;
5316
5946
  hl = null;
5317
5947
  pm = null;
5948
+ ul = null;
5949
+ fs = null;
5950
+ if (sp_on) sp_end();
5318
5951
  esc_prebuilt = true;
5319
5952
  esc_bits = null;
5320
5953
  if (dir_strict) dir_strict = false;
5954
+ if (ws_on) ws_on = false;
5321
5955
  if (comp_scope !== null) comp_end();
5956
+ if (extra) extra_end();
5322
5957
  wrap_end();
5323
5958
  if (code) module_end();
5324
5959
  this.template = undefined;
@@ -5416,10 +6051,26 @@ export class CursorHTMLRenderer {
5416
6051
  sink.release();
5417
6052
  }
5418
6053
 
6054
+ /**
6055
+ * the nodes changed after their block was rendered, as the node of a
6056
+ * deferred task that applied does, apply_ready gives them, each block that
6057
+ * holds one renders again at the next update, a renderer without a cache
6058
+ * renders every block anyway
6059
+ */
6060
+ invalidate(buf: NodeBuffer, nodes: readonly number[]): void {
6061
+ const closed = this.closed;
6062
+ if (closed === null) return;
6063
+ for (let i = 0; i < nodes.length; i++) {
6064
+ const block = top_block(buf, nodes[i]);
6065
+ if (block !== Slot.NONE) closed.delete(block);
6066
+ }
6067
+ }
6068
+
5419
6069
  reset(): void {
5420
6070
  this.blocks.length = 0;
5421
6071
  this.closed?.clear();
5422
6072
  this.html = '';
6073
+ this.noted = false;
5423
6074
  }
5424
6075
 
5425
6076
  /** drop every reference to the last render so a kept renderer holds no document */
@@ -5438,6 +6089,14 @@ export class CursorHTMLRenderer {
5438
6089
  // a tag is a slice that would keep the source alive
5439
6090
  comp_last = NO_WARNINGS;
5440
6091
  if (this.template !== undefined) this.template = undefined;
6092
+ if (this.imports !== undefined) this.imports = undefined;
6093
+ if (this.urls !== undefined) this.urls = undefined;
6094
+ if (this.fences !== undefined) this.fences = undefined;
6095
+ if (this.smart_punctuation !== undefined)
6096
+ this.smart_punctuation = undefined;
6097
+ if (this.footnotes !== undefined) this.footnotes = undefined;
6098
+ if (this.wrap_static !== undefined) this.wrap_static = undefined;
6099
+ this.noted = false;
5441
6100
  // the escape index is module state and would keep the source alive,
5442
6101
  // every render resets it, a zero length clamps any text of '' to empty
5443
6102
  esc_src = '';
@@ -5451,12 +6110,14 @@ export class CursorHTMLRenderer {
5451
6110
  export function _mapped_begin(
5452
6111
  buf: NodeBuffer,
5453
6112
  source: string,
5454
- html: string
6113
+ html: string,
6114
+ urls: UrlLookup | null = null
5455
6115
  ): void {
5456
6116
  esc_reset(source);
5457
6117
  prebuilt_begin(buf);
5458
6118
  // callers render parts, they place imports and scripts themselves
5459
6119
  hoist_reset();
6120
+ ul = urls;
5460
6121
  mo = html;
5461
6122
  }
5462
6123
 
@@ -5464,6 +6125,7 @@ export function _mapped_begin(
5464
6125
  export function _mapped_end(): string {
5465
6126
  esc_prebuilt = true;
5466
6127
  esc_bits = null;
6128
+ ul = null;
5467
6129
  const html = mo;
5468
6130
  mo = '';
5469
6131
  return html;
@@ -5483,6 +6145,121 @@ let hl: CodeHighlighter | null = null;
5483
6145
  /** the meta conventions of the render in progress for a plain pre replacement */
5484
6146
  let pm: PreMeta | null = null;
5485
6147
 
6148
+ /** the extra import lines of the render in progress, '' for none, hoist_begin adds them to comp_lines */
6149
+ let extra_lines = '';
6150
+
6151
+ /** extra_end follows it, in a finally */
6152
+ function extra_begin(lines: readonly string[]): void {
6153
+ let s = '';
6154
+ for (let i = 0; i < lines.length; i++) s = s + lines[i] + '\n';
6155
+ extra_lines = s;
6156
+ }
6157
+
6158
+ function extra_end(): void {
6159
+ extra_lines = '';
6160
+ // a render with no replacement never reaches comp_end
6161
+ comp_lines = '';
6162
+ comp_prefix = '';
6163
+ }
6164
+
6165
+ /** the url lookup of the render in progress, null keeps every url */
6166
+ let ul: UrlLookup | null = null;
6167
+
6168
+ /** the src or href attribute of an image or link, every walk writes it here or in the fold twin */
6169
+ function url_attr(url: string, image: boolean): string {
6170
+ if (ul === null)
6171
+ return (image ? ' src="' : ' href="') + escape_text_html(url) + '"';
6172
+ return url_swap(url, image);
6173
+ }
6174
+
6175
+ /** fold twin of url_attr, without a lookup the statics stay in the register */
6176
+ function fold_url(p: number, url: string, image: boolean): number {
6177
+ if (ul !== null) return push_dyn(p, url_swap(url, image));
6178
+ p = push_static(p, image ? S_SRC : S_HREF);
6179
+ p = push_dyn(p, escape_text_html(url));
6180
+ return push_static(p, S_QUOTE);
6181
+ }
6182
+
6183
+ /** url_attr of a render with a lookup */
6184
+ function url_swap(url: string, image: boolean): string {
6185
+ return url_value(image ? ' src=' : ' href=', url, ul!(url, image));
6186
+ }
6187
+
6188
+ /** the attribute for what the lookup gave, a picture is the src of a replacement */
6189
+ function url_value(
6190
+ name: string,
6191
+ url: string,
6192
+ to: UrlSource | null | undefined
6193
+ ): string {
6194
+ if (to == null) to = url;
6195
+ if (typeof to !== 'string') return name + '{' + to.value + '}';
6196
+ // an empty url of the source has no attribute either
6197
+ return to === '' ? '' : name + '"' + escape_text_html(to) + '"';
6198
+ }
6199
+
6200
+ /** the static that closes what img_head opened last */
6201
+ let img_close = S_SELF_CLOSE;
6202
+
6203
+ /**
6204
+ * a markdown image up to its alt, the img and its src, or for a picture the
6205
+ * markup enhanced-img gives an image it imports, one source per format and
6206
+ * the img with the fallback and its size
6207
+ */
6208
+ function img_head(src: unknown): string {
6209
+ img_close = S_SELF_CLOSE;
6210
+ if (!src) return '<img';
6211
+ if (ul === null) return '<img src="' + escape_text_html(src as string) + '"';
6212
+ const to = ul(src as string, true);
6213
+ if (to == null || typeof to === 'string' || to.type !== 'picture')
6214
+ return '<img' + url_value(' src=', src as string, to);
6215
+ img_close = S_PICTURE_CLOSE;
6216
+ const v = to.value;
6217
+ return (
6218
+ '<picture>{#each Object.entries(' +
6219
+ v +
6220
+ ".sources) as [format, srcset]}<source {srcset} type={'image/' + format} />{/each}<img src={" +
6221
+ v +
6222
+ '.img.src} width={' +
6223
+ v +
6224
+ '.img.w} height={' +
6225
+ v +
6226
+ '.img.h}'
6227
+ );
6228
+ }
6229
+
6230
+ /** the fences of the render in progress that take their code from elsewhere, null reads every one from the source */
6231
+ let fs: FenceSources | null = null;
6232
+
6233
+ /** fence_text of a render with fence sources */
6234
+ function fence_text_from(c: Cursor): string {
6235
+ const from = fs!.get(c.index);
6236
+ if (from !== undefined) return from.text;
6237
+ return held_text(c);
6238
+ }
6239
+
6240
+ /** fence_info of a render with fence sources */
6241
+ function fence_info_from(
6242
+ c: Cursor,
6243
+ meta: Record<string, unknown> | undefined
6244
+ ): string | undefined {
6245
+ const given = fs!.get(c.index)?.info;
6246
+ if (given !== undefined) return given;
6247
+ let info = meta?.info as string | undefined;
6248
+ if (!info) {
6249
+ const info_start = meta?.info_start as number | undefined;
6250
+ const info_end = meta?.info_end as number | undefined;
6251
+ if (info_start != null && info_end != null)
6252
+ info = c.slice(info_start, info_end);
6253
+ }
6254
+ return info;
6255
+ }
6256
+
6257
+ /** the extra props of the pre replacement of the fence at c */
6258
+ function fence_props(c: Cursor): string {
6259
+ const props = fs!.get(c.index)?.props;
6260
+ return props === undefined ? '' : props;
6261
+ }
6262
+
5486
6263
  /** the first space or tab of an info string, -1 for none */
5487
6264
  function info_space(info: string): number {
5488
6265
  for (let i = 0; i < info.length; i++) {
@@ -5504,6 +6281,12 @@ function info_meta(info: string): string {
5504
6281
  return space === -1 ? '' : info.slice(space).trim();
5505
6282
  }
5506
6283
 
6284
+ /** true for a fence whose language is math */
6285
+ function is_math(c: Cursor): boolean {
6286
+ const info = fence_info(c, c.meta());
6287
+ return info !== undefined && info_lang(info) === 'math';
6288
+ }
6289
+
5507
6290
  /** the fence at c through the highlighter, null renders it plain */
5508
6291
  function hl_block(
5509
6292
  c: Cursor,
@@ -5517,11 +6300,16 @@ function hl_block(
5517
6300
  );
5518
6301
  }
5519
6302
 
5520
- /** the code span at c through the highlighter when it has a #! hint */
6303
+ /**
6304
+ * the code span at c through the highlighter when it has a #! hint, or as
6305
+ * inline math when it is one, without a math renderer that is plain code
6306
+ */
5521
6307
  function hl_span(c: Cursor): HighlightedCode | null {
6308
+ if (c.extra === 1)
6309
+ return hl!.math === true ? hl!.inline_math!(held_text(c), c.start) : null;
5522
6310
  const lang = fence_info(c, c.meta());
5523
6311
  if (!lang) return null;
5524
- let code = c.text();
6312
+ let code = held_text(c);
5525
6313
  if (string_index_of.call(code, '\n') !== -1) code = code.replace(/\n/g, ' ');
5526
6314
  return hl!.inline(code, lang, c.start);
5527
6315
  }
@@ -5590,7 +6378,7 @@ function tr_hl(
5590
6378
  if (live !== null) hl_live_trace(sink, c, mo.length, body, live);
5591
6379
  else if (body.length !== 0) tr_point(sink, mo.length, c.value_start);
5592
6380
  mo += body + tail;
5593
- tr_point(sink, pre, c.start);
6381
+ tr_at(sink, pre, c);
5594
6382
  return 0;
5595
6383
  }
5596
6384
 
@@ -5659,6 +6447,7 @@ function comp_hl_pre(
5659
6447
  if (dropped !== null)
5660
6448
  for (let i = 0; i < dropped.length; i++)
5661
6449
  hl!.warn('meta_prop_ignored', dropped[i], c.start);
6450
+ if (fs !== null) open += fence_props(c);
5662
6451
  open +=
5663
6452
  b.props +
5664
6453
  (b.code_template === null
@@ -5680,6 +6469,40 @@ function comp_hl_pre(
5680
6469
  );
5681
6470
  }
5682
6471
 
6472
+ /**
6473
+ * display math as the math replacement, the children are what renders
6474
+ * without one, the block gives the props that say which math it is
6475
+ */
6476
+ function comp_hl_math(
6477
+ c: Cursor,
6478
+ sink: MapSink | undefined,
6479
+ p: number,
6480
+ b: HighlightedBlock,
6481
+ info: string,
6482
+ open: string,
6483
+ close: string
6484
+ ): number {
6485
+ const pre = mo.length;
6486
+ const meta = info_meta(info);
6487
+ open += b.props + js_prop('code', b.code);
6488
+ if (meta) open += js_prop('meta', meta);
6489
+ return cm_hl(c, sink, p, pre, open + _attrs(c) + '>', b.body, close, null);
6490
+ }
6491
+
6492
+ /** inline math as the math replacement, which takes code and no display */
6493
+ function comp_hl_span_math(
6494
+ c: Cursor,
6495
+ sink: MapSink | undefined,
6496
+ p: number,
6497
+ h: HighlightedCode,
6498
+ open: string,
6499
+ close: string
6500
+ ): number {
6501
+ const pre = mo.length;
6502
+ open += js_prop('code', c.text()) + _attrs(c) + '>';
6503
+ return cm_hl(c, sink, p, pre, open, h.body, close, null);
6504
+ }
6505
+
5683
6506
  /** a highlighted code span as the code replacement, which also takes lang */
5684
6507
  function comp_hl_code(
5685
6508
  c: Cursor,
@@ -5710,8 +6533,7 @@ function comp_hl_code(
5710
6533
  */
5711
6534
  function hl_source(c: Cursor, text: string, at: number, len: number): number {
5712
6535
  const vs = c.value_start;
5713
- if (c.prebuilt === undefined && bq_depth(c.words, c.index) === 0)
5714
- return vs + at;
6536
+ if (c.prebuilt === undefined) return vs + at;
5715
6537
  const src = c.source;
5716
6538
  const ve = c.value_end;
5717
6539
  let line_start = vs;
@@ -5744,8 +6566,21 @@ function hl_live_records(
5744
6566
  body: string,
5745
6567
  live: number[]
5746
6568
  ): void {
5747
- const text = fence_text(c);
5748
6569
  const idx = c.index;
6570
+ // code from elsewhere has no source to map an expression to
6571
+ if (fs !== null && fs.has(idx)) {
6572
+ put_record(
6573
+ sink,
6574
+ at,
6575
+ at + body.length,
6576
+ c.value_start,
6577
+ c.value_end,
6578
+ idx,
6579
+ Code.CODE_CONTENT
6580
+ );
6581
+ return;
6582
+ }
6583
+ const text = fence_text(c);
5749
6584
  let gen = at;
5750
6585
  let src = c.value_start;
5751
6586
  for (let i = 0; i < live.length; i += 3) {
@@ -5771,6 +6606,10 @@ function hl_live_trace(
5771
6606
  body: string,
5772
6607
  live: number[]
5773
6608
  ): void {
6609
+ if (fs !== null && fs.has(c.index)) {
6610
+ if (body.length !== 0) tr_point(sink, at, c.value_start);
6611
+ return;
6612
+ }
5774
6613
  const text = fence_text(c);
5775
6614
  let gen = at;
5776
6615
  let src = c.value_start;
@@ -5787,3 +6626,1074 @@ function hl_live_trace(
5787
6626
  const end = at + body.length;
5788
6627
  if (end > gen) tr_run(sink, gen, end, src, c.value_end);
5789
6628
  }
6629
+
6630
+ // smart punctuation is off for most renders, so its bindings sit last too
6631
+
6632
+ /** true while a render with smart punctuation runs, its text goes through smart_text */
6633
+ let sp_on = false;
6634
+ /** the option the fields below were read from */
6635
+ let sp_opt: SmartPunctuation | undefined;
6636
+ let sp_quotes = false;
6637
+ let sp_dashes = false;
6638
+ let sp_dots = false;
6639
+ let sp_open2 = '“';
6640
+ let sp_close2 = '”';
6641
+ let sp_open1 = '‘';
6642
+ let sp_close1 = '’';
6643
+
6644
+ // an escape index of its own, as esc_src and its pointers, so a render
6645
+ // without the option reads and writes none of it
6646
+ let sp_src = '';
6647
+ let sp_len = 0;
6648
+ let sp_lo = 0;
6649
+ let sp_amp = -1;
6650
+ let sp_lt = -1;
6651
+ let sp_gt = -1;
6652
+ let sp_quot = -1;
6653
+ let sp_apos = -1;
6654
+ let sp_dash = -1;
6655
+ let sp_dot = -1;
6656
+ /** the least of the seven when they were last moved, a slice that ends by it has no hit */
6657
+ let sp_min = -1;
6658
+
6659
+ /** the parent whose text sp_plain answered last, text nodes arrive in runs of one parent */
6660
+ let sp_parent = -1;
6661
+ let sp_parent_plain = false;
6662
+
6663
+ // a child list has no back links, the walk moves forward, so the pair the
6664
+ // last search found is where the next one starts
6665
+ let sp_trail: number = Slot.NONE;
6666
+ let sp_trail_prev: number = Slot.NONE;
6667
+
6668
+ /** how a neighbour of a quote reads */
6669
+ const enum Side {
6670
+ /** renders nothing, the neighbour is further on */
6671
+ NOTHING = -1,
6672
+ /** whitespace, a break, or the edge of the block */
6673
+ SPACE = 0,
6674
+ /** an opening bracket or a dash, a quote after one opens */
6675
+ OPENER = 1,
6676
+ /** a letter or a digit, or an inline node that is not text */
6677
+ WORD = 2,
6678
+ OTHER = 3,
6679
+ }
6680
+
6681
+ /** what a quote becomes */
6682
+ const enum Quote {
6683
+ STRAIGHT = 0,
6684
+ OPEN = 1,
6685
+ CLOSE = 2,
6686
+ /** a closing quote with a word on both sides */
6687
+ APOSTROPHE = 3,
6688
+ }
6689
+
6690
+ /** typed html that starts and ends a block, so no text reads on across its tags */
6691
+ const SP_BLOCK_TAGS = new Set([
6692
+ 'address',
6693
+ 'article',
6694
+ 'aside',
6695
+ 'blockquote',
6696
+ 'br',
6697
+ 'caption',
6698
+ 'dd',
6699
+ 'details',
6700
+ 'dialog',
6701
+ 'div',
6702
+ 'dl',
6703
+ 'dt',
6704
+ 'fieldset',
6705
+ 'figcaption',
6706
+ 'figure',
6707
+ 'footer',
6708
+ 'form',
6709
+ 'h1',
6710
+ 'h2',
6711
+ 'h3',
6712
+ 'h4',
6713
+ 'h5',
6714
+ 'h6',
6715
+ 'header',
6716
+ 'hr',
6717
+ 'li',
6718
+ 'main',
6719
+ 'nav',
6720
+ 'ol',
6721
+ 'p',
6722
+ 'pre',
6723
+ 'section',
6724
+ 'summary',
6725
+ 'table',
6726
+ 'tbody',
6727
+ 'td',
6728
+ 'tfoot',
6729
+ 'th',
6730
+ 'thead',
6731
+ 'tr',
6732
+ 'ul',
6733
+ ]);
6734
+
6735
+ /** typed html whose text is code, or is read as written */
6736
+ const SP_PLAIN_TAGS = new Set([
6737
+ 'code',
6738
+ 'kbd',
6739
+ 'pre',
6740
+ 'samp',
6741
+ 'var',
6742
+ 'tt',
6743
+ 'textarea',
6744
+ 'script',
6745
+ 'style',
6746
+ ]);
6747
+
6748
+ /**
6749
+ * escape_text_at of a value that ends past esc_len, which is zero for a
6750
+ * render with smart punctuation, so all of its text comes here and a render
6751
+ * without the option runs no check for it
6752
+ */
6753
+ function escape_past(
6754
+ c: Cursor,
6755
+ i: number,
6756
+ vs: number,
6757
+ ve: number,
6758
+ code: boolean
6759
+ ): string {
6760
+ if (sp_on)
6761
+ return code
6762
+ ? escape_code(c.source.slice(vs, ve))
6763
+ : smart_text(c, i, vs, ve);
6764
+ // a value cut short by the source, as a fed document has
6765
+ return esc_len <= vs ? '' : escape_text_at(c, i, vs, esc_len, code);
6766
+ }
6767
+
6768
+ /** after esc_reset, sp_end follows it, in a finally */
6769
+ function sp_begin(opt: SmartPunctuation): void {
6770
+ if (opt !== sp_opt) sp_read(opt);
6771
+ if (!(sp_quotes || sp_dashes || sp_dots)) return;
6772
+ sp_on = true;
6773
+ sp_parent = -1;
6774
+ sp_trail = Slot.NONE;
6775
+ // every value is then past the end and goes to escape_past
6776
+ esc_len = 0;
6777
+ }
6778
+
6779
+ function sp_end(): void {
6780
+ sp_on = false;
6781
+ // the indexes would keep the source alive, and the next text resets esc_len
6782
+ sp_src = '';
6783
+ sp_len = 0;
6784
+ esc_src = '';
6785
+ }
6786
+
6787
+ /** a quote string is text, a brace in it would start an expression */
6788
+ function sp_quote(s: unknown): string {
6789
+ if (typeof s !== 'string')
6790
+ throw new TypeError('smart_punctuation.quotes takes four strings');
6791
+ const text = escape_text_html(s);
6792
+ return BRACE_TEST.test(text) ? text.replace(BRACE_MATCH, code_replace) : text;
6793
+ }
6794
+
6795
+ function sp_read(opt: SmartPunctuation): void {
6796
+ // a throw below leaves no half read option behind
6797
+ sp_opt = undefined;
6798
+ sp_open2 = '“';
6799
+ sp_close2 = '”';
6800
+ sp_open1 = '‘';
6801
+ sp_close1 = '’';
6802
+ if (typeof opt === 'boolean') {
6803
+ sp_quotes = sp_dashes = sp_dots = opt;
6804
+ } else {
6805
+ const quotes = opt.quotes;
6806
+ sp_quotes = quotes !== false;
6807
+ sp_dashes = opt.dashes !== false;
6808
+ sp_dots = opt.ellipses !== false;
6809
+ if (typeof quotes === 'object') {
6810
+ if (!Array.isArray(quotes) || quotes.length !== 4)
6811
+ throw new TypeError('smart_punctuation.quotes takes four strings');
6812
+ sp_open2 = sp_quote(quotes[0]);
6813
+ sp_close2 = sp_quote(quotes[1]);
6814
+ sp_open1 = sp_quote(quotes[2]);
6815
+ sp_close1 = sp_quote(quotes[3]);
6816
+ }
6817
+ }
6818
+ sp_opt = opt;
6819
+ }
6820
+
6821
+ function sp_reset(src: string): void {
6822
+ sp_src = src;
6823
+ const len = (sp_len = src.length);
6824
+ sp_lo = 0;
6825
+ sp_amp = -1;
6826
+ sp_lt = -1;
6827
+ sp_gt = -1;
6828
+ sp_quot = -1;
6829
+ // a kind that is off never hits
6830
+ sp_apos = sp_quotes ? -1 : len;
6831
+ sp_dash = sp_dashes ? -1 : len;
6832
+ sp_dot = sp_dots ? -1 : len;
6833
+ sp_min = -1;
6834
+ }
6835
+
6836
+ function sp_next(needle: string, from: number): number {
6837
+ const i = sp_src.indexOf(needle, from);
6838
+ return i === -1 ? sp_len : i;
6839
+ }
6840
+
6841
+ /**
6842
+ * escape_text_at for the prose of a render with smart punctuation, the slice
6843
+ * is node i or a run of text and soft breaks from it, as fold_children joins
6844
+ * them, a quote at either end of the slice asks the tree
6845
+ */
6846
+ function smart_text(c: Cursor, i: number, vs: number, ve: number): string {
6847
+ const src = c.source;
6848
+ if (src !== sp_src || vs < sp_lo) sp_reset(src);
6849
+ if (ve > sp_len) {
6850
+ ve = sp_len;
6851
+ if (ve <= vs) return '';
6852
+ }
6853
+ let m = sp_min;
6854
+ // with the least pointer past the slice none is behind it and none hits
6855
+ if (m < ve) m = sp_seek(vs);
6856
+ const first = src.charCodeAt(vs);
6857
+ if ((first === 123 || first === 125) && brace_escaped(src, vs)) {
6858
+ const brace = first === 123 ? '&#123;' : '&#125;';
6859
+ if (m >= ve) return brace + src.slice(vs + 1, ve);
6860
+ return brace + smart_hits(c, i, src, vs, ve, m, vs + 1);
6861
+ }
6862
+ if (m >= ve) return src.slice(vs, ve);
6863
+ return smart_hits(c, i, src, vs, ve, m, vs);
6864
+ }
6865
+
6866
+ /** moves every pointer to its first match at or after vs, returns the least */
6867
+ function sp_seek(vs: number): number {
6868
+ if (sp_amp < vs) sp_amp = sp_next('&', vs);
6869
+ if (sp_lt < vs) sp_lt = sp_next('<', vs);
6870
+ if (sp_gt < vs) sp_gt = sp_next('>', vs);
6871
+ if (sp_quot < vs) sp_quot = sp_next('"', vs);
6872
+ if (sp_apos < vs) sp_apos = sp_next("'", vs);
6873
+ if (sp_dash < vs) sp_dash = sp_next('--', vs);
6874
+ if (sp_dot < vs) sp_dot = sp_next('...', vs);
6875
+ sp_lo = vs;
6876
+ let m = sp_amp;
6877
+ if (sp_lt < m) m = sp_lt;
6878
+ if (sp_gt < m) m = sp_gt;
6879
+ if (sp_quot < m) m = sp_quot;
6880
+ if (sp_apos < m) m = sp_apos;
6881
+ if (sp_dash < m) m = sp_dash;
6882
+ if (sp_dot < m) m = sp_dot;
6883
+ return (sp_min = m);
6884
+ }
6885
+
6886
+ /** m is the first hit at or after vs, the text is copied from pos */
6887
+ function smart_hits(
6888
+ c: Cursor,
6889
+ i: number,
6890
+ src: string,
6891
+ vs: number,
6892
+ ve: number,
6893
+ m: number,
6894
+ pos: number
6895
+ ): string {
6896
+ // asked once a node has a hit, a node with none never walks up
6897
+ const smart = !sp_plain(c, i);
6898
+ // an escape opens its text node at the escaped char
6899
+ const escaped = brace_escaped(src, vs);
6900
+ let text = '';
6901
+ while (m < ve) {
6902
+ const ch = src.charCodeAt(m);
6903
+ text += src.slice(pos, m);
6904
+ pos = m + 1;
6905
+ if (ch === 38) {
6906
+ sp_amp = sp_next('&', m + 1);
6907
+ if (text_reference(src, vs, ve, m)) pos = m;
6908
+ else text += '&amp;';
6909
+ } else if (ch === 60) {
6910
+ text += '&lt;';
6911
+ sp_lt = sp_next('<', m + 1);
6912
+ } else if (ch === 62) {
6913
+ text += '&gt;';
6914
+ sp_gt = sp_next('>', m + 1);
6915
+ } else if (ch === 34 || ch === 39) {
6916
+ if (ch === 34) sp_quot = sp_next('"', m + 1);
6917
+ else sp_apos = sp_next("'", m + 1);
6918
+ const side =
6919
+ smart && sp_quotes && !(escaped && m === vs)
6920
+ ? quote_side(c, i, src, vs, ve, m)
6921
+ : Quote.STRAIGHT;
6922
+ if (side === Quote.STRAIGHT) {
6923
+ if (ch === 34) text += '&quot;';
6924
+ else pos = m;
6925
+ } else if (side === Quote.OPEN) text += ch === 34 ? sp_open2 : sp_open1;
6926
+ else if (ch === 34) text += sp_close2;
6927
+ else text += side === Quote.APOSTROPHE ? '’' : sp_close1;
6928
+ } else {
6929
+ // a run of hyphens or dots, only one of an exact length is replaced
6930
+ let end = m + 1;
6931
+ while (end < ve && src.charCodeAt(end) === ch) end++;
6932
+ const dash = ch === 45;
6933
+ if (dash) sp_dash = sp_next('--', end);
6934
+ else sp_dot = sp_next('...', end);
6935
+ const run = end - m;
6936
+ if (
6937
+ smart &&
6938
+ (run === 3 || (run === 2 && dash)) &&
6939
+ !(escaped && m === vs) &&
6940
+ // the run goes on in the next text node, behind a backslash
6941
+ !(
6942
+ end === ve &&
6943
+ src.charCodeAt(ve) === 92 &&
6944
+ src.charCodeAt(ve + 1) === ch
6945
+ ) &&
6946
+ !(run === 2 && is_flag(c, i, src, vs, ve, m))
6947
+ )
6948
+ text += dash ? (run === 2 ? '–' : '—') : '…';
6949
+ else text += src.slice(m, end);
6950
+ pos = end;
6951
+ }
6952
+ m = sp_amp;
6953
+ if (sp_lt < m) m = sp_lt;
6954
+ if (sp_gt < m) m = sp_gt;
6955
+ if (sp_quot < m) m = sp_quot;
6956
+ if (sp_apos < m) m = sp_apos;
6957
+ if (sp_dash < m) m = sp_dash;
6958
+ if (sp_dot < m) m = sp_dot;
6959
+ }
6960
+ // every pointer is now at or past ve
6961
+ sp_lo = ve;
6962
+ sp_min = m;
6963
+ return text + src.slice(pos, ve);
6964
+ }
6965
+
6966
+ /** two hyphens after a space and before a letter are a command line flag */
6967
+ function is_flag(
6968
+ c: Cursor,
6969
+ i: number,
6970
+ src: string,
6971
+ vs: number,
6972
+ ve: number,
6973
+ m: number
6974
+ ): boolean {
6975
+ if (m + 2 >= ve) return false;
6976
+ const after = src.charCodeAt(m + 2) | 32;
6977
+ if (after < 97 || after > 122) return false;
6978
+ const before = m > vs ? sp_side(src.charCodeAt(m - 1)) : sp_left(c, i);
6979
+ return before === Side.SPACE;
6980
+ }
6981
+
6982
+ /**
6983
+ * which way the quote at m faces, from the character before it and the one
6984
+ * after, in an unbroken run of quotes each takes the side of the first
6985
+ */
6986
+ function quote_side(
6987
+ c: Cursor,
6988
+ i: number,
6989
+ src: string,
6990
+ vs: number,
6991
+ ve: number,
6992
+ m: number
6993
+ ): number {
6994
+ let q = m;
6995
+ while (q > vs) {
6996
+ const ch = src.charCodeAt(q - 1);
6997
+ if (ch !== 34 && ch !== 39) break;
6998
+ q--;
6999
+ }
7000
+ let before: number;
7001
+ if (q > vs) before = sp_side(src.charCodeAt(q - 1));
7002
+ else if (q < m && brace_escaped(src, vs)) {
7003
+ // the run starts at a quote a backslash escaped, which stays as written
7004
+ before = Side.OTHER;
7005
+ q = m;
7006
+ } else before = sp_left(c, i);
7007
+ const after =
7008
+ m + 1 < ve
7009
+ ? sp_side(src.charCodeAt(m + 1))
7010
+ : sp_right(c, sp_last(c, i, ve));
7011
+ if (q < m)
7012
+ return (before === Side.SPACE || before === Side.OPENER) &&
7013
+ after !== Side.SPACE
7014
+ ? Quote.OPEN
7015
+ : Quote.CLOSE;
7016
+ if (before === Side.SPACE)
7017
+ return after === Side.SPACE ? Quote.STRAIGHT : Quote.OPEN;
7018
+ if (before === Side.OPENER && after !== Side.SPACE) return Quote.OPEN;
7019
+ return before === Side.WORD && after === Side.WORD
7020
+ ? Quote.APOSTROPHE
7021
+ : Quote.CLOSE;
7022
+ }
7023
+
7024
+ /** how a character reads beside a quote, only the apostrophe asks for a word */
7025
+ function sp_side(ch: number): number {
7026
+ if (ch <= 32 || ch === 0xa0) return Side.SPACE;
7027
+ if (ch < 128) {
7028
+ if (ch === 40 || ch === 91 || ch === 123 || ch === 45) return Side.OPENER;
7029
+ if (ch >= 48 && ch <= 57) return Side.WORD;
7030
+ const lower = ch | 32;
7031
+ return lower >= 97 && lower <= 122 ? Side.WORD : Side.OTHER;
7032
+ }
7033
+ if (ch === 0x2013 || ch === 0x2014) return Side.OPENER;
7034
+ if (ch < 0xc0) return Side.OTHER;
7035
+ if (ch >= 0x2000 && ch <= 0x206f)
7036
+ return ch <= 0x200a ||
7037
+ ch === 0x2028 ||
7038
+ ch === 0x2029 ||
7039
+ ch === 0x202f ||
7040
+ ch === 0x205f
7041
+ ? Side.SPACE
7042
+ : Side.OTHER;
7043
+ if (ch === 0x1680 || ch === 0x3000) return Side.SPACE;
7044
+ if (ch > 0x3000 && ch <= 0x303f) return Side.OTHER;
7045
+ // a letter of any script, no table says which of the rest are not
7046
+ return Side.WORD;
7047
+ }
7048
+
7049
+ /** an inline node whose children read on from what is beside it */
7050
+ function sp_inline(c: Cursor, node: number): boolean {
7051
+ const kind = c.words[node * W.stride] & 0xff;
7052
+ if (kind === K.HTML) {
7053
+ const at = c.index;
7054
+ c.move_to(node);
7055
+ const tag = c.meta()?.tag;
7056
+ c.move_to(at);
7057
+ return typeof tag !== 'string' || !SP_BLOCK_TAGS.has(tag);
7058
+ }
7059
+ return (
7060
+ kind === K.EMPHASIS ||
7061
+ kind === K.STRONG ||
7062
+ kind === K.STRIKETHROUGH ||
7063
+ kind === K.SUPERSCRIPT ||
7064
+ kind === K.SUBSCRIPT ||
7065
+ kind === K.LINK ||
7066
+ kind === K.DIRECTIVE_INLINE
7067
+ );
7068
+ }
7069
+
7070
+ /** the sibling before node i, or NONE */
7071
+ function sp_before(n: Uint32Array, i: number): number {
7072
+ const parent = n[i * W.stride + W.parent];
7073
+ if (parent === Slot.NONE) return Slot.NONE;
7074
+ const first = n[parent * W.stride + W.first_child];
7075
+ let at = sp_trail;
7076
+ let prev = sp_trail_prev;
7077
+ if (at === Slot.NONE || n[at * W.stride + W.parent] !== parent) {
7078
+ at = first;
7079
+ prev = Slot.NONE;
7080
+ }
7081
+ // the second pass is for a node before the trail
7082
+ for (let pass = 0; pass < 2; pass++) {
7083
+ while (at !== Slot.NONE && n[at * W.stride + W.parent] === parent) {
7084
+ if (at === i) {
7085
+ sp_trail = at;
7086
+ sp_trail_prev = prev;
7087
+ return prev;
7088
+ }
7089
+ prev = at;
7090
+ at = n[at * W.stride + W.next];
7091
+ }
7092
+ at = first;
7093
+ prev = Slot.NONE;
7094
+ }
7095
+ return Slot.NONE;
7096
+ }
7097
+
7098
+ /** how the text before node i ends, in the sibling before it or before its inline parent */
7099
+ function sp_left(c: Cursor, i: number): number {
7100
+ const n = c.words;
7101
+ for (;;) {
7102
+ let prev = sp_before(n, i);
7103
+ while (prev !== Slot.NONE) {
7104
+ const side = sp_edge(c, prev, true);
7105
+ if (side !== Side.NOTHING) return side;
7106
+ prev = sp_before(n, prev);
7107
+ }
7108
+ const parent = n[i * W.stride + W.parent];
7109
+ if (parent === Slot.NONE || !sp_inline(c, parent)) return Side.SPACE;
7110
+ i = parent;
7111
+ }
7112
+ }
7113
+
7114
+ /** the text node of a run from i whose value ends at ve */
7115
+ function sp_last(c: Cursor, i: number, ve: number): number {
7116
+ const n = c.words;
7117
+ const parent = n[i * W.stride + W.parent];
7118
+ for (;;) {
7119
+ const b = i * W.stride;
7120
+ const kind = n[b] & 0xff;
7121
+ if (kind === K.TEXT) {
7122
+ if (n[b + W.value_end] >= ve) return i;
7123
+ } else if (kind !== K.SOFT_BREAK) return i;
7124
+ const next = n[b + W.next];
7125
+ if (next === Slot.NONE || n[next * W.stride + W.parent] !== parent)
7126
+ return i;
7127
+ i = next;
7128
+ }
7129
+ }
7130
+
7131
+ /** how the text after node i starts, in the sibling after it or after its inline parent */
7132
+ function sp_right(c: Cursor, i: number): number {
7133
+ const n = c.words;
7134
+ for (;;) {
7135
+ const parent = n[i * W.stride + W.parent];
7136
+ let next = n[i * W.stride + W.next];
7137
+ while (next !== Slot.NONE && n[next * W.stride + W.parent] === parent) {
7138
+ const side = sp_edge(c, next, false);
7139
+ if (side !== Side.NOTHING) return side;
7140
+ next = n[next * W.stride + W.next];
7141
+ }
7142
+ if (parent === Slot.NONE || !sp_inline(c, parent)) return Side.SPACE;
7143
+ i = parent;
7144
+ }
7145
+ }
7146
+
7147
+ /** how a node reads from outside, at its end when last is set, at its start otherwise */
7148
+ function sp_edge(c: Cursor, node: number, last: boolean): number {
7149
+ const n = c.words;
7150
+ const b = node * W.stride;
7151
+ const kind = n[b] & 0xff;
7152
+ if (kind === K.TEXT) {
7153
+ const s = c.prebuilt_at(node);
7154
+ if (s !== undefined) {
7155
+ if (s.length === 0) return Side.NOTHING;
7156
+ return sp_side(s.charCodeAt(last ? s.length - 1 : 0));
7157
+ }
7158
+ const vs = n[b + W.value_start];
7159
+ let ve = n[b + W.value_end];
7160
+ const src = c.source;
7161
+ if (ve !== Slot.NONE && ve > src.length) ve = src.length;
7162
+ if (vs === Slot.NONE || ve === Slot.NONE || ve <= vs) return Side.NOTHING;
7163
+ if (!last) return sp_side(src.charCodeAt(vs));
7164
+ const ch = src.charCodeAt(ve - 1);
7165
+ // a quote that opened is an opener for the quote after it
7166
+ if (
7167
+ (ch === 34 || ch === 39) &&
7168
+ sp_quotes &&
7169
+ !(ve - 1 === vs && brace_escaped(src, vs)) &&
7170
+ !sp_plain(c, node)
7171
+ )
7172
+ return quote_side(c, node, src, vs, ve, ve - 1) === Quote.OPEN
7173
+ ? Side.OPENER
7174
+ : Side.OTHER;
7175
+ return sp_side(ch);
7176
+ }
7177
+ if (kind === K.SOFT_BREAK || kind === K.HARD_BREAK || kind === K.LINE_BREAK)
7178
+ return Side.SPACE;
7179
+ if (kind === K.HTML_COMMENT) return Side.NOTHING;
7180
+ if (
7181
+ kind === K.CODE_SPAN ||
7182
+ kind === K.MUSTACHE ||
7183
+ kind === K.IMAGE ||
7184
+ kind === K.SVELTE_TAG
7185
+ )
7186
+ return Side.WORD;
7187
+ if (!sp_inline(c, node)) return Side.SPACE;
7188
+ let child = n[b + W.first_child];
7189
+ if (child === Slot.NONE) return Side.WORD;
7190
+ if (last)
7191
+ for (;;) {
7192
+ const next = n[child * W.stride + W.next];
7193
+ if (next === Slot.NONE || n[next * W.stride + W.parent] !== node) break;
7194
+ child = next;
7195
+ }
7196
+ const side = sp_edge(c, child, last);
7197
+ // an empty text or a comment at the edge, what is past it is not asked
7198
+ return side === Side.NOTHING ? Side.WORD : side;
7199
+ }
7200
+
7201
+ /**
7202
+ * true when the text of node i is not prose, the url of an autolink or the
7203
+ * text of typed html that holds code, at any depth
7204
+ */
7205
+ function sp_plain(c: Cursor, i: number): boolean {
7206
+ const n = c.words;
7207
+ let p = n[i * W.stride + W.parent];
7208
+ if (p === sp_parent) return sp_parent_plain;
7209
+ sp_parent = p;
7210
+ let plain = false;
7211
+ if (
7212
+ p !== Slot.NONE &&
7213
+ (n[p * W.stride] & 0xff) === K.LINK &&
7214
+ c.source.charCodeAt(n[p * W.stride + W.start]) === 60
7215
+ )
7216
+ plain = true;
7217
+ const at = c.index;
7218
+ while (!plain && p !== Slot.NONE) {
7219
+ if ((n[p * W.stride] & 0xff) === K.HTML) {
7220
+ c.move_to(p);
7221
+ const tag = c.meta()?.tag;
7222
+ if (typeof tag === 'string' && SP_PLAIN_TAGS.has(tag)) plain = true;
7223
+ }
7224
+ p = n[p * W.stride + W.parent];
7225
+ }
7226
+ c.move_to(at);
7227
+ return (sp_parent_plain = plain);
7228
+ }
7229
+
7230
+ // footnotes are cold beside the walks too, so their bindings follow
7231
+
7232
+ /** how the footnotes of a render are numbered and what its section says */
7233
+ export interface FootnoteRender extends FootnoteIndex {
7234
+ /** the accessible name of the section, Footnotes when unset */
7235
+ label?: string;
7236
+ /** the accessible name of a link back to a reference, {n} is its number, Back to reference {n} when unset */
7237
+ back_label?: string;
7238
+ }
7239
+
7240
+ /**
7241
+ * the footnotes of the render in progress, null until a walk meets one, a
7242
+ * render that was given none then numbers them itself, every reference as
7243
+ * its label is first met, which suits a document that is still arriving
7244
+ */
7245
+ let fn: FootnoteRender | null = null;
7246
+
7247
+ function fn_get(c: Cursor): FootnoteRender {
7248
+ if (fn !== null) return fn;
7249
+ const made: FootnoteRender = footnote_index(c.buffer, true)!;
7250
+ fn = made;
7251
+ return made;
7252
+ }
7253
+
7254
+ /** an id as the fragment of a url, a label with half a surrogate pair has no encoding and goes as it is */
7255
+ function fn_fragment(id: string): string {
7256
+ try {
7257
+ return encodeURIComponent(id);
7258
+ } catch {
7259
+ return id;
7260
+ }
7261
+ }
7262
+
7263
+ function fn_href(id: string): string {
7264
+ return escape_code(fn_fragment(id));
7265
+ }
7266
+
7267
+ /** the id a footnote id is given, another when a heading was given it first */
7268
+ function fn_id(notes: FootnoteRender, id: string): string {
7269
+ const moved = notes.moved;
7270
+ return moved === undefined || moved === null ? id : (moved.get(id) ?? id);
7271
+ }
7272
+
7273
+ /** the link a reference renders as, '' for one in a definition that is not rendered */
7274
+ function fn_reference(c: Cursor): string {
7275
+ const notes = fn_get(c);
7276
+ const at = notes.references.get(c.index);
7277
+ if (at === undefined) return '';
7278
+ const number = Math.floor(at / 65536);
7279
+ return (
7280
+ '<sup><a href="#' +
7281
+ fn_href(fn_id(notes, footnote_id(notes.labels[number - 1]))) +
7282
+ '" id="' +
7283
+ escape_code(fn_id(notes, footnote_ref_id(number, at % 65536))) +
7284
+ '" role="doc-noteref">' +
7285
+ number +
7286
+ '</a></sup>'
7287
+ );
7288
+ }
7289
+
7290
+ /** fn_reference for the trace and the mapped walk, the node is one record */
7291
+ function fn_reference_mapped(
7292
+ c: Cursor,
7293
+ sink: MapSink,
7294
+ p: number,
7295
+ trace: boolean
7296
+ ): number {
7297
+ const s = fn_reference(c);
7298
+ if (p !== 0) mo += FOLD_STR[p];
7299
+ const pre = mo.length;
7300
+ mo += s;
7301
+ if (trace) tr_at(sink, pre, c);
7302
+ else put_record(sink, pre, mo.length, c.start, c.end, c.index, 0);
7303
+ return 0;
7304
+ }
7305
+
7306
+ /** appends s in the walk of mode, returns the register */
7307
+ function fn_put(p: number, s: string, mode: number): number {
7308
+ if (mode === CM.FOLD) return push_dyn(p, s);
7309
+ if (p !== 0) mo += FOLD_STR[p];
7310
+ mo += s;
7311
+ return 0;
7312
+ }
7313
+
7314
+ /** the links from footnote number back to each of its count references */
7315
+ function fn_back(notes: FootnoteRender, number: number, count: number): string {
7316
+ const label = escape_code(notes.back_label ?? 'Back to reference {n}');
7317
+ let s = '';
7318
+ for (let nth = 1; nth <= count; nth++) {
7319
+ const name = nth === 1 ? '' + number : number + '-' + nth;
7320
+ s +=
7321
+ ' <a href="#' +
7322
+ fn_href(fn_id(notes, footnote_ref_id(number, nth))) +
7323
+ '" role="doc-backlink" aria-label="' +
7324
+ label.replace('&#123;n&#125;', name) +
7325
+ // the selector keeps the arrow from being drawn as an emoji
7326
+ '">\u21a9\ufe0e' +
7327
+ (nth === 1 ? '' : '<sup>' + nth + '</sup>') +
7328
+ '</a>';
7329
+ }
7330
+ return s;
7331
+ }
7332
+
7333
+ /**
7334
+ * the paragraph the cursor is at with links before its close tag, what the
7335
+ * paragraph case of each walk writes for one outside a tight list
7336
+ */
7337
+ function fn_paragraph(
7338
+ c: Cursor,
7339
+ sink: MapSink | undefined,
7340
+ p: number,
7341
+ mode: number,
7342
+ links: string
7343
+ ): number {
7344
+ if (mode === CM.FOLD) {
7345
+ p = fold_open(c, p, S_P, S_P_OPEN, S_GT);
7346
+ p = fold_children(c, p);
7347
+ return push_static(push_dyn(p, links), S_P_CLOSE);
7348
+ }
7349
+ const pre = mo.length + FOLD_LEN[p];
7350
+ const start = c.start;
7351
+ p = tr_open(c, p, S_P, S_P_OPEN, S_GT);
7352
+ if (mode === CM.TRACE) {
7353
+ p = tr_children(c, sink!, p);
7354
+ if (p !== 0) mo += FOLD_STR[p];
7355
+ mo += links;
7356
+ tr_point(sink!, pre, start);
7357
+ return S_P_CLOSE;
7358
+ }
7359
+ const ao = mo.length + FOLD_LEN[p];
7360
+ p = mp_children(c, sink!, p);
7361
+ if (p !== 0) mo += FOLD_STR[p];
7362
+ mo += links;
7363
+ const bc = mo.length;
7364
+ _spans(sink!, pre, ao, bc, bc + FOLD_LEN[S_P_CLOSE], c, Preset.TEXT);
7365
+ return S_P_CLOSE;
7366
+ }
7367
+
7368
+ /**
7369
+ * the blocks of the definition node with its back links, inside its last
7370
+ * paragraph when the walk writes that one itself, after its last block otherwise
7371
+ */
7372
+ function fn_definition(
7373
+ c: Cursor,
7374
+ sink: MapSink | undefined,
7375
+ p: number,
7376
+ mode: number,
7377
+ node: number,
7378
+ links: string
7379
+ ): number {
7380
+ const n = c.words;
7381
+ const first = n[node * W.stride + W.first_child];
7382
+ let last: number = Slot.NONE;
7383
+ for (
7384
+ let child = first;
7385
+ child !== Slot.NONE && n[child * W.stride + W.parent] === node;
7386
+ child = n[child * W.stride + W.next]
7387
+ ) {
7388
+ if ((n[child * W.stride] & 0xff) !== K.LINE_BREAK) last = child;
7389
+ }
7390
+ let placed = false;
7391
+ for (
7392
+ let child = first;
7393
+ child !== Slot.NONE && n[child * W.stride + W.parent] === node;
7394
+ child = n[child * W.stride + W.next]
7395
+ ) {
7396
+ const k = n[child * W.stride] & 0xff;
7397
+ if (k === K.LINE_BREAK) continue;
7398
+ c.move_to(child);
7399
+ if (
7400
+ child === last &&
7401
+ k === K.PARAGRAPH &&
7402
+ !(has_components && comp_ref(c) !== null)
7403
+ ) {
7404
+ p = fn_paragraph(c, sink, p, mode, links);
7405
+ placed = true;
7406
+ } else p = walk_child(c, sink, p, mode, child, k);
7407
+ }
7408
+ return placed ? p : fn_put(p, links, mode);
7409
+ }
7410
+
7411
+ /**
7412
+ * the section that holds every footnote a reference reached, in number
7413
+ * order, after the content of the root, nothing when no definition renders
7414
+ */
7415
+ function fn_section(
7416
+ c: Cursor,
7417
+ sink: MapSink | undefined,
7418
+ p: number,
7419
+ mode: number
7420
+ ): number {
7421
+ const notes = fn!;
7422
+ const definitions = notes.definitions;
7423
+ // a footnotes replacement is the section, and gets the list
7424
+ const ref = has_components ? comp_scope!.get('footnotes') : undefined;
7425
+ let open =
7426
+ ref !== undefined
7427
+ ? '<' + ref.local + '>\n<ol>\n'
7428
+ : '<section class="footnotes" role="doc-endnotes" aria-label="' +
7429
+ escape_code(notes.label ?? 'Footnotes') +
7430
+ '">\n<ol>\n';
7431
+ // the number an item gets with no value of its own
7432
+ let next = 1;
7433
+ for (let i = 0; i < definitions.length; i++) {
7434
+ const node = definitions[i];
7435
+ if (node === Slot.NONE) continue;
7436
+ let item =
7437
+ open +
7438
+ '<li id="' +
7439
+ escape_code(fn_id(notes, footnote_id(notes.labels[i]))) +
7440
+ '"';
7441
+ open = '';
7442
+ // a reference with no definition took a number, so the list skips it
7443
+ if (i + 1 !== next) item += ' value="' + (i + 1) + '"';
7444
+ next = i + 2;
7445
+ p = fn_put(p, item + '>', mode);
7446
+ p = fn_definition(
7447
+ c,
7448
+ sink,
7449
+ p,
7450
+ mode,
7451
+ node,
7452
+ fn_back(notes, i + 1, notes.counts[i])
7453
+ );
7454
+ p = fn_put(p, '</li>\n', mode);
7455
+ }
7456
+ c.move_to(0);
7457
+ if (open !== '') return p;
7458
+ return fn_put(
7459
+ p,
7460
+ ref !== undefined ? '</ol>\n</' + ref.local + '>' : '</ol>\n</section>',
7461
+ mode
7462
+ );
7463
+ }
7464
+
7465
+ const FOOTNOTE_HANDLED = new Set(['label']);
7466
+
7467
+ /** true while a note snippet is written, a reference in it gets none of its own */
7468
+ let fn_in_note = false;
7469
+
7470
+ /**
7471
+ * a reference as the footnote replacement, the link it renders as without
7472
+ * one is its children, and what its definition holds is the note snippet
7473
+ */
7474
+ function fn_component(
7475
+ c: Cursor,
7476
+ sink: MapSink | undefined,
7477
+ p: number,
7478
+ pre: number,
7479
+ open: string,
7480
+ close: string
7481
+ ): number {
7482
+ const notes = fn_get(c);
7483
+ const at = notes.references.get(c.index);
7484
+ // one in a definition that is not rendered
7485
+ if (at === undefined) return p;
7486
+ const number = Math.floor(at / 65536);
7487
+ const label = notes.labels[number - 1];
7488
+ const node = c.index;
7489
+ p = cm_put(
7490
+ p,
7491
+ open +
7492
+ js_prop('label', label) +
7493
+ ' number={' +
7494
+ number +
7495
+ '}' +
7496
+ js_prop('href', '#' + fn_fragment(fn_id(notes, footnote_id(label)))) +
7497
+ js_prop('id', fn_id(notes, footnote_ref_id(number, at % 65536))) +
7498
+ _attrs(c, FOOTNOTE_HANDLED) +
7499
+ '>'
7500
+ );
7501
+ const definition = notes.definitions[number - 1];
7502
+ // a note that refers to itself would never end
7503
+ if (definition !== Slot.NONE && !fn_in_note) {
7504
+ fn_in_note = true;
7505
+ try {
7506
+ p = cm_put(p, '{#snippet note()}');
7507
+ p = fn_definition(c, sink, p, comp_mode, definition, '');
7508
+ p = cm_put(p, '{/snippet}');
7509
+ } finally {
7510
+ fn_in_note = false;
7511
+ }
7512
+ c.move_to(node);
7513
+ }
7514
+ p = cm_put(p, fn_reference(c) + close);
7515
+ cm_void(c, sink, pre, Preset.TEXT);
7516
+ return p;
7517
+ }
7518
+
7519
+ /** fn_section as the html of the fold walk */
7520
+ function fn_section_folded(c: Cursor): string {
7521
+ fold_out = '';
7522
+ const p = fn_section(c, undefined, 0, CM.FOLD);
7523
+ let html = fold_out;
7524
+ fold_out = '';
7525
+ if (p !== 0) html += FOLD_STR[p];
7526
+ return html;
7527
+ }
7528
+
7529
+ // static runs
7530
+
7531
+ // hydration steps over the siblings before each node svelte owns one at a
7532
+ // time, a run of blocks in one wrapper is one step
7533
+
7534
+ /** set while a render wraps runs of static blocks, only the root walks read it */
7535
+ let ws_on = false;
7536
+
7537
+ const WS_OPEN = '<div style="display: contents">';
7538
+ const WS_CLOSE = '</div>';
7539
+
7540
+ /**
7541
+ * the root children as wrap_pass walks them, each block renders into an
7542
+ * output of its own and its records move when it joins its run and when the
7543
+ * run joins the document, so the html is never cut and a record moves twice
7544
+ * at most
7545
+ */
7546
+ function ws_pass(
7547
+ c: Cursor,
7548
+ sink: MapSink | undefined,
7549
+ p: number,
7550
+ mode: number,
7551
+ skip_hoisted: boolean
7552
+ ): number {
7553
+ const fold = mode === CM.FOLD;
7554
+ const stride = mode === CM.TRACE ? Trace.SIZE : Rec.SIZE;
7555
+ let doc = fold ? fold_out : mo;
7556
+ if (p !== 0) doc += FOLD_STR[p];
7557
+ // the static blocks not yet in doc, their records count from the start of run
7558
+ let run = '';
7559
+ let count = 0;
7560
+ let run_rec = 0;
7561
+ const n = c.words;
7562
+ let child = n[W.first_child];
7563
+ while (child !== Slot.NONE) {
7564
+ const b = child * W.stride;
7565
+ if (n[b + W.parent] !== 0) break;
7566
+ const k = n[b] & 0xff;
7567
+ if (k !== K.LINE_BREAK) {
7568
+ c.move_to(child);
7569
+ if (!skip_hoisted || !wrap_hoists(c, k)) {
7570
+ const rec = sink === undefined ? 0 : sink.n;
7571
+ if (fold) fold_out = '';
7572
+ else mo = '';
7573
+ p = walk_child(c, sink, 0, mode, child, k);
7574
+ let out = fold ? fold_out : mo;
7575
+ if (p !== 0) out += FOLD_STR[p];
7576
+ if (out.length === 0 && count === 0) {
7577
+ // renders nothing outside a run, an import or a definition
7578
+ ws_shift(sink, rec, stride, doc.length);
7579
+ } else if (out.length === 0 || ws_static(out)) {
7580
+ // a block that renders nothing inside a run neither ends it nor counts
7581
+ if (count === 0) run_rec = rec;
7582
+ ws_shift(sink, rec, stride, run.length);
7583
+ if (out.length !== 0) {
7584
+ run += out;
7585
+ count++;
7586
+ }
7587
+ } else {
7588
+ doc = ws_join(doc, run, count, sink, run_rec, rec, stride);
7589
+ run = '';
7590
+ count = 0;
7591
+ ws_shift(sink, rec, stride, doc.length);
7592
+ doc += out;
7593
+ }
7594
+ }
7595
+ }
7596
+ child = n[b + W.next];
7597
+ }
7598
+ const end = sink === undefined ? 0 : sink.n;
7599
+ doc = ws_join(doc, run, count, sink, run_rec, end, stride);
7600
+ if (fold) fold_out = doc;
7601
+ else mo = doc;
7602
+ c.move_to(0);
7603
+ return 0;
7604
+ }
7605
+
7606
+ /** doc and the run after it, inside a wrapper when the run holds two blocks or more */
7607
+ function ws_join(
7608
+ doc: string,
7609
+ run: string,
7610
+ count: number,
7611
+ sink: MapSink | undefined,
7612
+ from: number,
7613
+ to: number,
7614
+ stride: number
7615
+ ): string {
7616
+ if (count === 0) return doc;
7617
+ if (count === 1) {
7618
+ ws_move(sink, from, to, stride, doc.length);
7619
+ return doc + run;
7620
+ }
7621
+ ws_move(sink, from, to, stride, doc.length + WS_OPEN.length);
7622
+ return doc + WS_OPEN + run + WS_CLOSE;
7623
+ }
7624
+
7625
+ function ws_shift(
7626
+ sink: MapSink | undefined,
7627
+ from: number,
7628
+ stride: number,
7629
+ by: number
7630
+ ): void {
7631
+ if (sink !== undefined) ws_move(sink, from, sink.n, stride, by);
7632
+ }
7633
+
7634
+ function ws_move(
7635
+ sink: MapSink | undefined,
7636
+ from: number,
7637
+ to: number,
7638
+ stride: number,
7639
+ by: number
7640
+ ): void {
7641
+ if (sink === undefined || by === 0) return;
7642
+ // grow keeps every index, so the records sit where they were written
7643
+ const rec = sink.rec;
7644
+ for (let i = from; i < to; i += stride) rec[i] += by;
7645
+ }
7646
+
7647
+ /**
7648
+ * true when the markup holds nothing svelte finds while hydrating, a block
7649
+ * it wrongly takes is still correct inside a wrapper
7650
+ */
7651
+ function ws_static(s: string): boolean {
7652
+ const len = s.length;
7653
+ // the next brace from i, found again only once the scan passes it
7654
+ let brace = s.indexOf('{');
7655
+ let i = 0;
7656
+ for (;;) {
7657
+ const lt = s.indexOf('<', i);
7658
+ // a brace in text is an expression or a block
7659
+ if (brace !== -1 && (lt === -1 || brace < lt)) return false;
7660
+ if (lt === -1) return true;
7661
+ let ch = s.charCodeAt(lt + 1);
7662
+ if (ch === 33) {
7663
+ // a comment, svelte drops it, braces and all
7664
+ const e = s.indexOf('-->', lt + 4);
7665
+ if (e === -1) return false;
7666
+ i = e + 3;
7667
+ if (brace !== -1 && brace < i) brace = s.indexOf('{', i);
7668
+ continue;
7669
+ }
7670
+ let j = lt + 1;
7671
+ if (ch === 47) {
7672
+ // an end tag, its name was read at the start tag
7673
+ i = lt + 2;
7674
+ continue;
7675
+ }
7676
+ // a capital, a dot or a colon in the name is a component or a svelte element
7677
+ for (; j < len; j++) {
7678
+ ch = s.charCodeAt(j);
7679
+ if ((ch >= 65 && ch <= 90) || ch === 46 || ch === 58) return false;
7680
+ if (!((ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch === 45))
7681
+ break;
7682
+ }
7683
+ if (j === lt + 1) return false;
7684
+ const name = s.slice(lt + 1, j);
7685
+ if (name === 'script' || name === 'style' || name === 'slot') return false;
7686
+ // the attributes, a colon outside a value is a directive, a brace anywhere an expression
7687
+ let quote = 0;
7688
+ for (; j < len; j++) {
7689
+ ch = s.charCodeAt(j);
7690
+ if (ch === 123) return false;
7691
+ if (quote !== 0) {
7692
+ if (ch === quote) quote = 0;
7693
+ } else if (ch === 34 || ch === 39) quote = ch;
7694
+ else if (ch === 58) return false;
7695
+ else if (ch === 62) break;
7696
+ }
7697
+ i = j;
7698
+ }
7699
+ }