satorigear 0.0.14 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,11 +1,9 @@
1
1
  import { htmlBlockNames, htmlRawNames } from "micromark-util-html-tag-name";
2
2
  // src/profile/utils.ts
3
- const inlineColonBoundary = /(?<=[ \t\n\r\p{sc=Han}\p{sc=Hira}\p{sc=Kana}\p{sc=Hang}\p{P}])/uy;
4
- const nonSpaceAndLineEnding = /[^ \r\n]/;
5
3
  const maximumLabelUnits = 1998;
6
- const labelCharacters = /^.{1,999}$/su;
4
+ const malformedLabel = /^.{1000,}/su;
7
5
  function referenceIdentifier(label) {
8
- if (label.length > 999 && (label.length > 1998 || !labelCharacters.test(label.replaceAll("\r\n", "\n")))) return "";
6
+ if (label.length > 999 && (label.length > 1998 || malformedLabel.test(label.replaceAll("\r\n", "\n")))) return "";
9
7
  return normalizeIdentifier(label);
10
8
  }
11
9
  function isAsciiDigit(code) {
@@ -14,6 +12,12 @@ function isAsciiDigit(code) {
14
12
  function isAsciiLetter(code) {
15
13
  return code >= 65 && code <= 90 || code >= 97 && code <= 122;
16
14
  }
15
+ function isLowercaseLetter(code) {
16
+ return code >= 97 && code <= 122;
17
+ }
18
+ function isUppercaseLetter(code) {
19
+ return code >= 65 && code <= 90;
20
+ }
17
21
  function isAsciiPunctuation(code) {
18
22
  return code >= 33 && code <= 47 || code >= 58 && code <= 64 || code >= 91 && code <= 96 || code >= 123 && code <= 126;
19
23
  }
@@ -26,11 +30,7 @@ function isSpaceOrLineEnding(code) {
26
30
  function isSpaceTabOrLineEnding(code) {
27
31
  return code === 32 || code === 10 || code === 13 || code === 9;
28
32
  }
29
- function hasInlineColonBoundary(source, start) {
30
- if (start <= 0) return true;
31
- inlineColonBoundary.lastIndex = start;
32
- return source.charCodeAt(start - 1) !== 58 && inlineColonBoundary.test(source);
33
- }
33
+ const nonSpaceAndLineEnding = /[^ \r\n]/;
34
34
  function hasInlinePadding(value) {
35
35
  return isSpaceOrLineEnding(value.charCodeAt(0)) && isSpaceOrLineEnding(value.charCodeAt(value.length - 1)) && nonSpaceAndLineEnding.test(value);
36
36
  }
@@ -2196,9 +2196,7 @@ function decodeReference(source, start, end) {
2196
2196
  if (point < 9 || point === 11 || point > 13 && point < 32 || point >= 127 && point <= 159 || point >= 55296 && point <= 57343 || point >= 64976 && point <= 65007 || (point & 65535) >= 65534 || point > 1114111) point = 65533;
2197
2197
  return String.fromCodePoint(point);
2198
2198
  }
2199
- function decodeString(source) {
2200
- let ampersand = source.indexOf("&");
2201
- let backslash = source.indexOf("\\");
2199
+ function decodeFrom(source, ampersand, backslash) {
2202
2200
  let copied;
2203
2201
  let result = "";
2204
2202
  while (ampersand >= 0 || backslash >= 0) {
@@ -2227,8 +2225,17 @@ function decodeString(source) {
2227
2225
  }
2228
2226
  return copied ? result + source.slice(copied) : source;
2229
2227
  }
2228
+ function decodeEntities(source) {
2229
+ const ampersand = source.indexOf("&");
2230
+ return ampersand < 0 ? source : decodeFrom(source, ampersand, -1);
2231
+ }
2232
+ function decodeString(source) {
2233
+ const ampersand = source.indexOf("&");
2234
+ const backslash = source.indexOf("\\");
2235
+ return ampersand < 0 && backslash < 0 ? source : decodeFrom(source, ampersand, backslash);
2236
+ }
2230
2237
  // src/constants/block.ts
2231
- const boundaryBreak = Symbol();
2238
+ const paddedLineFeed = Symbol();
2232
2239
  // src/primitives.ts
2233
2240
  const emptyArray = [];
2234
2241
  const emptySet = new Set();
@@ -2243,22 +2250,6 @@ function shiftTail(items, oldEnd, newEnd) {
2243
2250
  items.length = length + delta;
2244
2251
  }
2245
2252
  }
2246
- // src/block/scratch.ts
2247
- const allocate = globalThis.Buffer?.allocUnsafeSlow;
2248
- var ScratchView = class {
2249
- static bytes;
2250
- static words;
2251
- static supports = allocate !== void 0;
2252
- static prepare(sourceLength) {
2253
- const wordLimit = sourceLength + 31 & -32;
2254
- if (!this.bytes || this.bytes.length < wordLimit) {
2255
- this.bytes = allocate(wordLimit);
2256
- this.words = new Uint32Array(this.bytes.buffer, this.bytes.byteOffset, wordLimit >>> 2);
2257
- }
2258
- for (let i = 1; i <= 8; i++) this.words[(wordLimit >>> 2) - i] = 538976288;
2259
- return wordLimit;
2260
- }
2261
- };
2262
2253
  // src/source-view.ts
2263
2254
  function cloneLocation(location) {
2264
2255
  return {
@@ -2278,17 +2269,14 @@ function containingSegment(mappings, offset) {
2278
2269
  return low * 2;
2279
2270
  }
2280
2271
  var SourceView = class {
2281
- #mappings = 0;
2282
- #withBoundaryBreak = false;
2272
+ mappings = 0;
2283
2273
  text = "";
2284
2274
  reset(source, tokens, tokenStart, nodeLength) {
2285
- this.#withBoundaryBreak = tokens.value(tokenStart) === boundaryBreak;
2286
2275
  const first = tokenStart + 1;
2287
- let count = nodeLength - 1;
2288
- if (count > 1 && tokens.kind(first + count - 1) === 0) count--;
2276
+ const count = nodeLength - 1;
2289
2277
  if (count === 1) {
2290
2278
  const start = tokens.start(first);
2291
- this.#mappings = start;
2279
+ this.mappings = start;
2292
2280
  this.text = source.slice(start, tokens.end(first));
2293
2281
  return;
2294
2282
  }
@@ -2301,26 +2289,23 @@ var SourceView = class {
2301
2289
  mappings[index * 2] = start - viewEnd;
2302
2290
  mappings[index * 2 + 1] = viewEnd += end - start;
2303
2291
  }
2304
- this.#mappings = mappings;
2292
+ this.mappings = mappings;
2305
2293
  this.text = parts.join("");
2306
2294
  }
2307
- tokenize(tokenize, scratch = false) {
2308
- return tokenize(this.text, this.#withBoundaryBreak, this.#withBoundaryBreak || !(scratch && ScratchView.supports) ? void 0 : this.#mappings);
2309
- }
2310
2295
  mapOffset(offset) {
2311
- const mappings = this.#mappings;
2296
+ const mappings = this.mappings;
2312
2297
  if (typeof mappings === "number") return mappings + offset;
2313
2298
  return mappings[containingSegment(mappings, offset)] + offset;
2314
2299
  }
2315
2300
  mapEnd(offset) {
2316
- const mappings = this.#mappings;
2301
+ const mappings = this.mappings;
2317
2302
  if (typeof mappings === "number") return mappings + offset;
2318
2303
  return mappings[containingSegment(mappings, Math.max(0, offset - 1))] + offset;
2319
2304
  }
2320
2305
  shift(delta) {
2321
- const mappings = this.#mappings;
2306
+ const mappings = this.mappings;
2322
2307
  if (typeof mappings === "number") {
2323
- this.#mappings = mappings + delta;
2308
+ this.mappings = mappings + delta;
2324
2309
  return;
2325
2310
  }
2326
2311
  for (let index = 0; index < mappings.length; index += 2) mappings[index] += delta;
@@ -2360,6 +2345,22 @@ function buildBlockChildren(tokenStart, context) {
2360
2345
  }
2361
2346
  return children ?? [];
2362
2347
  }
2348
+ // src/block/scratch.ts
2349
+ const allocate = globalThis.Buffer?.allocUnsafeSlow;
2350
+ var ScratchView = class {
2351
+ static bytes;
2352
+ static words;
2353
+ static supports = allocate !== void 0;
2354
+ static prepare(sourceLength) {
2355
+ const wordLimit = sourceLength + 31 & -32;
2356
+ if (!this.bytes || this.bytes.length < wordLimit) {
2357
+ this.bytes = allocate(wordLimit);
2358
+ this.words = new Uint32Array(this.bytes.buffer, this.bytes.byteOffset, wordLimit >>> 2);
2359
+ }
2360
+ for (let i = 1; i <= 8; i++) this.words[(wordLimit >>> 2) - i] = 538976288;
2361
+ return wordLimit;
2362
+ }
2363
+ };
2363
2364
  // src/block/lines.ts
2364
2365
  function getLineCapacity(sourceLength) {
2365
2366
  return Math.min(sourceLength + 15 >>> 4, 16384);
@@ -2450,14 +2451,14 @@ var Latin1 = class {
2450
2451
  }
2451
2452
  };
2452
2453
  var Index0 = class {
2453
- static scan(source, fields, sourceOffset) {
2454
+ static scan(source, fields, sourceOffset, start = sourceOffset) {
2454
2455
  let field = 0;
2455
- let start = 0;
2456
2456
  let lf = source.indexOf("\n");
2457
2457
  let cr = source.indexOf("\r");
2458
2458
  const normalized = cr < 0;
2459
2459
  const limit = source.length;
2460
- while (start < limit) {
2460
+ const sourceEnd = sourceOffset + limit;
2461
+ while (start < sourceEnd) {
2461
2462
  let end = lf;
2462
2463
  let next = end + 1;
2463
2464
  if (cr !== -1 && (end < 0 || cr < end)) {
@@ -2467,12 +2468,15 @@ var Index0 = class {
2467
2468
  if (lf !== -1 && lf < next) lf = source.indexOf("\n", next);
2468
2469
  } else if (lf < 0) end = next = limit;
2469
2470
  else lf = source.indexOf("\n", next);
2470
- if (field >= fields.length) fields = growLineFields(fields, field + 4);
2471
- fields[field | 0] = sourceOffset + start;
2471
+ if (field >= fields.length) {
2472
+ const capacity = Math.max(field / 4 + 1, getLineCapacity(limit));
2473
+ fields = growLineFields(fields, capacity * 4);
2474
+ }
2475
+ fields[field | 0] = start;
2472
2476
  fields[field | 1] = sourceOffset + end;
2473
2477
  fields[field | 2] = sourceOffset + next;
2474
2478
  field += 4;
2475
- start = next;
2479
+ start = sourceOffset + next;
2476
2480
  }
2477
2481
  return {
2478
2482
  fields,
@@ -2482,6 +2486,7 @@ var Index0 = class {
2482
2486
  }
2483
2487
  };
2484
2488
  var BlockLines = class BlockLines {
2489
+ #child;
2485
2490
  #fields;
2486
2491
  #length = 0;
2487
2492
  #locatorLine = 1;
@@ -2489,12 +2494,11 @@ var BlockLines = class BlockLines {
2489
2494
  #locatorStart = 0;
2490
2495
  #normalized = true;
2491
2496
  #sourceLength;
2492
- constructor(sourceLength = 0, capacity = 4) {
2497
+ constructor(sourceLength = 0, fields = new Int32Array(16)) {
2493
2498
  this.#sourceLength = sourceLength;
2494
- this.#fields = new Int32Array(capacity * 4);
2499
+ this.#fields = fields;
2495
2500
  }
2496
2501
  scan(source) {
2497
- this.#locatorLine = 1;
2498
2502
  this.#sourceLength = source.length;
2499
2503
  const fields = growLineFields(this.#fields, getLineCapacity(source.length) * 4);
2500
2504
  const result = ScratchView.supports ? Latin1.scan(source, fields) : Index0.scan(source, fields, 0);
@@ -2615,7 +2619,7 @@ var BlockLines = class BlockLines {
2615
2619
  fields[field | 3] = state;
2616
2620
  this.#length++;
2617
2621
  }
2618
- pushFrom(lines, index, start = lines.start(index), prefixColumns = lines.prefixColumns(index), lazy = lines.lazy(index)) {
2622
+ pushFrom(lines, index, start = lines.start(index), prefixColumns = lines.prefixColumns(index), lazy = false) {
2619
2623
  this.push(start, lines.end(index), lines.next(index), prefixColumns, lazy);
2620
2624
  }
2621
2625
  pushAfterColumns(source, lines, index, columns) {
@@ -2625,33 +2629,30 @@ var BlockLines = class BlockLines {
2625
2629
  while (offset < end && consumed < columns) {
2626
2630
  const code = source.charCodeAt(offset);
2627
2631
  if (code === 32) consumed++;
2628
- else if (code === 9) consumed += 4 - consumed % 4;
2632
+ else if (code === 9) consumed += tabWidthAt(source, offset);
2629
2633
  else break;
2630
2634
  offset++;
2631
2635
  }
2632
2636
  if (offset === end) return -2;
2633
2637
  if (consumed < columns) return consumed <= 3 ? offset : -1;
2634
2638
  if (trimLineWhitespaceStart(source, offset, end) < end) {
2635
- this.push(offset, end, lines.next(index), consumed - columns, lines.lazy(index));
2639
+ this.push(offset, end, lines.next(index), consumed - columns, false);
2636
2640
  return -3;
2637
2641
  }
2638
2642
  return -2;
2639
2643
  }
2640
- pushLazy(lines, index) {
2641
- this.pushFrom(lines, index, void 0, void 0, true);
2642
- }
2643
2644
  pop() {
2644
2645
  if (this.#length > 0) this.#length--;
2645
2646
  }
2646
- resetFrom(lines, index, start = lines.start(index), prefixColumns = lines.prefixColumns(index), lazy = lines.lazy(index)) {
2647
- const end = lines.end(index);
2648
- const next = lines.next(index);
2649
- this.#length = 0;
2650
- this.push(start, end, next, prefixColumns, lazy);
2647
+ child(index, start, prefixColumns) {
2648
+ const child = this.#child ??= new BlockLines();
2649
+ child.#length = 0;
2650
+ child.pushFrom(this, index, start, prefixColumns);
2651
+ return child;
2651
2652
  }
2652
- view(start, end = this.length) {
2653
- const result = new BlockLines(this.#sourceLength, 0);
2654
- result.#fields = this.#fields.subarray(start * 4, end * 4);
2653
+ view(start, end) {
2654
+ const result = new BlockLines(this.#sourceLength, this.#fields.subarray(start * 4, end * 4));
2655
+ result.#child = this.#child;
2655
2656
  result.#length = end - start;
2656
2657
  result.#normalized = this.#normalized;
2657
2658
  return result;
@@ -2661,14 +2662,17 @@ var BlockLines = class BlockLines {
2661
2662
  const newSuffixOffset = (suffix < this.length ? this.start(suffix) : nextSource.length - delta) + delta;
2662
2663
  const prefixEnd = Math.max(0, this.indexAtOrAfter(damageStart) - 1);
2663
2664
  const restartOffset = prefixEnd === 0 ? 0 : this.start(prefixEnd);
2664
- const changedSource = nextSource.slice(restartOffset, newSuffixOffset);
2665
- const capacity = getLineCapacity(changedSource.length);
2666
- const changed = Index0.scan(changedSource, new Int32Array(capacity * 4), restartOffset);
2665
+ const scanStart = Math.min(damageStart, this.end(prefixEnd));
2666
+ const changedSource = nextSource.slice(scanStart, newSuffixOffset);
2667
+ const firstField = prefixEnd * 4;
2668
+ const suffixField = suffix < this.#length ? suffix * 4 : void 0;
2669
+ const available = this.#fields.subarray(firstField, suffixField);
2670
+ const changed = Index0.scan(changedSource, available, scanStart, restartOffset);
2667
2671
  const nextSuffix = prefixEnd + changed.length;
2668
2672
  const length = nextSuffix + this.#length - suffix;
2669
2673
  const fields = this.#fields = growLineFields(this.#fields, length * 4);
2670
2674
  if (nextSuffix !== suffix) fields.copyWithin(nextSuffix * 4, suffix * 4, this.#length * 4);
2671
- fields.set(changed.fields.subarray(0, changed.length * 4), prefixEnd * 4);
2675
+ if (changed.fields !== available) fields.set(changed.fields.subarray(0, changed.length * 4), firstField);
2672
2676
  this.#length = length;
2673
2677
  this.#sourceLength = nextSource.length;
2674
2678
  this.#normalized &&= changed.normalized;
@@ -2684,14 +2688,40 @@ var BlockLines = class BlockLines {
2684
2688
  }
2685
2689
  }
2686
2690
  };
2687
- function lineIndentOffset(source, lines, index) {
2691
+ function dedentLine(source, lines, index, indent, end) {
2692
+ const lineEnd = lines.end(index);
2688
2693
  let offset = lines.start(index);
2689
- const limit = offset + 3 - lines.prefixColumns(index);
2694
+ let columns = lines.prefixColumns(index);
2695
+ while (offset < lineEnd && columns < indent) {
2696
+ const code = source.charCodeAt(offset);
2697
+ if (code === 32) columns++;
2698
+ else if (code === 9) columns += tabWidthAt(source, offset);
2699
+ else break;
2700
+ offset++;
2701
+ }
2702
+ return columns > indent ? " ".repeat(columns - indent) + source.slice(offset, end) : source.slice(offset, end);
2703
+ }
2704
+ function lineIndentOffset(source, lines, index) {
2690
2705
  const end = lines.end(index);
2691
- while (offset < end && offset < limit && source.charCodeAt(offset) === 32) offset++;
2692
- if (offset > limit || offset === end) return -1;
2693
- const code = source.charCodeAt(offset);
2694
- return code === 32 || code === 9 ? -1 : offset;
2706
+ let offset = lines.start(index);
2707
+ let columns = lines.prefixColumns(index);
2708
+ while (offset < end && columns <= 3) {
2709
+ const code = source.charCodeAt(offset);
2710
+ if (code === 32) {
2711
+ columns++;
2712
+ offset++;
2713
+ } else if (code === 9) {
2714
+ columns += tabWidthAt(source, offset);
2715
+ offset++;
2716
+ } else return offset;
2717
+ }
2718
+ return -1;
2719
+ }
2720
+ function lineIndentColumns(source, lines, index, contentOffset) {
2721
+ if (lines.physicallyContiguous()) return contentOffset - lines.start(index);
2722
+ let columns = lines.prefixColumns(index);
2723
+ for (let offset = lines.start(index); offset < contentOffset; offset++) columns += source.charCodeAt(offset) === 32 ? 1 : tabWidthAt(source, offset);
2724
+ return columns;
2695
2725
  }
2696
2726
  function isBlankLine(source, lines, index) {
2697
2727
  const end = lines.end(index);
@@ -2713,19 +2743,14 @@ function trimLineWhitespaceEnd(source, start, end) {
2713
2743
  }
2714
2744
  return end;
2715
2745
  }
2716
- function physicalColumnAt(source, offset) {
2746
+ function tabWidthAt(source, offset) {
2717
2747
  let start = offset;
2718
2748
  while (start > 0) {
2719
2749
  const code = source.charCodeAt(start - 1);
2720
- if (code === 10 || code === 13) break;
2750
+ if (code === 9 || code === 10 || code === 13) break;
2721
2751
  start--;
2722
2752
  }
2723
- let column = 0;
2724
- while (start < offset) {
2725
- column += source.charCodeAt(start) === 9 ? 4 - column % 4 : 1;
2726
- start++;
2727
- }
2728
- return column;
2753
+ return 4 - (offset - start & 3);
2729
2754
  }
2730
2755
  // src/block/tokens.ts
2731
2756
  function copyMetadata(target, source, start, end, targetStart) {
@@ -2737,14 +2762,12 @@ function copyMetadata(target, source, start, end, targetStart) {
2737
2762
  }
2738
2763
  var BlockTokenStream = class {
2739
2764
  #definitionCounts;
2740
- #definitionMembershipChanges;
2741
2765
  #fieldLength = 0;
2742
2766
  #fields = new Int32Array();
2743
2767
  #metadata;
2744
2768
  #opens = [];
2745
2769
  reset() {
2746
2770
  this.#definitionCounts = void 0;
2747
- this.#definitionMembershipChanges?.clear();
2748
2771
  const metadata = this.#metadata;
2749
2772
  if (metadata) {
2750
2773
  const used = Math.min(metadata.length, this.length);
@@ -2758,12 +2781,6 @@ var BlockTokenStream = class {
2758
2781
  get length() {
2759
2782
  return this.#fieldLength / 4;
2760
2783
  }
2761
- hasDefinition(key) {
2762
- return this.#definitionCounts?.has(key) === true;
2763
- }
2764
- hasDefinitions() {
2765
- return !!this.#definitionCounts?.size;
2766
- }
2767
2784
  push(kind, start, end, meta) {
2768
2785
  const field = this.#fieldLength;
2769
2786
  this.#ensureCapacity(field + 4);
@@ -2811,9 +2828,6 @@ var BlockTokenStream = class {
2811
2828
  this.#fields[open | 3] = (field - open) / 4 + 1;
2812
2829
  }
2813
2830
  }
2814
- nodeLength(index) {
2815
- return this.#fields[index * 4 | 3];
2816
- }
2817
2831
  replace(start, end, replacement, damageStart, damageEnd, sourceDelta) {
2818
2832
  const previousLength = this.length;
2819
2833
  const replacedLength = end - start;
@@ -2826,34 +2840,48 @@ var BlockTokenStream = class {
2826
2840
  const oldStart = start + stablePrefixLength;
2827
2841
  const oldEnd = end - stableSuffixLength;
2828
2842
  const newEnd = start + replacementLength - stableSuffixLength;
2829
- this.#definitionMembershipChanges?.clear();
2830
- for (let index = oldStart; index < oldEnd; index++) this.#updateDefinitionCount(this.#metadata?.[index]?.definitionKey, -1, true);
2831
- for (let index = stablePrefixLength; index < replacementLength - stableSuffixLength; index++) this.#updateDefinitionCount(replacement.#metadata?.[index]?.definitionKey, 1, true);
2843
+ let membershipChanges;
2844
+ for (let index = oldStart; index < oldEnd; index++) {
2845
+ const key = this.#metadata?.[index]?.definitionKey;
2846
+ if (key !== void 0 && this.#updateDefinitionCount(key, -1)) {
2847
+ membershipChanges ??= new Set();
2848
+ membershipChanges.add(key);
2849
+ }
2850
+ }
2851
+ for (let index = stablePrefixLength; index < replacementLength - stableSuffixLength; index++) {
2852
+ const key = replacement.#metadata?.[index]?.definitionKey;
2853
+ if (key !== void 0 && this.#updateDefinitionCount(key, 1) && !membershipChanges?.delete(key)) {
2854
+ membershipChanges ??= new Set();
2855
+ membershipChanges.add(key);
2856
+ }
2857
+ }
2832
2858
  const change = {
2833
- definitionMembershipChanges: this.#definitionMembershipChanges ?? emptySet,
2859
+ definitionMembershipChanges: membershipChanges ?? emptySet,
2834
2860
  newEnd,
2835
2861
  oldEnd,
2836
2862
  oldStart
2837
2863
  };
2838
- const fieldStart = start * 4;
2864
+ let preservedPrefix = 0;
2865
+ while (preservedPrefix < stablePrefixLength && this.nodeLength(start + preservedPrefix) === replacement.nodeLength(preservedPrefix) && this.end(start + preservedPrefix) < damageStart) preservedPrefix++;
2866
+ const writeStart = start + preservedPrefix;
2839
2867
  const indexDelta = replacementLength - replacedLength;
2840
2868
  const newSuffixStart = start + replacementLength;
2841
2869
  const fieldLength = this.#fieldLength + indexDelta * 4;
2842
2870
  this.#ensureCapacity(fieldLength);
2843
2871
  if (indexDelta !== 0 && end < previousLength) this.#fields.copyWithin(newSuffixStart * 4, end * 4, this.#fieldLength);
2844
- this.#fields.set(replacement.#fields.subarray(0, replacement.#fieldLength), fieldStart);
2872
+ this.#fields.set(replacement.#fields.subarray(preservedPrefix * 4, replacement.#fieldLength), writeStart * 4);
2845
2873
  this.#fieldLength = fieldLength;
2846
2874
  const metadata = this.#metadata;
2847
2875
  if (metadata) {
2848
- if (end === previousLength || end >= metadata.length) metadata.length = Math.min(start, metadata.length);
2849
- else if (indexDelta === 0) metadata.fill(void 0, start, end);
2876
+ if (end === previousLength || end >= metadata.length) metadata.length = Math.min(writeStart, metadata.length);
2877
+ else if (indexDelta === 0) metadata.fill(void 0, writeStart, end);
2850
2878
  else {
2851
2879
  const shifted = this.#metadata = [];
2852
- copyMetadata(shifted, metadata, 0, start, 0);
2880
+ copyMetadata(shifted, metadata, 0, writeStart, 0);
2853
2881
  copyMetadata(shifted, metadata, end, previousLength, newSuffixStart);
2854
2882
  }
2855
2883
  }
2856
- if (replacement.#metadata) copyMetadata(this.#metadata ??= [], replacement.#metadata, 0, replacementLength, start);
2884
+ if (replacement.#metadata) copyMetadata(this.#metadata ??= [], replacement.#metadata, preservedPrefix, replacementLength, writeStart);
2857
2885
  if (sourceDelta !== 0 && newSuffixStart < this.length) {
2858
2886
  const suffixFieldStart = newSuffixStart * 4;
2859
2887
  for (let field = suffixFieldStart; field < this.#fieldLength; field += 4) {
@@ -2863,6 +2891,14 @@ var BlockTokenStream = class {
2863
2891
  }
2864
2892
  return change;
2865
2893
  }
2894
+ #ensureCapacity(length) {
2895
+ if (length <= this.#fields.length) return;
2896
+ let capacity = Math.max(64, this.#fields.length * 2);
2897
+ while (capacity < length) capacity *= 2;
2898
+ const fields = new Int32Array(capacity);
2899
+ fields.set(this.#fields.subarray(0, this.#fieldLength));
2900
+ this.#fields = fields;
2901
+ }
2866
2902
  #matchesAfterShift(index, next, nextIndex, delta, damageStart, damageEnd) {
2867
2903
  const start = this.start(index);
2868
2904
  const end = this.end(index);
@@ -2871,12 +2907,7 @@ var BlockTokenStream = class {
2871
2907
  if (this.kind(index) !== next.kind(nextIndex) || start + delta !== nextStart || end + delta !== nextEnd) return false;
2872
2908
  const metadata = this.#metadata?.[index];
2873
2909
  const nextMetadata = next.#metadata?.[nextIndex];
2874
- if (metadata !== nextMetadata) {
2875
- if (metadata?.definitionKey !== nextMetadata?.definitionKey) return false;
2876
- const value = metadata?.value;
2877
- const nextValue = nextMetadata?.value;
2878
- if (value !== nextValue && (value === null || typeof value !== "object" || nextValue === null || typeof nextValue !== "object")) return false;
2879
- }
2910
+ if (metadata !== nextMetadata && (metadata?.definitionKey !== nextMetadata?.definitionKey || metadata?.value !== nextMetadata?.value)) return false;
2880
2911
  return start === end || end <= damageStart || start >= damageEnd;
2881
2912
  }
2882
2913
  kind(index) {
@@ -2894,6 +2925,9 @@ var BlockTokenStream = class {
2894
2925
  end(index) {
2895
2926
  return this.#fields[index * 4 | 2];
2896
2927
  }
2928
+ nodeLength(index) {
2929
+ return this.#fields[index * 4 | 3];
2930
+ }
2897
2931
  node(index) {
2898
2932
  return this.#metadata?.[index]?.node;
2899
2933
  }
@@ -2906,49 +2940,42 @@ var BlockTokenStream = class {
2906
2940
  this.#metadata[index].value = value;
2907
2941
  }
2908
2942
  addDefinition(key) {
2909
- this.#updateDefinitionCount(key, 1, false);
2943
+ this.#updateDefinitionCount(key, 1);
2944
+ }
2945
+ hasDefinition(key) {
2946
+ return this.#definitionCounts?.has(key) === true;
2910
2947
  }
2911
- #updateDefinitionCount(key, delta, trackMembership) {
2912
- if (key === void 0) return;
2948
+ hasDefinitions() {
2949
+ return !!this.#definitionCounts?.size;
2950
+ }
2951
+ #updateDefinitionCount(key, delta) {
2913
2952
  const counts = this.#definitionCounts ??= new Map();
2914
2953
  const previousCount = counts.get(key) ?? 0;
2915
2954
  const nextCount = previousCount + delta;
2916
2955
  if (nextCount === 0) counts.delete(key);
2917
2956
  else counts.set(key, nextCount);
2918
- if (trackMembership && (previousCount === 0 || nextCount === 0)) {
2919
- const changes = this.#definitionMembershipChanges ??= new Set();
2920
- if (!changes.delete(key)) changes.add(key);
2921
- }
2922
- }
2923
- #ensureCapacity(length) {
2924
- if (length <= this.#fields.length) return;
2925
- let capacity = Math.max(64, this.#fields.length * 2);
2926
- while (capacity < length) capacity *= 2;
2927
- const fields = new Int32Array(capacity);
2928
- fields.set(this.#fields.subarray(0, this.#fieldLength));
2929
- this.#fields = fields;
2957
+ return previousCount === 0 || nextCount === 0;
2930
2958
  }
2931
2959
  };
2932
2960
  // src/block/scanner.ts
2933
2961
  var BlockScanContext = class {
2934
- #lineViews = [];
2935
- #scanDepth = 0;
2936
2962
  #lookaheadEnd = 0;
2937
2963
  #profile;
2938
- #unwrappedLine = new BlockLines();
2939
2964
  startsInterruptingBlock;
2940
2965
  constructor(profile) {
2941
2966
  this.#profile = profile;
2942
2967
  this.startsInterruptingBlock = profile.interrupt;
2943
2968
  }
2944
2969
  endsWithParagraphLeaf(source, lines, index) {
2970
+ if (lines.lazy(index)) return true;
2945
2971
  let contentLines = lines;
2946
2972
  let contentIndex = index;
2947
2973
  while (true) {
2948
2974
  const contentOffset = lineIndentOffset(source, contentLines, contentIndex);
2949
2975
  if (contentOffset < 0) return false;
2950
- if ((0, this.#profile.unwraps[source.charCodeAt(contentOffset)])?.(source, contentLines, contentIndex, contentOffset, this.#unwrappedLine)) {
2951
- contentLines = this.#unwrappedLine;
2976
+ const child = (0, this.#profile.unwraps[source.charCodeAt(contentOffset)])?.(source, contentLines, contentIndex, contentOffset);
2977
+ if (child) {
2978
+ contentLines = child;
2952
2979
  contentIndex = 0;
2953
2980
  continue;
2954
2981
  }
@@ -2958,81 +2985,70 @@ var BlockScanContext = class {
2958
2985
  retainLookahead(end) {
2959
2986
  this.#lookaheadEnd = Math.max(this.#lookaheadEnd, end);
2960
2987
  }
2961
- createLineView(lines, index, start, prefixColumns) {
2962
- const depth = this.#scanDepth - 1;
2963
- const view = this.#lineViews[depth] ??= new BlockLines();
2964
- view.resetFrom(lines, index, start, prefixColumns);
2965
- return view;
2966
- }
2967
- scanBlockLines(source, lines, tokens, visit, stopsBefore, sequence) {
2988
+ scanBlockLines(source, lines, tokens, index = 0, visit, stopsBefore, sequence) {
2968
2989
  const { starts, fallback } = this.#profile;
2969
2990
  const physicallyContiguous = lines.physicallyContiguous();
2970
- this.#scanDepth++;
2971
- try {
2972
- for (let index = 0; index < lines.length;) {
2973
- let start = lines.start(index);
2974
- let lineEnd = lines.end(index);
2975
- while (start === lineEnd) {
2976
- if (++index === lines.length) return;
2977
- start = lines.start(index);
2978
- lineEnd = lines.end(index);
2979
- }
2980
- let contentOffset = start;
2981
- let columns = physicallyContiguous ? 0 : lines.prefixColumns(index);
2982
- let code = -1;
2983
- while (contentOffset < lineEnd) {
2984
- code = source.charCodeAt(contentOffset);
2985
- if (code !== 32 && code !== 9) break;
2986
- columns += code === 32 ? 1 : 4 - columns % 4;
2987
- contentOffset++;
2988
- }
2989
- if (contentOffset === lineEnd) {
2990
- index++;
2991
- continue;
2992
- }
2993
- if (stopsBefore?.(start)) return;
2994
- if (columns > 3) {
2995
- contentOffset = -1;
2996
- code = -1;
2997
- }
2998
- const lineStart = index;
2999
- const tokenStart = tokens.length;
3000
- const result = starts[code + 1]?.(source, lines, tokens, index, contentOffset, this) ?? fallback?.(source, lines, tokens, index, contentOffset, this);
3001
- if (result === void 0) throw new Error("No block fallback");
3002
- if (typeof result === "number") {
3003
- index = result;
3004
- sequence?.push(lineStart);
3005
- } else {
3006
- index = result.nextLine;
3007
- if (sequence) {
3008
- const last = index - 1;
3009
- const afterEnding = result.end > lines.end(last) && result.end === lines.next(last);
3010
- result.node.position = {
3011
- start: {
3012
- line: lineStart + 1,
3013
- column: result.start - start + 1,
3014
- offset: result.start
3015
- },
3016
- end: {
3017
- line: last + (afterEnding ? 2 : 1),
3018
- column: afterEnding ? 1 : result.end - lines.start(last) + 1,
3019
- offset: result.end
3020
- }
3021
- };
3022
- if (result.definitionKey !== void 0) tokens.addDefinition(result.definitionKey);
3023
- sequence.push(result.node);
3024
- } else tokens.push(256, result.start, result.end, result);
3025
- }
3026
- visit?.(lineStart, index, tokenStart);
2991
+ while (index < lines.length) {
2992
+ let start = lines.start(index);
2993
+ let lineEnd = lines.end(index);
2994
+ while (start === lineEnd) {
2995
+ if (++index === lines.length) return;
2996
+ start = lines.start(index);
2997
+ lineEnd = lines.end(index);
3027
2998
  }
3028
- } finally {
3029
- this.#scanDepth--;
2999
+ let contentOffset = start;
3000
+ let columns = physicallyContiguous ? 0 : lines.prefixColumns(index);
3001
+ let code = -1;
3002
+ while (contentOffset < lineEnd) {
3003
+ code = source.charCodeAt(contentOffset);
3004
+ if (code !== 32 && code !== 9) break;
3005
+ columns += code === 32 ? 1 : tabWidthAt(source, contentOffset);
3006
+ contentOffset++;
3007
+ }
3008
+ if (contentOffset === lineEnd) {
3009
+ index++;
3010
+ continue;
3011
+ }
3012
+ if (stopsBefore?.(start)) return;
3013
+ if (columns > 3) {
3014
+ contentOffset = -1;
3015
+ code = -1;
3016
+ }
3017
+ const lineStart = index;
3018
+ const tokenStart = tokens.length;
3019
+ const result = starts[code + 1]?.(source, lines, tokens, index, contentOffset, this) ?? fallback?.(source, lines, tokens, index, contentOffset, this);
3020
+ if (result === void 0) throw new Error("No block fallback");
3021
+ if (typeof result === "number") {
3022
+ index = result;
3023
+ sequence?.push(lineStart);
3024
+ } else {
3025
+ index = result.nextLine;
3026
+ if (sequence) {
3027
+ const last = index - 1;
3028
+ const afterEnding = result.end > lines.end(last) && result.end === lines.next(last);
3029
+ result.node.position = {
3030
+ start: {
3031
+ line: lineStart + 1,
3032
+ column: result.start - start + 1,
3033
+ offset: result.start
3034
+ },
3035
+ end: {
3036
+ line: last + (afterEnding ? 2 : 1),
3037
+ column: afterEnding ? 1 : result.end - lines.start(last) + 1,
3038
+ offset: result.end
3039
+ }
3040
+ };
3041
+ if (result.definitionKey !== void 0) tokens.addDefinition(result.definitionKey);
3042
+ sequence.push(result.node);
3043
+ } else tokens.push(256, result.start, result.end, result);
3044
+ }
3045
+ visit?.(lineStart, index, tokenStart);
3030
3046
  }
3031
3047
  }
3032
- scanRecords(source, lines, tokens, tokenOffset = 0, stopsBefore) {
3048
+ scanRecords(source, lines, tokens, firstLine = 0, tokenOffset = 0, stopsBefore) {
3033
3049
  const records = [];
3034
3050
  this.#lookaheadEnd = 0;
3035
- this.scanBlockLines(source, lines, tokens, (lineStart, lineEnd, tokenStart) => {
3051
+ this.scanBlockLines(source, lines, tokens, firstLine, (lineStart, lineEnd, tokenStart) => {
3036
3052
  const end = lines.next(lineEnd - 1);
3037
3053
  records.push({
3038
3054
  start: lines.start(lineStart),
@@ -3040,12 +3056,12 @@ var BlockScanContext = class {
3040
3056
  dependencyEnd: Math.max(end, this.#lookaheadEnd),
3041
3057
  tokenStart: tokenOffset + tokenStart
3042
3058
  });
3043
- this.#lookaheadEnd = 0;
3044
3059
  }, stopsBefore);
3045
3060
  return records;
3046
3061
  }
3047
3062
  };
3048
- var BlockScanner = class {
3063
+ var BlockScanner = class BlockScanner {
3064
+ static #replacement;
3049
3065
  #context;
3050
3066
  #lines = new BlockLines();
3051
3067
  #records = [];
@@ -3056,41 +3072,42 @@ var BlockScanner = class {
3056
3072
  this.builds = profile.builds;
3057
3073
  }
3058
3074
  parse(source, profile) {
3059
- this.#reset(source);
3060
- const children = [];
3061
- this.#context.scanBlockLines(source, this.#lines, this.tokens, void 0, void 0, children);
3062
3075
  const lines = this.#lines;
3063
- const locator = lines.locator();
3064
- const context = createBuildContext(source, this, profile, locator);
3065
- let token = 0;
3066
- for (let index = 0; index < children.length; index++) {
3067
- const entry = children[index];
3068
- if (typeof entry === "number") {
3069
- lines.advanceToLine(entry);
3070
- children[index] = buildBlockNode(token, context);
3071
- token += this.tokens.nodeLength(token);
3076
+ lines.scan(source);
3077
+ try {
3078
+ const children = [];
3079
+ this.#context.scanBlockLines(source, lines, this.tokens, 0, void 0, void 0, children);
3080
+ const locator = lines.locator();
3081
+ const context = createBuildContext(source, this, profile, locator);
3082
+ let token = 0;
3083
+ for (let index = 0; index < children.length; index++) {
3084
+ const entry = children[index];
3085
+ if (typeof entry === "number") {
3086
+ lines.advanceToLine(entry);
3087
+ children[index] = buildBlockNode(token, context);
3088
+ token += this.tokens.nodeLength(token);
3089
+ }
3072
3090
  }
3091
+ return {
3092
+ type: "root",
3093
+ children,
3094
+ position: {
3095
+ start: {
3096
+ line: 1,
3097
+ column: 1,
3098
+ offset: 0
3099
+ },
3100
+ end: locator.locationEnd()
3101
+ }
3102
+ };
3103
+ } finally {
3104
+ this.tokens.reset();
3073
3105
  }
3074
- return {
3075
- type: "root",
3076
- children,
3077
- position: {
3078
- start: {
3079
- line: 1,
3080
- column: 1,
3081
- offset: 0
3082
- },
3083
- end: locator.locationEnd()
3084
- }
3085
- };
3086
3106
  }
3087
3107
  scan(source) {
3088
- this.#reset(source);
3089
- this.#records = this.#context.scanRecords(source, this.#lines, this.tokens);
3090
- }
3091
- #reset(source) {
3092
3108
  this.#lines.scan(source);
3093
3109
  this.tokens.reset();
3110
+ this.#records = this.#context.scanRecords(source, this.#lines, this.tokens);
3094
3111
  }
3095
3112
  get records() {
3096
3113
  return this.#records;
@@ -3104,8 +3121,7 @@ var BlockScanner = class {
3104
3121
  const lines = this.#lines;
3105
3122
  const oldChangedEnd = changedSpan.end - offsetDelta;
3106
3123
  const records = this.#records;
3107
- let affectedIndex = records.findIndex((record) => record.dependencyEnd >= changedSpan.start);
3108
- if (affectedIndex < 0) affectedIndex = Math.max(0, records.length - 1);
3124
+ const affectedIndex = findAffectedRecord(records, changedSpan.start);
3109
3125
  const affectedRecord = records[affectedIndex];
3110
3126
  const restartIndex = affectedIndex === 0 && affectedRecord?.start > changedSpan.start ? -1 : affectedRecord && affectedRecord.end < changedSpan.start ? affectedIndex : Math.max(0, affectedIndex - 1);
3111
3127
  const restartRecord = records[restartIndex];
@@ -3114,11 +3130,11 @@ var BlockScanner = class {
3114
3130
  const scanLineStart = lines.indexAtOrAfter(restartOffset);
3115
3131
  const stableBlockCount = Math.max(0, restartIndex);
3116
3132
  const oldTokenStart = restartRecord?.tokenStart ?? 0;
3117
- const replacement = new BlockTokenStream();
3118
- const scanLines = scanLineStart === 0 ? lines : lines.view(scanLineStart);
3133
+ const replacement = BlockScanner.#replacement ?? new BlockTokenStream();
3134
+ BlockScanner.#replacement = void 0;
3119
3135
  let oldRecordEnd = records.length;
3120
3136
  let candidate = affectedIndex;
3121
- const rescannedRecords = context.scanRecords(nextSource, scanLines, replacement, oldTokenStart, (start) => {
3137
+ const rescannedRecords = context.scanRecords(nextSource, lines, replacement, scanLineStart, oldTokenStart, (start) => {
3122
3138
  if (start < changedSpan.end) return false;
3123
3139
  const oldStart = start - offsetDelta;
3124
3140
  if (offsetDelta && (start === 0 || oldStart === 0) && this.builds[257]) return false;
@@ -3130,6 +3146,8 @@ var BlockScanner = class {
3130
3146
  const oldTokenLength = this.tokens.length;
3131
3147
  const oldTokenEnd = records[oldRecordEnd]?.tokenStart ?? oldTokenLength;
3132
3148
  const tokenChange = this.tokens.replace(oldTokenStart, oldTokenEnd, replacement, changedSpan.start, oldChangedEnd, offsetDelta);
3149
+ replacement.reset();
3150
+ BlockScanner.#replacement = replacement;
3133
3151
  const tokenDelta = tokenChange.newEnd - tokenChange.oldEnd;
3134
3152
  let oldRecordStart = stableBlockCount;
3135
3153
  while (oldRecordStart < records.length && (records[oldRecordStart + 1]?.tokenStart ?? oldTokenLength) <= tokenChange.oldStart) oldRecordStart++;
@@ -3143,6 +3161,8 @@ var BlockScanner = class {
3143
3161
  record.dependencyEnd += offsetDelta;
3144
3162
  record.tokenStart += tokenDelta;
3145
3163
  }
3164
+ joinRecordDependencies(records, stableBlockCount, newRecordEnd);
3165
+ joinRecordDependencies(records, newRecordEnd, records.length);
3146
3166
  return {
3147
3167
  newRecordEnd,
3148
3168
  offsetDelta,
@@ -3153,8 +3173,33 @@ var BlockScanner = class {
3153
3173
  };
3154
3174
  }
3155
3175
  };
3176
+ function findAffectedRecord(records, offset) {
3177
+ let low = 0;
3178
+ let high = records.length - 1;
3179
+ while (low < high) {
3180
+ const middle = low + high >>> 1;
3181
+ if (records[middle].dependencyEnd < offset) low = middle + 1;
3182
+ else high = middle;
3183
+ }
3184
+ return low;
3185
+ }
3186
+ function joinRecordDependencies(records, start, end) {
3187
+ if (start > 0) {
3188
+ const dependencyEnd = records[start - 1].dependencyEnd;
3189
+ for (let index = start; index < end && records[index].dependencyEnd < dependencyEnd; index++) records[index].dependencyEnd = dependencyEnd;
3190
+ }
3191
+ }
3156
3192
  // src/inline/region.ts
3157
3193
  const definitionAny = Symbol();
3194
+ var InlineRegion = class extends SourceView {
3195
+ tokenStart = 0;
3196
+ tokens = emptyArray;
3197
+ rawTokens = emptyArray;
3198
+ dependencies;
3199
+ depends(changes) {
3200
+ return this.dependencies === definitionAny || (typeof this.dependencies === "string" ? changes.has(this.dependencies) : this.dependencies !== void 0 && isDefinitionShared(this.dependencies, changes));
3201
+ }
3202
+ };
3158
3203
  function isDefinitionShared(left, right) {
3159
3204
  const smaller = left.size < right.size ? left : right;
3160
3205
  const larger = smaller === left ? right : left;
@@ -3163,51 +3208,44 @@ function isDefinitionShared(left, right) {
3163
3208
  }
3164
3209
  var InlineRegionResolver = class {
3165
3210
  #profile;
3166
- #definitions;
3211
+ #blockTokens;
3167
3212
  #dependencies;
3168
- constructor(profile, definitions) {
3213
+ constructor(profile, blockTokens) {
3169
3214
  this.#profile = profile;
3170
- this.#definitions = definitions;
3215
+ this.#blockTokens = blockTokens;
3171
3216
  }
3172
3217
  hasDefinition(key) {
3173
- if (this.#dependencies !== definitionAny) {
3174
- this.#dependencies ??= new Set();
3175
- this.#dependencies.add(key);
3176
- }
3177
- return this.#definitions.hasDefinition(key);
3218
+ const dependencies = this.#dependencies;
3219
+ if (dependencies === void 0) this.#dependencies = key;
3220
+ else if (typeof dependencies === "string") {
3221
+ if (dependencies !== key) this.#dependencies = new Set([dependencies, key]);
3222
+ } else if (dependencies !== definitionAny) dependencies.add(key);
3223
+ return this.#blockTokens.hasDefinition(key);
3178
3224
  }
3179
3225
  hasDefinitions() {
3180
- const hasAny = this.#definitions.hasDefinitions();
3226
+ const hasAny = this.#blockTokens.hasDefinitions();
3181
3227
  if (!hasAny) this.#dependencies = definitionAny;
3182
3228
  return hasAny;
3183
3229
  }
3184
- update(previous, tokenStart, view, changes, initial) {
3185
- if (previous && previous.view.text === view.text) {
3186
- previous.tokenStart = tokenStart;
3187
- previous.view = view;
3188
- this.updateDefinitions(previous, changes);
3189
- return previous;
3190
- }
3191
- const rawTokens = view.tokenize(this.#profile.tokenize, initial);
3192
- return {
3193
- tokenStart,
3194
- view,
3195
- tokens: this.#resolve(view, rawTokens),
3196
- rawTokens,
3197
- dependencies: this.#dependencies
3198
- };
3230
+ updateTokens(region, initial) {
3231
+ const hasPaddedLineFeed = this.#blockTokens.value(region.tokenStart) === paddedLineFeed;
3232
+ const rawTokens = this.#profile.tokenize(region.text, hasPaddedLineFeed, hasPaddedLineFeed || !initial ? void 0 : region.mappings);
3233
+ this.#resolve(region, rawTokens);
3199
3234
  }
3200
3235
  updateDefinitions(region, changes) {
3201
3236
  const dependencies = region.dependencies;
3202
- if (!dependencies || changes.size === 0) return false;
3203
- if (dependencies !== definitionAny && !isDefinitionShared(dependencies, changes)) return false;
3204
- region.tokens = this.#resolve(region.view, region.rawTokens);
3205
- region.dependencies = this.#dependencies;
3206
- return true;
3237
+ if (changes.size === 0 || !region.depends(changes)) return false;
3238
+ this.#resolve(region, region.rawTokens);
3239
+ return dependencies !== definitionAny || region.depends(changes);
3207
3240
  }
3208
- #resolve(view, rawTokens) {
3209
- this.#dependencies = void 0;
3210
- return this.#profile.resolve(view.text, rawTokens, this);
3241
+ #resolve(region, rawTokens) {
3242
+ try {
3243
+ region.tokens = rawTokens.length ? this.#profile.resolve(region.text, rawTokens, this) : rawTokens;
3244
+ region.dependencies = this.#dependencies;
3245
+ region.rawTokens = this.#dependencies ? rawTokens : emptyArray;
3246
+ } finally {
3247
+ this.#dependencies = void 0;
3248
+ }
3211
3249
  }
3212
3250
  };
3213
3251
  var InlineRegionState = class {
@@ -3225,7 +3263,6 @@ var InlineRegionState = class {
3225
3263
  const structure = this.#structure;
3226
3264
  const tokens = structure.tokens;
3227
3265
  const regionsByBlock = this.#regionsByBlock;
3228
- const stableBlockCount = change?.stableBlockCount ?? 0;
3229
3266
  const oldRecordStart = change?.oldRecordStart ?? 0;
3230
3267
  const oldRecordEnd = change?.oldRecordEnd ?? 0;
3231
3268
  const newRecordEnd = change?.newRecordEnd ?? structure.records.length;
@@ -3238,8 +3275,7 @@ var InlineRegionState = class {
3238
3275
  for (let index = oldRecordStart; index < oldRecordEnd; index++) for (const region of regionsByBlock[index]) displacedRegions.push(region);
3239
3276
  let displacedIndex = 0;
3240
3277
  shiftTail(regionsByBlock, oldRecordEnd, newRecordEnd);
3241
- for (let blockIndex = stableBlockCount; blockIndex < newRecordEnd; blockIndex++) {
3242
- const previousRegions = blockIndex < oldRecordStart ? regionsByBlock[blockIndex] : void 0;
3278
+ for (let blockIndex = oldRecordStart; blockIndex < newRecordEnd; blockIndex++) {
3243
3279
  const tokenStart = structure.records[blockIndex].tokenStart;
3244
3280
  const blockLength = tokens.nodeLength(tokenStart);
3245
3281
  if (blockLength === 1) {
@@ -3250,16 +3286,18 @@ var InlineRegionState = class {
3250
3286
  for (let token = tokenStart; token < tokenStart + blockLength; token++) {
3251
3287
  const nodeLength = tokens.nodeLength(token);
3252
3288
  if (nodeLength <= 1 || tokens.kind(token + 1) !== 1) continue;
3253
- const regionIndex = regions?.length ?? 0;
3254
- let candidate = previousRegions?.[regionIndex];
3255
- if (!candidate && tokenChange && blockIndex >= oldRecordStart) {
3289
+ let candidate;
3290
+ if (tokenChange) {
3256
3291
  const previousTokenStart = token < tokenChange.oldStart ? token : token >= tokenChange.newEnd ? token - suffixTokenDelta : -1;
3257
3292
  while (displacedIndex < displacedRegions.length && displacedRegions[displacedIndex].tokenStart < previousTokenStart) displacedIndex++;
3258
3293
  if (displacedRegions[displacedIndex]?.tokenStart === previousTokenStart) candidate = displacedRegions[displacedIndex++];
3259
3294
  }
3260
- const view = new SourceView();
3261
- view.reset(source, tokens, token, nodeLength);
3262
- const region = this.#resolver.update(candidate, token, view, definitionMembershipChanges, change === void 0);
3295
+ const region = candidate ?? new InlineRegion();
3296
+ const previousText = candidate?.text;
3297
+ region.reset(source, tokens, token, nodeLength);
3298
+ region.tokenStart = token;
3299
+ if (previousText === region.text) this.#resolver.updateDefinitions(region, definitionMembershipChanges);
3300
+ else this.#resolver.updateTokens(region, change === void 0);
3263
3301
  regions?.push(region) ?? (regions = [region]);
3264
3302
  token += nodeLength - 1;
3265
3303
  }
@@ -3267,7 +3305,7 @@ var InlineRegionState = class {
3267
3305
  }
3268
3306
  if (offsetDelta !== 0 || suffixTokenDelta !== 0) for (let index = newRecordEnd; index < regionsByBlock.length; index++) for (const region of regionsByBlock[index]) {
3269
3307
  region.tokenStart += suffixTokenDelta;
3270
- region.view.shift(offsetDelta);
3308
+ region.shift(offsetDelta);
3271
3309
  }
3272
3310
  if (definitionMembershipChanges.size > 0) {
3273
3311
  const refreshDefinitions = (start, end) => {
@@ -3275,10 +3313,13 @@ var InlineRegionState = class {
3275
3313
  const regions = regionsByBlock[index];
3276
3314
  let changed = false;
3277
3315
  for (const region of regions) if (this.#resolver.updateDefinitions(region, definitionMembershipChanges)) changed = true;
3278
- if (changed) (invalidatedBlocks ??= []).push(index);
3316
+ if (changed) {
3317
+ invalidatedBlocks ??= [];
3318
+ invalidatedBlocks.push(index);
3319
+ }
3279
3320
  }
3280
3321
  };
3281
- refreshDefinitions(0, stableBlockCount);
3322
+ refreshDefinitions(0, oldRecordStart);
3282
3323
  refreshDefinitions(newRecordEnd, regionsByBlock.length);
3283
3324
  }
3284
3325
  return invalidatedBlocks ?? emptyArray;
@@ -3366,15 +3407,17 @@ var DocumentImpl = class {
3366
3407
  const blockChange = this.#blockScanner.edit(change);
3367
3408
  const invalidatedBlocks = this.#regionState.update(change.nextSource, blockChange);
3368
3409
  this.#source = change.nextSource;
3369
- this.#updateTree(invalidatedBlocks, blockChange);
3410
+ this.#updateTree(invalidatedBlocks, blockChange, change.changedSpan.start);
3370
3411
  return { changedSpan: change.changedSpan };
3371
3412
  }
3372
- #updateTree(invalidatedBlocks, change) {
3413
+ #updateTree(invalidatedBlocks, change, damageStart = 0) {
3373
3414
  const records = this.#blockScanner.records;
3374
3415
  const regionsByBlock = this.#regionState.regionsByBlock;
3375
3416
  const root = this.#tree;
3376
3417
  const children = root.children;
3377
- const rebuildStart = change?.stableBlockCount ?? 0;
3418
+ let rebuildStart = change?.stableBlockCount ?? 0;
3419
+ const matchedEnd = change?.oldRecordStart ?? 0;
3420
+ while (rebuildStart < matchedEnd && records[rebuildStart].dependencyEnd < damageStart) rebuildStart++;
3378
3421
  const startLine = children[Math.min(rebuildStart, invalidatedBlocks[0] ?? rebuildStart) - 1]?.position?.end.line ?? 1;
3379
3422
  const locator = this.#blockScanner.locator(startLine - 1);
3380
3423
  const context = createBuildContext(this.#source, this.#blockScanner, this.#profile.inline, locator, new InlineRegionCursor());
@@ -3383,9 +3426,9 @@ var DocumentImpl = class {
3383
3426
  const suffixOffsetDelta = change?.offsetDelta ?? 0;
3384
3427
  shiftTail(children, oldRebuildEnd, newRebuildEnd);
3385
3428
  let invalidatedIndex = 0;
3386
- while (invalidatedIndex < invalidatedBlocks.length && invalidatedBlocks[invalidatedIndex] < rebuildStart) {
3429
+ while (invalidatedIndex < invalidatedBlocks.length && invalidatedBlocks[invalidatedIndex] < newRebuildEnd) {
3387
3430
  const index = invalidatedBlocks[invalidatedIndex++];
3388
- children[index] = buildTreeBlock(records[index], regionsByBlock[index], context);
3431
+ if (index < rebuildStart) children[index] = buildTreeBlock(records[index], regionsByBlock[index], context);
3389
3432
  }
3390
3433
  for (let index = rebuildStart; index < newRebuildEnd; index++) children[index] = buildTreeBlock(records[index], regionsByBlock[index], context);
3391
3434
  const end = locator.locationEnd();
@@ -3424,7 +3467,7 @@ function composeUnwraps(unwraps) {
3424
3467
  for (let index = unwraps.length - 1; index >= 0; index--) {
3425
3468
  const unwrap = unwraps[index];
3426
3469
  const next = chain;
3427
- chain = next ? (source, lines, tokens, index, target) => unwrap(source, lines, tokens, index, target) || next(source, lines, tokens, index, target) : unwrap;
3470
+ chain = next ? (source, lines, index, offset) => unwrap(source, lines, index, offset) ?? next(source, lines, index, offset) : unwrap;
3428
3471
  }
3429
3472
  return chain;
3430
3473
  }
@@ -3489,6 +3532,115 @@ function createBlockProfile(features) {
3489
3532
  interrupt: composeInterrupts(interrupts)
3490
3533
  };
3491
3534
  }
3535
+ // src/inline/boundary.ts
3536
+ const punctuation = /\p{P}/uy;
3537
+ const symbol = /\p{S}/uy;
3538
+ const characters = [
3539
+ String.raw`\u1100-\u11FF\u20A9\u2329\u232A\u2630-\u2637\u268A-\u268F\u2E80-\u2E99\u2E9B-\u2EF3\u2F00-\u2FD5`,
3540
+ String.raw`\u2FF0-\u303E\u3041-\u3096\u309B-\u30FF\u3105-\u312F\u3131-\u318E\u3190-\u31E5\u31EF-\u321E`,
3541
+ String.raw`\u3220-\u3247\u3250-\uA48C\uA490-\uA4C6\uA960-\uA97C\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB`,
3542
+ String.raw`\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE66\uFE68-\uFE6B\uFF01-\uFFBE\uFFC2-\uFFC7`,
3543
+ String.raw`\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC\uFFE0-\uFFE6\uFFE8-\uFFEE`,
3544
+ String.raw`\u{16FE0}-\u{16FE4}\u{16FF0}-\u{16FF6}\u{17000}-\u{18CDA}\u{18CFF}-\u{18D20}\u{18D80}-\u{18DF2}`,
3545
+ String.raw`\u{18E00}-\u{19191}\u{191A0}-\u{191D2}\u{1AFF0}-\u{1AFF3}\u{1AFF5}-\u{1AFFB}\u{1AFFD}\u{1AFFE}`,
3546
+ String.raw`\u{1B000}-\u{1B128}\u{1B132}\u{1B150}-\u{1B152}\u{1B155}\u{1B164}-\u{1B168}\u{1B170}-\u{1B2FB}`,
3547
+ String.raw`\u{1D300}-\u{1D356}\u{1D360}-\u{1D376}\u{1F1AE}\u{1F200}\u{1F202}\u{1F210}-\u{1F219}`,
3548
+ String.raw`\u{1F21B}-\u{1F22E}\u{1F230}\u{1F231}\u{1F237}\u{1F23B}`,
3549
+ String.raw`\u{1F240}-\u{1F248}\u{1F260}-\u{1F265}\u{1F7DA}\u{20000}-\u{3FFFD}`
3550
+ ].join("");
3551
+ const eastAsians = new RegExp(`[${characters}]`, "uy");
3552
+ const boundaryAscii = Uint8Array.from({ length: 128 }, (item, code) => {
3553
+ if (isAsciiWhitespace(code)) return 1;
3554
+ if (!isAsciiPunctuation(code)) return 0;
3555
+ switch (code) {
3556
+ case 36:
3557
+ case 43:
3558
+ case 60:
3559
+ case 61:
3560
+ case 62:
3561
+ case 94:
3562
+ case 96:
3563
+ case 124:
3564
+ case 126: return 4;
3565
+ default: return 2;
3566
+ }
3567
+ });
3568
+ function isNonAsciiWhitespace(code) {
3569
+ if (code >= 8192 && code <= 8202) return true;
3570
+ switch (code) {
3571
+ case 160:
3572
+ case 5760:
3573
+ case 8232:
3574
+ case 8233:
3575
+ case 8239:
3576
+ case 8287:
3577
+ case 12288:
3578
+ case 65279: return true;
3579
+ default: return false;
3580
+ }
3581
+ }
3582
+ function isNonEmojiGeneralUseVs(code) {
3583
+ return code >= 65024 && code <= 65038;
3584
+ }
3585
+ function isIdeoVs(code) {
3586
+ return code >= 917760 && code <= 917999;
3587
+ }
3588
+ function isCJKAmbiguousPunctuation(base, vs) {
3589
+ return vs === 65025 && (base === 8216 || base === 8217 || base === 8220 || base === 8221);
3590
+ }
3591
+ function classifyNonAscii(source, index, code, checkCJK) {
3592
+ if (checkCJK && (code >= 19968 && code <= 40959 || code >= 44032 && code <= 55203 || code >= 12353 && code <= 12438 || code >= 12449 && code <= 12538 || code >= 131072 && code <= 262141)) return 8;
3593
+ if (isNonAsciiWhitespace(code)) return 1;
3594
+ if (checkCJK && isIdeoVs(code)) return 16;
3595
+ let flags = 0;
3596
+ if (punctuation.lastIndex = index, punctuation.test(source)) flags = 2;
3597
+ else if (symbol.lastIndex = index, symbol.test(source)) flags = 4;
3598
+ if (checkCJK && code >= 4352 && (code === 12441 || code === 12442 || code >= 12272 && code <= 12350 || code >= 65281 && code <= 65470 || (eastAsians.lastIndex = index, eastAsians.test(source)))) flags |= 8;
3599
+ return flags;
3600
+ }
3601
+ function codePointBefore(source, end) {
3602
+ let index = end - 1;
3603
+ let code = source.charCodeAt(index);
3604
+ if (code >= 56320 && code <= 57343 && index > 0) {
3605
+ const leading = source.charCodeAt(index - 1);
3606
+ if (leading >= 55296 && leading <= 56319) {
3607
+ index--;
3608
+ code = (leading - 55296 << 10) + code - 56320 + 65536;
3609
+ }
3610
+ }
3611
+ return [index, code];
3612
+ }
3613
+ function boundaryBefore(source, end, checkCJK) {
3614
+ if (end <= 0) return 1;
3615
+ const unit = source.charCodeAt(end - 1);
3616
+ if (unit < 128) return boundaryAscii[unit];
3617
+ const [index, code] = codePointBefore(source, end);
3618
+ if (checkCJK && isNonEmojiGeneralUseVs(code)) {
3619
+ if (index <= 0) return 0;
3620
+ const [baseIndex, baseCode] = codePointBefore(source, index);
3621
+ let baseFlags = baseCode < 128 ? boundaryAscii[baseCode] : classifyNonAscii(source, baseIndex, baseCode, true);
3622
+ if (baseFlags & 1) return 0;
3623
+ baseFlags &= -17;
3624
+ if (isCJKAmbiguousPunctuation(baseCode, code)) baseFlags |= 8;
3625
+ return baseFlags;
3626
+ }
3627
+ return classifyNonAscii(source, index, code, checkCJK);
3628
+ }
3629
+ function boundaryAfter(source, start, checkCJK) {
3630
+ if (start >= source.length) return 1;
3631
+ let code = source.charCodeAt(start);
3632
+ if (code < 128) return boundaryAscii[code];
3633
+ if (code >= 55296 && code <= 56319 && start + 1 < source.length) {
3634
+ const trailing = source.charCodeAt(start + 1);
3635
+ if (trailing >= 56320 && trailing <= 57343) code = (code - 55296 << 10) + trailing - 56320 + 65536;
3636
+ }
3637
+ return classifyNonAscii(source, start, code, checkCJK);
3638
+ }
3639
+ function hasInlineColonBoundary(source, start) {
3640
+ if (start <= 0) return true;
3641
+ if (source.charCodeAt(start - 1) === 58) return false;
3642
+ return Boolean(boundaryBefore(source, start, true) & 27);
3643
+ }
3492
3644
  // src/inline/tokens.ts
3493
3645
  function inlineTokenCount(tokens) {
3494
3646
  return tokens.length / 4;
@@ -3515,26 +3667,19 @@ function appendInlineToken(target, kind, start, end, data = 0) {
3515
3667
  target.push(kind, start, end, data);
3516
3668
  }
3517
3669
  // src/inline/delimiter.ts
3518
- const whitespace = /\s/uy;
3519
- const punctuation = /[\p{P}\p{S}]/uy;
3520
- const delimiterClasses = Uint8Array.from({ length: 128 }, (value, code) => isAsciiWhitespace(code) ? 0 : isAsciiPunctuation(code) ? 1 : 2);
3521
- function delimiterCharacterClass(source, index) {
3522
- if (index < 0 || index >= source.length) return 0;
3523
- const code = source.charCodeAt(index);
3524
- if (code <= 127) return delimiterClasses[code];
3525
- if (code >= 56320 && code <= 57343) {
3526
- const leading = source.charCodeAt(index - 1);
3527
- if (leading >= 55296 && leading <= 56319) index--;
3528
- }
3529
- if (whitespace.lastIndex = index, whitespace.test(source)) return 0;
3530
- if (punctuation.lastIndex = index, punctuation.test(source)) return 1;
3531
- return 2;
3532
- }
3670
+ const delimiterPunctuationMask = 6;
3533
3671
  function flanking(source, start, end, config) {
3534
- const before = delimiterCharacterClass(source, start - 1);
3535
- const after = delimiterCharacterClass(source, end);
3536
- if (before !== after) return before < after ? 1 : 2;
3537
- return before === 1 || before === 2 && config.bits & 1 ? 3 : 0;
3672
+ const cjk = (config.bits & 8) !== 0;
3673
+ const before = boundaryBefore(source, start, cjk);
3674
+ const after = boundaryAfter(source, end, cjk);
3675
+ const beforeSpace = before & 1;
3676
+ const afterSpace = after & 1;
3677
+ if (beforeSpace || afterSpace) return (afterSpace ? 0 : 1) | (beforeSpace ? 0 : 2);
3678
+ const beforePunctuation = before & delimiterPunctuationMask;
3679
+ const afterPunctuation = after & delimiterPunctuationMask;
3680
+ if (!(config.bits & 1)) return (beforePunctuation ? 1 : 0) | (afterPunctuation ? 2 : 0);
3681
+ const adjacentCJK = cjk && before & 24 | after & 8;
3682
+ return (!afterPunctuation || beforePunctuation || adjacentCJK ? 1 : 0) | (!beforePunctuation || afterPunctuation || adjacentCJK ? 2 : 0);
3538
3683
  }
3539
3684
  function canPair(opener, closer) {
3540
3685
  if (closer.config.bits & 2 && (opener.state & 12) !== (closer.state & 12)) return false;
@@ -3542,7 +3687,7 @@ function canPair(opener, closer) {
3542
3687
  return true;
3543
3688
  }
3544
3689
  var DelimiterStack = class {
3545
- #replacements = [];
3690
+ #replacements;
3546
3691
  #runs = [];
3547
3692
  add(source, tokens, tokenIndex, config) {
3548
3693
  const offset = inlineTokenStart(tokens, tokenIndex);
@@ -3575,7 +3720,7 @@ var DelimiterStack = class {
3575
3720
  runs[top++] = closer;
3576
3721
  continue;
3577
3722
  }
3578
- const bottomSlot = (closer.config.bits >>> 3) + (closer.state & 1 ? 3 : 0) + (closer.state >> 2);
3723
+ const bottomSlot = (closer.config.bits >>> 4) + (closer.state & 1 ? 3 : 0) + (closer.state >> 2);
3579
3724
  const bottom = openersBottom[bottomSlot] ?? 0;
3580
3725
  while (closer.remaining) {
3581
3726
  let candidate = top - 1;
@@ -3595,9 +3740,9 @@ var DelimiterStack = class {
3595
3740
  const openStart = openEnd - use;
3596
3741
  const closeStart = closer.start;
3597
3742
  const closeEnd = closeStart + use;
3598
- const pair = use === 2 ? closer.config.double : closer.config.single;
3599
- this.#record(tokens, opener.tokenIndex, openStart, openEnd, pair.open);
3600
- this.#record(tokens, closer.tokenIndex, closeStart, closeEnd, pair.close);
3743
+ const openKind = use === 2 ? closer.config.double : closer.config.single;
3744
+ this.#record(tokens, opener.tokenIndex, openStart, openEnd, openKind);
3745
+ this.#record(tokens, closer.tokenIndex, closeStart, closeEnd, openKind + 1);
3601
3746
  opener.remaining -= use;
3602
3747
  closer.start += use;
3603
3748
  closer.remaining -= use;
@@ -3611,8 +3756,9 @@ var DelimiterStack = class {
3611
3756
  tokens[index * 4 | 0] = kind;
3612
3757
  return;
3613
3758
  }
3614
- this.#replacements[index] ??= [];
3615
- this.#replacements[index].push({
3759
+ const replacements = this.#replacements ??= [];
3760
+ replacements[index] ??= [];
3761
+ replacements[index].push({
3616
3762
  start,
3617
3763
  end,
3618
3764
  kind
@@ -3656,16 +3802,17 @@ function emitResolvedTokens(tokens, replacements = emptyArray) {
3656
3802
  }
3657
3803
  return result;
3658
3804
  }
3659
- function resolveDelimiterConfigs(configs) {
3660
- const delimiterByKind = [];
3661
- configs.forEach((config, index) => {
3662
- delimiterByKind[config.token] = {
3805
+ function resolveDelimiterConfigs(configs, cjk) {
3806
+ const delimiters = [];
3807
+ for (let i = 0; i < configs.length; i++) {
3808
+ const config = configs[i];
3809
+ delimiters[config.token] = {
3663
3810
  single: config.single,
3664
3811
  double: config.double,
3665
- bits: index * 6 << 3 | (config.allowIntraword !== false ? 1 : 0) | (config.pairing.kind === "whole" ? 2 : 0) | (config.pairing.kind === "partial" && config.pairing.ruleOfThree ? 4 : 0)
3812
+ bits: i * 6 << 4 | (config.allowIntraword !== false ? 1 : 0) | (config.pairing.kind === "whole" ? 2 : 0) | (config.pairing.kind === "partial" && config.pairing.ruleOfThree ? 4 : 0) | (cjk ? 8 : 0)
3666
3813
  };
3667
- });
3668
- return delimiterByKind;
3814
+ }
3815
+ return delimiters;
3669
3816
  }
3670
3817
  // src/profile/features/attributes/shared.ts
3671
3818
  const brackets = {
@@ -3720,12 +3867,12 @@ function scanBracketed(source, offset, limit) {
3720
3867
  return -1;
3721
3868
  }
3722
3869
  function scanAttributes(source, start, attributes, limit = source.length) {
3723
- if (source.charCodeAt(start) !== 123 || source.charCodeAt(start + 1) === 123) return;
3870
+ if (source.charCodeAt(start) !== 123) return;
3724
3871
  let offset = start + 1;
3725
3872
  while (offset < limit) {
3726
3873
  offset = trimLineWhitespaceStart(source, offset, limit);
3727
- if (source.charCodeAt(offset) === 125) return offset + 1;
3728
3874
  const marker = source.charCodeAt(offset);
3875
+ if (marker === 125) return offset + 1;
3729
3876
  if (marker === 46 || marker === 35) {
3730
3877
  offset++;
3731
3878
  const valueStart = offset;
@@ -3735,8 +3882,8 @@ function scanAttributes(source, start, attributes, limit = source.length) {
3735
3882
  continue;
3736
3883
  }
3737
3884
  const nameStart = offset;
3738
- if (source.charCodeAt(offset) === 58) offset++;
3739
3885
  let code = source.charCodeAt(offset);
3886
+ if (code === 58) code = source.charCodeAt(++offset);
3740
3887
  if (!isAsciiLetter(code) && code !== 95) return;
3741
3888
  offset++;
3742
3889
  while (true) {
@@ -3749,7 +3896,7 @@ function scanAttributes(source, start, attributes, limit = source.length) {
3749
3896
  if (source.charCodeAt(offset) !== 61) {
3750
3897
  if (attributes) {
3751
3898
  const name = source.slice(nameStart, nameEnd);
3752
- assignAttribute(attributes, source.charCodeAt(nameStart) === 58 ? name : `:${name}`, "true");
3899
+ assignAttribute(attributes, code === 58 ? name : `:${name}`, "true");
3753
3900
  }
3754
3901
  continue;
3755
3902
  }
@@ -3782,8 +3929,8 @@ function parseAttributes(source, start, attributes = {}) {
3782
3929
  scanAttributes(source, start, attributes);
3783
3930
  return attributes;
3784
3931
  }
3785
- // src/profile/features/footnote/shared.ts
3786
- const footnoteDefinitionPrefix = "footnote\0";
3932
+ // src/profile/features/footnote.ts
3933
+ const footnoteDefinitionHead = "FOOTNOTE\0";
3787
3934
  function footnoteLabelEnd(source, start, limit) {
3788
3935
  if (source.charCodeAt(start) !== 91 || source.charCodeAt(start + 1) !== 94) return;
3789
3936
  const labelStart = start + 2;
@@ -3808,31 +3955,114 @@ function footnoteLabelAt(source, start, limit) {
3808
3955
  const label = source.slice(start + 2, end - 1);
3809
3956
  return {
3810
3957
  end,
3811
- definitionKey: footnoteDefinitionPrefix + normalizeIdentifier(label)
3958
+ definitionKey: footnoteDefinitionHead + normalizeIdentifier(label)
3812
3959
  };
3813
3960
  }
3814
3961
  }
3962
+ function definitionMarkerEnd(source, start, lineEnd) {
3963
+ const end = footnoteLabelEnd(source, start, lineEnd);
3964
+ if (end !== void 0 && source.charCodeAt(end) === 58) return end + 1;
3965
+ }
3966
+ const feature$18 = {
3967
+ block: {
3968
+ builds: [{
3969
+ token: 76,
3970
+ build(tokenStart, context) {
3971
+ const tokens = context.structure.tokens;
3972
+ const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
3973
+ const start = context.locator.locationAt(tokens.start(tokenStart));
3974
+ const children = buildBlockChildren(tokenStart, context);
3975
+ const label = context.source.slice(tokens.start(tokenStart) + 2, tokens.end(tokenStart) - 2);
3976
+ return {
3977
+ type: "footnoteDefinition",
3978
+ identifier: tokens.value(tokenStart),
3979
+ label: decodeString(label),
3980
+ children,
3981
+ position: {
3982
+ start,
3983
+ end: context.locator.locationAt(tokens.end(close))
3984
+ }
3985
+ };
3986
+ }
3987
+ }],
3988
+ starts: [{
3989
+ codes: [91],
3990
+ unwrap(source, lines, index, contentOffset) {
3991
+ const lineEnd = lines.end(index);
3992
+ const markerEnd = definitionMarkerEnd(source, contentOffset, lineEnd);
3993
+ if (markerEnd !== void 0) return lines.child(index, trimLineWhitespaceStart(source, markerEnd, lineEnd), 0);
3994
+ },
3995
+ interrupt(source, lines, index, contentOffset) {
3996
+ return definitionMarkerEnd(source, contentOffset, lines.end(index)) !== void 0;
3997
+ },
3998
+ start(source, lines, tokens, start, contentOffset, context) {
3999
+ const lineEnd = lines.end(start);
4000
+ const markerEnd = definitionMarkerEnd(source, contentOffset, lineEnd);
4001
+ if (markerEnd === void 0) return;
4002
+ const bodyStart = trimLineWhitespaceStart(source, markerEnd, lineEnd);
4003
+ const definitionLines = lines.child(start, bodyStart, 0);
4004
+ let index = start + 1;
4005
+ for (; index < lines.length; index++) {
4006
+ if (!lines.lazy(index)) {
4007
+ const line = definitionLines.pushAfterColumns(source, lines, index, 4);
4008
+ if (line === -2) {
4009
+ definitionLines.pushFrom(lines, index);
4010
+ continue;
4011
+ }
4012
+ if (line === -3) continue;
4013
+ if (context.startsInterruptingBlock(source, lines, index, line) || !context.endsWithParagraphLeaf(source, definitionLines, definitionLines.length - 1)) break;
4014
+ }
4015
+ definitionLines.pushFrom(lines, index, void 0, void 0, true);
4016
+ }
4017
+ while (definitionLines.length > 0 && isBlankLine(source, definitionLines, definitionLines.length - 1)) definitionLines.pop();
4018
+ const identifier = normalizeIdentifier(source.slice(contentOffset + 2, markerEnd - 2));
4019
+ tokens.push(76, contentOffset, markerEnd, {
4020
+ definitionKey: footnoteDefinitionHead + identifier,
4021
+ value: identifier
4022
+ });
4023
+ context.scanBlockLines(source, definitionLines, tokens);
4024
+ const end = definitionLines.length > 0 ? definitionLines.next(definitionLines.length - 1) : markerEnd;
4025
+ tokens.push(197, end, end);
4026
+ return index;
4027
+ }
4028
+ }]
4029
+ },
4030
+ inline: { builds: [{
4031
+ kind: "leaf",
4032
+ token: 132,
4033
+ build(tokenIndex, position, context) {
4034
+ const label = inlineTokenText(context.view.text, context.tokens, tokenIndex, 2, 1);
4035
+ return {
4036
+ type: "footnoteReference",
4037
+ identifier: normalizeIdentifier(label),
4038
+ label: decodeString(label),
4039
+ position
4040
+ };
4041
+ }
4042
+ }] }
4043
+ };
3815
4044
  // src/profile/resolve.ts
3816
4045
  const referenceTail = /\[(?:[^[\]\\]|\\[\s\S])*\]/y;
4046
+ function hasDirectTail(tokens, close) {
4047
+ return inlineTokenKind(tokens, close) === 68 || inlineTokenData(tokens, close) !== 0;
4048
+ }
4049
+ function setBoundary(tokens, index, kind, end) {
4050
+ const field = index * 4;
4051
+ tokens[field | 0] = kind;
4052
+ tokens[field | 2] = end;
4053
+ }
4054
+ function matchFootnote(source, tokens, definitions, open, close) {
4055
+ if (hasDirectTail(tokens, close)) return -1;
4056
+ const label = footnoteLabelAt(source, inlineTokenEnd(tokens, open) - 1, source.length);
4057
+ if (!label || label.end !== inlineTokenEnd(tokens, close) || !definitions.hasDefinition(label.definitionKey)) return -1;
4058
+ const field = open * 4;
4059
+ if (tokens[field | 0] === 4) tokens[field | 1]++;
4060
+ setBoundary(tokens, open, 132, label.end);
4061
+ return label.end;
4062
+ }
3817
4063
  function opensComponentLabel(tokens, open) {
3818
4064
  return open > 0 && inlineTokenKind(tokens, open - 1) === 136 && inlineTokenEnd(tokens, open - 1) === inlineTokenStart(tokens, open);
3819
4065
  }
3820
- function pairBrackets(tokens, start, end) {
3821
- const mates = new Array(end);
3822
- const openers = [];
3823
- for (let index = start; index < end; index++) {
3824
- const kind = inlineTokenKind(tokens, index);
3825
- if (kind === 5 || kind === 4) openers.push(index);
3826
- else if (kind === 6 || kind === 68) {
3827
- const open = openers.pop();
3828
- if (open !== void 0) {
3829
- mates[open] = index;
3830
- mates[index] = open;
3831
- }
3832
- }
3833
- }
3834
- return mates;
3835
- }
3836
4066
  function componentOpening(source, tokens, open, close, insideLink) {
3837
4067
  if (opensComponentLabel(tokens, open)) return 208;
3838
4068
  const suffix = inlineTokenStart(tokens, close) + 1;
@@ -3841,10 +4071,22 @@ function componentOpening(source, tokens, open, close, insideLink) {
3841
4071
  if (insideLink && (trailing !== 123 || attributesEnd(source, suffix) === void 0)) return 0;
3842
4072
  return 206;
3843
4073
  }
3844
- function matchedLinkEnd(source, tokens, definitions, open, close, lastVisibleOpener) {
3845
- const end = inlineTokenEnd(tokens, close);
3846
- if (inlineTokenKind(tokens, close) === 68) return end;
4074
+ function closeComponent(tokens, open, close, claim) {
4075
+ const at = inlineTokenStart(tokens, close);
4076
+ if (claim === 208) {
4077
+ const openingEnd = inlineTokenEnd(tokens, open);
4078
+ tokens[open * 4 | 0] = 0;
4079
+ if (openingEnd >= at) {
4080
+ tokens[close * 4 | 0] = 0;
4081
+ return;
4082
+ }
4083
+ setBoundary(tokens, open - 1, claim, openingEnd);
4084
+ }
4085
+ setBoundary(tokens, close, claim + 1, at + 1);
4086
+ }
4087
+ function matchedReferenceEnd(source, tokens, definitions, open, close, lastVisibleOpener) {
3847
4088
  if (!definitions.hasDefinitions()) return -1;
4089
+ const end = inlineTokenEnd(tokens, close);
3848
4090
  referenceTail.lastIndex = end;
3849
4091
  let suffixEnd = referenceTail.test(source) ? referenceTail.lastIndex : -1;
3850
4092
  let identifier = suffixEnd > end + 2 ? referenceIdentifier(source.slice(end + 1, suffixEnd - 1)) : "";
@@ -3857,10 +4099,27 @@ function matchedLinkEnd(source, tokens, definitions, open, close, lastVisibleOpe
3857
4099
  }
3858
4100
  return definitions.hasDefinition(identifier) ? suffixEnd : -1;
3859
4101
  }
3860
- function setBoundary(tokens, index, kind, end) {
3861
- const field = index * 4;
3862
- tokens[field | 0] = kind;
3863
- tokens[field | 2] = end;
4102
+ function pairBrackets(tokens, start, end) {
4103
+ const mates = new Array(end);
4104
+ const openers = [];
4105
+ let consumedEnd = -1;
4106
+ for (let index = start; index < end; index++) {
4107
+ if (consumedEnd >= 0 && inlineTokenStart(tokens, index) < consumedEnd) continue;
4108
+ const kind = inlineTokenKind(tokens, index);
4109
+ if (kind === 5 || kind === 4) openers.push(index);
4110
+ else if (kind === 6 || kind === 68) {
4111
+ const open = openers.pop();
4112
+ if (open !== void 0) {
4113
+ mates[open] = index;
4114
+ mates[index] = open;
4115
+ if (kind === 6 && !opensComponentLabel(tokens, open)) {
4116
+ const tailEnd = inlineTokenData(tokens, index);
4117
+ if (tailEnd > 0) consumedEnd = tailEnd;
4118
+ }
4119
+ }
4120
+ }
4121
+ }
4122
+ return mates;
3864
4123
  }
3865
4124
  function resolvePairs(source, tokens, definitions, options, delimiters, start) {
3866
4125
  const end = inlineTokenCount(tokens);
@@ -3873,15 +4132,15 @@ function resolvePairs(source, tokens, definitions, options, delimiters, start) {
3873
4132
  let resourceLabelEnd = -1;
3874
4133
  let consumedEnd = -1;
3875
4134
  for (let index = start; index < end; index++) {
3876
- const kind = inlineTokenKind(tokens, index);
3877
- const opener = kind === 5 || kind === 4;
3878
- if (opener && (options.component || options.footnote && source.charCodeAt(inlineTokenEnd(tokens, index)) === 94)) mates ??= pairBrackets(tokens, index, end);
3879
- const close = opener ? mates?.[index] : void 0;
3880
- const component = options.component && kind === 5 && close !== void 0;
3881
- const insideLink = index < resourceLabelEnd;
3882
- if (component && inlineTokenKind(tokens, close) === 68) resourceLabelEnd = Math.max(resourceLabelEnd, close);
3883
- if (consumedEnd >= 0 && inlineTokenStart(tokens, index) < consumedEnd) {
3884
- tokens[index * 4 | 0] = 0;
4135
+ const field = index * 4;
4136
+ const kind = tokens[field | 0];
4137
+ if (consumedEnd >= 0 && tokens[field | 1] < consumedEnd) {
4138
+ if (options.component && kind === 5) {
4139
+ mates ??= pairBrackets(tokens, index, end);
4140
+ const close = mates[index];
4141
+ if (close !== void 0 && hasDirectTail(tokens, close)) resourceLabelEnd = Math.max(resourceLabelEnd, close);
4142
+ }
4143
+ tokens[field | 0] = 0;
3885
4144
  continue;
3886
4145
  }
3887
4146
  const delimiter = delimiters[kind];
@@ -3890,26 +4149,29 @@ function resolvePairs(source, tokens, definitions, options, delimiters, start) {
3890
4149
  scopes.add(source, tokens, index, delimiter);
3891
4150
  continue;
3892
4151
  }
3893
- if (opener) {
3894
- if (options.footnote && close !== void 0 && inlineTokenKind(tokens, close) === 6) {
3895
- const label = footnoteLabelAt(source, inlineTokenEnd(tokens, index) - 1, source.length);
3896
- if (label && label.end === inlineTokenEnd(tokens, close) && definitions.hasDefinition(label.definitionKey)) {
3897
- consumedEnd = label.end;
3898
- setBoundary(tokens, index, 132, consumedEnd);
3899
- if (kind === 4) tokens[index * 4 | 1]++;
4152
+ if (kind === 5 || kind === 4) {
4153
+ if (options.component || options.footnote && source.charCodeAt(tokens[field | 2]) === 94) mates ??= pairBrackets(tokens, index, end);
4154
+ const close = mates?.[index];
4155
+ const component = options.component && kind === 5 && close !== void 0;
4156
+ const insideLink = index < resourceLabelEnd;
4157
+ if (component && hasDirectTail(tokens, close)) resourceLabelEnd = Math.max(resourceLabelEnd, close);
4158
+ if (options.footnote && close !== void 0) {
4159
+ const footnoteEnd = matchFootnote(source, tokens, definitions, index, close);
4160
+ if (footnoteEnd >= 0) {
4161
+ consumedEnd = footnoteEnd;
3900
4162
  continue;
3901
4163
  }
3902
4164
  }
3903
4165
  if (component) {
3904
4166
  const opening = componentOpening(source, tokens, index, close, insideLink);
3905
4167
  if (opening !== 0) {
3906
- tokens[index * 4 | 0] = opening;
4168
+ tokens[field | 0] = opening;
3907
4169
  if (insideLink) literalLabelEnd = Math.max(literalLabelEnd, close);
3908
4170
  continue;
3909
4171
  }
3910
4172
  }
3911
4173
  if (index < literalLabelEnd && kind !== 4) {
3912
- tokens[index * 4 | 0] = 0;
4174
+ tokens[field | 0] = 0;
3913
4175
  if (close !== void 0) tokens[close * 4 | 0] = 0;
3914
4176
  continue;
3915
4177
  }
@@ -3923,27 +4185,20 @@ function resolvePairs(source, tokens, definitions, options, delimiters, start) {
3923
4185
  let open;
3924
4186
  if (claim === 208 || claim === 206) {
3925
4187
  open = mate;
3926
- const at = inlineTokenStart(tokens, index);
3927
- const openingEnd = inlineTokenEnd(tokens, open);
3928
- if (claim === 208) {
3929
- tokens[open * 4 | 0] = 0;
3930
- if (openingEnd >= at) tokens[index * 4 | 0] = 0;
3931
- else {
3932
- setBoundary(tokens, open - 1, claim, openingEnd);
3933
- setBoundary(tokens, index, claim + 1, at + 1);
3934
- }
3935
- } else setBoundary(tokens, index, claim + 1, at + 1);
3936
- } else {
3937
- if (index < literalLabelEnd && mate !== void 0 && inlineTokenKind(tokens, mate) !== 4) continue;
4188
+ closeComponent(tokens, open, index, claim);
4189
+ } else if (index < literalLabelEnd && claim !== 4) continue;
4190
+ else {
3938
4191
  const label = openers.pop();
3939
4192
  if (label === void 0) continue;
3940
4193
  const image = inlineTokenKind(tokens, label) === 4;
3941
4194
  if (!image && label < lastLinkOpener) continue;
3942
- const closeEnd = matchedLinkEnd(source, tokens, definitions, label, index, lastVisibleOpener);
4195
+ const tokenEnd = tokens[field | 2];
4196
+ const directEnd = kind === 68 ? tokenEnd : tokens[field | 3];
4197
+ const direct = directEnd > 0;
4198
+ const closeEnd = direct ? directEnd : matchedReferenceEnd(source, tokens, definitions, label, index, lastVisibleOpener);
3943
4199
  if (closeEnd < 0) continue;
3944
4200
  open = label;
3945
- const opening = (kind === 68 ? 198 : 202) + (image ? 2 : 0);
3946
- const tokenEnd = inlineTokenEnd(tokens, index);
4201
+ const opening = (direct ? 198 : 202) + (image ? 2 : 0);
3947
4202
  tokens[open * 4 | 0] = opening;
3948
4203
  setBoundary(tokens, index, opening + 1, closeEnd);
3949
4204
  if (!image) lastLinkOpener = open;
@@ -3953,8 +4208,8 @@ function resolvePairs(source, tokens, definitions, options, delimiters, start) {
3953
4208
  }
3954
4209
  return scopes?.finish(tokens) ?? emitResolvedTokens(tokens);
3955
4210
  }
3956
- function createInlineResolver(options, delimiters) {
3957
- const delimiterByKind = resolveDelimiterConfigs(delimiters);
4211
+ function createInlineResolver(options, delimiterConfigs) {
4212
+ const delimiters = resolveDelimiterConfigs(delimiterConfigs, options.cjk);
3958
4213
  return (source, tokens, definitions, consume) => {
3959
4214
  let start = 0;
3960
4215
  let resolved = consume ? tokens : void 0;
@@ -3976,7 +4231,7 @@ function createInlineResolver(options, delimiters) {
3976
4231
  }
3977
4232
  const closing = close === end ? 0 : inlineTokenKind(tokens, close);
3978
4233
  const resource = closing === 68;
3979
- if (closing !== 0 && !resource && (closing !== 6 || options.component || options.footnote && source.charCodeAt(inlineTokenEnd(tokens, start)) === 94 || definitions.hasDefinitions())) break;
4234
+ if (closing !== 0 && !resource && (closing !== 6 || options.component || options.footnote && source.charCodeAt(inlineTokenEnd(tokens, start)) === 94 || inlineTokenData(tokens, close) !== 0 || definitions.hasDefinitions())) break;
3980
4235
  if (!resource) {
3981
4236
  literalBrackets += close < end ? 2 : 1;
3982
4237
  start = close < end ? close + 1 : end;
@@ -3996,7 +4251,7 @@ function createInlineResolver(options, delimiters) {
3996
4251
  default: return emitResolvedTokens(settled);
3997
4252
  }
3998
4253
  }
3999
- return resolvePairs(source, resolved ?? tokens.slice(), definitions, options, delimiterByKind, start);
4254
+ return resolvePairs(source, resolved ?? tokens.slice(), definitions, options, delimiters, start);
4000
4255
  };
4001
4256
  }
4002
4257
  // src/inline/tokenizer.ts
@@ -4008,8 +4263,8 @@ function createMasks(chars) {
4008
4263
  for (let i = 0; i < chars.length; i++) {
4009
4264
  const marker = chars.charCodeAt(i);
4010
4265
  for (let byte = 0; byte < 256; byte++) {
4011
- masks[byte << 8 | marker] |= 1;
4012
- masks[marker << 8 | byte] |= 2;
4266
+ masks[byte << 8 | marker] |= 2;
4267
+ masks[marker << 8 | byte] |= 1;
4013
4268
  }
4014
4269
  }
4015
4270
  cacheMasks.set(chars, masks);
@@ -4033,16 +4288,10 @@ function findClosingRunEnd(source, start, width, marker, code) {
4033
4288
  }
4034
4289
  return -1;
4035
4290
  }
4036
- function scanLineEnding(source, start, tokens) {
4291
+ function findLineEndingEnd(source, start) {
4037
4292
  let next = start + 1;
4038
4293
  if (source.charCodeAt(start) === 13 && source.charCodeAt(next) === 10) next++;
4039
- const lineStart = next;
4040
4294
  while (source.charCodeAt(next) === 32) next++;
4041
- if (source.charCodeAt(start - 1) === 32 && source.charCodeAt(start - 2) === 32) {
4042
- let spaces = start - 2;
4043
- while (source.charCodeAt(spaces - 1) === 32) spaces--;
4044
- tokens.push(128, spaces, next, lineStart);
4045
- } else tokens.push(67, start, next, 0);
4046
4295
  return next;
4047
4296
  }
4048
4297
  function createBitmap(words, masks, group) {
@@ -4051,7 +4300,7 @@ function createBitmap(words, masks, group) {
4051
4300
  const word1 = words[i | 1];
4052
4301
  const word2 = words[i | 2];
4053
4302
  const word3 = words[i | 3];
4054
- return masks[word0 & 65535] | masks[word0 >>> 16] << 2 | (masks[word1 & 65535] | masks[word1 >>> 16] << 2) << 4 | (masks[word2 & 65535] | masks[word2 >>> 16] << 2) << 8 | (masks[word3 & 65535] | masks[word3 >>> 16] << 2) << 12;
4303
+ return (masks[word0 & 65535] << 2 | masks[word0 >>> 16]) << 28 | (masks[word1 & 65535] << 2 | masks[word1 >>> 16]) << 24 | (masks[word2 & 65535] << 2 | masks[word2 >>> 16]) << 20 | (masks[word3 & 65535] << 2 | masks[word3 >>> 16]) << 16;
4055
4304
  }
4056
4305
  function tokenizeLatin1(scanners, masks, source, mapping) {
4057
4306
  const words = ScratchView.words;
@@ -4065,7 +4314,7 @@ function tokenizeLatin1(scanners, masks, source, mapping) {
4065
4314
  let cursor = base + offset;
4066
4315
  groups: for (let group = cursor & -16; cursor < limit;) {
4067
4316
  let bits;
4068
- let mask = -1 << (cursor & 15);
4317
+ let mask = -1 >>> (cursor & 15);
4069
4318
  do {
4070
4319
  bits = createBitmap(words, masks, group) & mask;
4071
4320
  mask = -1;
@@ -4073,13 +4322,13 @@ function tokenizeLatin1(scanners, masks, source, mapping) {
4073
4322
  if (!bits) break;
4074
4323
  const groupEnd = group + 16;
4075
4324
  do {
4076
- const markerOffset = group + 31 - Math.clz32(bits & -bits) - base;
4325
+ const markerOffset = group + Math.clz32(bits) - base;
4077
4326
  if (markerOffset >= end) break groups;
4078
4327
  const next = (0, scanners[source.charCodeAt(markerOffset)])?.(source, markerOffset, tokens ??= []) ?? markerOffset;
4079
4328
  offset = next > markerOffset ? next : markerOffset + 1;
4080
4329
  cursor = base + offset;
4081
4330
  if (cursor >= groupEnd) break;
4082
- bits &= -1 << (cursor & 15);
4331
+ bits &= -1 >>> (cursor & 15);
4083
4332
  } while (bits);
4084
4333
  cursor = Math.max(cursor, groupEnd);
4085
4334
  group = cursor & -16;
@@ -4091,9 +4340,9 @@ function tokenizeLatin1(scanners, masks, source, mapping) {
4091
4340
  }
4092
4341
  }
4093
4342
  function createInlineTokenizer(boundaryFast, boundarySlow, scanners, masks) {
4094
- return (source, withBoundaryBreak, mapping) => {
4343
+ return (source, hasPaddedLineFeed, mapping) => {
4095
4344
  if (masks && mapping !== void 0) return tokenizeLatin1(scanners, masks, source, mapping);
4096
- const boundary = withBoundaryBreak ? boundarySlow : boundaryFast;
4345
+ const boundary = hasPaddedLineFeed ? boundarySlow : boundaryFast;
4097
4346
  let tokens;
4098
4347
  for (let cursor = 0; cursor < source.length;) {
4099
4348
  const offset = findPatternEnd(source, cursor, boundary) - 1;
@@ -4123,12 +4372,12 @@ function generateInlineTokenizer(boundaryFast, boundarySlow, scanners, masks, co
4123
4372
  const dispatch = `if(t===ea)t=[];let n;switch(s.charCodeAt(mo)){${cases.join("")}default:n=mo}o=n>mo?n:mo+1;`;
4124
4373
  const scanBitmap = [
4125
4374
  `const l=b+e;let c=b+o;`,
4126
- `g:for(let g=c&~15;c<l;){let bs,m=-1<<(c&15);`,
4375
+ `g:for(let g=c&~15;c<l;){let bs,m=-1>>>(c&15);`,
4127
4376
  `do{bs=cb(w,ms,g)&m;m=-1}while(!bs&&(g+=16)<l);`,
4128
4377
  `if(!bs)break;const ge=g+16;`,
4129
- `do{const mo=g+31-Math.clz32(bs&-bs)-b;if(mo>=e)break g;`,
4378
+ `do{const mo=g+Math.clz32(bs)-b;if(mo>=e)break g;`,
4130
4379
  dispatch,
4131
- `c=b+o;if(c>=ge)break;bs&=-1<<(c&15)`,
4380
+ `c=b+o;if(c>=ge)break;bs&=-1>>>(c&15)`,
4132
4381
  `}while(bs);c=Math.max(c,ge);g=c&~15}`
4133
4382
  ].join("");
4134
4383
  return Function(...Object.keys(bindings), [
@@ -4140,7 +4389,7 @@ function generateInlineTokenizer(boundaryFast, boundarySlow, scanners, masks, co
4140
4389
  scanBitmap,
4141
4390
  `o=Math.max(o,e)}return t}`
4142
4391
  ].join(""),
4143
- `return(s,wb,mp)=>{let t=ea,o=0;`,
4392
+ `return(s,hp,mp)=>{let t=ea,o=0;`,
4144
4393
  masks && [
4145
4394
  `if(mp!==void 0){`,
4146
4395
  `if(typeof mp!=="number")return sg(s,mp);`,
@@ -4148,14 +4397,14 @@ function generateInlineTokenizer(boundaryFast, boundarySlow, scanners, masks, co
4148
4397
  scanBitmap,
4149
4398
  `return t}`
4150
4399
  ].join(""),
4151
- `const b0=wb?b2:b1;while(o<s.length){`,
4400
+ `const b0=hp?b2:b1;while(o<s.length){`,
4152
4401
  `b0.lastIndex=o;if(!b0.test(s))break;`,
4153
4402
  `const mo=b0.lastIndex-1;`,
4154
4403
  dispatch,
4155
4404
  `}return t}`
4156
4405
  ].join(""))(...Object.values(bindings));
4157
4406
  }
4158
- function create(rules) {
4407
+ function create(rules, scanAllLineFeeds) {
4159
4408
  const scanners = [];
4160
4409
  const codes = [];
4161
4410
  for (const rule of rules) {
@@ -4167,14 +4416,12 @@ function create(rules) {
4167
4416
  };
4168
4417
  if (previous === void 0) codes.push(code);
4169
4418
  }
4170
- const markers = String.fromCharCode(...[13, ...codes].sort((a, b) => a - b));
4171
- const masks = ScratchView.supports && codes.every((code) => code >= 0 && code <= 255) ? createMasks(markers) : void 0;
4172
- const boundaries = markers.replaceAll(/[\\\]-]/g, "\\$&");
4419
+ const markers = scanAllLineFeeds ? codes.slice() : codes.filter((code) => code !== 10);
4420
+ const chars = String.fromCharCode(...markers.sort((a, b) => a - b));
4421
+ const masks = ScratchView.supports && codes.every((code) => code >= 0 && code <= 255) ? createMasks(chars) : void 0;
4422
+ const boundaries = chars.replaceAll(/[\\\]-]/g, "\\$&");
4173
4423
  const boundaryFast = new RegExp(`[${boundaries}]`, "g");
4174
- const boundarySlow = new RegExp(`[${boundaries}]|\\n(?:(?<=[ \\t]\\n)|(?= ))`, "g");
4175
- scanners[10] = scanLineEnding;
4176
- scanners[13] = scanLineEnding;
4177
- codes.push(10, 13);
4424
+ const boundarySlow = scanAllLineFeeds ? boundaryFast : new RegExp(`[${boundaries}]|\\n(?:(?<=[ \\t]\\n)|(?= ))`, "g");
4178
4425
  try {
4179
4426
  return generateInlineTokenizer(boundaryFast, boundarySlow, scanners, masks, codes);
4180
4427
  } catch {
@@ -4203,35 +4450,37 @@ function createInlineProfile(features, resolverOptions) {
4203
4450
  return {
4204
4451
  builders,
4205
4452
  resolve: createInlineResolver(resolverOptions, delimiters),
4206
- tokenize: create(scanRules)
4453
+ tokenize: create(scanRules, resolverOptions.breaks)
4207
4454
  };
4208
4455
  }
4209
4456
  // src/profile/features/attributes/block.ts
4457
+ function promoteParagraphAttributes(owner) {
4458
+ const paragraph = owner.children.length === 1 ? owner.children[0] : void 0;
4459
+ if (paragraph?.type !== "paragraph" || !paragraph.attributes) return;
4460
+ owner.attributes = paragraph.attributes;
4461
+ owner.children[0] = {
4462
+ type: "paragraph",
4463
+ children: paragraph.children,
4464
+ position: paragraph.position
4465
+ };
4466
+ }
4210
4467
  const blockDecorators = [{
4211
- token: [73, 74],
4468
+ token: [71, 72],
4212
4469
  decorate(build) {
4213
4470
  return (tokenStart, context) => {
4214
4471
  const result = build(tokenStart, context);
4215
- if (!result.spread) for (const item of result.children) {
4216
- const paragraph = !item.spread && item.children.length === 1 && item.children[0].type === "paragraph" ? item.children[0] : void 0;
4217
- if (paragraph?.attributes) {
4218
- item.attributes = paragraph.attributes;
4219
- delete paragraph.attributes;
4220
- }
4472
+ if (!result.spread) {
4473
+ for (const item of result.children) if (!item.spread) promoteParagraphAttributes(item);
4221
4474
  }
4222
4475
  return result;
4223
4476
  };
4224
4477
  }
4225
4478
  }, {
4226
- token: 72,
4479
+ token: 70,
4227
4480
  decorate(build) {
4228
4481
  return (tokenStart, context) => {
4229
4482
  const result = build(tokenStart, context);
4230
- const paragraph = result.children.length === 1 && result.children[0].type === "paragraph" ? result.children[0] : void 0;
4231
- if (paragraph?.attributes) {
4232
- result.attributes = paragraph.attributes;
4233
- delete paragraph.attributes;
4234
- }
4483
+ promoteParagraphAttributes(result);
4235
4484
  return result;
4236
4485
  };
4237
4486
  }
@@ -4248,7 +4497,7 @@ function attributeEndAt(source, start) {
4248
4497
  }
4249
4498
  }
4250
4499
  // src/profile/features/attributes/index.ts
4251
- const feature$18 = {
4500
+ const feature$17 = {
4252
4501
  block: { decorators: blockDecorators },
4253
4502
  inline: {
4254
4503
  scans: [{
@@ -4270,9 +4519,7 @@ const feature$18 = {
4270
4519
  const previousEnd = previous >= 0 ? inlineTokenEnd(tokens, previous) : 0;
4271
4520
  if (previousEnd < start && (previous >= 0 && inlineTokenKind(tokens, previous) === 256 || hasVisibleText(source, previousEnd, start))) appendInlineToken(tokens, 64, previousEnd, start);
4272
4521
  appendInlineToken(tokens, 256, start, end, start > 0 && isSpaceTabOrLineEnding(source.charCodeAt(start - 1)) ? 1 : 0);
4273
- let next = end;
4274
- while (isSpaceTabOrLineEnding(source.charCodeAt(next))) next++;
4275
- if (next === source.length) for (let index = inlineTokenCount(tokens) - 1; index >= 0; index--) {
4522
+ if (!hasVisibleText(source, end, source.length)) for (let index = inlineTokenCount(tokens) - 1; index >= 0; index--) {
4276
4523
  const kind = inlineTokenKind(tokens, index);
4277
4524
  if (kind === 64 && !hasVisibleText(source, inlineTokenStart(tokens, index), inlineTokenEnd(tokens, index))) continue;
4278
4525
  if (kind !== 256) break;
@@ -4300,7 +4547,7 @@ const feature$18 = {
4300
4547
  };
4301
4548
  // src/profile/features/binding.ts
4302
4549
  const leadingWhitespace = /\s*/y;
4303
- const feature$17 = { inline: {
4550
+ const feature$16 = { inline: {
4304
4551
  scans: [{
4305
4552
  marker: 123,
4306
4553
  scan(source, start, tokens) {
@@ -4315,7 +4562,7 @@ const feature$17 = { inline: {
4315
4562
  }
4316
4563
  if (valueStart >= valueEnd) return -1;
4317
4564
  const end = close + 2;
4318
- appendInlineToken(tokens, 134, start, end, valueEnd - start);
4565
+ appendInlineToken(tokens, 134, start, end, valueEnd);
4319
4566
  return end;
4320
4567
  }
4321
4568
  }],
@@ -4324,10 +4571,9 @@ const feature$17 = { inline: {
4324
4571
  token: 134,
4325
4572
  build(tokenIndex, position, context) {
4326
4573
  const source = context.view.text;
4327
- const tokenStart = inlineTokenStart(context.tokens, tokenIndex);
4328
- const start = tokenStart + 2;
4574
+ const start = inlineTokenStart(context.tokens, tokenIndex) + 2;
4329
4575
  const end = inlineTokenEnd(context.tokens, tokenIndex) - 2;
4330
- const separator = tokenStart + inlineTokenData(context.tokens, tokenIndex);
4576
+ const separator = inlineTokenData(context.tokens, tokenIndex);
4331
4577
  const value = source.slice(start, separator).trim();
4332
4578
  const defaultValue = separator === end ? "" : source.slice(separator + 2, end).trim();
4333
4579
  const attributes = { ":value": value };
@@ -4343,13 +4589,14 @@ const feature$17 = { inline: {
4343
4589
  }]
4344
4590
  } };
4345
4591
  // src/profile/features/blockquote.ts
4346
- function blockQuoteOffset(source, lines, index, markerOffset) {
4592
+ function blockQuoteOffset(source, markerOffset) {
4347
4593
  if (markerOffset < 0 || source.charCodeAt(markerOffset) !== 62) return;
4348
4594
  let offset = markerOffset + 1;
4349
- let prefixColumns = lines.prefixColumns(index);
4350
- if (source.charCodeAt(offset) === 32) offset++;
4351
- else if (source.charCodeAt(offset) === 9) {
4352
- prefixColumns += 4 - physicalColumnAt(source, offset) % 4 - 1;
4595
+ let prefixColumns = 0;
4596
+ const code = source.charCodeAt(offset);
4597
+ if (code === 32) offset++;
4598
+ else if (code === 9) {
4599
+ prefixColumns = tabWidthAt(source, offset) - 1;
4353
4600
  offset++;
4354
4601
  }
4355
4602
  return {
@@ -4357,9 +4604,9 @@ function blockQuoteOffset(source, lines, index, markerOffset) {
4357
4604
  prefixColumns
4358
4605
  };
4359
4606
  }
4360
- const feature$16 = { block: {
4607
+ const feature$15 = { block: {
4361
4608
  builds: [{
4362
- token: 72,
4609
+ token: 70,
4363
4610
  build(tokenStart, context) {
4364
4611
  const tokens = context.structure.tokens;
4365
4612
  const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
@@ -4377,33 +4624,30 @@ const feature$16 = { block: {
4377
4624
  }],
4378
4625
  starts: [{
4379
4626
  codes: [62],
4380
- unwrap(source, lines, index, contentOffset, target) {
4381
- const marker = blockQuoteOffset(source, lines, index, contentOffset);
4382
- if (!marker) return false;
4383
- target.resetFrom(lines, index, marker.offset, marker.prefixColumns);
4384
- return true;
4627
+ unwrap(source, lines, index, contentOffset) {
4628
+ const marker = blockQuoteOffset(source, contentOffset);
4629
+ if (marker) return lines.child(index, marker.offset, marker.prefixColumns);
4385
4630
  },
4386
4631
  interrupt() {
4387
4632
  return true;
4388
4633
  },
4389
4634
  start(source, lines, tokens, start, contentOffset, context) {
4390
- const firstMarker = blockQuoteOffset(source, lines, start, contentOffset);
4391
- const quoteLines = context.createLineView(lines, start, firstMarker.offset, firstMarker.prefixColumns);
4635
+ const firstMarker = blockQuoteOffset(source, contentOffset);
4636
+ const quoteLines = lines.child(start, firstMarker.offset, firstMarker.prefixColumns);
4392
4637
  let index = start + 1;
4393
- let lazyParagraph;
4394
4638
  for (; index < lines.length; index++) {
4395
- const markerOffset = lineIndentOffset(source, lines, index);
4396
- const marker = blockQuoteOffset(source, lines, index, markerOffset);
4397
- if (marker) {
4398
- quoteLines.pushFrom(lines, index, marker.offset, marker.prefixColumns);
4399
- lazyParagraph = void 0;
4400
- continue;
4639
+ if (!lines.lazy(index)) {
4640
+ const markerOffset = lineIndentOffset(source, lines, index);
4641
+ const marker = blockQuoteOffset(source, markerOffset);
4642
+ if (marker) {
4643
+ quoteLines.pushFrom(lines, index, marker.offset, marker.prefixColumns);
4644
+ continue;
4645
+ }
4646
+ if (markerOffset < 0 && isBlankLine(source, lines, index) || context.startsInterruptingBlock(source, lines, index, markerOffset) || !context.endsWithParagraphLeaf(source, quoteLines, quoteLines.length - 1)) break;
4401
4647
  }
4402
- lazyParagraph ??= context.endsWithParagraphLeaf(source, quoteLines, quoteLines.length - 1);
4403
- if (!lazyParagraph || markerOffset < 0 && isBlankLine(source, lines, index) || !lines.lazy(index) && context.startsInterruptingBlock(source, lines, index, markerOffset)) break;
4404
- quoteLines.pushLazy(lines, index);
4648
+ quoteLines.pushFrom(lines, index, void 0, void 0, true);
4405
4649
  }
4406
- tokens.push(72, contentOffset, contentOffset + 1);
4650
+ tokens.push(70, contentOffset, contentOffset + 1);
4407
4651
  context.scanBlockLines(source, quoteLines, tokens);
4408
4652
  const end = quoteLines.end(quoteLines.length - 1);
4409
4653
  tokens.push(192, end, end);
@@ -4415,60 +4659,91 @@ const feature$16 = { block: {
4415
4659
  function isThematicBreak(source, lines, index, contentOffset) {
4416
4660
  const marker = source.charCodeAt(contentOffset);
4417
4661
  if (marker !== 42 && marker !== 45 && marker !== 95) return false;
4418
- let count = 0;
4419
- for (let offset = contentOffset, end = lines.end(index); offset < end; offset++) {
4662
+ let count = 1;
4663
+ for (let offset = contentOffset + 1, end = lines.end(index); offset < end; offset++) {
4420
4664
  const code = source.charCodeAt(offset);
4421
4665
  if (code === marker) count++;
4422
4666
  else if (code !== 32 && code !== 9) return false;
4423
4667
  }
4424
4668
  return count >= 3;
4425
4669
  }
4426
- const feature$15 = {
4427
- block: { starts: [{
4428
- codes: [
4429
- 42,
4430
- 45,
4431
- 95
4432
- ],
4433
- interrupt(source, lines, index, contentOffset) {
4434
- return isThematicBreak(source, lines, index, contentOffset);
4435
- },
4436
- start(source, lines, tokens, start, contentOffset) {
4437
- if (!isThematicBreak(source, lines, start, contentOffset)) return;
4438
- return {
4439
- node: {
4440
- type: "thematicBreak",
4441
- position: void 0
4442
- },
4443
- nextLine: start + 1,
4444
- start: contentOffset,
4445
- end: lines.end(start)
4446
- };
4447
- }
4448
- }] },
4449
- inline: { builds: [{
4450
- kind: "leaf",
4451
- token: 128,
4452
- build(tokenIndex, position, context) {
4453
- const lineStart = inlineTokenData(context.tokens, tokenIndex);
4454
- return {
4455
- type: "break",
4456
- position: {
4457
- start: position.start,
4458
- end: context.locator.locationAt(context.view.mapOffset(lineStart - 1) + 1)
4459
- }
4460
- };
4461
- }
4462
- }] }
4463
- };
4670
+ function scanHardBreak(source, start, tokens) {
4671
+ let from = start;
4672
+ let before = source.charCodeAt(from - 1);
4673
+ while (before === 32) {
4674
+ from--;
4675
+ before = source.charCodeAt(from - 1);
4676
+ }
4677
+ if (before === 9 && start - from < 2) from = trimLineWhitespaceEnd(source, 0, from - 1);
4678
+ const end = findLineEndingEnd(source, start);
4679
+ appendInlineToken(tokens, 128, from, end);
4680
+ return end;
4681
+ }
4682
+ function scanLineEnding(source, start, tokens) {
4683
+ const next = findLineEndingEnd(source, start);
4684
+ if (source.charCodeAt(start - 1) === 32 && source.charCodeAt(start - 2) === 32) {
4685
+ let spaces = start - 2;
4686
+ while (source.charCodeAt(spaces - 1) === 32) spaces--;
4687
+ appendInlineToken(tokens, 128, spaces, next);
4688
+ } else appendInlineToken(tokens, 67, start, next);
4689
+ return next;
4690
+ }
4691
+ const blockStarts$2 = [{
4692
+ codes: [
4693
+ 42,
4694
+ 45,
4695
+ 95
4696
+ ],
4697
+ interrupt(source, lines, index, contentOffset) {
4698
+ return isThematicBreak(source, lines, index, contentOffset);
4699
+ },
4700
+ start(source, lines, tokens, start, contentOffset) {
4701
+ if (!isThematicBreak(source, lines, start, contentOffset)) return;
4702
+ return {
4703
+ node: {
4704
+ type: "thematicBreak",
4705
+ position: void 0
4706
+ },
4707
+ nextLine: start + 1,
4708
+ start: contentOffset,
4709
+ end: lines.end(start)
4710
+ };
4711
+ }
4712
+ }];
4713
+ const inlineBuilds$2 = [{
4714
+ kind: "leaf",
4715
+ token: 128,
4716
+ build(tokenIndex, position) {
4717
+ position.end.offset -= position.end.column - 1;
4718
+ position.end.column = 1;
4719
+ return {
4720
+ type: "break",
4721
+ position
4722
+ };
4723
+ }
4724
+ }];
4725
+ const feature$14 = (soft) => ({
4726
+ block: { starts: blockStarts$2 },
4727
+ inline: {
4728
+ scans: [{
4729
+ marker: 10,
4730
+ scan: soft ? scanHardBreak : scanLineEnding
4731
+ }, {
4732
+ marker: 13,
4733
+ scan: soft ? scanHardBreak : scanLineEnding
4734
+ }],
4735
+ builds: inlineBuilds$2
4736
+ }
4737
+ });
4464
4738
  // src/block/fence.ts
4465
4739
  function createFence(options) {
4466
4740
  const { marker, minimumLength, allowMarkerInInfo, normalizeEndings, build } = options;
4467
4741
  const escaped = "\\u" + marker.toString(16).padStart(4, "0");
4468
- const group = `(?:${escaped.repeat(minimumLength)})`;
4469
- const remains = `${escaped}{0,${minimumLength - 1}}`;
4470
- const opening = new RegExp(`${group}+${remains + (allowMarkerInInfo ? "" : `(?=[^${escaped}\\r\\n]*(?:\\r|\\n|$))`)}`, "y");
4471
- const closing = new RegExp(`${group}*${remains}[ \t]*`, "y");
4742
+ const minimum = escaped.repeat(minimumLength);
4743
+ const remaining = `(?:${minimum})*${escaped}{0,${minimumLength - 1}}`;
4744
+ const checkInfo = allowMarkerInInfo ? "" : `(?=[^${escaped}\\r\\n]*(?:\\r|\\n|$))`;
4745
+ const opening = new RegExp(minimum + remaining + checkInfo, "y");
4746
+ const closing = new RegExp(remaining + "[ ]*", "y");
4472
4747
  function openingEnd(source, offset) {
4473
4748
  opening.lastIndex = offset;
4474
4749
  return opening.test(source) ? opening.lastIndex : -1;
@@ -4486,10 +4761,9 @@ function createFence(options) {
4486
4761
  const markerEnd = openingEnd(source, offset);
4487
4762
  if (markerEnd < 0) return;
4488
4763
  const close = findFenceClose(source, lines, start, lines.length, marker, markerEnd - offset, closing);
4489
- const indent = lines.prefixColumns(start) + offset - lines.start(start);
4490
4764
  const closed = close < lines.length;
4491
4765
  const last = closed ? close : close - 1;
4492
- const content = lines.physicallyContiguous() ? physicalFenceContent(source, lines, start, close - 1, indent, normalizeEndings) : logicalFenceContent(source, lines, start, close - 1, closed, indent, normalizeEndings);
4766
+ const content = lines.physicallyContiguous() ? physicalFenceContent(source, lines, start, close - 1, offset - lines.start(start), normalizeEndings) : logicalFenceContent(source, lines, start, close - 1, closed, lineIndentColumns(source, lines, start, offset), normalizeEndings);
4493
4767
  const lineEnd = lines.end(start);
4494
4768
  const infoStart = trimLineWhitespaceStart(source, markerEnd, lineEnd);
4495
4769
  return {
@@ -4511,7 +4785,11 @@ function findFenceClose(source, lines, open, limit, marker, length, closing) {
4511
4785
  const lastStart = Math.min(end - length, offset + 3 - lines.prefixColumns(line));
4512
4786
  while (first === 32 && offset < lastStart) first = source.charCodeAt(++offset);
4513
4787
  }
4514
- if (first !== marker) continue;
4788
+ if (first !== marker) {
4789
+ if (first !== 9 || lines.physicallyContiguous()) continue;
4790
+ offset = lineIndentOffset(source, lines, line);
4791
+ if (offset < 0 || end - offset < length || source.charCodeAt(offset) !== marker) continue;
4792
+ }
4515
4793
  if (source.charCodeAt(offset + length - 1) !== marker) continue;
4516
4794
  closing.lastIndex = offset + 1;
4517
4795
  if (closing.test(source) && closing.lastIndex === end) return line;
@@ -4540,17 +4818,8 @@ function logicalFenceContent(source, lines, start, last, closed, indent, normali
4540
4818
  if (!closed && last > start && lines.start(last) === lines.next(last) && !lines.prefixColumns(last)) last--;
4541
4819
  const parts = [];
4542
4820
  for (let index = start + 1; index <= last; index++) {
4543
- let offset = lines.start(index);
4544
- const lineEnd = lines.end(index);
4545
- let columns = lines.prefixColumns(index);
4546
- while (offset < lineEnd && columns < indent) {
4547
- const code = source.charCodeAt(offset);
4548
- if (code === 32) columns++;
4549
- else if (code === 9) columns += 4 - physicalColumnAt(source, offset) % 4;
4550
- else break;
4551
- offset++;
4552
- }
4553
- parts.push(" ".repeat(Math.max(0, columns - indent)) + source.slice(offset, normalizeEndings || index >= last ? lineEnd : lines.next(index)));
4821
+ const end = normalizeEndings || index >= last ? lines.end(index) : lines.next(index);
4822
+ parts.push(dedentLine(source, lines, index, indent, end));
4554
4823
  }
4555
4824
  return parts.join(normalizeEndings ? "\n" : "");
4556
4825
  }
@@ -4594,24 +4863,7 @@ function codeFenceClosingLine(source, lines, open, limit, offset) {
4594
4863
  if (marker === 96) return backtickFence.closingLineAt(source, lines, open, limit, offset);
4595
4864
  if (marker === 126) return tildeFence.closingLineAt(source, lines, open, limit, offset);
4596
4865
  }
4597
- function indentedContent(source, lines, start, end) {
4598
- const parts = [];
4599
- for (let index = start; index < end; index++) {
4600
- let offset = lines.start(index);
4601
- let columns = lines.prefixColumns(index);
4602
- const lineEnd = lines.end(index);
4603
- while (offset < lineEnd && columns < 4) {
4604
- const code = source.charCodeAt(offset);
4605
- if (code === 32) columns++;
4606
- else if (code === 9) columns += Math.max(4 - columns, 4 - physicalColumnAt(source, offset) % 4);
4607
- else break;
4608
- offset++;
4609
- }
4610
- parts.push(" ".repeat(Math.max(0, columns - 4)) + source.slice(offset, lineEnd));
4611
- }
4612
- return parts.join("\n");
4613
- }
4614
- const feature$14 = {
4866
+ const feature$13 = {
4615
4867
  block: { starts: [
4616
4868
  {
4617
4869
  codes: [-1],
@@ -4619,12 +4871,14 @@ const feature$14 = {
4619
4871
  let end = start + 1;
4620
4872
  while (end < lines.length && lineIndentOffset(source, lines, end) < 0) end++;
4621
4873
  while (end > start + 1 && isBlankLine(source, lines, end - 1)) end--;
4874
+ const parts = [];
4875
+ for (let index = start; index < end; index++) parts.push(dedentLine(source, lines, index, 4, lines.end(index)));
4622
4876
  return {
4623
4877
  node: {
4624
4878
  type: "code",
4625
4879
  lang: null,
4626
4880
  meta: null,
4627
- value: indentedContent(source, lines, start, end),
4881
+ value: parts.join("\n"),
4628
4882
  position: void 0
4629
4883
  },
4630
4884
  nextLine: end,
@@ -4641,7 +4895,6 @@ const feature$14 = {
4641
4895
  marker: 96,
4642
4896
  scan(source, start, tokens) {
4643
4897
  const openEnd = findMarkerRunEnd(source, start, 96);
4644
- if (source.charCodeAt(start - 1) === 96) return openEnd;
4645
4898
  const markerLength = openEnd - start;
4646
4899
  const end = findClosingRunEnd(source, openEnd, markerLength, "`", 96);
4647
4900
  if (end < 0) return openEnd;
@@ -4785,7 +5038,8 @@ function buildFragment(context, cursor, closer) {
4785
5038
  const startOffset = inlineTokenStart(tokens, index);
4786
5039
  if (cursor < startOffset) {
4787
5040
  const node = buildLiteral(context, cursor, startOffset);
4788
- children?.push(node) ?? (children = [node]);
5041
+ children ??= [];
5042
+ children.push(node);
4789
5043
  }
4790
5044
  const sourceStart = context.view.mapOffset(startOffset);
4791
5045
  const start = context.locator.locationAt(sourceStart);
@@ -4818,24 +5072,44 @@ function buildFragment(context, cursor, closer) {
4818
5072
  }
4819
5073
  return output ?? { children: children ??= [] };
4820
5074
  }
4821
- function buildInlineFragment(tokenStart, tableCell, context) {
5075
+ function buildLiteralChunk(context, chunkStart, chunkEnd, text) {
5076
+ const trimmedEnd = trimLineWhitespaceEnd(context.source, chunkStart, chunkEnd);
5077
+ return { children: trimmedEnd <= chunkStart ? [] : [{
5078
+ type: "text",
5079
+ value: trimmedEnd === chunkEnd ? text : context.source.slice(chunkStart, trimmedEnd),
5080
+ position: context.locator.positionAt(chunkStart, trimmedEnd)
5081
+ }] };
5082
+ }
5083
+ function buildInlineFragment(tokenStart, context) {
4822
5084
  let tokens;
4823
5085
  let view;
4824
5086
  if (context.cursor) {
4825
5087
  const region = context.cursor.take(tokenStart);
4826
5088
  if (!region) return { children: [] };
4827
5089
  tokens = region.tokens;
4828
- view = region.view;
5090
+ view = region;
5091
+ if (tokens.length === 0 && typeof view.mappings === "number") return buildLiteralChunk(context, view.mappings, view.mappings + view.text.length, view.text);
4829
5092
  } else {
4830
5093
  const blockTokens = context.structure.tokens;
4831
5094
  const nodeLength = blockTokens.nodeLength(tokenStart);
4832
5095
  if (nodeLength <= 1 || blockTokens.kind(tokenStart + 1) !== 1) return { children: [] };
4833
- view = context.view;
4834
- view.reset(context.source, blockTokens, tokenStart, nodeLength);
4835
- tokens = view.tokenize(context.profile.tokenize, true);
5096
+ const hasPaddedLineFeed = blockTokens.value(tokenStart) === paddedLineFeed;
5097
+ if (nodeLength === 2) {
5098
+ const chunkStart = blockTokens.start(tokenStart + 1);
5099
+ const chunkEnd = blockTokens.end(tokenStart + 1);
5100
+ const text = context.source.slice(chunkStart, chunkEnd);
5101
+ tokens = context.profile.tokenize(text, hasPaddedLineFeed, hasPaddedLineFeed ? void 0 : chunkStart);
5102
+ if (tokens.length === 0) return buildLiteralChunk(context, chunkStart, chunkEnd, text);
5103
+ view = context.view;
5104
+ view.mappings = chunkStart;
5105
+ view.text = text;
5106
+ } else {
5107
+ view = context.view;
5108
+ view.reset(context.source, blockTokens, tokenStart, nodeLength);
5109
+ tokens = context.profile.tokenize(view.text, hasPaddedLineFeed, hasPaddedLineFeed ? void 0 : view.mappings);
5110
+ }
4836
5111
  if (tokens.length) tokens = context.profile.resolve(view.text, tokens, blockTokens, true);
4837
5112
  }
4838
- context.tableCell = tableCell;
4839
5113
  context.tokens = tokens;
4840
5114
  context.view = view;
4841
5115
  if (tokens.length === 0) {
@@ -4863,7 +5137,7 @@ const buildYamlBlock = (tokenStart, context) => {
4863
5137
  position: context.locator.positionAt(tokens.start(tokenStart), tokens.end(tokenStart))
4864
5138
  };
4865
5139
  };
4866
- function feature$13(marker) {
5140
+ function feature$12(marker) {
4867
5141
  const code = marker.charCodeAt(0);
4868
5142
  return { block: {
4869
5143
  builds: [{
@@ -4895,24 +5169,24 @@ function componentNameEnd(source, start, block) {
4895
5169
  }
4896
5170
  return end;
4897
5171
  }
5172
+ function isNameSeparator(code) {
5173
+ return code === 45 || code === 95 || code === 46;
5174
+ }
4898
5175
  function normalizeComponentName(value) {
4899
5176
  let result = "";
4900
- let partLength = 0;
4901
- let previousUpper;
4902
- for (let index = 0; index < value.length; index++) {
4903
- const character = value[index];
4904
- const code = value.charCodeAt(index);
4905
- if (code === 45 || code === 95 || code === 47 || code === 46) {
4906
- partLength = 0;
4907
- previousUpper = void 0;
5177
+ for (let index = 0; index < value.length;) {
5178
+ if (isNameSeparator(value.charCodeAt(index))) {
5179
+ index++;
4908
5180
  continue;
4909
5181
  }
4910
- const upper = isAsciiLetter(code) ? code <= 90 : void 0;
4911
- const next = value.charCodeAt(index + 1);
4912
- if (result && (partLength === 0 || upper === true && (previousUpper === false || next >= 97 && next <= 122))) result += "-";
4913
- result += character;
4914
- partLength++;
4915
- previousUpper = upper;
5182
+ const start = index++;
5183
+ while (index < value.length) {
5184
+ const code = value.charCodeAt(index);
5185
+ if (isNameSeparator(code) || isUppercaseLetter(code) && (isLowercaseLetter(value.charCodeAt(index - 1)) || isLowercaseLetter(value.charCodeAt(index + 1)))) break;
5186
+ index++;
5187
+ }
5188
+ if (result) result += "-";
5189
+ result += value.slice(start, index);
4916
5190
  }
4917
5191
  return result.toLowerCase();
4918
5192
  }
@@ -4940,39 +5214,29 @@ function yamlPropsEnd(source, lines, start, end) {
4940
5214
  if (source.endsWith(marker, lineEnd) && lineIndentOffset(source, lines, index) === lineEnd - marker.length) return index + 1;
4941
5215
  }
4942
5216
  }
5217
+ function openingAttributesEnd(source, start, lineEnd) {
5218
+ if (start === lineEnd) return start;
5219
+ const end = attributesEnd(source, start, lineEnd);
5220
+ if (end !== void 0 && trimLineWhitespaceStart(source, end, lineEnd) === lineEnd) return end;
5221
+ }
4943
5222
  function slotOpeningAt(source, lines, index, contentOffset) {
4944
5223
  if (source.charCodeAt(contentOffset) !== 35) return;
4945
- const code = source.charCodeAt(contentOffset + 1);
4946
- if (code === 35 || code === 32 || code === 9) return;
4947
5224
  const nameEnd = componentNameEnd(source, contentOffset + 1, true);
5225
+ if (nameEnd === void 0) return;
4948
5226
  const lineEnd = lines.end(index);
4949
- if (nameEnd === void 0 || nameEnd > lineEnd) return;
4950
- let offset = trimLineWhitespaceStart(source, nameEnd, lineEnd);
4951
- let attributesStart;
4952
- let attributesEndValue;
4953
- if (source.charCodeAt(offset) === 123) {
4954
- attributesStart = offset;
4955
- attributesEndValue = attributesEnd(source, offset, lineEnd);
4956
- if (attributesEndValue === void 0) return;
4957
- offset = attributesEndValue;
4958
- }
4959
- if (trimLineWhitespaceStart(source, offset, lineEnd) !== lineEnd) return;
5227
+ const offset = trimLineWhitespaceStart(source, nameEnd, lineEnd);
5228
+ const end = openingAttributesEnd(source, offset, lineEnd);
5229
+ if (end === void 0) return;
4960
5230
  return {
4961
- attributesEnd: attributesEndValue,
4962
- attributesStart,
5231
+ attributesEnd: end,
5232
+ attributesStart: offset,
4963
5233
  nameEnd,
4964
5234
  offset: contentOffset
4965
5235
  };
4966
5236
  }
4967
- function blockOpeningAt(source, lines, index, contentOffset, shorthand) {
4968
- let offset = contentOffset;
4969
- while (source.charCodeAt(offset) === 58) offset++;
4970
- const fenceSize = offset - contentOffset;
4971
- if (shorthand ? fenceSize !== 1 : fenceSize < 2) return;
4972
- const lineEnd = lines.end(index);
4973
- if (!shorthand) offset = trimLineWhitespaceStart(source, offset, lineEnd);
5237
+ function componentHeaderAt(source, offset, lineEnd, fenceSize) {
4974
5238
  const nameEnd = componentNameEnd(source, offset, true);
4975
- if (nameEnd === void 0 || nameEnd > lineEnd) return;
5239
+ if (nameEnd === void 0) return;
4976
5240
  offset = trimLineWhitespaceStart(source, nameEnd, lineEnd);
4977
5241
  let labelStart;
4978
5242
  let labelEnd;
@@ -4983,24 +5247,26 @@ function blockOpeningAt(source, lines, index, contentOffset, shorthand) {
4983
5247
  labelEnd = close + 1;
4984
5248
  offset = trimLineWhitespaceStart(source, labelEnd, lineEnd);
4985
5249
  }
4986
- let attributesStart;
4987
- let attributesEndValue;
4988
- if (source.charCodeAt(offset) === 123) {
4989
- attributesStart = offset;
4990
- attributesEndValue = attributesEnd(source, offset, lineEnd);
4991
- if (attributesEndValue === void 0) return;
4992
- offset = attributesEndValue;
4993
- }
4994
- if (trimLineWhitespaceStart(source, offset, lineEnd) !== lineEnd) return;
5250
+ const end = openingAttributesEnd(source, offset, lineEnd);
5251
+ if (end === void 0) return;
4995
5252
  return {
4996
- attributesEnd: attributesEndValue,
4997
- attributesStart,
5253
+ attributesEnd: end,
5254
+ attributesStart: offset,
4998
5255
  fenceSize,
4999
5256
  labelEnd,
5000
5257
  labelStart,
5001
5258
  nameEnd
5002
5259
  };
5003
5260
  }
5261
+ function blockOpeningAt(source, lines, index, contentOffset, allowShorthand) {
5262
+ let offset = contentOffset;
5263
+ while (source.charCodeAt(offset) === 58) offset++;
5264
+ const fenceSize = offset - contentOffset;
5265
+ if (fenceSize === 0 || !allowShorthand && fenceSize < 2) return;
5266
+ const lineEnd = lines.end(index);
5267
+ if (fenceSize >= 2) offset = trimLineWhitespaceStart(source, offset, lineEnd);
5268
+ return componentHeaderAt(source, offset, lineEnd, fenceSize);
5269
+ }
5004
5270
  function blockClose(source, lines, start, end, opening) {
5005
5271
  let depth = 0;
5006
5272
  for (let index = start + 1; index < end; index++) {
@@ -5013,15 +5279,16 @@ function blockClose(source, lines, start, end, opening) {
5013
5279
  }
5014
5280
  let markerEnd = contentOffset;
5015
5281
  while (source.charCodeAt(markerEnd) === 58) markerEnd++;
5016
- if (markerEnd - contentOffset === opening.fenceSize && trimLineWhitespaceStart(source, markerEnd, lines.end(index)) === lines.end(index)) {
5282
+ if (markerEnd - contentOffset !== opening.fenceSize) continue;
5283
+ const lineEnd = lines.end(index);
5284
+ const nameStart = trimLineWhitespaceStart(source, markerEnd, lineEnd);
5285
+ if (nameStart === lineEnd) {
5017
5286
  if (depth === 0) return {
5018
5287
  index,
5019
5288
  offset: contentOffset
5020
5289
  };
5021
5290
  depth--;
5022
- continue;
5023
- }
5024
- if (blockOpeningAt(source, lines, index, contentOffset, false)?.fenceSize === opening.fenceSize) depth++;
5291
+ } else if (componentHeaderAt(source, nameStart, lineEnd, opening.fenceSize)) depth++;
5025
5292
  }
5026
5293
  }
5027
5294
  function nextSlot(source, lines, start, end) {
@@ -5042,6 +5309,9 @@ function nextSlot(source, lines, start, end) {
5042
5309
  if (nested) index = blockClose(source, lines, index, end, nested)?.index ?? index;
5043
5310
  }
5044
5311
  }
5312
+ function emitOpeningAttributes(tokens, opening) {
5313
+ if (opening.attributesStart < opening.attributesEnd) tokens.push(2, opening.attributesStart, opening.attributesEnd);
5314
+ }
5045
5315
  function emitComponentBody(source, lines, tokens, start, end, closingOffset, context) {
5046
5316
  let cursor = start;
5047
5317
  const yamlEnd = cursor < end ? yamlPropsEnd(source, lines, cursor, end) : void 0;
@@ -5059,8 +5329,8 @@ function emitComponentBody(source, lines, tokens, start, end, closingOffset, con
5059
5329
  const following = nextSlot(source, lines, slot.index + 1, end);
5060
5330
  const next = following?.index ?? end;
5061
5331
  const opening = slot.opening;
5062
- tokens.push(84, opening.offset, opening.nameEnd);
5063
- if (opening.attributesStart !== void 0 && opening.attributesEnd !== void 0) tokens.push(2, opening.attributesStart, opening.attributesEnd);
5332
+ tokens.push(82, opening.offset, opening.nameEnd);
5333
+ emitOpeningAttributes(tokens, opening);
5064
5334
  context.scanBlockLines(source, lines.view(slot.index + 1, next), tokens);
5065
5335
  const slotEnd = following ? following.opening.offset : closingOffset;
5066
5336
  tokens.push(201, slotEnd, slotEnd);
@@ -5068,46 +5338,25 @@ function emitComponentBody(source, lines, tokens, start, end, closingOffset, con
5068
5338
  }
5069
5339
  }
5070
5340
  function emitOpening(tokens, contentOffset, opening) {
5071
- tokens.push(83, contentOffset, opening.nameEnd);
5072
- if (opening.labelStart !== void 0 && opening.labelEnd !== void 0) tokens.pushInlineFrame(85, opening.labelStart, opening.labelEnd, opening.labelStart + 1, opening.labelEnd - 1);
5073
- if (opening.attributesStart !== void 0 && opening.attributesEnd !== void 0) tokens.push(2, opening.attributesStart, opening.attributesEnd);
5341
+ tokens.push(81, contentOffset, opening.nameEnd);
5342
+ if (opening.labelStart !== void 0 && opening.labelEnd !== void 0) tokens.pushInlineFrame(83, opening.labelStart, opening.labelEnd, opening.labelStart + 1, opening.labelEnd - 1);
5343
+ emitOpeningAttributes(tokens, opening);
5074
5344
  }
5075
5345
  const buildBlockLabel = (tokenStart, context) => {
5076
5346
  const tokens = context.structure.tokens;
5077
5347
  const start = context.locator.locationAt(tokens.start(tokenStart));
5078
5348
  return {
5079
5349
  type: "paragraph",
5080
- children: buildInlineFragment(tokenStart, false, context).children,
5350
+ children: buildInlineFragment(tokenStart, context).children,
5081
5351
  position: {
5082
5352
  start,
5083
5353
  end: context.locator.locationAt(tokens.end(tokenStart))
5084
5354
  }
5085
5355
  };
5086
5356
  };
5087
- function createBlockStart(shorthand) {
5088
- return (source, lines, tokens, start, contentOffset, context) => {
5089
- const opening = blockOpeningAt(source, lines, start, contentOffset, shorthand);
5090
- if (!opening) return;
5091
- if (shorthand) {
5092
- emitOpening(tokens, contentOffset, opening);
5093
- const lineEnd = lines.end(start);
5094
- tokens.push(200, lineEnd, lineEnd);
5095
- return start + 1;
5096
- }
5097
- const closing = blockClose(source, lines, start, lines.length, opening);
5098
- if (!closing) {
5099
- context.retainLookahead(lines.next(lines.length - 1));
5100
- return;
5101
- }
5102
- emitOpening(tokens, contentOffset, opening);
5103
- emitComponentBody(source, lines, tokens, start + 1, closing.index, closing.offset, context);
5104
- tokens.push(200, closing.offset, lines.end(closing.index));
5105
- return closing.index + 1;
5106
- };
5107
- }
5108
- const blockBuilds$1 = [
5357
+ const blockBuilds = [
5109
5358
  {
5110
- token: 85,
5359
+ token: 83,
5111
5360
  build: buildBlockLabel
5112
5361
  },
5113
5362
  {
@@ -5115,17 +5364,14 @@ const blockBuilds$1 = [
5115
5364
  build: buildYamlBlock
5116
5365
  },
5117
5366
  {
5118
- token: 83,
5367
+ token: 81,
5119
5368
  build(tokenStart, context) {
5120
5369
  const tokens = context.structure.tokens;
5121
5370
  const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
5122
5371
  let openingToken = tokenStart + 1;
5123
5372
  let parsed;
5124
- if (tokens.kind(openingToken) === 85) openingToken += tokens.nodeLength(openingToken);
5125
- if (tokens.kind(openingToken) === 2) {
5126
- parsed = parseAttributes(context.source, tokens.start(openingToken));
5127
- openingToken++;
5128
- }
5373
+ if (tokens.kind(openingToken) === 83) openingToken += tokens.nodeLength(openingToken);
5374
+ if (tokens.kind(openingToken) === 2) parsed = parseAttributes(context.source, tokens.start(openingToken));
5129
5375
  const nameEnd = tokens.end(tokenStart);
5130
5376
  const start = context.locator.locationAt(tokens.start(tokenStart));
5131
5377
  const children = buildBlockChildren(tokenStart, context);
@@ -5133,47 +5379,63 @@ const blockBuilds$1 = [
5133
5379
  type: "blockComponent",
5134
5380
  name: normalizeComponentName(context.source.slice(context.source.lastIndexOf(":", nameEnd) + 1, nameEnd).trim()),
5135
5381
  attributes: parsed ?? {},
5382
+ children,
5136
5383
  position: {
5137
5384
  start,
5138
5385
  end: context.locator.locationAt(tokens.end(close))
5139
- },
5140
- children
5386
+ }
5141
5387
  };
5142
5388
  }
5143
5389
  },
5144
5390
  {
5145
- token: 84,
5391
+ token: 82,
5146
5392
  build(tokenStart, context) {
5147
5393
  const tokens = context.structure.tokens;
5148
5394
  const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
5149
5395
  const attributesToken = tokens.kind(tokenStart + 1) === 2 ? tokenStart + 1 : void 0;
5150
5396
  const parsed = attributesToken !== void 0 ? parseAttributes(context.source, tokens.start(attributesToken)) : void 0;
5151
5397
  const start = context.locator.locationAt(tokens.start(tokenStart));
5398
+ const children = buildBlockChildren(tokenStart, context);
5152
5399
  return {
5153
5400
  type: "blockComponent",
5154
5401
  name: "template",
5155
- children: buildBlockChildren(tokenStart, context),
5402
+ attributes: {
5403
+ ...parsed,
5404
+ name: normalizeComponentName(context.source.slice(tokens.start(tokenStart) + 1, tokens.end(tokenStart)))
5405
+ },
5406
+ children,
5156
5407
  position: {
5157
5408
  start,
5158
5409
  end: context.locator.locationAt(tokens.end(close))
5159
- },
5160
- attributes: {
5161
- ...parsed,
5162
- name: normalizeComponentName(context.source.slice(tokens.start(tokenStart) + 1, tokens.end(tokenStart)).trim())
5163
5410
  }
5164
5411
  };
5165
5412
  }
5166
5413
  }
5167
5414
  ];
5168
- const blockStarts$2 = [{
5415
+ const blockStarts$1 = [{
5169
5416
  codes: [58],
5170
5417
  interrupt(source, lines, index, contentOffset) {
5171
- return blockOpeningAt(source, lines, index, contentOffset, false) !== void 0 || blockOpeningAt(source, lines, index, contentOffset, true) !== void 0;
5418
+ return blockOpeningAt(source, lines, index, contentOffset, true) !== void 0;
5172
5419
  },
5173
- start: createBlockStart(false)
5174
- }, {
5175
- codes: [58],
5176
- start: createBlockStart(true)
5420
+ start(source, lines, tokens, start, contentOffset, context) {
5421
+ const opening = blockOpeningAt(source, lines, start, contentOffset, true);
5422
+ if (!opening) return;
5423
+ if (opening.fenceSize === 1) {
5424
+ emitOpening(tokens, contentOffset, opening);
5425
+ const lineEnd = lines.end(start);
5426
+ tokens.push(200, lineEnd, lineEnd);
5427
+ return start + 1;
5428
+ }
5429
+ const closing = blockClose(source, lines, start, lines.length, opening);
5430
+ if (!closing) {
5431
+ context.retainLookahead(lines.next(lines.length - 1));
5432
+ return;
5433
+ }
5434
+ emitOpening(tokens, contentOffset, opening);
5435
+ emitComponentBody(source, lines, tokens, start + 1, closing.index, closing.offset, context);
5436
+ tokens.push(200, closing.offset, lines.end(closing.index));
5437
+ return closing.index + 1;
5438
+ }
5177
5439
  }];
5178
5440
  // src/profile/features/component/inline.ts
5179
5441
  const inlineScans = [{
@@ -5196,10 +5458,10 @@ const buildInlineComponent = (open, close, position, children, context) => {
5196
5458
  };
5197
5459
  };
5198
5460
  // src/profile/features/component/index.ts
5199
- const feature$12 = {
5461
+ const feature$11 = {
5200
5462
  block: {
5201
- builds: blockBuilds$1,
5202
- starts: blockStarts$2
5463
+ builds: blockBuilds,
5464
+ starts: blockStarts$1
5203
5465
  },
5204
5466
  inline: {
5205
5467
  scans: inlineScans,
@@ -5236,14 +5498,15 @@ const feature$12 = {
5236
5498
  function isShortcodeCharacter(code) {
5237
5499
  return isAsciiLetter(code) || isAsciiDigit(code) || code === 43 || code === 45 || code === 95;
5238
5500
  }
5239
- const feature$11 = { inline: {
5501
+ const feature$10 = { inline: {
5240
5502
  scans: [{
5241
5503
  marker: 58,
5242
5504
  scan(source, start, tokens) {
5243
5505
  if (!hasInlineColonBoundary(source, start)) return -1;
5244
5506
  let end = start + 1;
5245
- while (isShortcodeCharacter(source.charCodeAt(end))) end++;
5246
- if (end === start + 1 || source.charCodeAt(end) !== 58) return -1;
5507
+ let code = source.charCodeAt(end);
5508
+ while (isShortcodeCharacter(code)) code = source.charCodeAt(++end);
5509
+ if (end === start + 1 || code !== 58) return -1;
5247
5510
  end++;
5248
5511
  appendInlineToken(tokens, 135, start, end);
5249
5512
  return end;
@@ -5261,103 +5524,6 @@ const feature$11 = { inline: {
5261
5524
  }
5262
5525
  }]
5263
5526
  } };
5264
- // src/profile/features/footnote/block.ts
5265
- function definitionMarkerEnd(source, start, lineEnd) {
5266
- const end = footnoteLabelEnd(source, start, lineEnd);
5267
- if (end !== void 0 && source.charCodeAt(end) === 58) return end + 1;
5268
- }
5269
- // src/profile/features/footnote/index.ts
5270
- const feature$10 = {
5271
- block: {
5272
- builds: [{
5273
- token: 78,
5274
- build(tokenStart, context) {
5275
- const tokens = context.structure.tokens;
5276
- const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
5277
- const start = context.locator.locationAt(tokens.start(tokenStart));
5278
- const children = buildBlockChildren(tokenStart, context);
5279
- const fields = tokens.value(tokenStart);
5280
- return {
5281
- type: "footnoteDefinition",
5282
- identifier: fields.normalizedLabel,
5283
- label: decodeString(fields.label),
5284
- children,
5285
- position: {
5286
- start,
5287
- end: context.locator.locationAt(tokens.end(close))
5288
- }
5289
- };
5290
- }
5291
- }],
5292
- starts: [{
5293
- codes: [91],
5294
- unwrap(source, lines, index, contentOffset, target) {
5295
- const lineEnd = lines.end(index);
5296
- const markerEnd = definitionMarkerEnd(source, contentOffset, lineEnd);
5297
- if (markerEnd === void 0) return false;
5298
- target.resetFrom(lines, index, trimLineWhitespaceStart(source, markerEnd, lineEnd), 0);
5299
- return true;
5300
- },
5301
- interrupt(source, lines, index, contentOffset) {
5302
- return definitionMarkerEnd(source, contentOffset, lines.end(index)) !== void 0;
5303
- },
5304
- start(source, lines, tokens, start, contentOffset, context) {
5305
- const lineEnd = lines.end(start);
5306
- const markerEnd = definitionMarkerEnd(source, contentOffset, lineEnd);
5307
- if (markerEnd === void 0) return;
5308
- const bodyStart = trimLineWhitespaceStart(source, markerEnd, lineEnd);
5309
- const definitionLines = context.createLineView(lines, start, bodyStart, 0);
5310
- let index = start + 1;
5311
- let lazyParagraph;
5312
- while (index < lines.length) {
5313
- const line = definitionLines.pushAfterColumns(source, lines, index, 4);
5314
- if (line === -2) {
5315
- definitionLines.pushFrom(lines, index);
5316
- lazyParagraph = false;
5317
- index++;
5318
- continue;
5319
- }
5320
- if (line === -3) {
5321
- lazyParagraph = void 0;
5322
- index++;
5323
- continue;
5324
- }
5325
- lazyParagraph ??= context.endsWithParagraphLeaf(source, definitionLines, definitionLines.length - 1);
5326
- if (!lazyParagraph || !lines.lazy(index) && context.startsInterruptingBlock(source, lines, index, line)) break;
5327
- definitionLines.pushLazy(lines, index);
5328
- index++;
5329
- }
5330
- while (definitionLines.length > 0 && isBlankLine(source, definitionLines, definitionLines.length - 1)) definitionLines.pop();
5331
- const label = source.slice(contentOffset + 2, markerEnd - 2);
5332
- const normalizedLabel = normalizeIdentifier(label);
5333
- tokens.push(78, contentOffset, markerEnd, {
5334
- definitionKey: footnoteDefinitionPrefix + normalizedLabel,
5335
- value: {
5336
- label,
5337
- normalizedLabel
5338
- }
5339
- });
5340
- context.scanBlockLines(source, definitionLines, tokens);
5341
- const end = definitionLines.length > 0 ? definitionLines.next(definitionLines.length - 1) : markerEnd;
5342
- tokens.push(197, end, end);
5343
- return index;
5344
- }
5345
- }]
5346
- },
5347
- inline: { builds: [{
5348
- kind: "leaf",
5349
- token: 132,
5350
- build(tokenIndex, position, context) {
5351
- const label = inlineTokenText(context.view.text, context.tokens, tokenIndex, 2, 1);
5352
- return {
5353
- type: "footnoteReference",
5354
- identifier: normalizeIdentifier(label),
5355
- label: decodeString(label),
5356
- position
5357
- };
5358
- }
5359
- }] }
5360
- };
5361
5527
  // src/profile/features/formatting.ts
5362
5528
  function createFormatting(type, marker, buildToken, delimiter) {
5363
5529
  return {
@@ -5383,14 +5549,8 @@ function createFormatting(type, marker, buildToken, delimiter) {
5383
5549
  }
5384
5550
  const formattingAsterisk = createFormatting("emphasis", 42, 192, {
5385
5551
  token: 1,
5386
- single: {
5387
- open: 192,
5388
- close: 193
5389
- },
5390
- double: {
5391
- open: 194,
5392
- close: 195
5393
- },
5552
+ single: 192,
5553
+ double: 194,
5394
5554
  pairing: {
5395
5555
  kind: "partial",
5396
5556
  ruleOfThree: true
@@ -5398,14 +5558,8 @@ const formattingAsterisk = createFormatting("emphasis", 42, 192, {
5398
5558
  });
5399
5559
  const formattingUnderscore = createFormatting("strong", 95, 194, {
5400
5560
  token: 2,
5401
- single: {
5402
- open: 192,
5403
- close: 193
5404
- },
5405
- double: {
5406
- open: 194,
5407
- close: 195
5408
- },
5561
+ single: 192,
5562
+ double: 194,
5409
5563
  pairing: {
5410
5564
  kind: "partial",
5411
5565
  ruleOfThree: true
@@ -5414,10 +5568,7 @@ const formattingUnderscore = createFormatting("strong", 95, 194, {
5414
5568
  });
5415
5569
  const formattingStrikethrough = createFormatting("delete", 126, 196, {
5416
5570
  token: 3,
5417
- double: {
5418
- open: 196,
5419
- close: 197
5420
- },
5571
+ double: 196,
5421
5572
  pairing: { kind: "whole" }
5422
5573
  });
5423
5574
  function feature$9(strikethroughOptions) {
@@ -5441,39 +5592,18 @@ function feature$9(strikethroughOptions) {
5441
5592
  // src/profile/features/heading.ts
5442
5593
  function atxMarkerEnd(source, markerOffset, lineEnd) {
5443
5594
  let markerEnd = markerOffset + 1;
5444
- while (markerEnd < lineEnd && markerEnd - markerOffset < 6 && source.charCodeAt(markerEnd) === 35) markerEnd++;
5445
- if (markerEnd < lineEnd) {
5446
- const code = source.charCodeAt(markerEnd);
5447
- if (code !== 32 && code !== 9) return 0;
5448
- }
5449
- return markerEnd;
5595
+ let code = source.charCodeAt(markerEnd);
5596
+ while (markerEnd < lineEnd && markerEnd - markerOffset < 6 && code === 35) code = source.charCodeAt(++markerEnd);
5597
+ return markerEnd < lineEnd && code !== 32 && code !== 9 ? 0 : markerEnd;
5450
5598
  }
5451
5599
  function setextMarkerAt(source, marker, markerOffset, lineEnd) {
5452
5600
  if (marker !== 61 && marker !== 45) return;
5453
5601
  let offset = markerOffset + 1;
5454
- while (offset < lineEnd && source.charCodeAt(offset) === marker) offset++;
5455
- offset = trimLineWhitespaceStart(source, offset, lineEnd);
5602
+ let code = source.charCodeAt(offset);
5603
+ while (offset < lineEnd && code === marker) code = source.charCodeAt(++offset);
5604
+ if (code === 32 || code === 9) offset = trimLineWhitespaceStart(source, offset + 1, lineEnd);
5456
5605
  return offset === lineEnd ? marker : void 0;
5457
5606
  }
5458
- function createBuildHeading(openKind) {
5459
- return (tokenStart, context) => {
5460
- const tokens = context.structure.tokens;
5461
- const kind = tokens.kind(tokenStart);
5462
- const start = context.locator.locationAt(tokens.start(tokenStart));
5463
- const inline = buildInlineFragment(tokenStart, false, context);
5464
- const result = {
5465
- type: "heading",
5466
- depth: kind - openKind + 1,
5467
- children: inline.children,
5468
- position: {
5469
- start,
5470
- end: context.locator.locationAt(tokens.end(tokenStart))
5471
- }
5472
- };
5473
- if (inline.attributes) result.attributes = inline.attributes;
5474
- return result;
5475
- };
5476
- }
5477
5607
  const feature$8 = { block: {
5478
5608
  builds: [{
5479
5609
  token: [
@@ -5484,10 +5614,22 @@ const feature$8 = { block: {
5484
5614
  68,
5485
5615
  69
5486
5616
  ],
5487
- build: createBuildHeading(64)
5488
- }, {
5489
- token: [70, 71],
5490
- build: createBuildHeading(70)
5617
+ build(tokenStart, context) {
5618
+ const tokens = context.structure.tokens;
5619
+ const start = context.locator.locationAt(tokens.start(tokenStart));
5620
+ const inline = buildInlineFragment(tokenStart, context);
5621
+ const result = {
5622
+ type: "heading",
5623
+ depth: tokens.kind(tokenStart) - 64 + 1,
5624
+ children: inline.children,
5625
+ position: {
5626
+ start,
5627
+ end: context.locator.locationAt(tokens.end(tokenStart))
5628
+ }
5629
+ };
5630
+ if (inline.attributes) result.attributes = inline.attributes;
5631
+ return result;
5632
+ }
5491
5633
  }],
5492
5634
  starts: [{
5493
5635
  codes: [35],
@@ -5650,8 +5792,8 @@ function linkDestinationEnd(source, start, end) {
5650
5792
  }
5651
5793
  const titleLineEnding = /[\r\n]/;
5652
5794
  const titleIndent = /(?<=[\r\n])[ \t]+/g;
5653
- const titleTail = /(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\((?:[^)\\]|\\.)*\))[ \t\r\n]*/sy;
5654
- const destinationTail = /[^<\\()\t\n\r ]*\)?/y;
5795
+ const titleTail = /(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\((?:[^()\\]|\\.)*\))[ \t\r\n]*/sy;
5796
+ const destinationBoundary = /.*?[<\\()\t\n\r ]/sy;
5655
5797
  function decodeLinkTitle(source) {
5656
5798
  return decodeString(titleLineEnding.test(source) ? source.replace(titleIndent, "") : source);
5657
5799
  }
@@ -5665,23 +5807,23 @@ function resourceTail(source, destinationEnd) {
5665
5807
  function scanLinkTail(source, start, tokens) {
5666
5808
  if (source.charCodeAt(start + 1) !== 40) return -1;
5667
5809
  const contentStart = start + 2;
5668
- destinationTail.lastIndex = contentStart;
5669
- destinationTail.test(source);
5670
- const prefixEnd = destinationTail.lastIndex;
5671
- if (source.charCodeAt(prefixEnd - 1) === 41) {
5672
- appendInlineToken(tokens, 68, start, prefixEnd);
5673
- return prefixEnd;
5810
+ destinationBoundary.lastIndex = contentStart;
5811
+ if (!destinationBoundary.test(source)) return -1;
5812
+ const prefixEnd = destinationBoundary.lastIndex - 1;
5813
+ if (source.charCodeAt(prefixEnd) === 41) {
5814
+ const end = prefixEnd + 1;
5815
+ appendInlineToken(tokens, 68, start, end);
5816
+ return end;
5674
5817
  }
5675
5818
  let offset = skipWhitespace(source, contentStart);
5676
- let destinationEnd = offset;
5677
5819
  if (source.charCodeAt(offset) !== 41) {
5678
- destinationEnd = linkDestinationEnd(source, offset, Math.max(offset, prefixEnd));
5820
+ const destinationEnd = linkDestinationEnd(source, offset, Math.max(offset, prefixEnd));
5679
5821
  if (destinationEnd < 0) return -1;
5680
5822
  offset = resourceTail(source, destinationEnd);
5681
5823
  if (offset < 0) return -1;
5682
5824
  }
5683
- const end = offset + 1;
5684
- appendInlineToken(tokens, 68, start, end, destinationEnd - start);
5825
+ const end = start + 1;
5826
+ appendInlineToken(tokens, 6, start, end, offset + 1);
5685
5827
  return end;
5686
5828
  }
5687
5829
  function phrasingText(children) {
@@ -5714,23 +5856,22 @@ function createBuildMedia(media, resourceKind) {
5714
5856
  position
5715
5857
  } : {
5716
5858
  type: "linkReference",
5717
- children,
5718
5859
  identifier,
5719
5860
  label,
5720
5861
  referenceType,
5862
+ children,
5721
5863
  position
5722
5864
  };
5723
5865
  } : (openToken, closeToken, position, children, context) => {
5724
5866
  const source = context.view.text;
5725
- const tokenStart = inlineTokenStart(context.tokens, closeToken);
5726
5867
  const end = inlineTokenEnd(context.tokens, closeToken) - 1;
5727
- let start = tokenStart + 2;
5868
+ let start = inlineTokenStart(context.tokens, closeToken) + 2;
5728
5869
  let destinationEnd = end;
5729
5870
  let title = null;
5730
- const endInTail = inlineTokenData(context.tokens, closeToken);
5731
- if (endInTail) {
5732
- const resourceEnd = tokenStart + endInTail;
5733
- start = skipWhitespace(source, start, resourceEnd);
5871
+ const general = inlineTokenData(context.tokens, closeToken) !== 0;
5872
+ if (general) {
5873
+ start = skipWhitespace(source, start, end);
5874
+ const resourceEnd = start < end ? linkDestinationEnd(source, start, start) : start;
5734
5875
  destinationEnd = resourceEnd;
5735
5876
  if (source.charCodeAt(start) === 60) {
5736
5877
  start++;
@@ -5743,7 +5884,8 @@ function createBuildMedia(media, resourceKind) {
5743
5884
  title = decodeLinkTitle(source.slice(titleStart + 1, titleEnd - 1));
5744
5885
  }
5745
5886
  }
5746
- const url = decodeString(source.slice(start, destinationEnd));
5887
+ const destination = source.slice(start, destinationEnd);
5888
+ const url = general ? decodeString(destination) : decodeEntities(destination);
5747
5889
  return isImage ? {
5748
5890
  type: "image",
5749
5891
  alt: phrasingText(children),
@@ -5752,9 +5894,9 @@ function createBuildMedia(media, resourceKind) {
5752
5894
  position
5753
5895
  } : {
5754
5896
  type: "link",
5755
- children,
5756
5897
  url,
5757
5898
  title,
5899
+ children,
5758
5900
  position
5759
5901
  };
5760
5902
  };
@@ -5845,7 +5987,7 @@ const feature$6 = { inline: {
5845
5987
  {
5846
5988
  kind: "text",
5847
5989
  token: 68,
5848
- build: (tokenIndex, context) => decodeString(inlineTokenText(context.view.text, context.tokens, tokenIndex))
5990
+ build: (tokenIndex, context) => decodeEntities(inlineTokenText(context.view.text, context.tokens, tokenIndex))
5849
5991
  }
5850
5992
  ]
5851
5993
  } };
@@ -5860,26 +6002,26 @@ function listMarkerAt(source, lines, index, contentOffset) {
5860
6002
  if (marker === 45 || marker === 43 || marker === 42) delimiter = marker;
5861
6003
  else {
5862
6004
  if (!isAsciiDigit(marker)) return;
5863
- while (markerEnd < lineEnd && markerEnd - contentOffset < 9 && isAsciiDigit(source.charCodeAt(markerEnd))) markerEnd++;
5864
6005
  delimiter = source.charCodeAt(markerEnd);
6006
+ while (markerEnd < lineEnd && markerEnd - contentOffset < 9 && isAsciiDigit(delimiter)) delimiter = source.charCodeAt(++markerEnd);
5865
6007
  if (delimiter !== 46 && delimiter !== 41) return;
5866
6008
  markerEnd++;
5867
6009
  }
5868
6010
  const padding = source.charCodeAt(markerEnd);
5869
6011
  if (markerEnd < lineEnd && padding !== 32 && padding !== 9) return;
5870
- const markerColumn = lines.prefixColumns(index) + markerEnd - lines.start(index);
6012
+ const markerColumn = lineIndentColumns(source, lines, index, contentOffset) + markerEnd - contentOffset;
5871
6013
  let contentIndent = markerColumn + 1;
5872
6014
  let contentStart = markerEnd;
5873
6015
  let hasContent = false;
5874
6016
  let prefixColumns = 0;
5875
6017
  if (markerEnd < lineEnd) {
5876
- const firstPaddingColumns = padding === 9 ? 4 - markerColumn % 4 : 1;
6018
+ const firstPaddingColumns = padding === 9 ? tabWidthAt(source, markerEnd) : 1;
5877
6019
  let column = markerColumn + firstPaddingColumns;
5878
6020
  contentStart++;
5879
6021
  while (contentStart < lineEnd) {
5880
6022
  const code = source.charCodeAt(contentStart);
5881
6023
  if (code === 32) column++;
5882
- else if (code === 9) column += 4 - column % 4;
6024
+ else if (code === 9) column += tabWidthAt(source, contentStart);
5883
6025
  else {
5884
6026
  if (code === marker && isThematicBreak(source, lines, index, contentOffset)) return;
5885
6027
  break;
@@ -5904,7 +6046,6 @@ function listMarkerAt(source, lines, index, contentOffset) {
5904
6046
  };
5905
6047
  }
5906
6048
  function taskListMarkerAt(source, start, end) {
5907
- while (source.charCodeAt(start) === 32) start++;
5908
6049
  if (source.charCodeAt(start++) !== 91) return;
5909
6050
  const state = source.charCodeAt(start++);
5910
6051
  const checked = state === 88 || state === 120;
@@ -5936,8 +6077,8 @@ const buildListItem = (tokenStart, context) => {
5936
6077
  const children = buildBlockChildren(tokenStart, context);
5937
6078
  return {
5938
6079
  type: "listItem",
6080
+ checked: markerKind === 75 ? true : markerKind === 74 ? false : null,
5939
6081
  spread: tokens.kind(close) === 194,
5940
- checked: markerKind === 77 ? true : markerKind === 76 ? false : null,
5941
6082
  children,
5942
6083
  position: {
5943
6084
  start,
@@ -5972,7 +6113,7 @@ const listStart = (source, lines, tokens, start, contentOffset, context) => {
5972
6113
  let marker = listMarkerAt(source, lines, start, contentOffset);
5973
6114
  if (!marker) return;
5974
6115
  const delimiter = marker.delimiter;
5975
- const listOpen = delimiter === 46 || delimiter === 41 ? 74 : 73;
6116
+ const listOpen = delimiter === 46 || delimiter === 41 ? 72 : 71;
5976
6117
  tokens.push(listOpen, marker.offset, marker.end);
5977
6118
  let index = start;
5978
6119
  let listEnd = marker.end;
@@ -5989,47 +6130,42 @@ const listStart = (source, lines, tokens, start, contentOffset, context) => {
5989
6130
  let sibling;
5990
6131
  listSpread ||= trailingBlank;
5991
6132
  trailingBlank = false;
5992
- tokens.push(75, marker.offset, marker.end);
5993
- itemLines = context.createLineView(lines, index, marker.contentOffset, marker.contentPrefixColumns);
6133
+ tokens.push(73, marker.offset, marker.end);
6134
+ itemLines = lines.child(index, marker.contentOffset, marker.contentPrefixColumns);
5994
6135
  let hasContent = marker.hasContent;
5995
- let paragraphLeaf = hasContent ? 2 : 0;
5996
6136
  index++;
5997
- while (index < lines.length) {
5998
- const line = itemLines.pushAfterColumns(source, lines, index, marker.contentIndent);
5999
- if (line === -2) {
6000
- if (!hasContent) {
6137
+ for (; index < lines.length; index++) {
6138
+ if (!lines.lazy(index)) {
6139
+ const line = itemLines.pushAfterColumns(source, lines, index, marker.contentIndent);
6140
+ if (line === -2) {
6141
+ if (!hasContent) {
6142
+ trailingBlank = true;
6143
+ index++;
6144
+ sibling = index < lines.length ? listMarkerAt(source, lines, index, lineIndentOffset(source, lines, index)) : void 0;
6145
+ break;
6146
+ }
6147
+ itemLines.pushFrom(lines, index);
6001
6148
  trailingBlank = true;
6002
- index++;
6003
- sibling = index < lines.length ? listMarkerAt(source, lines, index, lineIndentOffset(source, lines, index)) : void 0;
6149
+ continue;
6150
+ }
6151
+ if (line === -3) {
6152
+ trailingBlank = false;
6153
+ hasContent = true;
6154
+ continue;
6155
+ }
6156
+ const candidate = listMarkerAt(source, lines, index, line);
6157
+ if (candidate) {
6158
+ sibling = candidate;
6004
6159
  break;
6005
6160
  }
6006
- itemLines.pushFrom(lines, index);
6007
- trailingBlank = true;
6008
- paragraphLeaf = 0;
6009
- index++;
6010
- continue;
6011
- }
6012
- if (line === -3) {
6013
- trailingBlank = false;
6014
- hasContent = true;
6015
- paragraphLeaf = 2;
6016
- index++;
6017
- continue;
6161
+ if (trailingBlank || !hasContent || context.startsInterruptingBlock(source, lines, index, line) || !context.endsWithParagraphLeaf(source, itemLines, itemLines.length - 1)) break;
6018
6162
  }
6019
- const candidate = listMarkerAt(source, lines, index, line);
6020
- if (candidate) {
6021
- sibling = candidate;
6022
- break;
6023
- }
6024
- if (paragraphLeaf === 2) paragraphLeaf = context.endsWithParagraphLeaf(source, itemLines, itemLines.length - 1) ? 1 : 0;
6025
- if (paragraphLeaf === 0 || context.startsInterruptingBlock(source, lines, index, line)) break;
6026
- itemLines.pushLazy(lines, index);
6027
- index++;
6163
+ itemLines.pushFrom(lines, index, void 0, void 0, true);
6028
6164
  }
6029
6165
  previousEnd = -1;
6030
6166
  itemSpread = false;
6031
- context.scanBlockLines(source, itemLines, tokens, visit);
6032
- const itemEnd = itemLines.length ? itemLines.end(itemLines.length - 1) : marker.end;
6167
+ context.scanBlockLines(source, itemLines, tokens, 0, visit);
6168
+ const itemEnd = itemLines.end(itemLines.length - 1);
6033
6169
  tokens.push(itemSpread ? 194 : 193, itemEnd, itemEnd);
6034
6170
  listEnd = itemEnd;
6035
6171
  marker = sibling;
@@ -6048,11 +6184,11 @@ const taskListStart = (source, lines, tokens, start, contentOffset, context) =>
6048
6184
  paragraph += tokens.nodeLength(paragraph);
6049
6185
  contentKind = tokens.kind(paragraph);
6050
6186
  }
6051
- if (contentKind !== 79) continue;
6187
+ if (contentKind !== 77) continue;
6052
6188
  const firstChunk = paragraph + 1;
6053
6189
  const marker = taskListMarkerAt(source, tokens.start(firstChunk), tokens.end(firstChunk));
6054
6190
  if (!marker || !hasTaskListContent(source, tokens, firstChunk, marker.contentStart)) continue;
6055
- tokens.setKind(item, marker.checked ? 77 : 76);
6191
+ tokens.setKind(item, marker.checked ? 75 : 74);
6056
6192
  tokens.setStart(firstChunk, marker.contentStart);
6057
6193
  }
6058
6194
  return nextLine;
@@ -6062,18 +6198,18 @@ function feature$5(taskList = false) {
6062
6198
  builds: [
6063
6199
  {
6064
6200
  token: [
6065
- 75,
6066
- 76,
6067
- 77
6201
+ 73,
6202
+ 74,
6203
+ 75
6068
6204
  ],
6069
6205
  build: buildListItem
6070
6206
  },
6071
6207
  {
6072
- token: 73,
6208
+ token: 71,
6073
6209
  build: createBuildList(false)
6074
6210
  },
6075
6211
  {
6076
- token: 74,
6212
+ token: 72,
6077
6213
  build: createBuildList(true)
6078
6214
  }
6079
6215
  ],
@@ -6093,11 +6229,9 @@ function feature$5(taskList = false) {
6093
6229
  56,
6094
6230
  57
6095
6231
  ],
6096
- unwrap(source, lines, index, contentOffset, target) {
6232
+ unwrap(source, lines, index, contentOffset) {
6097
6233
  const marker = listMarkerAt(source, lines, index, contentOffset);
6098
- if (!marker) return false;
6099
- target.resetFrom(lines, index, marker.contentOffset, marker.contentPrefixColumns);
6100
- return true;
6234
+ if (marker) return lines.child(index, marker.contentOffset, marker.contentPrefixColumns);
6101
6235
  },
6102
6236
  interrupt(source, lines, index, contentOffset) {
6103
6237
  interruptingMarker.lastIndex = contentOffset;
@@ -6163,39 +6297,51 @@ function feature$4(options) {
6163
6297
  const feature$3 = { block: {
6164
6298
  fallbacks: [(source, lines, tokens, start, contentOffset, context) => {
6165
6299
  const openToken = tokens.openInlineFrame();
6166
- let kind = 79;
6300
+ let kind = 77;
6167
6301
  let underlineEnd;
6168
6302
  let chunkOffset = contentOffset;
6169
6303
  let chunkEnd = lines.end(start);
6170
- let withBoundaryBreak = false;
6304
+ let hasPaddedLineFeed = false;
6171
6305
  let index = start + 1;
6172
6306
  while (index < lines.length) {
6173
6307
  const lineStart = lines.start(index);
6174
6308
  const lineEnd = lines.end(index);
6309
+ const prefixColumns = lines.prefixColumns(index);
6175
6310
  let spaceEnd = lineStart;
6176
6311
  let first = source.charCodeAt(spaceEnd);
6177
6312
  while (spaceEnd < lineEnd && first === 32) first = source.charCodeAt(++spaceEnd);
6178
- const contentStart = spaceEnd < lineEnd && first === 9 ? trimLineWhitespaceStart(source, spaceEnd + 1, lineEnd) : spaceEnd;
6313
+ let contentStart = spaceEnd;
6314
+ let columns = prefixColumns + spaceEnd - lineStart;
6315
+ if (spaceEnd < lineEnd && first === 9) while (contentStart < lineEnd) {
6316
+ const code = source.charCodeAt(contentStart);
6317
+ if (code === 32) columns++;
6318
+ else if (code === 9) columns += tabWidthAt(source, contentStart);
6319
+ else {
6320
+ first = code;
6321
+ break;
6322
+ }
6323
+ contentStart++;
6324
+ }
6179
6325
  if (contentStart === lineEnd) break;
6180
- const chunkStart = Math.min(spaceEnd, lineStart + Math.max(0, 3 - lines.prefixColumns(index)));
6181
- if (!lines.lazy(index) && chunkStart === contentStart) {
6326
+ if (!lines.lazy(index) && columns <= 3) {
6182
6327
  const setext = setextMarkerAt(source, first, contentStart, lineEnd);
6183
6328
  if (setext) {
6184
- kind = setext === 61 ? 70 : 71;
6329
+ kind = setext === 61 ? 64 : 65;
6185
6330
  underlineEnd = lineEnd;
6186
6331
  index++;
6187
6332
  break;
6188
6333
  }
6189
6334
  if (context.startsInterruptingBlock(source, lines, index, contentStart)) break;
6190
6335
  }
6336
+ const chunkStart = contentStart > spaceEnd ? contentStart : Math.min(spaceEnd, lineStart + Math.max(0, 3 - prefixColumns));
6191
6337
  const previousEnd = lines.next(index - 1);
6192
- if (!withBoundaryBreak) {
6338
+ if (!hasPaddedLineFeed) {
6193
6339
  let padded = chunkStart < spaceEnd;
6194
6340
  if (!padded) {
6195
6341
  const previous = source.charCodeAt(chunkEnd - 1);
6196
6342
  padded = previous === 32 || previous === 9;
6197
6343
  }
6198
- if (padded && source.charCodeAt(chunkEnd) === 10) withBoundaryBreak = true;
6344
+ if (padded && source.charCodeAt(chunkEnd) === 10) hasPaddedLineFeed = true;
6199
6345
  }
6200
6346
  if (chunkStart !== previousEnd) {
6201
6347
  tokens.push(1, chunkOffset, previousEnd);
@@ -6205,15 +6351,15 @@ const feature$3 = { block: {
6205
6351
  index++;
6206
6352
  }
6207
6353
  tokens.closeInlineFrame(openToken, kind, contentOffset, underlineEnd ?? chunkEnd, chunkOffset, chunkEnd);
6208
- if (withBoundaryBreak) tokens.setValue(openToken, boundaryBreak);
6354
+ if (hasPaddedLineFeed) tokens.setValue(openToken, paddedLineFeed);
6209
6355
  return index;
6210
6356
  }],
6211
6357
  builds: [{
6212
- token: 79,
6358
+ token: 77,
6213
6359
  build(tokenStart, context) {
6214
6360
  const tokens = context.structure.tokens;
6215
6361
  const start = context.locator.locationAt(tokens.start(tokenStart));
6216
- const inline = buildInlineFragment(tokenStart, false, context);
6362
+ const inline = buildInlineFragment(tokenStart, context);
6217
6363
  const result = {
6218
6364
  type: "paragraph",
6219
6365
  children: inline.children,
@@ -6235,10 +6381,11 @@ function titleCloser(code) {
6235
6381
  }
6236
6382
  function scanMultilineLabel(source, lines, startIndex, labelStart, offset, context) {
6237
6383
  let lineIndex = startIndex;
6238
- let label = "";
6384
+ let lineStart = labelStart;
6385
+ let remaining = maximumLabelUnits;
6239
6386
  while (true) {
6240
6387
  const lineEnd = lines.end(lineIndex);
6241
- const limit = Math.min(lineEnd, labelStart + maximumLabelUnits - label.length + 1);
6388
+ const limit = Math.min(lineEnd, lineStart + remaining + 1);
6242
6389
  while (offset < limit) {
6243
6390
  const code = source.charCodeAt(offset);
6244
6391
  if (code === 92 && offset + 1 < lineEnd) {
@@ -6248,7 +6395,7 @@ function scanMultilineLabel(source, lines, startIndex, labelStart, offset, conte
6248
6395
  if (code === 91) return;
6249
6396
  if (code === 93) {
6250
6397
  if (source.charCodeAt(offset + 1) === 58) return {
6251
- label: label + source.slice(labelStart, offset),
6398
+ label: sliceLines(source, lines, startIndex, labelStart, lineIndex, offset),
6252
6399
  lineIndex,
6253
6400
  offset: offset + 2
6254
6401
  };
@@ -6258,11 +6405,11 @@ function scanMultilineLabel(source, lines, startIndex, labelStart, offset, conte
6258
6405
  }
6259
6406
  const nextLine = lineIndex + 1;
6260
6407
  context.retainLookahead(nextLine < lines.length ? lines.end(nextLine) : lines.next(lineIndex));
6261
- if (label.length + lines.next(lineIndex) - labelStart > 1998 || nextLine >= lines.length || isBlankLine(source, lines, nextLine)) return;
6262
- label += source.slice(labelStart, lines.next(lineIndex));
6408
+ remaining -= lines.next(lineIndex) - lineStart;
6409
+ if (remaining < 0 || nextLine >= lines.length || isBlankLine(source, lines, nextLine)) return;
6263
6410
  lineIndex = nextLine;
6264
6411
  offset = lines.start(lineIndex);
6265
- labelStart = offset;
6412
+ lineStart = offset;
6266
6413
  }
6267
6414
  }
6268
6415
  function scanAngleDestination(source, start, lineEnd) {
@@ -6281,7 +6428,7 @@ function scanAngleDestination(source, start, lineEnd) {
6281
6428
  }
6282
6429
  return end;
6283
6430
  }
6284
- function scanBalancedDestination(source, start, offset, lineEnd) {
6431
+ function scanBalancedDestination(source, offset, lineEnd) {
6285
6432
  let depth = 0;
6286
6433
  while (offset < lineEnd) {
6287
6434
  const code = source.charCodeAt(offset);
@@ -6295,19 +6442,20 @@ function scanBalancedDestination(source, start, offset, lineEnd) {
6295
6442
  } else if (code === 41 && --depth < 0) return -1;
6296
6443
  offset++;
6297
6444
  }
6298
- return offset > start && depth === 0 ? offset : -1;
6445
+ return depth === 0 ? offset : -1;
6299
6446
  }
6300
- function titleText(source, lines, first, start, last, end) {
6447
+ function sliceLines(source, lines, first, start, last, end) {
6301
6448
  if (first === last || lines.physicallyContiguous()) return source.slice(start, end);
6302
- let title = "";
6449
+ let text = "";
6303
6450
  for (let line = first; line < last; line++) {
6304
- title += source.slice(start, lines.next(line));
6451
+ text += source.slice(start, lines.next(line));
6305
6452
  start = lines.start(line + 1);
6306
6453
  }
6307
- return title + source.slice(start, end);
6454
+ return text + source.slice(start, end);
6308
6455
  }
6309
- function scanTitle(source, lines, destinationLine, lineIndex, offset, closer, titleOnNextLine, context) {
6456
+ function scanTitle(source, lines, destinationLine, lineIndex, offset, closer, context) {
6310
6457
  const firstLine = lineIndex;
6458
+ const titleOnNextLine = lineIndex !== destinationLine;
6311
6459
  const titleStart = ++offset;
6312
6460
  scan: for (;;) {
6313
6461
  const lineEnd = lines.end(lineIndex);
@@ -6319,7 +6467,7 @@ function scanTitle(source, lines, destinationLine, lineIndex, offset, closer, ti
6319
6467
  }
6320
6468
  if (code === closer) {
6321
6469
  if (trimLineWhitespaceStart(source, offset + 1, lineEnd) === lineEnd) return {
6322
- title: titleText(source, lines, firstLine, titleStart, lineIndex, offset),
6470
+ title: sliceLines(source, lines, firstLine, titleStart, lineIndex, offset),
6323
6471
  endLine: lineIndex + 1
6324
6472
  };
6325
6473
  break scan;
@@ -6366,11 +6514,12 @@ const linkDefinitionStart = (source, lines, tokens, start, contentOffset, contex
6366
6514
  offset = trimLineWhitespaceStart(source, offset, lineEnd);
6367
6515
  if (offset === lineEnd) {
6368
6516
  const nextLine = lineIndex + 1;
6369
- context.retainLookahead(nextLine < lines.length ? lines.end(nextLine) : lines.next(lineIndex));
6370
- if (nextLine >= lines.length || isBlankLine(source, lines, nextLine)) return;
6517
+ lineEnd = nextLine < lines.length ? lines.end(nextLine) : lines.next(lineIndex);
6518
+ context.retainLookahead(lineEnd);
6519
+ if (nextLine >= lines.length) return;
6371
6520
  lineIndex = nextLine;
6372
- lineEnd = lines.end(lineIndex);
6373
6521
  offset = trimLineWhitespaceStart(source, lines.start(lineIndex), lineEnd);
6522
+ if (offset === lineEnd) return;
6374
6523
  }
6375
6524
  let destination;
6376
6525
  if (source.charCodeAt(offset) === 60) {
@@ -6383,7 +6532,7 @@ const linkDefinitionStart = (source, lines, tokens, start, contentOffset, contex
6383
6532
  let end = destinationHead.test(source) ? Math.min(destinationHead.lastIndex, lineEnd) : offset;
6384
6533
  if (end < lineEnd) {
6385
6534
  const code = source.charCodeAt(end);
6386
- if (code !== 32 && code !== 9) end = scanBalancedDestination(source, offset, end, lineEnd);
6535
+ if (code !== 32 && code !== 9) end = scanBalancedDestination(source, end, lineEnd);
6387
6536
  }
6388
6537
  if (end <= offset) return;
6389
6538
  destination = source.slice(offset, end);
@@ -6391,18 +6540,16 @@ const linkDefinitionStart = (source, lines, tokens, start, contentOffset, contex
6391
6540
  }
6392
6541
  const destinationLine = lineIndex;
6393
6542
  offset = trimLineWhitespaceStart(source, offset, lineEnd);
6394
- let titleOnNextLine = false;
6395
6543
  if (offset === lineEnd && lineIndex + 1 < lines.length) {
6396
6544
  lineIndex++;
6397
6545
  lineEnd = lines.end(lineIndex);
6398
6546
  offset = trimLineWhitespaceStart(source, lines.start(lineIndex), lineEnd);
6399
- titleOnNextLine = true;
6400
6547
  }
6401
6548
  let title;
6402
6549
  let endLine = destinationLine + 1;
6403
6550
  const closer = titleCloser(source.charCodeAt(offset));
6404
6551
  if (closer) {
6405
- const match = scanTitle(source, lines, destinationLine, lineIndex, offset, closer, titleOnNextLine, context);
6552
+ const match = scanTitle(source, lines, destinationLine, lineIndex, offset, closer, context);
6406
6553
  if (!match) return;
6407
6554
  title = match.title;
6408
6555
  endLine = match.endLine;
@@ -6451,8 +6598,7 @@ function delimiterAt(source, lines, index, contentOffset) {
6451
6598
  const first = source.charCodeAt(contentOffset);
6452
6599
  if (first !== 45 && first !== 58 && first !== 124) return;
6453
6600
  const lineEnd = lines.end(index);
6454
- const delimiter = [];
6455
- let spanStart = contentOffset;
6601
+ const alignments = [];
6456
6602
  let offset = first === 124 ? contentOffset + 1 : contentOffset;
6457
6603
  while (offset < lineEnd) {
6458
6604
  offset = trimLineWhitespaceStart(source, offset, lineEnd);
@@ -6465,24 +6611,21 @@ function delimiterAt(source, lines, index, contentOffset) {
6465
6611
  const right = offset < lineEnd && source.charCodeAt(offset) === 58;
6466
6612
  if (right) offset++;
6467
6613
  offset = trimLineWhitespaceStart(source, offset, lineEnd);
6468
- let nextSpanStart = lineEnd;
6469
6614
  if (offset < lineEnd) {
6470
6615
  if (source.charCodeAt(offset) !== 124) return;
6471
- nextSpanStart = offset;
6472
6616
  offset++;
6473
6617
  }
6474
- delimiter.push(spanStart, left ? right ? 262 : 260 : right ? 261 : 259);
6475
- spanStart = nextSpanStart;
6618
+ alignments.push(left ? right ? 262 : 260 : right ? 261 : 259);
6476
6619
  }
6477
- return delimiter.length > 0 ? delimiter : void 0;
6620
+ return alignments.length ? alignments : void 0;
6478
6621
  }
6479
6622
  function emitTableCell(source, tokens, spanStart, contentStart, contentEnd) {
6480
6623
  contentStart = trimLineWhitespaceStart(source, contentStart, contentEnd);
6481
6624
  contentEnd = trimLineWhitespaceEnd(source, contentStart, contentEnd);
6482
- tokens.pushInlineFrame(82, spanStart, spanStart, contentStart, contentEnd);
6625
+ tokens.pushInlineFrame(80, spanStart, spanStart, contentStart, contentEnd);
6483
6626
  }
6484
6627
  function emitTableRow(source, tokens, contentOffset, lineEnd, visibleEnd, nextPipe) {
6485
- tokens.push(81, contentOffset, contentOffset);
6628
+ tokens.push(79, contentOffset, contentOffset);
6486
6629
  let contentStart = contentOffset;
6487
6630
  let spanStart = contentOffset;
6488
6631
  let lastPipe = -1;
@@ -6511,13 +6654,14 @@ function buildTableRow(tokenStart, context) {
6511
6654
  const start = context.locator.locationAt(offset);
6512
6655
  const columnShift = start.column - offset;
6513
6656
  const children = new Array(close - tokenStart - 1 >>> 1);
6657
+ context.tableCell = true;
6514
6658
  for (let index = 0; index < children.length; index++) {
6515
6659
  const cell = tokenStart + 1 + index * 2;
6516
6660
  const from = tokens.start(cell);
6517
6661
  const to = tokens.start(cell + 2);
6518
6662
  children[index] = {
6519
6663
  type: "tableCell",
6520
- children: buildInlineFragment(cell, true, context).children,
6664
+ children: buildInlineFragment(cell, context).children,
6521
6665
  position: {
6522
6666
  start: {
6523
6667
  line: start.line,
@@ -6532,6 +6676,7 @@ function buildTableRow(tokenStart, context) {
6532
6676
  }
6533
6677
  };
6534
6678
  }
6679
+ context.tableCell = false;
6535
6680
  const end = tokens.start(close);
6536
6681
  return {
6537
6682
  type: "tableRow",
@@ -6546,28 +6691,19 @@ function buildTableRow(tokenStart, context) {
6546
6691
  }
6547
6692
  };
6548
6693
  }
6549
- function tableAlignment(tokenStart, context) {
6550
- const tokens = context.structure.tokens;
6551
- const result = [];
6552
- for (let token = tokenStart; tokens.kind(token) !== 81 && tokens.kind(token) !== 198; token++) {
6553
- const kind = tokens.kind(token);
6554
- result.push(kind === 260 ? "left" : kind === 261 ? "right" : kind === 262 ? "center" : null);
6555
- }
6556
- return result;
6557
- }
6558
6694
  const feature$1 = { block: {
6559
6695
  fallbacks: [(source, lines, tokens, start, contentOffset, context) => {
6560
6696
  const delimiterLine = start + 1;
6561
6697
  if (delimiterLine >= lines.length) return;
6562
6698
  const delimiterOffset = lineIndentOffset(source, lines, delimiterLine);
6563
6699
  if (delimiterOffset < 0 || context.startsInterruptingBlock(source, lines, delimiterLine, delimiterOffset)) return;
6564
- const delimiter = delimiterAt(source, lines, delimiterLine, delimiterOffset);
6565
- if (!delimiter || countHeaderCells(source, lines, start, contentOffset) !== delimiter.length / 2) return;
6566
- tokens.push(80, contentOffset, contentOffset);
6700
+ const alignments = delimiterAt(source, lines, delimiterLine, delimiterOffset);
6701
+ if (!alignments || countHeaderCells(source, lines, start, contentOffset) !== alignments.length) return;
6702
+ tokens.push(78, contentOffset, contentOffset);
6567
6703
  const headerEnd = lines.end(start);
6568
6704
  let nextPipe = emitTableRow(source, tokens, contentOffset, headerEnd, trimLineWhitespaceEnd(source, contentOffset, headerEnd), source.indexOf("|", contentOffset));
6569
6705
  const delimiterEnd = lines.end(delimiterLine);
6570
- for (let cell = 0; cell < delimiter.length; cell += 2) tokens.push(delimiter[cell + 1], delimiter[cell + 0], delimiter[cell + 2 + 0] ?? delimiterEnd);
6706
+ for (const alignment of alignments) tokens.push(alignment, delimiterOffset, delimiterEnd);
6571
6707
  let end = start + 2;
6572
6708
  while (end < lines.length) {
6573
6709
  const contentOffset = lineIndentOffset(source, lines, end);
@@ -6583,16 +6719,30 @@ const feature$1 = { block: {
6583
6719
  return end;
6584
6720
  }],
6585
6721
  builds: [{
6586
- token: 80,
6722
+ token: 78,
6587
6723
  build(tokenStart, context) {
6588
6724
  const tokens = context.structure.tokens;
6589
6725
  const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
6590
6726
  const start = context.locator.locationAt(tokens.start(tokenStart));
6591
6727
  const header = tokenStart + 1;
6728
+ const headerRow = buildTableRow(header, context);
6729
+ const width = headerRow.children.length;
6592
6730
  const delimiter = header + tokens.nodeLength(header);
6593
- const align = tableAlignment(delimiter, context);
6594
- const rows = [buildTableRow(header, context)];
6595
- for (let row = delimiter + align.length; row < close; row += tokens.nodeLength(row)) rows.push(buildTableRow(row, context));
6731
+ const align = new Array(width);
6732
+ for (let column = 0; column < width; column++) switch (tokens.kind(delimiter + column)) {
6733
+ case 260:
6734
+ align[column] = "left";
6735
+ break;
6736
+ case 261:
6737
+ align[column] = "right";
6738
+ break;
6739
+ case 262:
6740
+ align[column] = "center";
6741
+ break;
6742
+ default: align[column] = null;
6743
+ }
6744
+ const rows = [headerRow];
6745
+ for (let row = delimiter + width; row < close; row += tokens.nodeLength(row)) rows.push(buildTableRow(row, context));
6596
6746
  return {
6597
6747
  type: "table",
6598
6748
  align,
@@ -6620,11 +6770,8 @@ const feature = { inline: {
6620
6770
  scan(source, start, tokens) {
6621
6771
  const next = source.charCodeAt(start + 1);
6622
6772
  if (next === 10 || next === 13) {
6623
- let offset = start + 2;
6624
- if (next === 13 && source.charCodeAt(offset) === 10) offset++;
6625
- const lineStart = offset;
6626
- while (source.charCodeAt(offset) === 32) offset++;
6627
- appendInlineToken(tokens, 128, start, offset, lineStart);
6773
+ const offset = findLineEndingEnd(source, start + 1);
6774
+ appendInlineToken(tokens, 128, start, offset);
6628
6775
  return offset;
6629
6776
  }
6630
6777
  if (isAsciiPunctuation(next)) {
@@ -6632,7 +6779,7 @@ const feature = { inline: {
6632
6779
  appendInlineToken(tokens, 65, start, end);
6633
6780
  return end;
6634
6781
  }
6635
- return start + (next === 32 || next === 9 ? 2 : 1);
6782
+ return start + 1;
6636
6783
  }
6637
6784
  }],
6638
6785
  builds: [
@@ -6666,21 +6813,21 @@ const feature = { inline: {
6666
6813
  function createProfile(options = {}) {
6667
6814
  const features = [
6668
6815
  feature$8,
6816
+ feature$14(options.breaks),
6669
6817
  feature$15,
6670
- feature$16,
6671
6818
  feature$5(options.taskList),
6672
- feature$14,
6819
+ feature$13,
6673
6820
  feature$7
6674
6821
  ];
6675
- if (options.footnote) features.push(feature$10);
6676
- if (options.frontmatter) features.unshift(feature$13(typeof options.frontmatter === "object" ? options.frontmatter.marker : "-"));
6822
+ if (options.footnote) features.push(feature$18);
6823
+ if (options.frontmatter) features.unshift(feature$12(typeof options.frontmatter === "object" ? options.frontmatter.marker : "-"));
6677
6824
  if (options.table) features.push(feature$1);
6678
6825
  if (options.math) features.push(feature$4(options.math));
6679
6826
  features.push(feature$3, feature, feature$9(options.strikethrough), feature$6);
6680
- if (options.attributes) features.push(feature$18);
6681
- if (options.binding) features.push(feature$17);
6682
- if (options.emoji) features.push(feature$11);
6683
- if (options.component) features.push(feature$12);
6827
+ if (options.attributes) features.push(feature$17);
6828
+ if (options.binding) features.push(feature$16);
6829
+ if (options.emoji) features.push(feature$10);
6830
+ if (options.component) features.push(feature$11);
6684
6831
  features.push(feature$2);
6685
6832
  const blockFeatures = [];
6686
6833
  const inlineFeatures = [];
@@ -6691,6 +6838,8 @@ function createProfile(options = {}) {
6691
6838
  return {
6692
6839
  block: createBlockProfile(blockFeatures),
6693
6840
  inline: createInlineProfile(inlineFeatures, {
6841
+ breaks: options.breaks === true,
6842
+ cjk: options.cjk === true,
6694
6843
  component: options.component === true,
6695
6844
  footnote: options.footnote === true
6696
6845
  })