satorigear 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,251 +1,5 @@
1
1
  import { decodeHTMLStrict } from "entities";
2
2
  import { parse } from "yaml";
3
- //#region src/block/arena.ts
4
- function leaf(tokenIndex) {
5
- return ~tokenIndex;
6
- }
7
- var BlockArena = class {
8
- #buildStarts = [];
9
- #buildTokenEnds = [];
10
- #buildTokenStarts = [];
11
- #edgeCounts = [0];
12
- #edgeLength = 0;
13
- #edges = [];
14
- #edgeStarts = [0];
15
- #freeEdges = [];
16
- #freeIds = [];
17
- #lengths = [0];
18
- #nodeCount = 0;
19
- #records = [];
20
- #releaseStack = [];
21
- #rules;
22
- #schema;
23
- #tokens;
24
- constructor(schema, tokens) {
25
- this.#rules = [];
26
- this.#schema = schema;
27
- this.#tokens = tokens;
28
- }
29
- build() {
30
- this.#edgeLength = 0;
31
- this.#freeEdges.length = 0;
32
- this.#freeIds.length = 0;
33
- this.#nodeCount = 0;
34
- this.#records = this.#buildRange(0, this.#tokens.length);
35
- }
36
- get records() {
37
- return this.#records;
38
- }
39
- get tokens() {
40
- return this.#tokens;
41
- }
42
- update(change) {
43
- const previous = this.#records;
44
- let prefixEnd = 0;
45
- while (prefixEnd < previous.length && previous[prefixEnd].tokenEnd <= change.oldStart) prefixEnd++;
46
- let suffixStart = prefixEnd;
47
- while (suffixStart < previous.length && previous[suffixStart].tokenStart < change.oldEnd) suffixStart++;
48
- const tokenDelta = change.newEnd - change.oldStart - (change.oldEnd - change.oldStart);
49
- const buildStart = previous[prefixEnd - 1]?.tokenEnd ?? 0;
50
- const oldSuffixTokenStart = previous[suffixStart]?.tokenStart;
51
- const buildEnd = oldSuffixTokenStart === void 0 ? this.#tokens.length : oldSuffixTokenStart + tokenDelta;
52
- for (let index = prefixEnd; index < suffixStart; index++) this.#release(previous[index].id);
53
- this.#coalesceFreeEdges();
54
- const changed = this.#buildRange(buildStart, buildEnd);
55
- const suffix = previous.slice(suffixStart);
56
- if (tokenDelta !== 0) for (const block of suffix) {
57
- block.tokenStart += tokenDelta;
58
- block.tokenEnd += tokenDelta;
59
- }
60
- this.#records = [
61
- ...previous.slice(0, prefixEnd),
62
- ...changed,
63
- ...suffix
64
- ];
65
- return {
66
- newEnd: prefixEnd + changed.length,
67
- oldEnd: suffixStart,
68
- oldStart: prefixEnd
69
- };
70
- }
71
- #allocate(rule, children) {
72
- const id = this.#freeIds.pop() ?? this.#nodeCount++;
73
- const first = children[0];
74
- const last = children.at(-1);
75
- const start = first === void 0 ? 0 : this.#entryStart(first);
76
- const end = last === void 0 ? start : this.#entryEnd(last);
77
- const tokenBase = first === void 0 ? 0 : this.#entryTokenStart(first);
78
- const tokenLimit = last === void 0 ? tokenBase : this.#entryTokenEnd(last);
79
- const edgeStart = this.#allocateEdges(children.length);
80
- this.#edgeStarts[id] = edgeStart;
81
- this.#edgeCounts[id] = children.length;
82
- for (let index = 0; index < children.length; index++) {
83
- const entry = children[index];
84
- const edge = (edgeStart + index) * 3;
85
- this.#edges[edge] = entry < 0 ? leaf(~entry - tokenBase) : entry;
86
- this.#edges[edge + 1] = this.#entryStart(entry) - start;
87
- this.#edges[edge + 2] = this.#entryTokenStart(entry) - tokenBase;
88
- }
89
- this.#lengths[id] = end - start;
90
- this.#rules[id] = rule;
91
- this.#buildStarts[id] = start;
92
- this.#buildTokenStarts[id] = tokenBase;
93
- this.#buildTokenEnds[id] = tokenLimit;
94
- return id;
95
- }
96
- #allocateEdges(count) {
97
- if (count === 0) return 0;
98
- let selected = -1;
99
- for (let index = 0; index < this.#freeEdges.length; index++) {
100
- const candidate = this.#freeEdges[index];
101
- if (candidate.count >= count && (selected < 0 || candidate.count < this.#freeEdges[selected].count)) selected = index;
102
- }
103
- if (selected < 0) {
104
- const start = this.#edgeLength;
105
- this.#edgeLength += count;
106
- return start;
107
- }
108
- const range = this.#freeEdges[selected];
109
- const start = range.start;
110
- if (range.count === count) this.#freeEdges.splice(selected, 1);
111
- else {
112
- range.start += count;
113
- range.count -= count;
114
- }
115
- return start;
116
- }
117
- #buildRange(start, end) {
118
- const document = {
119
- close: 0,
120
- children: []
121
- };
122
- const stack = [document];
123
- for (let index = start; index < end; index++) {
124
- const kind = this.#tokens.kind(index);
125
- const current = stack.at(-1);
126
- const spec = this.#schema.frameByOpen[kind];
127
- if (spec !== void 0) {
128
- stack.push({
129
- close: spec.close,
130
- children: [leaf(index)],
131
- rule: spec.rule
132
- });
133
- continue;
134
- }
135
- const groupedRule = this.#schema.groupedRuleByToken[kind];
136
- if (groupedRule !== void 0) {
137
- const children = [];
138
- do
139
- children.push(leaf(index++));
140
- while (index < end && this.#schema.groupedRuleByToken[this.#tokens.kind(index)] === groupedRule);
141
- index--;
142
- current.children.push(this.#allocate(groupedRule, children));
143
- continue;
144
- }
145
- if (current.close === kind) {
146
- current.children.push(leaf(index));
147
- stack.pop();
148
- const id = this.#allocate(current.rule, current.children);
149
- stack.at(-1).children.push(id);
150
- continue;
151
- }
152
- const leafRule = this.#schema.ruleByLeaf[kind];
153
- if (leafRule !== void 0) {
154
- current.children.push(this.#allocate(leafRule, [leaf(index)]));
155
- continue;
156
- }
157
- current.children.push(leaf(index));
158
- }
159
- if (stack.length !== 1) throw new Error(`Block token stream did not close ${stack.at(-1).rule?.name}`);
160
- return document.children.map((id) => ({
161
- id,
162
- tokenStart: this.#buildTokenStarts[id],
163
- tokenEnd: this.#buildTokenEnds[id]
164
- }));
165
- }
166
- #entryEnd(entry) {
167
- return entry < 0 ? this.#tokens.end(~entry) : this.#buildStarts[entry] + this.#lengths[entry];
168
- }
169
- #entryStart(entry) {
170
- return entry < 0 ? this.#tokens.start(~entry) : this.#buildStarts[entry];
171
- }
172
- #entryTokenEnd(entry) {
173
- return entry < 0 ? ~entry + 1 : this.#buildTokenEnds[entry];
174
- }
175
- #entryTokenStart(entry) {
176
- return entry < 0 ? ~entry : this.#buildTokenStarts[entry];
177
- }
178
- #release(id) {
179
- const stack = this.#releaseStack;
180
- stack.length = 1;
181
- stack[0] = id;
182
- while (stack.length > 0) {
183
- const released = stack.pop();
184
- const edgeStart = this.#edgeStarts[released];
185
- const edgeCount = this.#edgeCounts[released];
186
- for (let index = 0; index < edgeCount; index++) {
187
- const child = this.#edges[(edgeStart + index) * 3];
188
- if (child >= 0) stack.push(child);
189
- }
190
- this.#releaseEdges(released);
191
- this.#freeIds.push(released);
192
- }
193
- }
194
- #releaseEdges(id) {
195
- const count = this.#edgeCounts[id];
196
- if (count > 0) {
197
- this.#freeEdges.push({
198
- start: this.#edgeStarts[id],
199
- count
200
- });
201
- this.#edgeCounts[id] = 0;
202
- }
203
- }
204
- #coalesceFreeEdges() {
205
- const ranges = this.#freeEdges;
206
- if (ranges.length < 2) return;
207
- ranges.sort((left, right) => left.start - right.start);
208
- let write = 0;
209
- for (let read = 1; read < ranges.length; read++) {
210
- const current = ranges[write];
211
- const next = ranges[read];
212
- if (current.start + current.count === next.start) current.count += next.count;
213
- else ranges[++write] = next;
214
- }
215
- ranges.length = write + 1;
216
- }
217
- childAt(id, index) {
218
- return this.#edges[(this.#edgeStarts[id] + index) * 3];
219
- }
220
- childCount(id) {
221
- return this.#edgeCounts[id];
222
- }
223
- childRelAt(id, index) {
224
- return this.#edges[(this.#edgeStarts[id] + index) * 3 + 1];
225
- }
226
- childTokRelAt(id, index) {
227
- return this.#edges[(this.#edgeStarts[id] + index) * 3 + 2];
228
- }
229
- leafToken(entry, tokenBase) {
230
- return tokenBase + ~entry;
231
- }
232
- leafTokenKind(entry, tokenBase) {
233
- return this.#tokens.kind(this.leafToken(entry, tokenBase));
234
- }
235
- lenOf(id) {
236
- return this.#lengths[id];
237
- }
238
- isBlock(id) {
239
- return this.#rules[id].block;
240
- }
241
- ruleNameOf(id) {
242
- return this.#rules[id].name;
243
- }
244
- ruleOf(id) {
245
- return this.#rules[id];
246
- }
247
- };
248
- //#endregion
249
3
  //#region src/block/lines.ts
250
4
  function firstLineIndexAtOrAfter(lines, offset) {
251
5
  let low = 0;
@@ -257,6 +11,27 @@ function firstLineIndexAtOrAfter(lines, offset) {
257
11
  }
258
12
  return low;
259
13
  }
14
+ function firstNonspace(source, start, end) {
15
+ while (start < end && (source[start] === " " || source[start] === " ")) start++;
16
+ return start;
17
+ }
18
+ function lineEnd(source, offset, limit = source.length) {
19
+ for (; offset < limit; offset++) {
20
+ const character = source.charCodeAt(offset);
21
+ if (character === 10 || character === 13) return offset;
22
+ }
23
+ return limit;
24
+ }
25
+ function lineContentEnd(source, start, end) {
26
+ if (end <= start) return end;
27
+ if (source.charCodeAt(end - 1) === 10) return end - (end > start + 1 && source.charCodeAt(end - 2) === 13 ? 2 : 1);
28
+ return source.charCodeAt(end - 1) === 13 ? end - 1 : end;
29
+ }
30
+ function lineIndent(source, line) {
31
+ const indent = indentOf(source, line, 3);
32
+ if (source[indent.offset] === " " || source[indent.offset] === " ") return;
33
+ return indent;
34
+ }
260
35
  function indentOf(source, line, limit = Number.POSITIVE_INFINITY) {
261
36
  let offset = line.start;
262
37
  let columns = line.prefixColumns ?? 0;
@@ -315,11 +90,6 @@ function logicalLine(source, line) {
315
90
  }
316
91
  return result + source.slice(offset, line.next);
317
92
  }
318
- function lineIndent(source, line) {
319
- const indent = indentOf(source, line, 3);
320
- if (source[indent.offset] === " " || source[indent.offset] === " ") return;
321
- return indent;
322
- }
323
93
  function contentAfterColumns(source, line, columns) {
324
94
  let offset = line.start;
325
95
  let consumed = line.prefixColumns ?? 0;
@@ -345,6 +115,9 @@ function contentAfterColumns(source, line, columns) {
345
115
  prefixColumns: Math.max(0, consumed - columns)
346
116
  };
347
117
  }
118
+ function normalizeLines(value) {
119
+ return value.replace(/\r\n|\r/g, "\n");
120
+ }
348
121
  function removeIndent(value, columns) {
349
122
  let offset = 0;
350
123
  let consumed = 0;
@@ -357,46 +130,104 @@ function removeIndent(value, columns) {
357
130
  return " ".repeat(Math.max(0, consumed - columns)) + value.slice(offset);
358
131
  }
359
132
  //#endregion
133
+ //#region src/primitives.ts
134
+ const emptyArray = [];
135
+ const emptySet = /* @__PURE__ */ new Set();
136
+ function isArrayEqual(foo, bar) {
137
+ if (foo.length !== bar.length) return false;
138
+ for (let index = 0; index < foo.length; index++) if (foo[index] !== bar[index]) return false;
139
+ return true;
140
+ }
141
+ function isSetEqual(foo, bar) {
142
+ if (foo.size !== bar.size) return false;
143
+ for (const val of foo) if (!bar.has(val)) return false;
144
+ return true;
145
+ }
146
+ //#endregion
360
147
  //#region src/block/tokens.ts
361
148
  const blockTokenStride = 4;
362
149
  var BlockTokenStream = class {
150
+ #fieldLength;
363
151
  #fields;
364
152
  #metadata;
365
153
  #relativeStart;
366
154
  #sourceLength;
367
155
  constructor(sourceLength = 0) {
368
- this.#fields = [];
156
+ this.#fieldLength = 0;
157
+ this.#fields = /* @__PURE__ */ new Int32Array();
369
158
  this.#relativeStart = Number.POSITIVE_INFINITY;
370
159
  this.#sourceLength = sourceLength;
371
160
  }
372
161
  reset(sourceLength) {
373
- this.#fields.length = 0;
162
+ this.#fieldLength = 0;
374
163
  this.#metadata = void 0;
375
164
  this.#relativeStart = Number.POSITIVE_INFINITY;
376
165
  this.#sourceLength = sourceLength;
377
166
  }
378
167
  get length() {
379
- return this.#fields.length / blockTokenStride;
168
+ return this.#fieldLength / blockTokenStride;
380
169
  }
381
170
  push(kind, start, end, meta) {
382
- this.#fields.push(kind, start, end, 0);
171
+ const field = this.#fieldLength;
172
+ this.#ensureCapacity(field + blockTokenStride);
173
+ this.#fields[field] = kind;
174
+ this.#fields[field + 1] = start;
175
+ this.#fields[field + 2] = end;
176
+ this.#fields[field + 3] = 0;
177
+ this.#fieldLength += blockTokenStride;
383
178
  if (meta) {
384
179
  this.#metadata ??= /* @__PURE__ */ new Map();
385
180
  this.#metadata.set(this.length - 1, meta);
386
181
  }
387
182
  }
183
+ indexStructure(schema) {
184
+ const fields = this.#fields;
185
+ const opens = [];
186
+ const closes = [];
187
+ for (let index = 0; index < this.length; index++) {
188
+ const kind = this.kind(index);
189
+ const frame = schema.frameByOpen[kind];
190
+ if (frame) {
191
+ opens.push(index);
192
+ closes.push(frame.close);
193
+ continue;
194
+ }
195
+ const groupedRule = schema.groupedRuleByToken[kind];
196
+ if (groupedRule) {
197
+ const start = index;
198
+ do
199
+ index++;
200
+ while (index < this.length && schema.groupedRuleByToken[this.kind(index)] === groupedRule);
201
+ fields[start * blockTokenStride + 3] = index - start;
202
+ index--;
203
+ continue;
204
+ }
205
+ if (closes.at(-1) === kind) {
206
+ const open = opens.pop();
207
+ closes.pop();
208
+ fields[open * blockTokenStride + 3] = index - open + 1;
209
+ continue;
210
+ }
211
+ if (schema.ruleByLeaf[kind]) fields[index * blockTokenStride + 3] = 1;
212
+ }
213
+ if (opens.length > 0) throw new Error(`Block token stream did not close token ${this.kind(opens.at(-1))}`);
214
+ }
215
+ nodeLength(index) {
216
+ return this.#fields[index * blockTokenStride + 3];
217
+ }
388
218
  equalsAfterShift(index, source, next, nextIndex, nextSource, delta) {
389
- if (this.kind(index) !== next.kind(nextIndex) || this.start(index) + delta !== next.start(nextIndex) || this.end(index) + delta !== next.end(nextIndex)) return false;
219
+ const start = this.start(index);
220
+ const end = this.end(index);
221
+ const nextStart = next.start(nextIndex);
222
+ const nextEnd = next.end(nextIndex);
223
+ if (this.kind(index) !== next.kind(nextIndex) || start + delta !== nextStart || end + delta !== nextEnd) return false;
390
224
  const ranges = this.#metadata?.get(index)?.rangeOffsets;
391
225
  const nextRanges = next.#metadata?.get(nextIndex)?.rangeOffsets;
392
- if ((ranges?.length ?? 0) !== (nextRanges?.length ?? 0)) return false;
393
- if (ranges && nextRanges) {
394
- for (let range = 0; range < ranges.length; range++) if (ranges[range] !== nextRanges[range]) return false;
395
- }
226
+ if (ranges !== nextRanges && !isArrayEqual(ranges ?? emptyArray, nextRanges ?? emptyArray)) return false;
396
227
  const text = this.#metadata?.get(index)?.text;
397
228
  const nextText = next.#metadata?.get(nextIndex)?.text;
398
229
  if (text !== void 0 || nextText !== void 0) return text === nextText;
399
- return equalSourceText(source, this.start(index), this.end(index), nextSource, next.start(nextIndex), next.end(nextIndex));
230
+ return end - start === nextEnd - nextStart && source.slice(start, end) === nextSource.slice(nextStart, nextEnd);
400
231
  }
401
232
  rangeCount(index) {
402
233
  return (this.#metadata?.get(index)?.rangeOffsets?.length ?? 2) / 2;
@@ -437,12 +268,24 @@ var BlockTokenStream = class {
437
268
  }
438
269
  }
439
270
  if (end === previousLength) {
440
- this.#fields.length = start * blockTokenStride;
441
- for (let index = 0; index < replacement.#fields.length; index++) this.#fields.push(replacement.#fields[index]);
271
+ const fieldStart = start * blockTokenStride;
272
+ const fieldEnd = fieldStart + replacement.#fieldLength;
273
+ this.#ensureCapacity(fieldEnd);
274
+ this.#fields.set(replacement.#fields.subarray(0, replacement.#fieldLength), fieldStart);
275
+ this.#fieldLength = fieldEnd;
442
276
  } else if (replacementLength === replacedLength) {
443
277
  const fieldStart = start * blockTokenStride;
444
- for (let index = 0; index < replacement.#fields.length; index++) this.#fields[fieldStart + index] = replacement.#fields[index];
445
- } else this.#fields = this.#fields.slice(0, start * blockTokenStride).concat(replacement.#fields, this.#fields.slice(end * blockTokenStride));
278
+ this.#fields.set(replacement.#fields.subarray(0, replacement.#fieldLength), fieldStart);
279
+ } else {
280
+ const fieldStart = start * blockTokenStride;
281
+ const oldFieldEnd = end * blockTokenStride;
282
+ const newFieldEnd = fieldStart + replacement.#fieldLength;
283
+ const nextFieldLength = this.#fieldLength + newFieldEnd - oldFieldEnd;
284
+ this.#ensureCapacity(nextFieldLength);
285
+ this.#fields.copyWithin(newFieldEnd, oldFieldEnd, this.#fieldLength);
286
+ this.#fields.set(replacement.#fields.subarray(0, replacement.#fieldLength), fieldStart);
287
+ this.#fieldLength = nextFieldLength;
288
+ }
446
289
  const indexDelta = replacementLength - replacedLength;
447
290
  if (end === previousLength || indexDelta === 0) {
448
291
  if (this.#metadata) {
@@ -466,7 +309,7 @@ var BlockTokenStream = class {
466
309
  return change;
467
310
  }
468
311
  truncate(length) {
469
- this.#fields.length = length * blockTokenStride;
312
+ this.#fieldLength = length * blockTokenStride;
470
313
  if (this.#metadata) {
471
314
  for (const index of this.#metadata.keys()) if (index >= length) this.#metadata.delete(index);
472
315
  if (this.#metadata.size === 0) this.#metadata = void 0;
@@ -487,6 +330,14 @@ var BlockTokenStream = class {
487
330
  value(index) {
488
331
  return this.#metadata?.get(index)?.value;
489
332
  }
333
+ #ensureCapacity(length) {
334
+ if (length <= this.#fields.length) return;
335
+ let capacity = Math.max(64, this.#fields.length * 2);
336
+ while (capacity < length) capacity *= 2;
337
+ const fields = new Int32Array(capacity);
338
+ fields.set(this.#fields.subarray(0, this.#fieldLength));
339
+ this.#fields = fields;
340
+ }
490
341
  #position(position) {
491
342
  return position < 0 ? position + this.#sourceLength + 1 : position;
492
343
  }
@@ -516,9 +367,6 @@ function appendLogicalToken(out, kind, source, lines, start, end, value) {
516
367
  value
517
368
  });
518
369
  }
519
- function equalSourceText(previousSource, previousStart, previousEnd, nextSource, nextStart, nextEnd) {
520
- return previousEnd - previousStart === nextEnd - nextStart && previousSource.slice(previousStart, previousEnd) === nextSource.slice(nextStart, nextEnd);
521
- }
522
370
  //#endregion
523
371
  //#region src/block/scanner.ts
524
372
  function profileStarts(profile, context, source, lines, start, out) {
@@ -710,6 +558,7 @@ var BlockScanner = class {
710
558
  });
711
559
  return false;
712
560
  });
561
+ tokens.indexStructure(this.#profile.schema);
713
562
  checkpoints.length = checkpointIndex;
714
563
  this.#source = source;
715
564
  this.#lines = lines;
@@ -769,6 +618,7 @@ var BlockScanner = class {
769
618
  });
770
619
  return false;
771
620
  });
621
+ replacement.indexStructure(this.#profile.schema);
772
622
  const oldTokenEnd = converged < 0 ? this.#tokens.length : this.#checkpoints[converged].tokenStart;
773
623
  const tokenDelta = replacement.length - (oldTokenEnd - oldTokenStart);
774
624
  const tokenChange = this.#tokens.replace(previousSource, nextSource, oldTokenStart, oldTokenEnd, replacement);
@@ -794,74 +644,140 @@ var BlockScanner = class {
794
644
  }
795
645
  };
796
646
  //#endregion
647
+ //#region src/block/structure.ts
648
+ const noBlockEntry = Number.MIN_SAFE_INTEGER;
649
+ var BlockStructure = class {
650
+ #records = [];
651
+ #schema;
652
+ #tokens;
653
+ constructor(schema, tokens) {
654
+ this.#schema = schema;
655
+ this.#tokens = tokens;
656
+ }
657
+ build() {
658
+ this.#records = this.#buildRange(0, this.#tokens.length);
659
+ }
660
+ get records() {
661
+ return this.#records;
662
+ }
663
+ get tokens() {
664
+ return this.#tokens;
665
+ }
666
+ update(change) {
667
+ const previous = this.#records;
668
+ let prefixEnd = 0;
669
+ while (prefixEnd < previous.length && previous[prefixEnd].tokenEnd <= change.oldStart) prefixEnd++;
670
+ let suffixStart = prefixEnd;
671
+ while (suffixStart < previous.length && previous[suffixStart].tokenStart < change.oldEnd) suffixStart++;
672
+ const tokenDelta = change.newEnd - change.oldStart - (change.oldEnd - change.oldStart);
673
+ const buildStart = previous[prefixEnd - 1]?.tokenEnd ?? 0;
674
+ const oldSuffixTokenStart = previous[suffixStart]?.tokenStart;
675
+ const buildEnd = oldSuffixTokenStart === void 0 ? this.#tokens.length : oldSuffixTokenStart + tokenDelta;
676
+ const changed = this.#buildRange(buildStart, buildEnd);
677
+ const suffix = previous.slice(suffixStart);
678
+ if (tokenDelta !== 0) for (const block of suffix) {
679
+ block.tokenStart += tokenDelta;
680
+ block.tokenEnd += tokenDelta;
681
+ }
682
+ this.#records = [
683
+ ...previous.slice(0, prefixEnd),
684
+ ...changed,
685
+ ...suffix
686
+ ];
687
+ return {
688
+ newEnd: prefixEnd + changed.length,
689
+ oldEnd: suffixStart,
690
+ oldStart: prefixEnd,
691
+ tokenDelta
692
+ };
693
+ }
694
+ #buildRange(start, end) {
695
+ const records = [];
696
+ while (start < end) {
697
+ const length = this.#tokens.nodeLength(start);
698
+ if (length === 0) throw new Error(`Block token ${start} does not begin a semantic node`);
699
+ records.push({
700
+ tokenStart: start,
701
+ tokenEnd: start + length
702
+ });
703
+ start += length;
704
+ }
705
+ return records;
706
+ }
707
+ firstChild(tokenStart) {
708
+ return ~tokenStart;
709
+ }
710
+ nextChild(tokenStart, entry) {
711
+ const next = entry < 0 ? ~entry + 1 : entry + this.#tokens.nodeLength(entry);
712
+ if (next >= tokenStart + this.#tokens.nodeLength(tokenStart)) return noBlockEntry;
713
+ return this.#tokens.nodeLength(next) > 0 ? next : ~next;
714
+ }
715
+ leafToken(entry) {
716
+ return ~entry;
717
+ }
718
+ lenOf(tokenStart) {
719
+ const end = tokenStart + this.#tokens.nodeLength(tokenStart);
720
+ return this.#tokens.end(end - 1) - this.#tokens.start(tokenStart);
721
+ }
722
+ isBlock(tokenStart) {
723
+ return this.ruleOf(tokenStart).block;
724
+ }
725
+ ruleNameOf(tokenStart) {
726
+ return this.ruleOf(tokenStart).name;
727
+ }
728
+ ruleOf(tokenStart) {
729
+ const kind = this.#tokens.kind(tokenStart);
730
+ const rule = this.#schema.frameByOpen[kind]?.rule ?? this.#schema.groupedRuleByToken[kind] ?? this.#schema.ruleByLeaf[kind];
731
+ if (!rule) throw new Error(`Block token ${tokenStart} does not begin a semantic node`);
732
+ return rule;
733
+ }
734
+ };
735
+ //#endregion
797
736
  //#region src/fragment/block.ts
798
- function blockEnd(nodeId, offset, context) {
799
- let end = offset + context.arena.lenOf(nodeId);
800
- if (end > offset && context.source[end - 1] === "\n") end--;
801
- if (end > offset && context.source[end - 1] === "\r") end--;
802
- return end;
737
+ function blockEnd(tokenStart, context) {
738
+ const offset = context.structure.tokens.start(tokenStart);
739
+ return lineContentEnd(context.source, offset, offset + context.structure.lenOf(tokenStart));
803
740
  }
804
- function firstNonspace(source, start, end) {
805
- while (start < end && (source[start] === " " || source[start] === " ")) start++;
806
- return start;
807
- }
808
- function directBlockToken(nodeId, tokenBase, kind, context) {
809
- const arena = context.arena;
810
- const childCount = arena.childCount(nodeId);
811
- for (let index = 0; index < childCount; index++) {
812
- const entry = arena.childAt(nodeId, index);
813
- if (entry < 0) {
814
- const token = arena.leafToken(entry, tokenBase);
815
- if (arena.tokens.kind(token) === kind) return token;
816
- }
741
+ function directBlockToken(tokenStart, kind, context) {
742
+ const structure = context.structure;
743
+ for (let entry = structure.firstChild(tokenStart); entry !== noBlockEntry; entry = structure.nextChild(tokenStart, entry)) if (entry < 0) {
744
+ const token = structure.leafToken(entry);
745
+ if (structure.tokens.kind(token) === kind) return token;
817
746
  }
818
747
  }
819
- function blockToken(nodeId, tokenBase, kind, context) {
820
- const token = directBlockToken(nodeId, tokenBase, kind, context);
821
- if (token === void 0) throw new Error(`Expected ${context.arena.ruleNameOf(nodeId)} syntax to contain block token ${kind}`);
748
+ function blockToken(tokenStart, kind, context) {
749
+ const token = directBlockToken(tokenStart, kind, context);
750
+ if (token === void 0) throw new Error(`Expected ${context.structure.ruleNameOf(tokenStart)} syntax to contain block token ${kind}`);
822
751
  return token;
823
752
  }
824
- function payloadBounds(nodeId, offset, tokenBase, context) {
825
- const arena = context.arena;
753
+ function payloadBounds(tokenStart, context) {
754
+ const structure = context.structure;
755
+ const offset = structure.tokens.start(tokenStart);
826
756
  const result = {
827
- start: offset + arena.lenOf(nodeId),
757
+ start: offset + structure.lenOf(tokenStart),
828
758
  end: offset
829
759
  };
830
- const visit = (currentId, currentTokenBase) => {
831
- const childCount = arena.childCount(currentId);
832
- for (let index = 0; index < childCount; index++) {
833
- const child = arena.childAt(currentId, index);
834
- if (child < 0) {
835
- const token = arena.leafToken(child, currentTokenBase);
836
- const start = arena.tokens.start(token);
837
- const end = arena.tokens.end(token);
838
- if (end > start) {
839
- result.start = Math.min(result.start, start);
840
- result.end = Math.max(result.end, end);
841
- }
842
- } else visit(child, currentTokenBase + arena.childTokRelAt(currentId, index));
760
+ const endToken = tokenStart + structure.tokens.nodeLength(tokenStart);
761
+ for (let token = tokenStart; token < endToken; token++) {
762
+ const start = structure.tokens.start(token);
763
+ const end = structure.tokens.end(token);
764
+ if (end > start) {
765
+ result.start = Math.min(result.start, start);
766
+ result.end = Math.max(result.end, end);
843
767
  }
844
- };
845
- visit(nodeId, tokenBase);
768
+ }
846
769
  return result;
847
770
  }
848
- function normalizeLines(value) {
849
- return value.replace(/\r\n|\r/g, "\n");
850
- }
851
- const buildBlockNode = (nodeId, offset, tokenBase, context) => {
852
- const rule = context.arena.ruleOf(nodeId);
771
+ const buildBlockNode = (tokenStart, context) => {
772
+ const rule = context.structure.ruleOf(tokenStart);
853
773
  const build = rule.build;
854
774
  if (!build) throw new Error(`Unexpected block syntax rule: ${rule.name}`);
855
- return build(nodeId, offset, tokenBase, context);
775
+ return build(tokenStart, context);
856
776
  };
857
- const buildBlockChildren = (nodeId, offset, tokenBase, context) => {
858
- const arena = context.arena;
777
+ const buildBlockChildren = (tokenStart, context) => {
778
+ const structure = context.structure;
859
779
  const children = [];
860
- const childCount = arena.childCount(nodeId);
861
- for (let index = 0; index < childCount; index++) {
862
- const childId = arena.childAt(nodeId, index);
863
- if (childId >= 0 && arena.isBlock(childId)) children.push(buildBlockNode(childId, offset + arena.childRelAt(nodeId, index), tokenBase + arena.childTokRelAt(nodeId, index), context));
864
- }
780
+ for (let childId = structure.firstChild(tokenStart); childId !== noBlockEntry; childId = structure.nextChild(tokenStart, childId)) if (childId >= 0 && structure.isBlock(childId)) children.push(buildBlockNode(childId, context));
865
781
  return children;
866
782
  };
867
783
  //#endregion
@@ -915,15 +831,6 @@ function snapshot(fragments, sourceLength, locate) {
915
831
  };
916
832
  }
917
833
  //#endregion
918
- //#region src/primitives.ts
919
- const emptyArray = [];
920
- const emptySet = /* @__PURE__ */ new Set();
921
- function isSetEqual(foo, bar) {
922
- if (foo.size !== bar.size) return false;
923
- for (const val of foo) if (!bar.has(val)) return false;
924
- return true;
925
- }
926
- //#endregion
927
834
  //#region src/inline/region.ts
928
835
  function hasDefinition(key) {
929
836
  (this.dependencies ??= /* @__PURE__ */ new Set()).add(key);
@@ -933,19 +840,19 @@ var InlineRegion = class {
933
840
  #definitionDependencies;
934
841
  #definitions;
935
842
  #rawTokens;
936
- #syntax;
843
+ #profile;
937
844
  #tokenSource;
938
845
  #tokens;
939
- id;
846
+ offset;
940
847
  revision = 0;
941
848
  rule;
942
- span;
849
+ tokenStart;
943
850
  view;
944
- constructor(syntax, binding, definitions) {
945
- this.#syntax = syntax;
946
- this.id = binding.id;
851
+ constructor(profile, binding, definitions) {
852
+ this.#profile = profile;
853
+ this.offset = binding.offset;
947
854
  this.rule = binding.rule;
948
- this.span = binding.span;
855
+ this.tokenStart = binding.tokenStart;
949
856
  this.view = binding.view;
950
857
  this.#updateTokens(binding.view.text, definitions);
951
858
  }
@@ -962,20 +869,19 @@ var InlineRegion = class {
962
869
  updateDefinitions(definitions) {
963
870
  if (this.#updateTokens(this.view.text, definitions)) this.revision++;
964
871
  }
965
- shift(delta) {
966
- this.span.start += delta;
967
- this.span.end += delta;
872
+ shift(delta, tokenDelta) {
873
+ this.tokenStart += tokenDelta;
874
+ this.offset += delta;
968
875
  this.view.shift(delta);
969
876
  }
970
877
  #dependenciesChanged(definitions) {
971
- if (!this.#definitionDependencies || !this.#definitions) return true;
972
878
  for (const candidate of this.#definitionDependencies) if (this.#definitions.has(candidate) !== definitions.has(candidate)) return true;
973
879
  return false;
974
880
  }
975
881
  #rebind(binding) {
976
- this.id = binding.id;
882
+ this.offset = binding.offset;
977
883
  this.rule = binding.rule;
978
- this.span = binding.span;
884
+ this.tokenStart = binding.tokenStart;
979
885
  this.view = binding.view;
980
886
  }
981
887
  #updateTokens(source, definitions) {
@@ -985,11 +891,10 @@ var InlineRegion = class {
985
891
  }
986
892
  const context = {
987
893
  definitions,
988
- hasDefinition,
989
- tokenize: this.#syntax.tokenize
894
+ hasDefinition
990
895
  };
991
- const rawTokens = source === this.#tokenSource && this.#rawTokens ? this.#rawTokens : this.#syntax.tokenize(source);
992
- const tokens = this.#syntax.resolve(source, rawTokens, context);
896
+ const rawTokens = source === this.#tokenSource && this.#rawTokens ? this.#rawTokens : this.#profile.tokenize(source);
897
+ const tokens = this.#profile.resolve(source, rawTokens, context);
993
898
  this.#tokenSource = source;
994
899
  this.#definitionDependencies = context.dependencies ?? emptySet;
995
900
  this.#definitions = definitions;
@@ -1004,9 +909,9 @@ var InlineRegionCursor = class {
1004
909
  constructor(regions) {
1005
910
  this.#regions = regions;
1006
911
  }
1007
- take(nodeId) {
912
+ take(tokenStart) {
1008
913
  const region = this.#regions[this.#index];
1009
- if (region?.id !== nodeId) return;
914
+ if (region?.tokenStart !== tokenStart) return;
1010
915
  this.#index++;
1011
916
  return region;
1012
917
  }
@@ -1112,21 +1017,22 @@ var SyntaxState = class {
1112
1017
  #definitionEntries;
1113
1018
  #definitions = emptySet;
1114
1019
  #profile;
1115
- #arena;
1116
- constructor(source, profile, arena) {
1020
+ #sourceLength = 0;
1021
+ #structure;
1022
+ constructor(source, profile, structure) {
1117
1023
  this.#profile = profile;
1118
- this.#arena = arena;
1024
+ this.#structure = structure;
1119
1025
  this.update(source);
1120
1026
  }
1121
1027
  blocks() {
1122
1028
  return this.#blocks;
1123
1029
  }
1124
1030
  update(source, change, stableBlockCount = 0) {
1125
- const arena = this.#arena;
1031
+ const structure = this.#structure;
1126
1032
  const previousBlocks = this.#blocks;
1127
1033
  const oldStart = change?.oldStart ?? 0;
1128
1034
  const oldEnd = change?.oldEnd ?? 0;
1129
- const newEnd = change?.newEnd ?? arena.records.length;
1035
+ const newEnd = change?.newEnd ?? structure.records.length;
1130
1036
  const blocks = stableBlockCount === 0 ? [] : previousBlocks.slice(0, stableBlockCount);
1131
1037
  const previousDefinitionEntries = this.#definitionEntries;
1132
1038
  const definitions = /* @__PURE__ */ new Set();
@@ -1138,11 +1044,11 @@ var SyntaxState = class {
1138
1044
  definitions.add(entry.key);
1139
1045
  }
1140
1046
  const bindings = [];
1141
- const collectNode = (nodeId, offset, tokenBase, blockIndex) => {
1142
- const rule = arena.ruleOf(nodeId);
1047
+ const collectNode = (tokenStart, offset, tokenBase, blockIndex) => {
1048
+ const rule = structure.ruleOf(tokenStart);
1143
1049
  const definitionKey = rule.definitionKey;
1144
1050
  if (definitionKey) {
1145
- const key = definitionKey(arena.tokens, tokenBase);
1051
+ const key = definitionKey(structure.tokens, tokenBase);
1146
1052
  (definitionEntries ??= []).push({
1147
1053
  blockIndex,
1148
1054
  key
@@ -1150,36 +1056,27 @@ var SyntaxState = class {
1150
1056
  definitions.add(key);
1151
1057
  }
1152
1058
  if (rule.inlineContent) {
1153
- const inlineView = inlineViewOf(source, arena, nodeId, tokenBase);
1059
+ const inlineView = inlineViewOf(source, structure, tokenStart);
1154
1060
  if (inlineView) bindings.push({
1155
- id: nodeId,
1061
+ offset,
1156
1062
  rule: rule.name,
1157
- span: {
1158
- start: offset,
1159
- end: offset + arena.lenOf(nodeId)
1160
- },
1063
+ tokenStart,
1161
1064
  view: inlineView
1162
1065
  });
1163
1066
  return;
1164
1067
  }
1165
- const childCount = arena.childCount(nodeId);
1166
- for (let index = 0; index < childCount; index++) {
1167
- const child = arena.childAt(nodeId, index);
1168
- if (child >= 0) collectNode(child, offset + arena.childRelAt(nodeId, index), tokenBase + arena.childTokRelAt(nodeId, index), blockIndex);
1169
- }
1068
+ for (let child = structure.firstChild(tokenStart); child !== noBlockEntry; child = structure.nextChild(tokenStart, child)) if (child >= 0) collectNode(child, structure.tokens.start(child), child, blockIndex);
1170
1069
  };
1171
1070
  const bindingOffsets = [];
1172
1071
  for (let index = stableBlockCount; index < newEnd; index++) {
1173
- const record = arena.records[index];
1072
+ const record = structure.records[index];
1174
1073
  const tokenBase = record.tokenStart;
1175
- const offset = arena.tokens.start(tokenBase);
1074
+ const offset = structure.tokens.start(tokenBase);
1176
1075
  bindingOffsets.push(bindings.length);
1177
- collectNode(record.id, offset, tokenBase, index);
1076
+ collectNode(record.tokenStart, offset, tokenBase, index);
1178
1077
  blocks.push({
1179
- offset,
1180
1078
  record,
1181
1079
  regions: emptyArray,
1182
- tokenBase,
1183
1080
  version: 0
1184
1081
  });
1185
1082
  }
@@ -1197,15 +1094,11 @@ var SyntaxState = class {
1197
1094
  definitions.add(entry.key);
1198
1095
  }
1199
1096
  }
1200
- const firstSuffixBlock = previousBlocks[oldEnd];
1201
- const firstSuffixRecord = arena.records[newEnd];
1202
- const suffixOffsetDelta = firstSuffixBlock && firstSuffixRecord ? arena.tokens.start(firstSuffixRecord.tokenStart) - firstSuffixBlock.offset : 0;
1203
- const suffixTokenDelta = firstSuffixBlock && firstSuffixRecord ? firstSuffixRecord.tokenStart - firstSuffixBlock.tokenBase : 0;
1097
+ const suffixOffsetDelta = source.length - this.#sourceLength;
1098
+ const suffixTokenDelta = change?.tokenDelta ?? 0;
1204
1099
  for (let index = oldEnd; index < previousBlocks.length; index++) {
1205
1100
  const block = previousBlocks[index];
1206
- if (suffixOffsetDelta !== 0) for (const region of block.regions) region.shift(suffixOffsetDelta);
1207
- block.offset += suffixOffsetDelta;
1208
- block.tokenBase += suffixTokenDelta;
1101
+ if (suffixOffsetDelta !== 0 || suffixTokenDelta !== 0) for (const region of block.regions) region.shift(suffixOffsetDelta, suffixTokenDelta);
1209
1102
  blocks.push(block);
1210
1103
  }
1211
1104
  const displacedRegions = [];
@@ -1226,12 +1119,12 @@ var SyntaxState = class {
1226
1119
  let nearestDistance = Number.POSITIVE_INFINITY;
1227
1120
  for (let index = 0; index < displacedRegions.length; index++) {
1228
1121
  const value = displacedRegions[index];
1229
- if (value.id === binding.id) {
1122
+ if (value.tokenStart === binding.tokenStart) {
1230
1123
  candidateIndex = index;
1231
1124
  break;
1232
1125
  }
1233
1126
  if (value.rule === binding.rule) {
1234
- const distance = Math.abs(value.span.start - binding.span.start);
1127
+ const distance = Math.abs(value.offset - binding.offset);
1235
1128
  if (distance < nearestDistance) {
1236
1129
  nearestDistance = distance;
1237
1130
  candidateIndex = index;
@@ -1264,66 +1157,56 @@ var SyntaxState = class {
1264
1157
  this.#blocks = blocks;
1265
1158
  this.#definitionEntries = definitionEntries;
1266
1159
  this.#definitions = definitions;
1160
+ this.#sourceLength = source.length;
1267
1161
  }
1268
1162
  };
1269
- function createInlineRegions(source, profile, arena) {
1163
+ function resolveInlineRegions(source, profile, structure) {
1270
1164
  const definitions = /* @__PURE__ */ new Set();
1271
1165
  const regions = [];
1272
- const collect = (nodeId, tokenBase) => {
1273
- const rule = arena.ruleOf(nodeId);
1274
- if (rule.definitionKey) definitions.add(rule.definitionKey(arena.tokens, tokenBase));
1166
+ const collect = (tokenStart, tokenBase) => {
1167
+ const rule = structure.ruleOf(tokenStart);
1168
+ if (rule.definitionKey) definitions.add(rule.definitionKey(structure.tokens, tokenBase));
1275
1169
  if (rule.inlineContent) {
1276
- const inlineView = inlineViewOf(source, arena, nodeId, tokenBase);
1170
+ const inlineView = inlineViewOf(source, structure, tokenStart);
1277
1171
  if (inlineView) regions.push({
1278
- id: nodeId,
1279
1172
  rule: rule.name,
1173
+ tokenStart,
1280
1174
  tokens: emptyArray,
1281
1175
  view: inlineView
1282
1176
  });
1283
1177
  return;
1284
1178
  }
1285
- const childCount = arena.childCount(nodeId);
1286
- for (let index = 0; index < childCount; index++) {
1287
- const child = arena.childAt(nodeId, index);
1288
- if (child >= 0) collect(child, tokenBase + arena.childTokRelAt(nodeId, index));
1289
- }
1290
- };
1291
- for (const record of arena.records) collect(record.id, record.tokenStart);
1292
- const context = {
1293
- hasDefinition: (key) => definitions.has(key),
1294
- tokenize: profile.tokenize
1179
+ for (let child = structure.firstChild(tokenStart); child !== noBlockEntry; child = structure.nextChild(tokenStart, child)) if (child >= 0) collect(child, child);
1295
1180
  };
1181
+ for (const record of structure.records) collect(record.tokenStart, record.tokenStart);
1182
+ const context = { hasDefinition: (key) => definitions.has(key) };
1296
1183
  for (const region of regions) {
1297
1184
  const text = region.view.text;
1298
1185
  region.tokens = profile.resolve(text, profile.tokenize(text), context);
1299
1186
  }
1300
1187
  return regions;
1301
1188
  }
1302
- function inlineViewOf(source, arena, nodeId, tokenBase) {
1189
+ function inlineViewOf(source, structure, tokenStart) {
1303
1190
  let firstStart = -1;
1304
1191
  let firstEnd = -1;
1305
1192
  let spans;
1306
- const childCount = arena.childCount(nodeId);
1307
- for (let index = 0; index < childCount; index++) {
1308
- const entry = arena.childAt(nodeId, index);
1309
- if (entry < 0) {
1310
- const token = arena.leafToken(entry, tokenBase);
1311
- if (arena.tokens.kind(token) === 1) {
1312
- const start = arena.tokens.start(token);
1313
- const end = arena.tokens.end(token);
1314
- if (firstStart < 0) {
1315
- firstStart = start;
1316
- firstEnd = end;
1317
- } else {
1318
- spans ??= [{
1319
- start: firstStart,
1320
- end: firstEnd
1321
- }];
1322
- spans.push({
1323
- start,
1324
- end
1325
- });
1326
- }
1193
+ for (let entry = structure.firstChild(tokenStart); entry !== noBlockEntry; entry = structure.nextChild(tokenStart, entry)) if (entry < 0) {
1194
+ const token = structure.leafToken(entry);
1195
+ if (structure.tokens.kind(token) === 1) {
1196
+ const start = structure.tokens.start(token);
1197
+ const end = structure.tokens.end(token);
1198
+ if (firstStart < 0) {
1199
+ firstStart = start;
1200
+ firstEnd = end;
1201
+ } else {
1202
+ spans ??= [{
1203
+ start: firstStart,
1204
+ end: firstEnd
1205
+ }];
1206
+ spans.push({
1207
+ start,
1208
+ end
1209
+ });
1327
1210
  }
1328
1211
  }
1329
1212
  }
@@ -1356,7 +1239,7 @@ function applyEdits(source, edits) {
1356
1239
  };
1357
1240
  }
1358
1241
  var DocumentImpl = class {
1359
- #blockArena;
1242
+ #blockStructure;
1360
1243
  #blockScanner;
1361
1244
  #fragments = [];
1362
1245
  #previousFragments;
@@ -1365,10 +1248,10 @@ var DocumentImpl = class {
1365
1248
  constructor(source, profile) {
1366
1249
  this.#profile = profile;
1367
1250
  this.#blockScanner = new BlockScanner(profile.block);
1368
- this.#blockArena = new BlockArena(profile.block.schema, this.#blockScanner.tokens);
1251
+ this.#blockStructure = new BlockStructure(profile.block.schema, this.#blockScanner.tokens);
1369
1252
  this.#blockScanner.scan(source);
1370
- this.#blockArena.build();
1371
- this.#syntaxState = new SyntaxState(source, profile.inline, this.#blockArena);
1253
+ this.#blockStructure.build();
1254
+ this.#syntaxState = new SyntaxState(source, profile.inline, this.#blockStructure);
1372
1255
  }
1373
1256
  get source() {
1374
1257
  return this.#blockScanner.source;
@@ -1386,8 +1269,8 @@ var DocumentImpl = class {
1386
1269
  this.#previousFragments = fragments;
1387
1270
  }
1388
1271
  const { stableBlockCount, tokenChange } = this.#blockScanner.edit(applied.source, applied.changedSpan, applied.oldChangedEnd);
1389
- const arenaChange = this.#blockArena.update(tokenChange);
1390
- this.#syntaxState.update(this.source, arenaChange, stableBlockCount);
1272
+ const structureChange = this.#blockStructure.update(tokenChange);
1273
+ this.#syntaxState.update(this.source, structureChange, stableBlockCount);
1391
1274
  return { changedSpan: applied.changedSpan };
1392
1275
  }
1393
1276
  snapshot() {
@@ -1401,36 +1284,37 @@ var DocumentImpl = class {
1401
1284
  const regions = [];
1402
1285
  for (const block of changedBlocks) for (const region of block.regions) regions.push(region);
1403
1286
  const context = {
1404
- arena: this.#blockArena,
1287
+ structure: this.#blockStructure,
1405
1288
  inline: new InlineRegionCursor(regions),
1406
1289
  profile: this.#profile.inline,
1407
1290
  source: this.source
1408
1291
  };
1409
1292
  const nextFragments = blocks.map((block) => {
1293
+ const offset = this.#blockStructure.tokens.start(block.record.tokenStart);
1410
1294
  const previous = previousFragments?.get(block.record);
1411
1295
  const fragment = previous?.version === block.version ? previous : {
1412
- node: buildBlockNode(block.record.id, block.offset, block.tokenBase, context),
1413
- offset: block.offset,
1414
- origin: block.offset,
1296
+ node: buildBlockNode(block.record.tokenStart, context),
1297
+ offset,
1298
+ origin: offset,
1415
1299
  version: block.version
1416
1300
  };
1417
- fragment.offset = block.offset;
1301
+ fragment.offset = offset;
1418
1302
  return fragment;
1419
1303
  });
1420
1304
  this.#previousFragments = void 0;
1421
1305
  this.#fragments = nextFragments;
1422
1306
  return nextFragments;
1423
1307
  }
1424
- static parse(source, profile, blockScanner, blockArena) {
1308
+ static parse(source, profile, blockScanner, blockStructure) {
1425
1309
  blockScanner.scan(source);
1426
- blockArena.build();
1310
+ blockStructure.build();
1427
1311
  const context = {
1428
- arena: blockArena,
1429
- inline: new InlineRegionCursor(createInlineRegions(source, profile.inline, blockArena)),
1312
+ structure: blockStructure,
1313
+ inline: new InlineRegionCursor(resolveInlineRegions(source, profile.inline, blockStructure)),
1430
1314
  profile: profile.inline,
1431
1315
  source
1432
1316
  };
1433
- return materialize(blockArena.records.map((block) => buildBlockNode(block.id, blockArena.tokens.start(block.tokenStart), block.tokenStart, context)), source.length, blockScanner.locator());
1317
+ return materialize(blockStructure.records.map((block) => buildBlockNode(block.tokenStart, context)), source.length, blockScanner.locator());
1434
1318
  }
1435
1319
  };
1436
1320
  //#endregion
@@ -1503,202 +1387,32 @@ function compileBlockProfile(features) {
1503
1387
  starts
1504
1388
  };
1505
1389
  }
1506
- function inlineTokenCount(tokens) {
1507
- return tokens.length / 4;
1508
- }
1509
- function inlineTokenKind(tokens, index) {
1510
- return tokens[index * 4];
1390
+ //#endregion
1391
+ //#region src/inline/lexer.ts
1392
+ function matchInlinePatternEnd(pattern, source, start) {
1393
+ pattern.lastIndex = start;
1394
+ const match = pattern.exec(source);
1395
+ return match === null ? -1 : start + match[0].length;
1511
1396
  }
1512
- function inlineTokenStart(tokens, index) {
1513
- return tokens[index * 4 + 1];
1397
+ function inlineMarkerRunEnd(source, start) {
1398
+ const marker = source.charCodeAt(start);
1399
+ let end = start + 1;
1400
+ while (source.charCodeAt(end) === marker) end++;
1401
+ return end;
1514
1402
  }
1515
- function inlineTokenEnd(tokens, index) {
1516
- return tokens[index * 4 + 2];
1403
+ function inlineTextEnd(source, start, boundary) {
1404
+ boundary.lastIndex = start;
1405
+ let match = boundary.exec(source);
1406
+ while (match !== null && source.charCodeAt(match.index) === 92) {
1407
+ const next = source.charCodeAt(match.index + 1);
1408
+ if (next !== 32 && next !== 9) break;
1409
+ boundary.lastIndex = match.index + 2;
1410
+ match = boundary.exec(source);
1411
+ }
1412
+ return match?.index ?? source.length;
1517
1413
  }
1518
- function inlineTokenFlags(tokens, index) {
1519
- return tokens[index * 4 + 3];
1520
- }
1521
- function setInlineTokenFlags(tokens, index, flags) {
1522
- tokens[index * 4 + 3] = flags;
1523
- }
1524
- function inlineTokenText(source, tokens, index) {
1525
- return source.slice(inlineTokenStart(tokens, index), inlineTokenEnd(tokens, index));
1526
- }
1527
- function appendInlineToken(target, kind, start, end, flags = 0) {
1528
- target.push(kind, start, end, flags);
1529
- }
1530
- function copyInlineToken(target, tokens, index) {
1531
- const offset = index * 4;
1532
- for (let field = 0; field < 4; field++) target.push(tokens[offset + field]);
1533
- }
1534
- function firstInlineTokenEndingAfter(tokens, offset) {
1535
- let low = 0;
1536
- let high = inlineTokenCount(tokens);
1537
- while (low < high) {
1538
- const middle = low + high >>> 1;
1539
- if (inlineTokenEnd(tokens, middle) <= offset) low = middle + 1;
1540
- else high = middle;
1541
- }
1542
- return low;
1543
- }
1544
- //#endregion
1545
- //#region src/inline/lexer.ts
1546
- const htmlComment = /<!-->|<!--->|<!--[\s\S]*?(?:-->|$)/y;
1547
- const autolink = /<(?:[A-Z][A-Z0-9+.\-]{1,31}:[^ \t\n\r<>]+|[\w!#$%&'*+\-/=?^`{|}~.]+@[A-Z0-9](?:[A-Z0-9]|-(?=[A-Z0-9]))*(?:\.[A-Z0-9](?:[A-Z0-9]|-(?=[A-Z0-9]))*)+)>/iy;
1548
- const inlineHtml = /<[A-Za-z][A-Za-z0-9-]*(?:[ \t\n\r]+[A-Za-z_:][\w.:-]*(?:[ \t\n\r]*=[ \t\n\r]*(?:[^ \t\n\r"'=<>`]+|'[^']*'|"[^"]*"))?)*[ \t\n\r]*\/?>|<\/[A-Za-z][A-Za-z0-9-]*[ \t\n\r]*>|<\?[\s\S]*?\?>|<![A-Z][\s\S]*?>|<!\[CDATA\[[\s\S]*?\]\]>/y;
1549
- const entity = /&(?:#x[0-9A-F]{1,6}|#\d{1,7}|[A-Z][A-Z0-9]{0,30});/iy;
1550
- function matchEnd(pattern, source, start) {
1551
- pattern.lastIndex = start;
1552
- const match = pattern.exec(source);
1553
- return match === null ? -1 : start + match[0].length;
1554
- }
1555
- function needsTextDecode(source, start, end) {
1556
- for (let index = start; index < end; index++) {
1557
- const code = source.charCodeAt(index);
1558
- if (code === 38 || code === 92) return true;
1559
- }
1560
- return false;
1561
- }
1562
- function runEnd(source, start, marker) {
1563
- let end = start + 1;
1564
- while (source.charCodeAt(end) === marker) end++;
1565
- return end;
1566
- }
1567
- function codeSpanEnd(source, start) {
1568
- if (source.charCodeAt(start - 1) === 96) return -1;
1569
- const openEnd = runEnd(source, start, 96);
1570
- const markerLength = openEnd - start;
1571
- let offset = openEnd;
1572
- while (offset < source.length) {
1573
- if (source.charCodeAt(offset) !== 96) {
1574
- offset++;
1575
- continue;
1576
- }
1577
- const closeEnd = runEnd(source, offset, 96);
1578
- if (closeEnd - offset === markerLength) return closeEnd;
1579
- offset = closeEnd;
1580
- }
1581
- return -1;
1582
- }
1583
- function skipWhitespace(source, start) {
1584
- let end = start;
1585
- while (end < source.length) {
1586
- const code = source.charCodeAt(end);
1587
- if (code !== 9 && code !== 10 && code !== 13 && code !== 32) break;
1588
- end++;
1589
- }
1590
- return end;
1591
- }
1592
- function linkDestinationEnd(source, start) {
1593
- if (source.charCodeAt(start) === 60) {
1594
- let end = start + 1;
1595
- while (end < source.length) {
1596
- const code = source.charCodeAt(end);
1597
- if (code === 62) return end + 1;
1598
- if (code === 10 || code === 13 || code === 60) return -1;
1599
- end += code === 92 && end + 1 < source.length ? 2 : 1;
1600
- }
1601
- return -1;
1602
- }
1603
- let depth = 0;
1604
- let end = start;
1605
- let consumed = false;
1606
- while (end < source.length) {
1607
- const code = source.charCodeAt(end);
1608
- if (code === 92) {
1609
- if (end + 1 >= source.length) break;
1610
- consumed = true;
1611
- end += 2;
1612
- continue;
1613
- }
1614
- if (code === 40) {
1615
- if (depth === 32) return -1;
1616
- depth++;
1617
- consumed = true;
1618
- end++;
1619
- continue;
1620
- }
1621
- if (code === 41) {
1622
- if (depth === 0) break;
1623
- depth--;
1624
- consumed = true;
1625
- end++;
1626
- continue;
1627
- }
1628
- if (code === 9 || code === 10 || code === 13 || code === 32) break;
1629
- consumed = true;
1630
- end++;
1631
- }
1632
- return consumed && depth === 0 ? end : -1;
1633
- }
1634
- function linkTitleEnd(source, start) {
1635
- const marker = source.charCodeAt(start);
1636
- const close = marker === 40 ? 41 : marker;
1637
- if (marker !== 34 && marker !== 39 && marker !== 40) return -1;
1638
- let end = start + 1;
1639
- while (end < source.length) {
1640
- const code = source.charCodeAt(end);
1641
- if (code === close) return end + 1;
1642
- if (code === 10 || code === 13) return -1;
1643
- end += code === 92 && end + 1 < source.length ? 2 : 1;
1644
- }
1645
- return -1;
1646
- }
1647
- function linkTailEnd(source, start) {
1648
- if (source.charCodeAt(start + 1) !== 40) return -1;
1649
- let offset = skipWhitespace(source, start + 2);
1650
- if (source.charCodeAt(offset) === 41) return offset + 1;
1651
- const destinationEnd = linkDestinationEnd(source, offset);
1652
- if (destinationEnd < 0) return -1;
1653
- offset = destinationEnd;
1654
- const whitespaceEnd = skipWhitespace(source, offset);
1655
- if (whitespaceEnd > offset && source.charCodeAt(whitespaceEnd) !== 41) {
1656
- const titleEnd = linkTitleEnd(source, whitespaceEnd);
1657
- if (titleEnd < 0) return -1;
1658
- offset = titleEnd;
1659
- } else offset = whitespaceEnd;
1660
- offset = skipWhitespace(source, offset);
1661
- return source.charCodeAt(offset) === 41 ? offset + 1 : -1;
1662
- }
1663
- function referenceTailEnd(source, start) {
1664
- if (source.charCodeAt(start + 1) !== 91) return -1;
1665
- let offset = start + 2;
1666
- if (source.charCodeAt(offset) === 93) return offset + 1;
1667
- let characters = 0;
1668
- let hasContent = false;
1669
- while (offset < source.length && characters < 999) {
1670
- const code = source.charCodeAt(offset);
1671
- if (code === 93) return hasContent ? offset + 1 : -1;
1672
- if (code === 91) return -1;
1673
- if (code === 92) {
1674
- if (offset + 1 >= source.length) return -1;
1675
- hasContent = true;
1676
- offset += 2;
1677
- } else {
1678
- hasContent ||= code !== 9 && code !== 10 && code !== 13 && code !== 32;
1679
- offset++;
1680
- }
1681
- characters++;
1682
- }
1683
- return -1;
1684
- }
1685
- function isAsciiPunctuation(code) {
1686
- return code >= 33 && code <= 47 || code >= 58 && code <= 64 || code >= 91 && code <= 96 || code >= 123 && code <= 126;
1687
- }
1688
- function textEnd(source, start, boundary) {
1689
- boundary.lastIndex = start;
1690
- let match = boundary.exec(source);
1691
- while (match !== null && source.charCodeAt(match.index) === 92) {
1692
- const next = source.charCodeAt(match.index + 1);
1693
- if (next !== 32 && next !== 9) break;
1694
- boundary.lastIndex = match.index + 2;
1695
- match = boundary.exec(source);
1696
- }
1697
- return match?.index ?? source.length;
1698
- }
1699
- function tokenizeInline(source) {
1414
+ function tokenize(source, textBoundary, lexicalByCode) {
1700
1415
  const tokens = [];
1701
- const textBoundary = / {2,}(?=[\n\r]|$)|[\n\r\\`*_[\]<!&~]/g;
1702
1416
  let offset = 0;
1703
1417
  let lineStart = true;
1704
1418
  while (offset < source.length) {
@@ -1723,14 +1437,14 @@ function tokenizeInline(source) {
1723
1437
  }
1724
1438
  }
1725
1439
  offset = content;
1726
- if (tokens.length > 0) tokens.push(39, offset, offset, 0);
1440
+ if (tokens.length > 0) tokens.push(42, offset, offset, 0);
1727
1441
  lineStart = false;
1728
1442
  continue;
1729
1443
  }
1730
1444
  const code = source.charCodeAt(offset);
1731
1445
  if (code === 32 || code === 9) {
1732
1446
  if (code === 32) {
1733
- const end = runEnd(source, offset, 32);
1447
+ const end = inlineMarkerRunEnd(source, offset);
1734
1448
  const next = source.charCodeAt(end);
1735
1449
  if (end - offset >= 2 && (next === 10 || next === 13)) {
1736
1450
  tokens.push(7, offset, end, 0);
@@ -1751,71 +1465,78 @@ function tokenizeInline(source) {
1751
1465
  lineStart = true;
1752
1466
  continue;
1753
1467
  }
1754
- let end = -1;
1755
- let kind = 38;
1756
- if (code === 60) {
1757
- end = matchEnd(htmlComment, source, offset);
1758
- kind = 1;
1759
- if (end < 0) {
1760
- end = matchEnd(autolink, source, offset);
1761
- kind = 4;
1762
- }
1763
- if (end < 0) {
1764
- end = matchEnd(inlineHtml, source, offset);
1765
- kind = 5;
1766
- }
1767
- } else if (code === 38) {
1768
- end = matchEnd(entity, source, offset);
1769
- kind = 6;
1770
- } else if (code === 92) {
1771
- const next = source.charCodeAt(offset + 1);
1772
- if (next === 10 || next === 13) {
1773
- end = offset + 1;
1774
- kind = 7;
1775
- } else if (isAsciiPunctuation(next)) {
1776
- end = offset + 2;
1777
- kind = 8;
1778
- } else if (next === 32 || next === 9) {
1779
- end = textEnd(source, offset, textBoundary);
1780
- kind = 9;
1781
- }
1782
- } else if (code === 96) {
1783
- end = codeSpanEnd(source, offset);
1784
- kind = 2;
1785
- } else if (code === 42 || code === 95 || code === 126) {
1786
- end = runEnd(source, offset, code);
1787
- kind = code === 42 ? 10 : code === 95 ? 11 : 12;
1788
- } else if (code === 33 && source.charCodeAt(offset + 1) === 91) {
1789
- end = offset + 2;
1790
- kind = 17;
1791
- } else if (code === 91) {
1792
- end = offset + 1;
1793
- kind = 18;
1794
- } else if (code === 93) {
1795
- end = linkTailEnd(source, offset);
1796
- kind = 19;
1797
- if (end < 0) {
1798
- end = referenceTailEnd(source, offset);
1799
- kind = 20;
1800
- }
1801
- if (end < 0) {
1802
- end = offset + 1;
1803
- kind = 21;
1468
+ const scan = lexicalByCode[code];
1469
+ if (scan) {
1470
+ const lexicalEnd = scan(source, offset, tokens);
1471
+ if (lexicalEnd > offset) {
1472
+ offset = lexicalEnd;
1473
+ continue;
1804
1474
  }
1805
- } else {
1806
- end = textEnd(source, offset, textBoundary);
1807
- kind = 9;
1808
- }
1809
- if (end <= offset) {
1810
- end = offset + 1;
1811
- kind = 38;
1475
+ const end = inlineTextEnd(source, offset + 1, textBoundary);
1476
+ tokens.push(9, offset, end, 0);
1477
+ offset = end;
1478
+ continue;
1812
1479
  }
1813
- const decode = kind === 8 || kind === 6 || (kind === 19 || kind === 20) && needsTextDecode(source, offset, end);
1814
- tokens.push(kind, offset, end, decode ? 16 : 0);
1480
+ const end = inlineTextEnd(source, offset, textBoundary);
1481
+ tokens.push(9, offset, end, 0);
1815
1482
  offset = end;
1816
1483
  }
1817
1484
  return tokens;
1818
1485
  }
1486
+ function compileInlineTokenizer(rules) {
1487
+ const lexicalByCode = [];
1488
+ const boundaryCodes = [];
1489
+ for (const rule of rules) {
1490
+ const code = rule.marker;
1491
+ const previous = lexicalByCode[code];
1492
+ lexicalByCode[code] = previous === void 0 ? rule.scan : (source, start, tokens) => {
1493
+ const end = previous(source, start, tokens);
1494
+ return end > start ? end : rule.scan(source, start, tokens);
1495
+ };
1496
+ if (previous === void 0) boundaryCodes.push(code);
1497
+ }
1498
+ const boundaries = String.fromCharCode(...boundaryCodes).replaceAll(/[\\\]-]/g, "\\$&");
1499
+ const textBoundary = new RegExp(` {2,}(?=[\\n\\r]|$)|[\\n\\r${boundaries}]`, "g");
1500
+ return (source) => tokenize(source, textBoundary, lexicalByCode);
1501
+ }
1502
+ function inlineTokenCount(tokens) {
1503
+ return tokens.length / 4;
1504
+ }
1505
+ function inlineTokenKind(tokens, index) {
1506
+ return tokens[index * 4];
1507
+ }
1508
+ function inlineTokenStart(tokens, index) {
1509
+ return tokens[index * 4 + 1];
1510
+ }
1511
+ function inlineTokenEnd(tokens, index) {
1512
+ return tokens[index * 4 + 2];
1513
+ }
1514
+ function inlineTokenFlags(tokens, index) {
1515
+ return tokens[index * 4 + 3];
1516
+ }
1517
+ function setInlineTokenFlags(tokens, index, flags) {
1518
+ tokens[index * 4 + 3] = flags;
1519
+ }
1520
+ function inlineTokenText(source, tokens, index) {
1521
+ return source.slice(inlineTokenStart(tokens, index), inlineTokenEnd(tokens, index));
1522
+ }
1523
+ function appendInlineToken(target, kind, start, end, flags = 0) {
1524
+ target.push(kind, start, end, flags);
1525
+ }
1526
+ function copyInlineToken(target, tokens, index) {
1527
+ const offset = index * 4;
1528
+ for (let field = 0; field < 4; field++) target.push(tokens[offset + field]);
1529
+ }
1530
+ function firstInlineTokenEndingAfter(tokens, offset) {
1531
+ let low = 0;
1532
+ let high = inlineTokenCount(tokens);
1533
+ while (low < high) {
1534
+ const middle = low + high >>> 1;
1535
+ if (inlineTokenEnd(tokens, middle) <= offset) low = middle + 1;
1536
+ else high = middle;
1537
+ }
1538
+ return low;
1539
+ }
1819
1540
  //#endregion
1820
1541
  //#region src/inline/pairing.ts
1821
1542
  const whitespace = /\s/u;
@@ -1963,9 +1684,9 @@ function resolvePairedTokens(source, tokens, index, state) {
1963
1684
  delimiterIsolations
1964
1685
  };
1965
1686
  }
1966
- function applyPairReplacements(tokens, replacements) {
1687
+ function applyPairReplacements(tokens, replacements, ownsTokens) {
1967
1688
  if (replacements.length === 0) return tokens;
1968
- const result = tokens.slice();
1689
+ const result = ownsTokens ? tokens : tokens.slice();
1969
1690
  for (let tokenIndex = 0; tokenIndex < replacements.length; tokenIndex++) {
1970
1691
  const kind = replacements[tokenIndex];
1971
1692
  if (kind !== void 0) {
@@ -2031,12 +1752,12 @@ function matchDelimiterRuns(runs, first, replacements) {
2031
1752
  }
2032
1753
  const opener = runs[openerIndex];
2033
1754
  const use = opener.remaining >= 2 && closer.remaining >= 2 && closer.config.double ? 2 : 1;
2034
- const openEnd = opener.offset + (opener.start + opener.remaining) * opener.config.marker.length;
2035
- const openStart = openEnd - use * opener.config.marker.length;
2036
- const closeStart = closer.offset + closer.start * closer.config.marker.length;
2037
- const closeEnd = closeStart + use * closer.config.marker.length;
1755
+ const openEnd = opener.offset + opener.start + opener.remaining;
1756
+ const openStart = openEnd - use;
1757
+ const closeStart = closer.offset + closer.start;
1758
+ const closeEnd = closeStart + use;
2038
1759
  const pair = use === 2 ? closer.config.double : closer.config.single;
2039
- if (!pair) throw new Error(`Delimiter ${closer.config.marker} has no replacement for a run of ${use}`);
1760
+ if (!pair) throw new Error(`Delimiter ${String.fromCharCode(closer.config.marker)} has no replacement for a run of ${use}`);
2040
1761
  addReplacement(replacements, opener.tokenIndex, {
2041
1762
  offset: openStart,
2042
1763
  end: openEnd,
@@ -2067,7 +1788,7 @@ function resolveDelimiterRuns(source, tokens, configByKind, isolations) {
2067
1788
  if (!config) continue;
2068
1789
  const offset = inlineTokenStart(tokens, tokenIndex);
2069
1790
  const end = inlineTokenEnd(tokens, tokenIndex);
2070
- const length = (end - offset) / config.marker.length;
1791
+ const length = end - offset;
2071
1792
  if (config.matchWholeRun && (length > 2 || (length === 1 ? !config.single : !config.double))) continue;
2072
1793
  const flags = flanking(source, offset, end, config);
2073
1794
  runs.push({
@@ -2100,10 +1821,11 @@ function resolveDelimiterRuns(source, tokens, configByKind, isolations) {
2100
1821
  }
2101
1822
  const replacements = [];
2102
1823
  for (const first of scopeFirst) if (first !== void 0) matchDelimiterRuns(runs, first, replacements);
1824
+ if (replacements.length === 0) return tokens;
2103
1825
  const result = [];
2104
1826
  for (let tokenIndex = 0; tokenIndex < count; tokenIndex++) {
2105
- const config = configByKind[inlineTokenKind(tokens, tokenIndex)];
2106
- if (!config) {
1827
+ const kind = inlineTokenKind(tokens, tokenIndex);
1828
+ if (!configByKind[kind]) {
2107
1829
  copyInlineToken(result, tokens, tokenIndex);
2108
1830
  continue;
2109
1831
  }
@@ -2111,12 +1833,12 @@ function resolveDelimiterRuns(source, tokens, configByKind, isolations) {
2111
1833
  if (matched && matched.length > 1) matched.sort((left, right) => left.offset - right.offset);
2112
1834
  let offset = inlineTokenStart(tokens, tokenIndex);
2113
1835
  if (matched) for (const replacement of matched) {
2114
- if (replacement.offset > offset) appendFragment(result, tokens, tokenIndex, offset, replacement.offset, config.fallbackKind);
1836
+ if (replacement.offset > offset) appendFragment(result, tokens, tokenIndex, offset, replacement.offset, kind);
2115
1837
  appendFragment(result, tokens, tokenIndex, replacement.offset, replacement.end, replacement.kind);
2116
1838
  offset = replacement.end;
2117
1839
  }
2118
1840
  const end = inlineTokenEnd(tokens, tokenIndex);
2119
- if (offset < end) appendFragment(result, tokens, tokenIndex, offset, end, config.fallbackKind);
1841
+ if (offset < end) appendFragment(result, tokens, tokenIndex, offset, end, kind);
2120
1842
  }
2121
1843
  return result;
2122
1844
  }
@@ -2125,7 +1847,6 @@ function createPairingResolver(delimiterConfigs, pairConfigs = []) {
2125
1847
  delimiterConfigs.forEach((config, index) => {
2126
1848
  delimiterByKind[config.token] = {
2127
1849
  marker: config.marker,
2128
- fallbackKind: config.fallbackToken,
2129
1850
  single: config.single ? {
2130
1851
  open: config.single.open,
2131
1852
  close: config.single.close
@@ -2152,7 +1873,7 @@ function createPairingResolver(delimiterConfigs, pairConfigs = []) {
2152
1873
  phasesByKind[openerKind] |= 1;
2153
1874
  phasesByKind[closerKind] |= 1;
2154
1875
  }
2155
- return (source, tokens, state) => {
1876
+ return (source, tokens, state, ownsTokens = false) => {
2156
1877
  const count = inlineTokenCount(tokens);
2157
1878
  let activePhases = 0;
2158
1879
  for (let tokenIndex = 0; tokenIndex < count; tokenIndex++) {
@@ -2176,50 +1897,13 @@ function createPairingResolver(delimiterConfigs, pairConfigs = []) {
2176
1897
  } else if (expanded) copyInlineToken(expanded, tokens, tokenIndex);
2177
1898
  }
2178
1899
  if (expanded) paired = resolvePairedTokens(source, expanded, pairIndex, state);
2179
- const resolvedPairs = applyPairReplacements(expanded ?? tokens, paired.replacements);
1900
+ const resolvedPairs = applyPairReplacements(expanded ?? tokens, paired.replacements, expanded !== void 0 || ownsTokens);
2180
1901
  return activePhases & 2 ? resolveDelimiterRuns(source, resolvedPairs, delimiterByKind, paired.delimiterIsolations) : resolvedPairs;
2181
1902
  };
2182
1903
  }
2183
1904
  //#endregion
2184
1905
  //#region src/inline/profile.ts
2185
- const ignoreInlineToken = () => false;
2186
- function compileInlineSyntax(definitions) {
2187
- const tokenBuilders = [];
2188
- const containerByKind = [];
2189
- const pairByOpenKind = [];
2190
- const registerToken = (kind, build) => {
2191
- tokenBuilders[kind] = build;
2192
- return kind;
2193
- };
2194
- for (const definition of definitions) {
2195
- if (definition.kind === "leaf") {
2196
- registerToken(definition.token, definition.build);
2197
- continue;
2198
- }
2199
- if (definition.kind === "container") {
2200
- const tokenKind = registerToken(definition.token, ignoreInlineToken);
2201
- containerByKind[tokenKind] = {
2202
- closeKind: registerToken(definition.close, ignoreInlineToken),
2203
- contentOpenKind: registerToken(definition.contentOpen, ignoreInlineToken),
2204
- build: definition.build
2205
- };
2206
- continue;
2207
- }
2208
- const openKind = registerToken(definition.open, ignoreInlineToken);
2209
- pairByOpenKind[openKind] = {
2210
- closeKind: registerToken(definition.close, ignoreInlineToken),
2211
- build: definition.build
2212
- };
2213
- }
2214
- return {
2215
- schema: {
2216
- containerByKind,
2217
- pairByOpenKind
2218
- },
2219
- tokenBuilders
2220
- };
2221
- }
2222
- function composeTransforms(...rewrites) {
1906
+ function composeRewrites(rewrites) {
2223
1907
  if (rewrites.length === 1) return rewrites[0];
2224
1908
  return (source, tokens, context) => {
2225
1909
  for (const rewrite of rewrites) tokens = rewrite(source, tokens, context);
@@ -2228,25 +1912,58 @@ function composeTransforms(...rewrites) {
2228
1912
  }
2229
1913
  function compileInlineProfile(features, decodeText) {
2230
1914
  const delimiters = [];
2231
- const syntax = [];
1915
+ const lexicalRules = [];
1916
+ const syntaxDefinitions = [];
2232
1917
  const pairs = [];
2233
- const postTransforms = [];
2234
- const transforms = [];
1918
+ const rewrites = [];
2235
1919
  for (const feature of features) {
2236
- if (feature.syntax) syntax.push(...feature.syntax);
2237
- const resolution = feature.resolution;
2238
- if (resolution) {
2239
- if (resolution.delimiters) delimiters.push(...resolution.delimiters);
2240
- if (resolution.pairs) pairs.push(...resolution.pairs);
2241
- if (resolution.transform) transforms.push(resolution.transform);
2242
- if (resolution.postTransform) postTransforms.push(resolution.postTransform);
1920
+ if (feature.delimiters) delimiters.push(...feature.delimiters);
1921
+ if (feature.lexical) lexicalRules.push(...feature.lexical);
1922
+ if (feature.pairs) pairs.push(...feature.pairs);
1923
+ if (feature.rewrite) rewrites.push(feature.rewrite);
1924
+ if (feature.syntax) syntaxDefinitions.push(...feature.syntax);
1925
+ }
1926
+ const tokenHandlers = [];
1927
+ const containerByKind = [];
1928
+ const pairByOpenKind = [];
1929
+ for (const definition of syntaxDefinitions) {
1930
+ if (definition.kind === "decorate") {
1931
+ tokenHandlers[definition.token] = definition.apply;
1932
+ continue;
2243
1933
  }
1934
+ if (definition.kind === "leaf") {
1935
+ tokenHandlers[definition.token] = definition.build;
1936
+ continue;
1937
+ }
1938
+ if (definition.kind === "container") containerByKind[definition.token] = {
1939
+ closeKind: definition.close,
1940
+ contentOpenKind: definition.contentOpen,
1941
+ build: definition.build
1942
+ };
1943
+ else pairByOpenKind[definition.open] = {
1944
+ closeKind: definition.close,
1945
+ build: definition.build
1946
+ };
1947
+ if (definition.isolateDelimiters) pairs.push({
1948
+ opener: definition.kind === "container" ? definition.contentOpen : definition.open,
1949
+ closer: definition.close,
1950
+ isolateDelimiters: true
1951
+ });
2244
1952
  }
1953
+ const pair = createPairingResolver(delimiters, pairs);
1954
+ const rewrite = rewrites.length === 0 ? void 0 : composeRewrites(rewrites);
2245
1955
  return {
2246
- ...compileInlineSyntax(syntax),
2247
1956
  decodeText,
2248
- resolve: composeTransforms(...transforms, ...postTransforms, createPairingResolver(delimiters, pairs)),
2249
- tokenize: tokenizeInline
1957
+ resolve: rewrite === void 0 ? pair : (source, tokens, context) => {
1958
+ const rewritten = rewrite(source, tokens, context);
1959
+ return pair(source, rewritten, context, rewritten !== tokens);
1960
+ },
1961
+ schema: {
1962
+ containerByKind,
1963
+ pairByOpenKind
1964
+ },
1965
+ tokenHandlers,
1966
+ tokenize: compileInlineTokenizer(lexicalRules)
2250
1967
  };
2251
1968
  }
2252
1969
  //#endregion
@@ -2255,182 +1972,15 @@ function extendSpan(value, end) {
2255
1972
  const node = value;
2256
1973
  node.position.end = Math.max(node.position.end, end);
2257
1974
  }
2258
- function firstChildStart(children) {
2259
- const first = children[0];
2260
- if (!first) throw new Error("mdast container unexpectedly has no children");
2261
- return first.position.start;
2262
- }
2263
- function lastChildEnd(children, emptyEnd) {
2264
- const last = children.at(-1);
2265
- return last ? last.position.end : emptyEnd;
2266
- }
2267
- //#endregion
2268
- //#region src/fragment/inline.ts
2269
- function lineStart(source, offset) {
2270
- while (offset > 0) {
2271
- const character = source.charCodeAt(--offset);
2272
- if (character === 10 || character === 13) return offset + 1;
2273
- }
2274
- return 0;
2275
- }
2276
- function lineEnd(source, offset, limit = source.length) {
2277
- for (; offset < limit; offset++) {
2278
- const character = source.charCodeAt(offset);
2279
- if (character === 10 || character === 13) return offset;
2280
- }
2281
- return limit;
2282
- }
2283
- function lineEndingStart(source, offset) {
2284
- const start = lineStart(source, offset);
2285
- if (start === 0) return offset;
2286
- return source[start - 1] === "\n" && source[start - 2] === "\r" ? start - 2 : start - 1;
2287
- }
2288
- function appendText(target, value, start, end) {
2289
- if (!value) return;
2290
- const previous = target.at(-1);
2291
- if (previous?.type === "text" && !("attributes" in previous)) {
2292
- previous.value += value;
2293
- extendSpan(previous, end);
2294
- } else target.push({
2295
- type: "text",
2296
- value,
2297
- position: {
2298
- start,
2299
- end
2300
- }
2301
- });
2302
- }
2303
- function appendPhrasing(target, value) {
2304
- if (value.type === "text") appendText(target, value.value, value.position.start, value.position.end);
2305
- else target.push(value);
2306
- }
2307
- function createInlineAccumulator(context, target, cursor) {
2308
- return {
2309
- context,
2310
- cursor,
2311
- gapEnd: -1,
2312
- gapStart: -1,
2313
- target
2314
- };
2315
- }
2316
- function appendInlineGap(accumulator, start, end) {
2317
- accumulator.gapStart = -1;
2318
- accumulator.gapEnd = -1;
2319
- const { context, target } = accumulator;
2320
- const gapSpan = context.view.mapSpan(start, end);
2321
- appendText(target, context.decodeText(context.view.text.slice(start, end).replace(/[\r\n]/g, "")), gapSpan.start, gapSpan.end);
2322
- }
2323
- function appendInline(accumulator, value) {
2324
- const { context, target } = accumulator;
2325
- const nextLineOffset = value.position.start;
2326
- const newline = value.type === "text" && value.value.startsWith("\n");
2327
- if (accumulator.gapStart >= 0) {
2328
- if (!newline) appendInlineGap(accumulator, accumulator.gapStart, accumulator.gapEnd);
2329
- else {
2330
- accumulator.gapStart = -1;
2331
- accumulator.gapEnd = -1;
2332
- }
2333
- }
2334
- if (newline) {
2335
- const previous = target.at(-1);
2336
- if (previous?.type === "break") {
2337
- extendSpan(previous, lineStart(context.source, nextLineOffset));
2338
- return;
2339
- }
2340
- value.position.start = lineEndingStart(context.source, nextLineOffset);
2341
- if (previous?.type === "text") previous.value = previous.value.slice(0, trailingWhitespaceStart(previous.value));
2342
- }
2343
- appendPhrasing(target, value);
2344
- }
2345
- function appendInlineLeaf(tokenIndex, sourceSpan, accumulator) {
2346
- const { context } = accumulator;
2347
- const build = context.tokenBuilders[inlineTokenKind(context.tokens, tokenIndex)];
2348
- if (!build) throw new Error(`Unexpected inline token kind ${inlineTokenKind(context.tokens, tokenIndex)}`);
2349
- return build(tokenIndex, sourceSpan, accumulator);
2350
- }
2351
- function trailingWhitespaceStart(value) {
2352
- let offset = value.length;
2353
- while (offset > 0 && (value[offset - 1] === " " || value[offset - 1] === " ")) offset--;
2354
- return offset;
2355
- }
2356
- function appendInlineRange(startToken, endToken, accumulator, closeKind) {
2357
- const { context } = accumulator;
2358
- let index = startToken;
2359
- while (index < endToken) {
2360
- if (inlineTokenKind(context.tokens, index) === closeKind) break;
2361
- const childOffset = inlineTokenStart(context.tokens, index);
2362
- if (accumulator.cursor !== void 0 && childOffset > accumulator.cursor) {
2363
- accumulator.gapStart = accumulator.cursor;
2364
- accumulator.gapEnd = childOffset;
2365
- }
2366
- const next = buildInlineSemantic(index, endToken, accumulator);
2367
- const childEnd = inlineTokenEnd(context.tokens, next === void 0 ? index : next - 1);
2368
- const childEmitted = next === void 0 ? appendInlineLeaf(index, context.view.mapSpan(childOffset, childEnd), accumulator) : true;
2369
- index = next ?? index + 1;
2370
- if (!childEmitted) continue;
2371
- accumulator.cursor = childEnd;
2372
- }
2373
- return index;
2374
- }
2375
- function buildInlineSemantic(openToken, endToken, accumulator) {
2376
- const { context } = accumulator;
2377
- const kind = inlineTokenKind(context.tokens, openToken);
2378
- const container = context.schema.containerByKind[kind];
2379
- if (container) {
2380
- let closeToken = openToken;
2381
- let next = openToken + 1;
2382
- const children = [];
2383
- if (next < endToken && inlineTokenKind(context.tokens, next) === container.contentOpenKind) {
2384
- const childAccumulator = createInlineAccumulator(context, children, inlineTokenEnd(context.tokens, next++));
2385
- closeToken = appendInlineRange(next, endToken, childAccumulator, container.closeKind);
2386
- if (closeToken >= endToken || inlineTokenKind(context.tokens, closeToken) !== container.closeKind) throw new Error(`Resolved inline stream did not close token kind ${kind}`);
2387
- const contentEnd = inlineTokenStart(context.tokens, closeToken);
2388
- if (childAccumulator.cursor !== void 0 && contentEnd > childAccumulator.cursor) appendInlineGap(childAccumulator, childAccumulator.cursor, contentEnd);
2389
- next = closeToken + 1;
2390
- }
2391
- container.build(openToken, closeToken, children, context.view.mapSpan(inlineTokenStart(context.tokens, openToken), inlineTokenEnd(context.tokens, closeToken)), accumulator);
2392
- return next;
2393
- }
2394
- const pair = context.schema.pairByOpenKind[kind];
2395
- if (!pair) return;
2396
- const contentStart = inlineTokenEnd(context.tokens, openToken);
2397
- const children = [];
2398
- const childAccumulator = createInlineAccumulator(context, children, contentStart);
2399
- const closeToken = appendInlineRange(openToken + 1, endToken, childAccumulator, pair.closeKind);
2400
- if (closeToken >= endToken || inlineTokenKind(context.tokens, closeToken) !== pair.closeKind) throw new Error(`Resolved inline stream did not close token kind ${kind}`);
2401
- const contentEnd = inlineTokenStart(context.tokens, closeToken);
2402
- if (childAccumulator.cursor !== void 0 && contentEnd > childAccumulator.cursor) appendInlineGap(childAccumulator, childAccumulator.cursor, contentEnd);
2403
- pair.build(openToken, closeToken, children, context.view.mapSpan(inlineTokenStart(context.tokens, openToken), inlineTokenEnd(context.tokens, closeToken)), accumulator);
2404
- return closeToken + 1;
2405
- }
2406
- function buildInlineChildren(nodeId, context, allowEmpty = false) {
2407
- const region = context.inline.take(nodeId);
2408
- if (!region) {
2409
- const rule = context.arena.ruleNameOf(nodeId);
2410
- if (allowEmpty) return [];
2411
- throw new Error(`Expected ${rule} syntax to contain inline content`);
2412
- }
2413
- const inlineContext = {
2414
- blockRule: region.rule,
2415
- decodeText: context.profile.decodeText,
2416
- schema: context.profile.schema,
2417
- source: context.source,
2418
- tokenBuilders: context.profile.tokenBuilders,
2419
- tokens: region.tokens,
2420
- view: region.view
2421
- };
2422
- const result = [];
2423
- appendInlineRange(0, inlineTokenCount(region.tokens), createInlineAccumulator(inlineContext, result));
2424
- const last = result.at(-1);
2425
- if (last?.type === "text") {
2426
- const end = trailingWhitespaceStart(last.value);
2427
- const removed = last.value.length - end;
2428
- last.value = last.value.slice(0, end);
2429
- last.position.end -= removed;
2430
- if (!last.value) result.pop();
2431
- }
2432
- return result;
2433
- }
1975
+ function firstChildStart(children) {
1976
+ const first = children[0];
1977
+ if (!first) throw new Error("mdast container unexpectedly has no children");
1978
+ return first.position.start;
1979
+ }
1980
+ function lastChildEnd(children, emptyEnd) {
1981
+ const last = children.at(-1);
1982
+ return last ? last.position.end : emptyEnd;
1983
+ }
2434
1984
  //#endregion
2435
1985
  //#region src/profile/features/attributes/syntax.ts
2436
1986
  const brackets = {
@@ -2448,8 +1998,8 @@ function isAsciiDigit(character) {
2448
1998
  const code = character.charCodeAt(0);
2449
1999
  return code >= 48 && code <= 57;
2450
2000
  }
2451
- function isSpace(character) {
2452
- return character === " " || character === " " || character === "\n" || character === "\r";
2001
+ function isHorizontalWhitespace(character) {
2002
+ return character === " " || character === " ";
2453
2003
  }
2454
2004
  function componentNameEnd(source, start, block) {
2455
2005
  if (!isAsciiLetter(source[start]) && source[start] !== "$") return;
@@ -2506,13 +2056,13 @@ function scanAttributes(source, start, attributes, limit = source.length) {
2506
2056
  let offset = start + 1;
2507
2057
  const readUntil = (stops) => {
2508
2058
  const valueStart = offset;
2509
- while (offset < limit && !stops.includes(source[offset])) if (source[offset] === "\\" && offset + 1 < limit) offset += 2;
2059
+ while (offset < limit && source[offset] !== "\n" && source[offset] !== "\r" && !stops.includes(source[offset])) if (source[offset] === "\\" && offset + 1 < limit) offset += 2;
2510
2060
  else offset++;
2511
2061
  return source.slice(valueStart, offset);
2512
2062
  };
2513
2063
  const readQuoted = (quote) => {
2514
2064
  const valueStart = ++offset;
2515
- while (offset < limit && source[offset] !== quote) offset += source[offset] === "\\" && offset + 1 < limit ? 2 : 1;
2065
+ while (offset < limit && source[offset] !== "\n" && source[offset] !== "\r" && source[offset] !== quote) offset += source[offset] === "\\" && offset + 1 < limit ? 2 : 1;
2516
2066
  if (source[offset] !== quote) return;
2517
2067
  const value = source.slice(valueStart, offset);
2518
2068
  offset++;
@@ -2524,6 +2074,7 @@ function scanAttributes(source, start, attributes, limit = source.length) {
2524
2074
  offset++;
2525
2075
  while (offset < limit && stack.length > 0) {
2526
2076
  const character = source[offset];
2077
+ if (character === "\n" || character === "\r") return;
2527
2078
  if (character === "\\" && offset + 1 < limit) {
2528
2079
  offset += 2;
2529
2080
  continue;
@@ -2540,7 +2091,7 @@ function scanAttributes(source, start, attributes, limit = source.length) {
2540
2091
  return stack.length === 0 ? source.slice(valueStart, offset) : void 0;
2541
2092
  };
2542
2093
  while (offset < limit) {
2543
- while (isSpace(source[offset])) offset++;
2094
+ while (isHorizontalWhitespace(source[offset])) offset++;
2544
2095
  if (source[offset] === "}") return offset + 1;
2545
2096
  const marker = source[offset];
2546
2097
  if (marker === "." || marker === "#") {
@@ -2556,13 +2107,13 @@ function scanAttributes(source, start, attributes, limit = source.length) {
2556
2107
  offset++;
2557
2108
  while (isAsciiLetter(source[offset]) || isAsciiDigit(source[offset]) || source[offset] === "_" || source[offset] === "-") offset++;
2558
2109
  const rawName = source.slice(nameStart, offset);
2559
- while (isSpace(source[offset])) offset++;
2110
+ while (isHorizontalWhitespace(source[offset])) offset++;
2560
2111
  if (source[offset] !== "=") {
2561
2112
  if (attributes) assignAttribute(attributes, rawName.startsWith(":") ? rawName : `:${rawName}`, "true");
2562
2113
  continue;
2563
2114
  }
2564
2115
  offset++;
2565
- while (isSpace(source[offset])) offset++;
2116
+ while (isHorizontalWhitespace(source[offset])) offset++;
2566
2117
  const character = source[offset];
2567
2118
  let value;
2568
2119
  if (character === "\"" || character === "'" || character === "`") value = readQuoted(character);
@@ -2605,14 +2156,14 @@ function takeTerminalAttributes(children) {
2605
2156
  }
2606
2157
  //#endregion
2607
2158
  //#region src/profile/features/attributes/index.ts
2608
- const decorateInlineContainer = (build) => (nodeId, offset, tokenBase, context) => {
2609
- const result = build(nodeId, offset, tokenBase, context);
2159
+ const decorateInlineContainer = (build) => (tokenStart, context) => {
2160
+ const result = build(tokenStart, context);
2610
2161
  const attributes = takeTerminalAttributes(result.children);
2611
2162
  if (attributes) result.attributes = attributes;
2612
2163
  return result;
2613
2164
  };
2614
- const decorateList = (build) => (nodeId, offset, tokenBase, context) => {
2615
- const result = build(nodeId, offset, tokenBase, context);
2165
+ const decorateList = (build) => (tokenStart, context) => {
2166
+ const result = build(tokenStart, context);
2616
2167
  if (!result.spread) for (const item of result.children) {
2617
2168
  const paragraph = !item.spread && item.children.length === 1 && item.children[0].type === "paragraph" ? item.children[0] : void 0;
2618
2169
  if (paragraph?.attributes) {
@@ -2622,8 +2173,8 @@ const decorateList = (build) => (nodeId, offset, tokenBase, context) => {
2622
2173
  }
2623
2174
  return result;
2624
2175
  };
2625
- const decorateBlockquote = (build) => (nodeId, offset, tokenBase, context) => {
2626
- const result = build(nodeId, offset, tokenBase, context);
2176
+ const decorateBlockquote = (build) => (tokenStart, context) => {
2177
+ const result = build(tokenStart, context);
2627
2178
  const paragraph = result.children.length === 1 && result.children[0].type === "paragraph" ? result.children[0] : void 0;
2628
2179
  if (paragraph?.attributes) {
2629
2180
  result.attributes = paragraph.attributes;
@@ -2638,73 +2189,39 @@ function hasVisibleText(source, start, end) {
2638
2189
  for (let offset = start; offset < end; offset++) if (!isMarkdownWhitespace(source.charCodeAt(offset))) return true;
2639
2190
  return false;
2640
2191
  }
2641
- function attributeSpansOf(source, tokens) {
2642
- const spans = [];
2643
- let consumedEnd = 0;
2644
- let hasContent = false;
2645
- let regionEnd = source.length;
2646
- const boundaries = [];
2647
- for (let index = 0; index < inlineTokenCount(tokens); index++) if (inlineTokenKind(tokens, index) === 40) boundaries.push(inlineTokenStart(tokens, index));
2648
- regionEnd = boundaries[0] ?? source.length;
2649
- let boundaryIndex = 0;
2650
- let attributeLineEnd = 0;
2651
- for (let index = 0; index < inlineTokenCount(tokens); index++) {
2652
- const kind = inlineTokenKind(tokens, index);
2653
- const start = inlineTokenStart(tokens, index);
2654
- const end = inlineTokenEnd(tokens, index);
2655
- while (boundaryIndex < boundaries.length && boundaries[boundaryIndex] <= start) {
2656
- regionEnd = boundaries[boundaryIndex + 1] ?? source.length;
2657
- boundaryIndex++;
2658
- hasContent = false;
2659
- attributeLineEnd = 0;
2660
- }
2661
- if (kind === 40 || end <= consumedEnd) continue;
2662
- if (kind !== 9) {
2663
- hasContent = true;
2664
- continue;
2665
- }
2666
- let cursor = Math.max(start, consumedEnd);
2667
- for (let offset = source.indexOf("{", cursor); offset >= cursor && offset < end;) {
2668
- if (hasVisibleText(source, cursor, offset)) hasContent = true;
2669
- const invalidPrefix = source[offset + 1] === "{" || source[offset - 1] === "{" || source[offset - 1] === "$";
2670
- if (!invalidPrefix && offset >= attributeLineEnd) attributeLineEnd = lineEnd(source, offset, regionEnd);
2671
- const parsedEnd = invalidPrefix ? void 0 : attributesEnd(source, offset, attributeLineEnd);
2672
- if (parsedEnd === void 0) {
2673
- hasContent = true;
2674
- cursor = offset + 1;
2675
- offset = source.indexOf("{", cursor);
2676
- continue;
2677
- }
2678
- if (!hasContent) {
2679
- cursor = parsedEnd;
2680
- offset = source.indexOf("{", cursor);
2681
- continue;
2682
- }
2683
- spans.push({
2684
- detached: offset > 0 && isMarkdownWhitespace(source.charCodeAt(offset - 1)),
2685
- start: offset,
2686
- end: parsedEnd
2687
- });
2688
- consumedEnd = parsedEnd;
2689
- cursor = parsedEnd;
2690
- offset = source.indexOf("{", cursor);
2691
- }
2692
- if (hasVisibleText(source, cursor, end)) hasContent = true;
2693
- }
2694
- return spans;
2192
+ function attributeEndAt(source, start) {
2193
+ return source[start + 1] === "{" || source[start - 1] === "{" || source[start - 1] === "$" ? void 0 : attributesEnd(source, start);
2695
2194
  }
2696
- function copyRange$2(target, tokens, start, end) {
2697
- for (let index = firstInlineTokenEndingAfter(tokens, start); index < inlineTokenCount(tokens); index++) {
2698
- const tokenStart = inlineTokenStart(tokens, index);
2699
- if (tokenStart >= end) break;
2700
- const tokenEnd = inlineTokenEnd(tokens, index);
2701
- const fragmentStart = Math.max(start, tokenStart);
2702
- const fragmentEnd = Math.min(end, tokenEnd);
2703
- if (fragmentStart === tokenStart && fragmentEnd === tokenEnd) copyInlineToken(target, tokens, index);
2704
- else appendInlineToken(target, 9, fragmentStart, fragmentEnd, fragmentStart === tokenStart ? inlineTokenFlags(tokens, index) : 0);
2195
+ function scanAttribute(source, start, tokens) {
2196
+ const end = attributeEndAt(source, start);
2197
+ if (end === void 0) return -1;
2198
+ if (tokens.length === 0) {
2199
+ let textEnd = end;
2200
+ while (true) {
2201
+ let next = textEnd;
2202
+ while (source.charCodeAt(next) === 9 || source.charCodeAt(next) === 32) next++;
2203
+ const nextEnd = source.charCodeAt(next) === 123 ? attributeEndAt(source, next) : void 0;
2204
+ if (nextEnd === void 0) break;
2205
+ textEnd = nextEnd;
2206
+ }
2207
+ appendInlineToken(tokens, 9, start, textEnd);
2208
+ return textEnd;
2209
+ }
2210
+ const previous = inlineTokenCount(tokens) - 1;
2211
+ const previousEnd = previous >= 0 ? inlineTokenEnd(tokens, previous) : start;
2212
+ if (previousEnd <= start && inlineTokenKind(tokens, previous) === 38) appendInlineToken(tokens, 9, previousEnd, start);
2213
+ appendInlineToken(tokens, 38, start, end, start > 0 && isMarkdownWhitespace(source.charCodeAt(start - 1)) ? 4 : 0);
2214
+ let next = end;
2215
+ while (isMarkdownWhitespace(source.charCodeAt(next))) next++;
2216
+ if (next === source.length) for (let index = inlineTokenCount(tokens) - 1; index >= 0; index--) {
2217
+ const kind = inlineTokenKind(tokens, index);
2218
+ if (kind === 9 && !hasVisibleText(source, inlineTokenStart(tokens, index), inlineTokenEnd(tokens, index))) continue;
2219
+ if (kind !== 38) break;
2220
+ setInlineTokenFlags(tokens, index, inlineTokenFlags(tokens, index) | 8);
2705
2221
  }
2222
+ return end;
2706
2223
  }
2707
- const feature$16 = {
2224
+ const feature$17 = {
2708
2225
  block: { decorators: [
2709
2226
  {
2710
2227
  rule: "Paragraph",
@@ -2732,19 +2249,22 @@ const feature$16 = {
2732
2249
  }
2733
2250
  ] },
2734
2251
  inline: {
2735
- resolution: { transform: transformAttributeTokens },
2252
+ lexical: [{
2253
+ marker: 123,
2254
+ scan: scanAttribute
2255
+ }],
2736
2256
  syntax: [{
2737
- kind: "leaf",
2738
- token: 35,
2739
- build(tokenIndex, sourceSpan, accumulator) {
2740
- const previous = accumulator.target.at(-1);
2741
- const parsed = parseAttributes(accumulator.context.view.text, inlineTokenStart(accumulator.context.tokens, tokenIndex));
2257
+ kind: "decorate",
2258
+ token: 38,
2259
+ apply(tokenIndex, sourceSpan, context, target) {
2260
+ const previous = target.at(-1);
2261
+ const parsed = parseAttributes(context.view.text, inlineTokenStart(context.tokens, tokenIndex));
2742
2262
  if (!previous || !parsed) return false;
2743
- const flags = inlineTokenFlags(accumulator.context.tokens, tokenIndex);
2263
+ const flags = inlineTokenFlags(context.tokens, tokenIndex);
2744
2264
  const terminal = Boolean(flags & 8);
2745
2265
  const detached = Boolean(flags & 4);
2746
- if (terminal && (detached || previous.type === "text" || hasTerminalAttributes(accumulator.target))) {
2747
- carryTerminalAttributes(accumulator.target, parsed.attributes);
2266
+ if (terminal && (detached || previous.type === "text" || hasTerminalAttributes(target))) {
2267
+ carryTerminalAttributes(target, parsed.attributes);
2748
2268
  return true;
2749
2269
  }
2750
2270
  if (previous.attributes) mergeAttributes(previous.attributes, parsed.attributes);
@@ -2755,28 +2275,49 @@ const feature$16 = {
2755
2275
  }]
2756
2276
  }
2757
2277
  };
2758
- function transformAttributeTokens(source, tokens) {
2759
- if (!source.includes("{")) return tokens;
2760
- const spans = attributeSpansOf(source, tokens);
2761
- if (spans.length === 0) return tokens;
2762
- const result = [];
2763
- let cursor = 0;
2764
- for (const span of spans) {
2765
- copyRange$2(result, tokens, cursor, span.start);
2766
- appendInlineToken(result, 35, span.start, span.end, span.detached ? 4 : 0);
2767
- cursor = span.end;
2768
- }
2769
- copyRange$2(result, tokens, cursor, source.length);
2770
- let terminal = true;
2771
- for (let index = inlineTokenCount(result) - 1; index >= 0; index--) {
2772
- const kind = inlineTokenKind(result, index);
2773
- if (kind === 40) terminal = true;
2774
- else if (kind === 35) {
2775
- if (terminal) setInlineTokenFlags(result, index, inlineTokenFlags(result, index) | 8);
2776
- } else if (kind !== 9 || hasVisibleText(source, inlineTokenStart(result, index), inlineTokenEnd(result, index))) terminal = false;
2777
- }
2778
- return result;
2779
- }
2278
+ //#endregion
2279
+ //#region src/profile/features/binding.ts
2280
+ function bindingEnd(source, start) {
2281
+ if (source.charCodeAt(start + 1) !== 123) return -1;
2282
+ const close = source.indexOf("}}", start + 2);
2283
+ if (close < 0) return -1;
2284
+ const separator = source.indexOf("||", start + 2);
2285
+ const valueEnd = separator < 0 || separator > close ? close : separator;
2286
+ return source.slice(start + 2, valueEnd).trim() ? close + 2 : -1;
2287
+ }
2288
+ const feature$16 = { inline: {
2289
+ lexical: [{
2290
+ marker: 123,
2291
+ scan(source, start, tokens) {
2292
+ const end = bindingEnd(source, start);
2293
+ if (end < 0) return -1;
2294
+ appendInlineToken(tokens, 34, start, end);
2295
+ return end;
2296
+ }
2297
+ }],
2298
+ syntax: [{
2299
+ kind: "leaf",
2300
+ token: 34,
2301
+ build(tokenIndex, sourceSpan, context) {
2302
+ const source = context.view.text;
2303
+ const start = inlineTokenStart(context.tokens, tokenIndex) + 2;
2304
+ const end = inlineTokenEnd(context.tokens, tokenIndex) - 2;
2305
+ const match = source.indexOf("||", start);
2306
+ const separator = match < 0 || match > end ? end : match;
2307
+ const value = source.slice(start, separator).trim();
2308
+ const defaultValue = separator === end ? "" : source.slice(separator + 2, end).trim();
2309
+ const attributes = { ":value": value };
2310
+ if (defaultValue) attributes.defaultValue = defaultValue;
2311
+ return {
2312
+ type: "inlineComponent",
2313
+ name: "binding",
2314
+ attributes,
2315
+ children: [],
2316
+ position: sourceSpan
2317
+ };
2318
+ }
2319
+ }]
2320
+ } };
2780
2321
  //#endregion
2781
2322
  //#region src/profile/features/blockquote.ts
2782
2323
  function blockQuoteOffset(source, line) {
@@ -2810,14 +2351,15 @@ const feature$15 = { block: {
2810
2351
  open: 3,
2811
2352
  close: 4
2812
2353
  },
2813
- build(nodeId, offset, tokenBase, context) {
2814
- const marker = blockToken(nodeId, tokenBase, 3, context);
2354
+ build(tokenStart, context) {
2355
+ const offset = context.structure.tokens.start(tokenStart);
2356
+ const marker = blockToken(tokenStart, 3, context);
2815
2357
  return {
2816
2358
  type: "blockquote",
2817
- children: buildBlockChildren(nodeId, offset, tokenBase, context),
2359
+ children: buildBlockChildren(tokenStart, context),
2818
2360
  position: {
2819
- start: firstNonspace(context.source, context.arena.tokens.start(marker), lineEnd(context.source, offset)),
2820
- end: blockEnd(nodeId, offset, context)
2361
+ start: firstNonspace(context.source, context.structure.tokens.start(marker), lineEnd(context.source, offset)),
2362
+ end: blockEnd(tokenStart, context)
2821
2363
  }
2822
2364
  };
2823
2365
  }
@@ -2878,13 +2420,14 @@ const feature$14 = {
2878
2420
  kind: "leaf",
2879
2421
  token: 2
2880
2422
  },
2881
- build(nodeId, offset, tokenBase, context) {
2882
- const end = offset + context.arena.lenOf(nodeId);
2423
+ build(tokenStart, context) {
2424
+ const offset = context.structure.tokens.start(tokenStart);
2425
+ const end = offset + context.structure.lenOf(tokenStart);
2883
2426
  return {
2884
2427
  type: "thematicBreak",
2885
2428
  position: {
2886
2429
  start: firstNonspace(context.source, offset, end),
2887
- end: blockEnd(nodeId, offset, context)
2430
+ end: blockEnd(tokenStart, context)
2888
2431
  }
2889
2432
  };
2890
2433
  }
@@ -2909,23 +2452,21 @@ const feature$14 = {
2909
2452
  inline: { syntax: [{
2910
2453
  kind: "leaf",
2911
2454
  token: 7,
2912
- build(tokenIndex, sourceSpan, accumulator) {
2913
- appendInline(accumulator, {
2455
+ build(tokenIndex, sourceSpan) {
2456
+ return {
2914
2457
  type: "break",
2915
2458
  position: sourceSpan
2916
- });
2917
- return true;
2459
+ };
2918
2460
  }
2919
2461
  }, {
2920
2462
  kind: "leaf",
2921
- token: 39,
2922
- build(tokenIndex, sourceSpan, accumulator) {
2923
- appendInline(accumulator, {
2463
+ token: 42,
2464
+ build(tokenIndex, sourceSpan) {
2465
+ return {
2924
2466
  type: "text",
2925
2467
  value: "\n",
2926
2468
  position: sourceSpan
2927
- });
2928
- return true;
2469
+ };
2929
2470
  }
2930
2471
  }] }
2931
2472
  };
@@ -2944,6 +2485,7 @@ function fenceAt$1(source, line, rule) {
2944
2485
  while (offset < line.end) if (source[offset++] === marker) return;
2945
2486
  }
2946
2487
  return {
2488
+ indent: indent.columns,
2947
2489
  marker,
2948
2490
  length,
2949
2491
  offset: indent.offset
@@ -2958,51 +2500,33 @@ function closesFence(source, line, fence) {
2958
2500
  while (offset < line.end && (source[offset] === " " || source[offset] === " ")) offset++;
2959
2501
  return offset === line.end;
2960
2502
  }
2961
- function lineContentEnd(source, start, end) {
2962
- if (end <= start) return end;
2963
- if (source.charCodeAt(end - 1) === 10) return end > start + 1 && source.charCodeAt(end - 2) === 13 ? end - 2 : end - 1;
2964
- return source.charCodeAt(end - 1) === 13 ? end - 1 : end;
2965
- }
2966
- function readFencedBlock(source, rule) {
2967
- if (!source) throw new Error("Fenced block token is empty");
2968
- let openingEnd = 0;
2969
- while (openingEnd < source.length && source[openingEnd] !== "\n" && source[openingEnd] !== "\r") openingEnd++;
2970
- let contentStart = openingEnd;
2971
- if (source[contentStart] === "\r") contentStart += source[contentStart + 1] === "\n" ? 2 : 1;
2972
- else if (source[contentStart] === "\n") contentStart++;
2973
- const fence = fenceAt$1(source, {
2974
- start: 0,
2975
- end: openingEnd,
2976
- next: contentStart
2977
- }, rule);
2978
- if (!fence) throw new Error("Fenced block token has no opening fence");
2979
- const finalLineEnd = lineContentEnd(source, 0, source.length);
2980
- let finalLineStart = finalLineEnd;
2981
- while (finalLineStart > 0 && source[finalLineStart - 1] !== "\n" && source[finalLineStart - 1] !== "\r") finalLineStart--;
2982
- const closed = finalLineStart >= contentStart && closesFence(source, {
2983
- start: finalLineStart,
2984
- end: finalLineEnd,
2985
- next: source.length
2986
- }, fence);
2503
+ function fencedBlock(source, line, fence, closed) {
2987
2504
  let infoStart = fence.offset + fence.length;
2988
- while (infoStart < openingEnd) {
2505
+ while (infoStart < line.end) {
2989
2506
  const code = source.charCodeAt(infoStart);
2990
2507
  if (code !== 32 && code !== 9) break;
2991
2508
  infoStart++;
2992
2509
  }
2993
2510
  return {
2994
2511
  closed,
2995
- contentEnd: closed ? finalLineStart : source.length,
2996
- contentStart,
2997
- indent: fence.offset,
2998
- info: source.slice(infoStart, openingEnd)
2512
+ indent: fence.indent,
2513
+ info: source.slice(infoStart, line.end)
2999
2514
  };
3000
2515
  }
3001
2516
  function fencedBlockContent(source, block, indentation = "spaces") {
3002
- const end = lineContentEnd(source, block.contentStart, block.contentEnd);
3003
- if (!block.indent) return source.slice(block.contentStart, end);
2517
+ let contentStart = 0;
2518
+ while (contentStart < source.length && source[contentStart] !== "\n" && source[contentStart] !== "\r") contentStart++;
2519
+ if (source[contentStart] === "\r") contentStart += source[contentStart + 1] === "\n" ? 2 : 1;
2520
+ else if (source[contentStart] === "\n") contentStart++;
2521
+ let contentEnd = source.length;
2522
+ if (block.closed) {
2523
+ contentEnd = lineContentEnd(source, contentStart, contentEnd);
2524
+ while (contentEnd > contentStart && source[contentEnd - 1] !== "\n" && source[contentEnd - 1] !== "\r") contentEnd--;
2525
+ }
2526
+ const end = lineContentEnd(source, contentStart, contentEnd);
2527
+ if (!block.indent) return source.slice(contentStart, end);
3004
2528
  const chunks = [];
3005
- let lineStart = block.contentStart;
2529
+ let lineStart = contentStart;
3006
2530
  while (lineStart < end) {
3007
2531
  const lf = source.indexOf("\n", lineStart);
3008
2532
  const cr = source.indexOf("\r", lineStart);
@@ -3022,37 +2546,74 @@ function fencedBlockContent(source, block, indentation = "spaces") {
3022
2546
  //#endregion
3023
2547
  //#region src/profile/features/text.ts
3024
2548
  const semanticCharacter = /\\([!"#$%&'()*+,./:;<=>?@[\\\]^_`{|}~-])|&(?:#x[\da-f]{1,6}|#\d{1,7}|[a-z][\da-z]{1,31});/gi;
2549
+ const entity = /&(?:#x[0-9A-F]{1,6}|#\d{1,7}|[A-Z][A-Z0-9]{0,30});/iy;
2550
+ function isAsciiPunctuation(code) {
2551
+ return code >= 33 && code <= 47 || code >= 58 && code <= 64 || code >= 91 && code <= 96 || code >= 123 && code <= 126;
2552
+ }
3025
2553
  function semanticText(value) {
3026
2554
  if (!value.includes("\\") && !value.includes("&")) return value;
3027
2555
  return value.replace(semanticCharacter, (match, escaped) => escaped ?? decodeHTMLStrict(match));
3028
2556
  }
3029
- const buildInlineText = (tokenIndex, sourceSpan, accumulator) => {
3030
- const { context } = accumulator;
2557
+ const buildInlineText = (tokenIndex, sourceSpan, context) => {
3031
2558
  const text = inlineTokenText(context.view.text, context.tokens, tokenIndex);
3032
- appendInline(accumulator, {
2559
+ return {
3033
2560
  type: "text",
3034
2561
  value: inlineTokenFlags(context.tokens, tokenIndex) & 16 ? semanticText(text) : text,
3035
2562
  position: sourceSpan
3036
- });
3037
- return true;
2563
+ };
3038
2564
  };
3039
- const feature$13 = { inline: { syntax: [
3040
- {
3041
- kind: "leaf",
3042
- token: 9,
3043
- build: buildInlineText
3044
- },
3045
- {
3046
- kind: "leaf",
3047
- token: 8,
3048
- build: buildInlineText
3049
- },
3050
- {
3051
- kind: "leaf",
3052
- token: 6,
3053
- build: buildInlineText
3054
- }
3055
- ] } };
2565
+ const feature$13 = { inline: {
2566
+ lexical: [{
2567
+ marker: 38,
2568
+ scan(source, start, tokens) {
2569
+ const matchEnd = matchInlinePatternEnd(entity, source, start);
2570
+ const end = matchEnd < 0 ? start + 1 : matchEnd;
2571
+ appendInlineToken(tokens, matchEnd < 0 ? 41 : 6, start, end, matchEnd < 0 ? 0 : 16);
2572
+ return end;
2573
+ }
2574
+ }, {
2575
+ marker: 92,
2576
+ scan(source, start, tokens) {
2577
+ const next = source.charCodeAt(start + 1);
2578
+ let end = start + 1;
2579
+ let flags = 0;
2580
+ let kind = 41;
2581
+ if (next === 10 || next === 13) kind = 7;
2582
+ else if (isAsciiPunctuation(next)) {
2583
+ end++;
2584
+ flags = 16;
2585
+ kind = 8;
2586
+ } else if (next === 32 || next === 9) {
2587
+ end++;
2588
+ kind = 9;
2589
+ }
2590
+ appendInlineToken(tokens, kind, start, end, flags);
2591
+ return end;
2592
+ }
2593
+ }],
2594
+ syntax: [
2595
+ {
2596
+ kind: "leaf",
2597
+ token: 9,
2598
+ build: buildInlineText
2599
+ },
2600
+ {
2601
+ kind: "leaf",
2602
+ token: 41,
2603
+ build: buildInlineText
2604
+ },
2605
+ {
2606
+ kind: "leaf",
2607
+ token: 8,
2608
+ build: buildInlineText
2609
+ },
2610
+ {
2611
+ kind: "leaf",
2612
+ token: 6,
2613
+ build: buildInlineText
2614
+ }
2615
+ ]
2616
+ } };
3056
2617
  //#endregion
3057
2618
  //#region src/profile/features/code.ts
3058
2619
  const codeFenceRule = {
@@ -3063,29 +2624,12 @@ const codeFenceRule = {
3063
2624
  function codeFenceAt(source, line) {
3064
2625
  return fenceAt$1(source, line, codeFenceRule);
3065
2626
  }
3066
- function codeSpanValue(value) {
3067
- const markerLength = /^`+/.exec(value)?.[0].length;
3068
- if (!markerLength) throw new Error("CodeSpan token does not start with a backtick run");
3069
- let result = normalizeLines(value.slice(markerLength, -markerLength));
3070
- if (/^[ \n]/.test(result) && /[ \n]$/.test(result) && /[^ \n]/.test(result)) result = result.slice(1, -1);
3071
- return result;
3072
- }
3073
- const buildInlineCode = (tokenIndex, sourceSpan, accumulator) => {
3074
- const { context } = accumulator;
3075
- const value = codeSpanValue(inlineTokenText(context.view.text, context.tokens, tokenIndex));
3076
- appendInline(accumulator, {
3077
- type: "inlineCode",
3078
- value: context.blockRule === "TableCell" ? value.replace(/\\\|/g, "|") : value,
3079
- position: sourceSpan
3080
- });
3081
- return true;
3082
- };
3083
- function indentedCodeEnd(nodeId, tokenBase, context) {
3084
- const token = blockToken(nodeId, tokenBase, 20, context);
3085
- const count = context.arena.tokens.rangeCount(token);
2627
+ function indentedCodeEnd(tokenStart, context) {
2628
+ const token = blockToken(tokenStart, 20, context);
2629
+ const count = context.structure.tokens.rangeCount(token);
3086
2630
  for (let index = count - 1; index >= 0; index--) {
3087
- const start = context.arena.tokens.rangeStart(token, index);
3088
- const rangeEnd = context.arena.tokens.rangeEnd(token, index);
2631
+ const start = context.structure.tokens.rangeStart(token, index);
2632
+ const rangeEnd = context.structure.tokens.rangeEnd(token, index);
3089
2633
  if (/[^\r\n]/.test(context.source.slice(start, rangeEnd))) {
3090
2634
  let end = rangeEnd;
3091
2635
  while (end > start && /[\r\n]/.test(context.source[end - 1])) end--;
@@ -3109,10 +2653,13 @@ const feature$12 = {
3109
2653
  kind: "leaf",
3110
2654
  token: 19
3111
2655
  },
3112
- build(nodeId, offset, tokenBase, context) {
3113
- const end = offset + context.arena.lenOf(nodeId);
3114
- const source = normalizeLines(context.arena.tokens.text(context.source, blockToken(nodeId, tokenBase, 19, context)));
3115
- const block = readFencedBlock(source, codeFenceRule);
2656
+ build(tokenStart, context) {
2657
+ const offset = context.structure.tokens.start(tokenStart);
2658
+ const end = offset + context.structure.lenOf(tokenStart);
2659
+ const token = blockToken(tokenStart, 19, context);
2660
+ const source = normalizeLines(context.structure.tokens.text(context.source, token));
2661
+ const block = context.structure.tokens.value(token);
2662
+ if (!block) throw new Error("FencedCodeBlock token has no fence metadata");
3116
2663
  const rawInfo = semanticText(block.info);
3117
2664
  const langEnd = rawInfo.search(/[ \t]/);
3118
2665
  const lang = rawInfo ? langEnd < 0 ? rawInfo : rawInfo.slice(0, langEnd) : null;
@@ -3124,7 +2671,7 @@ const feature$12 = {
3124
2671
  value: fencedBlockContent(source, block),
3125
2672
  position: {
3126
2673
  start: firstNonspace(context.source, offset, lineEnd(context.source, offset)),
3127
- end: block.closed || end < context.source.length ? blockEnd(nodeId, offset, context) : end
2674
+ end: block.closed || end < context.source.length ? blockEnd(tokenStart, context) : end
3128
2675
  }
3129
2676
  };
3130
2677
  }
@@ -3134,9 +2681,10 @@ const feature$12 = {
3134
2681
  kind: "leaf",
3135
2682
  token: 20
3136
2683
  },
3137
- build(nodeId, offset, tokenBase, context) {
3138
- const token = blockToken(nodeId, tokenBase, 20, context);
3139
- const lines = normalizeLines(context.arena.tokens.text(context.source, token)).split("\n").map((line) => removeIndent(line, 4));
2684
+ build(tokenStart, context) {
2685
+ const offset = context.structure.tokens.start(tokenStart);
2686
+ const token = blockToken(tokenStart, 20, context);
2687
+ const lines = normalizeLines(context.structure.tokens.text(context.source, token)).split("\n").map((line) => removeIndent(line, 4));
3140
2688
  while (lines.length && /^[ \t]*$/.test(lines[lines.length - 1])) lines.pop();
3141
2689
  return {
3142
2690
  type: "code",
@@ -3145,7 +2693,7 @@ const feature$12 = {
3145
2693
  value: lines.join("\n"),
3146
2694
  position: {
3147
2695
  start: offset,
3148
- end: indentedCodeEnd(nodeId, tokenBase, context)
2696
+ end: indentedCodeEnd(tokenStart, context)
3149
2697
  }
3150
2698
  };
3151
2699
  }
@@ -3160,19 +2708,216 @@ const feature$12 = {
3160
2708
  if (!fence) return;
3161
2709
  let end = start + 1;
3162
2710
  while (end < lines.length && !closesFence(source, lines[end], fence)) end++;
2711
+ const closed = end < lines.length;
3163
2712
  if (end < lines.length) end++;
3164
- appendLogicalToken(out, 19, source, lines, start, end);
2713
+ appendLogicalToken(out, 19, source, lines, start, end, fencedBlock(source, lines[start], fence, closed));
3165
2714
  return end;
3166
2715
  }
3167
2716
  }]
3168
2717
  },
3169
- inline: { syntax: [{
3170
- kind: "leaf",
3171
- token: 2,
3172
- build: buildInlineCode
3173
- }] }
2718
+ inline: {
2719
+ lexical: [{
2720
+ marker: 96,
2721
+ scan(source, start, tokens) {
2722
+ let end = -1;
2723
+ if (source.charCodeAt(start - 1) !== 96) {
2724
+ const openEnd = inlineMarkerRunEnd(source, start);
2725
+ const markerLength = openEnd - start;
2726
+ let offset = openEnd;
2727
+ while (offset < source.length) {
2728
+ if (source.charCodeAt(offset) !== 96) {
2729
+ offset++;
2730
+ continue;
2731
+ }
2732
+ const closeEnd = inlineMarkerRunEnd(source, offset);
2733
+ if (closeEnd - offset === markerLength) {
2734
+ end = closeEnd;
2735
+ break;
2736
+ }
2737
+ offset = closeEnd;
2738
+ }
2739
+ }
2740
+ if (end < 0) end = start + 1;
2741
+ appendInlineToken(tokens, end === start + 1 ? 41 : 2, start, end);
2742
+ return end;
2743
+ }
2744
+ }],
2745
+ syntax: [{
2746
+ kind: "leaf",
2747
+ token: 2,
2748
+ build: (tokenIndex, sourceSpan, context) => {
2749
+ const text = inlineTokenText(context.view.text, context.tokens, tokenIndex);
2750
+ const markerLength = /^`+/.exec(text)?.[0].length;
2751
+ if (!markerLength) throw new Error("CodeSpan token does not start with a backtick run");
2752
+ let value = normalizeLines(text.slice(markerLength, -markerLength));
2753
+ if (/^[ \n]/.test(value) && /[ \n]$/.test(value) && /[^ \n]/.test(value)) value = value.slice(1, -1);
2754
+ return {
2755
+ type: "inlineCode",
2756
+ value: context.blockRule === "TableCell" ? value.replace(/\\\|/g, "|") : value,
2757
+ position: sourceSpan
2758
+ };
2759
+ }
2760
+ }]
2761
+ }
3174
2762
  };
3175
2763
  //#endregion
2764
+ //#region src/fragment/inline.ts
2765
+ function lineStart(source, offset) {
2766
+ while (offset > 0) {
2767
+ const character = source.charCodeAt(--offset);
2768
+ if (character === 10 || character === 13) return offset + 1;
2769
+ }
2770
+ return 0;
2771
+ }
2772
+ function lineEndingStart(source, offset) {
2773
+ const start = lineStart(source, offset);
2774
+ if (start === 0) return offset;
2775
+ return source[start - 1] === "\n" && source[start - 2] === "\r" ? start - 2 : start - 1;
2776
+ }
2777
+ function appendText(target, value, start, end) {
2778
+ if (!value) return;
2779
+ const previous = target.at(-1);
2780
+ if (previous?.type === "text" && !("attributes" in previous)) {
2781
+ previous.value += value;
2782
+ extendSpan(previous, end);
2783
+ } else target.push({
2784
+ type: "text",
2785
+ value,
2786
+ position: {
2787
+ start,
2788
+ end
2789
+ }
2790
+ });
2791
+ }
2792
+ function appendPhrasing(target, value) {
2793
+ if (value.type === "text") appendText(target, value.value, value.position.start, value.position.end);
2794
+ else target.push(value);
2795
+ }
2796
+ function appendInlineGap(output, context, start, end) {
2797
+ output.gapStart = -1;
2798
+ output.gapEnd = -1;
2799
+ const gapSpan = context.view.mapSpan(start, end);
2800
+ appendText(output.target, context.decodeText(context.view.text.slice(start, end).replace(/[\r\n]/g, "")), gapSpan.start, gapSpan.end);
2801
+ }
2802
+ function appendInline(output, context, value) {
2803
+ const { target } = output;
2804
+ const nextLineOffset = value.position.start;
2805
+ const newline = value.type === "text" && value.value.startsWith("\n");
2806
+ if (output.gapStart >= 0) {
2807
+ if (!newline) appendInlineGap(output, context, output.gapStart, output.gapEnd);
2808
+ else {
2809
+ output.gapStart = -1;
2810
+ output.gapEnd = -1;
2811
+ }
2812
+ }
2813
+ if (newline) {
2814
+ const previous = target.at(-1);
2815
+ if (previous?.type === "break") {
2816
+ extendSpan(previous, lineStart(context.source, nextLineOffset));
2817
+ return;
2818
+ }
2819
+ value.position.start = lineEndingStart(context.source, nextLineOffset);
2820
+ if (previous?.type === "text") previous.value = previous.value.slice(0, trailingWhitespaceStart(previous.value));
2821
+ }
2822
+ appendPhrasing(target, value);
2823
+ }
2824
+ function appendInlineLeaf(tokenIndex, sourceSpan, output, context) {
2825
+ const kind = inlineTokenKind(context.tokens, tokenIndex);
2826
+ const handle = context.tokenHandlers[kind];
2827
+ if (!handle) throw new Error(`Unexpected inline token kind ${kind}`);
2828
+ const value = handle(tokenIndex, sourceSpan, context, output.target);
2829
+ if (typeof value === "boolean") return value;
2830
+ if (!value) return false;
2831
+ appendInline(output, context, value);
2832
+ return true;
2833
+ }
2834
+ function trailingWhitespaceStart(value) {
2835
+ let offset = value.length;
2836
+ while (offset > 0 && (value[offset - 1] === " " || value[offset - 1] === " ")) offset--;
2837
+ return offset;
2838
+ }
2839
+ function appendInlineRange(startToken, endToken, context, target, cursor, closeKind) {
2840
+ const output = {
2841
+ cursor,
2842
+ gapEnd: -1,
2843
+ gapStart: -1,
2844
+ target
2845
+ };
2846
+ let index = startToken;
2847
+ while (index < endToken) {
2848
+ if (inlineTokenKind(context.tokens, index) === closeKind) break;
2849
+ const childOffset = inlineTokenStart(context.tokens, index);
2850
+ if (output.cursor !== void 0 && childOffset > output.cursor) {
2851
+ output.gapStart = output.cursor;
2852
+ output.gapEnd = childOffset;
2853
+ }
2854
+ const next = buildInlineSemantic(index, endToken, output, context);
2855
+ const childEnd = inlineTokenEnd(context.tokens, next === void 0 ? index : next - 1);
2856
+ const childEmitted = next === void 0 ? appendInlineLeaf(index, context.view.mapSpan(childOffset, childEnd), output, context) : true;
2857
+ index = next ?? index + 1;
2858
+ if (!childEmitted) continue;
2859
+ output.cursor = childEnd;
2860
+ }
2861
+ if (closeKind !== void 0 && index < endToken && output.cursor !== void 0) {
2862
+ const contentEnd = inlineTokenStart(context.tokens, index);
2863
+ if (contentEnd > output.cursor) appendInlineGap(output, context, output.cursor, contentEnd);
2864
+ }
2865
+ return index;
2866
+ }
2867
+ function buildInlineSemantic(openToken, endToken, output, context) {
2868
+ const kind = inlineTokenKind(context.tokens, openToken);
2869
+ const container = context.schema.containerByKind[kind];
2870
+ if (container) {
2871
+ let closeToken = openToken;
2872
+ let next = openToken + 1;
2873
+ const children = [];
2874
+ if (next < endToken && inlineTokenKind(context.tokens, next) === container.contentOpenKind) {
2875
+ const contentStart = inlineTokenEnd(context.tokens, next++);
2876
+ closeToken = appendInlineRange(next, endToken, context, children, contentStart, container.closeKind);
2877
+ if (closeToken >= endToken || inlineTokenKind(context.tokens, closeToken) !== container.closeKind) throw new Error(`Resolved inline stream did not close token kind ${kind}`);
2878
+ next = closeToken + 1;
2879
+ }
2880
+ appendInline(output, context, container.build(openToken, closeToken, context.view.mapSpan(inlineTokenStart(context.tokens, openToken), inlineTokenEnd(context.tokens, closeToken)), children, context));
2881
+ return next;
2882
+ }
2883
+ const pair = context.schema.pairByOpenKind[kind];
2884
+ if (!pair) return;
2885
+ const contentStart = inlineTokenEnd(context.tokens, openToken);
2886
+ const children = [];
2887
+ const closeToken = appendInlineRange(openToken + 1, endToken, context, children, contentStart, pair.closeKind);
2888
+ if (closeToken >= endToken || inlineTokenKind(context.tokens, closeToken) !== pair.closeKind) throw new Error(`Resolved inline stream did not close token kind ${kind}`);
2889
+ appendInline(output, context, pair.build(openToken, closeToken, context.view.mapSpan(inlineTokenStart(context.tokens, openToken), inlineTokenEnd(context.tokens, closeToken)), children, context));
2890
+ return closeToken + 1;
2891
+ }
2892
+ function buildInlineChildren(tokenStart, context, allowEmpty = false) {
2893
+ const region = context.inline.take(tokenStart);
2894
+ if (!region) {
2895
+ const rule = context.structure.ruleNameOf(tokenStart);
2896
+ if (allowEmpty) return [];
2897
+ throw new Error(`Expected ${rule} syntax to contain inline content`);
2898
+ }
2899
+ const inlineContext = {
2900
+ blockRule: region.rule,
2901
+ decodeText: context.profile.decodeText,
2902
+ schema: context.profile.schema,
2903
+ source: context.source,
2904
+ tokenHandlers: context.profile.tokenHandlers,
2905
+ tokens: region.tokens,
2906
+ view: region.view
2907
+ };
2908
+ const result = [];
2909
+ appendInlineRange(0, inlineTokenCount(region.tokens), inlineContext, result);
2910
+ const last = result.at(-1);
2911
+ if (last?.type === "text") {
2912
+ const end = trailingWhitespaceStart(last.value);
2913
+ const removed = last.value.length - end;
2914
+ last.value = last.value.slice(0, end);
2915
+ last.position.end -= removed;
2916
+ if (!last.value) result.pop();
2917
+ }
2918
+ return result;
2919
+ }
2920
+ //#endregion
3176
2921
  //#region src/profile/features/component/block.ts
3177
2922
  function skipSpaces(source, offset, end) {
3178
2923
  while (offset < end && (source[offset] === " " || source[offset] === " ")) offset++;
@@ -3355,31 +3100,24 @@ function emitOpening(source, contentOffset, opening, out) {
3355
3100
  if (opening.attributesStart !== void 0 && opening.attributesEnd !== void 0) out.push(43, opening.attributesStart, opening.attributesEnd);
3356
3101
  }
3357
3102
  function parseYamlAttributes(token, context) {
3358
- const value = parse(context.arena.tokens.text(context.source, token), { schema: "core" });
3103
+ const value = parse(context.structure.tokens.text(context.source, token), { schema: "core" });
3359
3104
  if (value === null || value === void 0) return {};
3360
3105
  if (typeof value !== "object" || Array.isArray(value)) throw new TypeError("Component YAML props must be a mapping");
3361
3106
  return value;
3362
3107
  }
3363
- function directRule(nodeId, offset, tokenBase, rule, context) {
3364
- const arena = context.arena;
3365
- for (let index = 0; index < arena.childCount(nodeId); index++) {
3366
- const child = arena.childAt(nodeId, index);
3367
- if (child >= 0 && arena.ruleNameOf(child) === rule) return {
3368
- id: child,
3369
- offset: offset + arena.childRelAt(nodeId, index),
3370
- tokenBase: tokenBase + arena.childTokRelAt(nodeId, index)
3371
- };
3372
- }
3108
+ function directRule(tokenStart, rule, context) {
3109
+ const structure = context.structure;
3110
+ for (let child = structure.firstChild(tokenStart); child !== noBlockEntry; child = structure.nextChild(tokenStart, child)) if (child >= 0 && structure.ruleNameOf(child) === rule) return child;
3373
3111
  }
3374
- const buildBlockLabel = (nodeId, offset, tokenBase, context) => {
3375
- const open = blockToken(nodeId, tokenBase, 41, context);
3376
- const close = blockToken(nodeId, tokenBase, 42, context);
3112
+ const buildBlockLabel = (tokenStart, context) => {
3113
+ const open = blockToken(tokenStart, 41, context);
3114
+ const close = blockToken(tokenStart, 42, context);
3377
3115
  return {
3378
3116
  type: "paragraph",
3379
- children: buildInlineChildren(nodeId, context, true),
3117
+ children: buildInlineChildren(tokenStart, context, true),
3380
3118
  position: {
3381
- start: context.arena.tokens.start(open),
3382
- end: context.arena.tokens.end(close)
3119
+ start: context.structure.tokens.start(open),
3120
+ end: context.structure.tokens.end(close)
3383
3121
  }
3384
3122
  };
3385
3123
  };
@@ -3418,17 +3156,17 @@ const blockRules$2 = [
3418
3156
  open: 39,
3419
3157
  close: 40
3420
3158
  },
3421
- build(nodeId, offset, tokenBase, context) {
3422
- const open = blockToken(nodeId, tokenBase, 39, context);
3423
- const close = blockToken(nodeId, tokenBase, 40, context);
3424
- const attributesToken = directBlockToken(nodeId, tokenBase, 43, context);
3425
- const parsed = attributesToken !== void 0 ? parseAttributes(context.source, context.arena.tokens.start(attributesToken)) : void 0;
3426
- const yamlToken = directBlockToken(nodeId, tokenBase, 44, context);
3427
- const label = directRule(nodeId, offset, tokenBase, "BlockComponentLabel", context);
3159
+ build(tokenStart, context) {
3160
+ const open = blockToken(tokenStart, 39, context);
3161
+ const close = blockToken(tokenStart, 40, context);
3162
+ const attributesToken = directBlockToken(tokenStart, 43, context);
3163
+ const parsed = attributesToken !== void 0 ? parseAttributes(context.source, context.structure.tokens.start(attributesToken)) : void 0;
3164
+ const yamlToken = directBlockToken(tokenStart, 44, context);
3165
+ const label = directRule(tokenStart, "BlockComponentLabel", context);
3428
3166
  const children = [];
3429
- if (label) children.push(buildBlockLabel(label.id, label.offset, label.tokenBase, context));
3430
- children.push(...buildBlockChildren(nodeId, offset, tokenBase, context));
3431
- const opening = context.arena.tokens.text(context.source, open);
3167
+ if (label) children.push(buildBlockLabel(label, context));
3168
+ children.push(...buildBlockChildren(tokenStart, context));
3169
+ const opening = context.structure.tokens.text(context.source, open);
3432
3170
  return {
3433
3171
  type: "blockComponent",
3434
3172
  name: normalizeComponentName(opening.slice(opening.lastIndexOf(":") + 1).trim()),
@@ -3437,8 +3175,8 @@ const blockRules$2 = [
3437
3175
  ...parsed?.attributes
3438
3176
  } : parsed?.attributes ?? {},
3439
3177
  position: {
3440
- start: context.arena.tokens.start(open),
3441
- end: context.arena.tokens.end(close)
3178
+ start: context.structure.tokens.start(open),
3179
+ end: context.structure.tokens.end(close)
3442
3180
  },
3443
3181
  children
3444
3182
  };
@@ -3451,23 +3189,23 @@ const blockRules$2 = [
3451
3189
  open: 45,
3452
3190
  close: 46
3453
3191
  },
3454
- build(nodeId, offset, tokenBase, context) {
3455
- const open = blockToken(nodeId, tokenBase, 45, context);
3456
- const close = blockToken(nodeId, tokenBase, 46, context);
3457
- const attributesToken = directBlockToken(nodeId, tokenBase, 43, context);
3458
- const parsed = attributesToken !== void 0 ? parseAttributes(context.source, context.arena.tokens.start(attributesToken)) : void 0;
3459
- const children = buildBlockChildren(nodeId, offset, tokenBase, context);
3192
+ build(tokenStart, context) {
3193
+ const open = blockToken(tokenStart, 45, context);
3194
+ const close = blockToken(tokenStart, 46, context);
3195
+ const attributesToken = directBlockToken(tokenStart, 43, context);
3196
+ const parsed = attributesToken !== void 0 ? parseAttributes(context.source, context.structure.tokens.start(attributesToken)) : void 0;
3197
+ const children = buildBlockChildren(tokenStart, context);
3460
3198
  return {
3461
3199
  type: "blockComponent",
3462
3200
  name: "template",
3463
3201
  attributes: {
3464
3202
  ...parsed?.attributes,
3465
- name: normalizeComponentName(context.arena.tokens.text(context.source, open).slice(1).trim())
3203
+ name: normalizeComponentName(context.structure.tokens.text(context.source, open).slice(1).trim())
3466
3204
  },
3467
3205
  children,
3468
3206
  position: {
3469
- start: context.arena.tokens.start(open),
3470
- end: context.arena.tokens.end(close)
3207
+ start: context.structure.tokens.start(open),
3208
+ end: context.structure.tokens.end(close)
3471
3209
  }
3472
3210
  };
3473
3211
  }
@@ -3485,177 +3223,141 @@ const blockStarts$2 = [{
3485
3223
  }];
3486
3224
  //#endregion
3487
3225
  //#region src/profile/features/component/inline.ts
3488
- function bracketIndex(tokens) {
3489
- const stack = [];
3490
- const pairs = /* @__PURE__ */ new Map();
3491
- const linkLabels = [];
3492
- const normalClosers = /* @__PURE__ */ new Set();
3493
- const referenceSuffixes = /* @__PURE__ */ new Map();
3494
- for (let index = 0; index < inlineTokenCount(tokens); index++) {
3495
- const kind = inlineTokenKind(tokens, index);
3496
- if (kind === 40) stack.length = 0;
3497
- else if (kind === 18) stack.push({
3498
- image: false,
3499
- start: inlineTokenStart(tokens, index)
3500
- });
3501
- else if (kind === 17) stack.push({
3502
- image: true,
3503
- start: inlineTokenEnd(tokens, index) - 1
3504
- });
3505
- else if (kind === 21 || kind === 19 || kind === 20) {
3506
- const open = stack.pop();
3507
- if (open !== void 0) {
3508
- const close = inlineTokenStart(tokens, index);
3509
- if (!open.image) {
3510
- pairs.set(open.start, close);
3511
- normalClosers.add(close);
3512
- if (kind === 19 || kind === 20) {
3513
- let nested = linkLabels.at(-1);
3514
- while (nested && nested.start > open.start) {
3515
- linkLabels.pop();
3516
- nested = linkLabels.at(-1);
3517
- }
3518
- linkLabels.push({
3519
- start: open.start,
3520
- end: close
3521
- });
3522
- }
3523
- if (kind === 20) referenceSuffixes.set(close, inlineTokenEnd(tokens, index));
3524
- }
3525
- }
3526
- }
3527
- }
3528
- return {
3529
- linkLabels,
3530
- normalClosers,
3531
- pairs,
3532
- referenceSuffixes
3533
- };
3534
- }
3535
- function componentCandidate(source, start, flags, pairs) {
3226
+ function inlineComponentEnd(source, start) {
3536
3227
  const previous = source[start - 1];
3537
3228
  if (start > 0 && previous !== " " && previous !== " " && previous !== "\n" && previous !== "\r" && previous !== "*" && previous !== "_" && previous !== "[") return;
3538
- const nameEnd = componentNameEnd(source, start + 1, false);
3539
- if (nameEnd === void 0) return;
3540
- const close = source[nameEnd] === "[" ? pairs.get(nameEnd) : void 0;
3541
- if (close === void 0) return {
3542
- children: [],
3543
- close: nameEnd,
3544
- contentEnd: nameEnd,
3545
- contentStart: nameEnd,
3546
- end: nameEnd,
3547
- flags,
3548
- inLinkLabel: false,
3549
- kind: "component",
3550
- nameEnd,
3551
- start
3552
- };
3553
- return {
3554
- children: [],
3555
- close,
3556
- contentEnd: close,
3557
- contentStart: nameEnd + 1,
3558
- end: close + 1,
3559
- flags,
3560
- inLinkLabel: false,
3561
- kind: "component",
3562
- nameEnd,
3563
- start
3564
- };
3565
- }
3566
- function insideLinkLabel(offset, labels) {
3567
- let start = 0;
3568
- let end = labels.length;
3569
- while (start < end) {
3570
- const middle = start + end >>> 1;
3571
- if (labels[middle].end <= offset) start = middle + 1;
3572
- else end = middle;
3229
+ return componentNameEnd(source, start + 1, false);
3230
+ }
3231
+ const inlineLexical = [{
3232
+ marker: 58,
3233
+ scan(source, start, tokens) {
3234
+ const end = inlineComponentEnd(source, start);
3235
+ if (end === void 0) return -1;
3236
+ appendInlineToken(tokens, 35, start, end);
3237
+ return end;
3238
+ }
3239
+ }];
3240
+ function appendLinkCandidates(target, candidates) {
3241
+ for (const candidate of candidates) {
3242
+ candidate.inLinkLabel = true;
3243
+ if (candidate.children.length > 0) {
3244
+ const children = [];
3245
+ appendLinkCandidates(children, candidate.children);
3246
+ candidate.children = children;
3247
+ }
3248
+ if (candidate.literalInLink) target.push(...candidate.children);
3249
+ else target.push(candidate);
3573
3250
  }
3574
- const label = labels[start];
3575
- return label !== void 0 && offset > label.start;
3576
3251
  }
3577
3252
  function candidates(source, tokens) {
3578
- const brackets = bracketIndex(tokens);
3579
- const result = [];
3580
- const componentLabels = /* @__PURE__ */ new Set();
3581
- let attributeLineEnd = 0;
3253
+ const bracketStack = [];
3254
+ const normalClosers = /* @__PURE__ */ new Set();
3255
+ const roots = [];
3582
3256
  for (let index = 0; index < inlineTokenCount(tokens); index++) {
3583
- if (inlineTokenKind(tokens, index) !== 9) continue;
3584
- const start = inlineTokenStart(tokens, index);
3585
- const end = inlineTokenEnd(tokens, index);
3586
- for (let offset = source.indexOf(":", start); offset >= start && offset < end; offset = source.indexOf(":", offset + 1)) {
3587
- const candidate = componentCandidate(source, offset, inlineTokenFlags(tokens, index), brackets.pairs);
3588
- if (candidate) {
3589
- candidate.inLinkLabel = insideLinkLabel(candidate.start, brackets.linkLabels);
3590
- result.push(candidate);
3591
- if (candidate.close > candidate.nameEnd) {
3592
- componentLabels.add(candidate.nameEnd);
3593
- const suffixEnd = brackets.referenceSuffixes.get(candidate.close);
3594
- if (suffixEnd !== void 0) {
3595
- const suffixStart = candidate.close + 1;
3596
- const suffixClose = suffixEnd - 1;
3597
- result.push({
3598
- children: [],
3599
- close: suffixClose,
3600
- contentEnd: suffixClose,
3601
- contentStart: suffixStart + 1,
3602
- end: suffixEnd,
3603
- flags: candidate.flags,
3604
- inLinkLabel: insideLinkLabel(suffixStart, brackets.linkLabels),
3605
- kind: "span",
3606
- nameEnd: suffixStart + 1,
3607
- start: suffixStart
3608
- });
3609
- }
3610
- }
3611
- offset = candidate.nameEnd - 1;
3257
+ const kind = inlineTokenKind(tokens, index);
3258
+ if (kind === 20) {
3259
+ const start = inlineTokenStart(tokens, index);
3260
+ const componentIndex = index - 1;
3261
+ bracketStack.push({
3262
+ children: [],
3263
+ componentIndex: componentIndex >= 0 && inlineTokenKind(tokens, componentIndex) === 35 && inlineTokenEnd(tokens, componentIndex) === start ? componentIndex : -1,
3264
+ image: false,
3265
+ start,
3266
+ tokenIndex: index
3267
+ });
3268
+ continue;
3269
+ }
3270
+ if (kind === 19) {
3271
+ bracketStack.push({
3272
+ children: [],
3273
+ componentIndex: -1,
3274
+ image: true,
3275
+ start: inlineTokenEnd(tokens, index) - 1,
3276
+ tokenIndex: index
3277
+ });
3278
+ continue;
3279
+ }
3280
+ if (kind !== 23 && kind !== 21 && kind !== 22) continue;
3281
+ const open = bracketStack.pop();
3282
+ if (!open) continue;
3283
+ const parent = bracketStack.at(-1)?.children ?? roots;
3284
+ if (open.image) {
3285
+ parent.push(...open.children);
3286
+ continue;
3287
+ }
3288
+ const close = inlineTokenStart(tokens, index);
3289
+ normalClosers.add(close);
3290
+ let children = open.children;
3291
+ if (kind === 21 || kind === 22) {
3292
+ children = [];
3293
+ appendLinkCandidates(children, open.children);
3294
+ }
3295
+ if (open.componentIndex >= 0) {
3296
+ const start = inlineTokenStart(tokens, open.componentIndex);
3297
+ const flags = inlineTokenFlags(tokens, open.componentIndex);
3298
+ parent.push({
3299
+ children,
3300
+ close,
3301
+ contentEnd: close,
3302
+ contentStart: open.start + 1,
3303
+ end: close + 1,
3304
+ flags,
3305
+ inLinkLabel: false,
3306
+ kind: "component",
3307
+ literalInLink: false,
3308
+ nameEnd: open.start,
3309
+ start
3310
+ });
3311
+ if (kind === 22) {
3312
+ const suffixStart = close + 1;
3313
+ const suffixEnd = inlineTokenEnd(tokens, index);
3314
+ const suffixClose = suffixEnd - 1;
3315
+ parent.push({
3316
+ children: [],
3317
+ close: suffixClose,
3318
+ contentEnd: suffixClose,
3319
+ contentStart: suffixStart + 1,
3320
+ end: suffixEnd,
3321
+ flags,
3322
+ inLinkLabel: false,
3323
+ kind: "span",
3324
+ literalInLink: false,
3325
+ nameEnd: suffixStart + 1,
3326
+ start: suffixStart
3327
+ });
3612
3328
  }
3329
+ continue;
3330
+ }
3331
+ if (source[close + 1] === "(" || source[close + 1] === "[") {
3332
+ parent.push(...children);
3333
+ continue;
3613
3334
  }
3614
- }
3615
- for (let index = 0; index < inlineTokenCount(tokens); index++) {
3616
- if (inlineTokenKind(tokens, index) !== 18) continue;
3617
- const start = inlineTokenStart(tokens, index);
3618
- if (componentLabels.has(start)) continue;
3619
- const close = brackets.pairs.get(start);
3620
- if (close === void 0 || source[close + 1] === "(" || source[close + 1] === "[") continue;
3621
3335
  const attributesStart = close + 1;
3622
- let attributed = false;
3623
- if (source[attributesStart] === "{") {
3624
- if (attributesStart >= attributeLineEnd) attributeLineEnd = lineEnd(source, attributesStart);
3625
- attributed = attributesEnd(source, attributesStart, attributeLineEnd) !== void 0;
3626
- }
3627
- const inLinkLabel = insideLinkLabel(start, brackets.linkLabels);
3628
- if (!attributed && inLinkLabel) continue;
3629
- result.push({
3630
- children: [],
3336
+ const attributed = source[attributesStart] === "{" && attributesEnd(source, attributesStart) !== void 0;
3337
+ parent.push({
3338
+ children,
3631
3339
  close,
3632
3340
  contentEnd: close,
3633
- contentStart: start + 1,
3341
+ contentStart: open.start + 1,
3634
3342
  end: close + 1,
3635
- flags: inlineTokenFlags(tokens, index),
3636
- inLinkLabel,
3343
+ flags: inlineTokenFlags(tokens, open.tokenIndex),
3344
+ inLinkLabel: false,
3637
3345
  kind: "span",
3638
- nameEnd: start + 1,
3639
- start
3346
+ literalInLink: !attributed,
3347
+ nameEnd: open.start + 1,
3348
+ start: open.start
3640
3349
  });
3641
3350
  }
3642
- result.sort((left, right) => left.start - right.start || right.end - left.end);
3643
- const roots = [];
3644
- const stack = [];
3645
- for (const candidate of result) {
3646
- while (stack.length > 0 && candidate.start >= stack.at(-1).end) stack.pop();
3647
- const parent = stack.at(-1);
3648
- if (parent && candidate.start >= parent.contentStart && candidate.end <= parent.contentEnd) parent.children.push(candidate);
3649
- else if (!parent) roots.push(candidate);
3650
- else continue;
3651
- if (candidate.end > candidate.start) stack.push(candidate);
3351
+ while (bracketStack.length > 0) {
3352
+ const opening = bracketStack.pop();
3353
+ (bracketStack.at(-1)?.children ?? roots).push(...opening.children);
3652
3354
  }
3653
3355
  return {
3654
- normalClosers: brackets.normalClosers,
3356
+ normalClosers,
3655
3357
  roots
3656
3358
  };
3657
3359
  }
3658
- function copyRange$1(target, tokens, start, end, inLinkLabel, normalClosers) {
3360
+ function copyRange(target, tokens, start, end, inLinkLabel, normalClosers) {
3659
3361
  for (let index = firstInlineTokenEndingAfter(tokens, start); index < inlineTokenCount(tokens); index++) {
3660
3362
  const tokenStart = inlineTokenStart(tokens, index);
3661
3363
  if (tokenStart >= end) break;
@@ -3663,56 +3365,37 @@ function copyRange$1(target, tokens, start, end, inLinkLabel, normalClosers) {
3663
3365
  const tokenEnd = inlineTokenEnd(tokens, index);
3664
3366
  const fragmentStart = Math.max(start, tokenStart);
3665
3367
  const fragmentEnd = Math.min(end, tokenEnd);
3666
- appendInlineToken(target, inLinkLabel && (kind === 18 || normalClosers.has(tokenStart) && (kind === 19 || kind === 20 || kind === 21)) || fragmentStart !== tokenStart || fragmentEnd !== tokenEnd ? 9 : kind, fragmentStart, fragmentEnd, fragmentStart === tokenStart ? inlineTokenFlags(tokens, index) : 0);
3368
+ appendInlineToken(target, inLinkLabel && (kind === 20 || normalClosers.has(tokenStart) && (kind === 21 || kind === 22 || kind === 23)) || fragmentStart !== tokenStart || fragmentEnd !== tokenEnd ? 9 : kind, fragmentStart, fragmentEnd, fragmentStart === tokenStart ? inlineTokenFlags(tokens, index) : 0);
3667
3369
  }
3668
3370
  }
3669
3371
  function emitRange(target, tokens, start, end, nested, inLinkLabel, normalClosers) {
3670
3372
  let cursor = start;
3671
3373
  for (const candidate of nested) {
3672
- copyRange$1(target, tokens, cursor, candidate.start, inLinkLabel, normalClosers);
3374
+ copyRange(target, tokens, cursor, candidate.start, inLinkLabel, normalClosers);
3673
3375
  if (candidate.kind === "component") {
3674
- appendInlineToken(target, 32, candidate.start, candidate.nameEnd, candidate.flags);
3376
+ appendInlineToken(target, 35, candidate.start, candidate.nameEnd, candidate.flags);
3675
3377
  if (candidate.contentStart < candidate.contentEnd) {
3676
- appendInlineToken(target, 33, candidate.nameEnd, candidate.contentStart);
3378
+ appendInlineToken(target, 36, candidate.nameEnd, candidate.contentStart);
3677
3379
  emitRange(target, tokens, candidate.contentStart, candidate.contentEnd, candidate.children, candidate.inLinkLabel, normalClosers);
3678
- appendInlineToken(target, 34, candidate.close, candidate.close + 1);
3380
+ appendInlineToken(target, 37, candidate.close, candidate.close + 1);
3679
3381
  }
3680
3382
  } else {
3681
- appendInlineToken(target, 36, candidate.start, candidate.contentStart, candidate.flags);
3383
+ appendInlineToken(target, 39, candidate.start, candidate.contentStart, candidate.flags);
3682
3384
  emitRange(target, tokens, candidate.contentStart, candidate.contentEnd, candidate.children, candidate.inLinkLabel, normalClosers);
3683
- appendInlineToken(target, 37, candidate.close, candidate.close + 1);
3385
+ appendInlineToken(target, 40, candidate.close, candidate.close + 1);
3684
3386
  }
3685
3387
  cursor = candidate.end;
3686
3388
  }
3687
- copyRange$1(target, tokens, cursor, end, inLinkLabel, normalClosers);
3389
+ copyRange(target, tokens, cursor, end, inLinkLabel, normalClosers);
3688
3390
  }
3689
- function transformComponentTokens(source, tokens) {
3690
- if (!source.includes(":") && !source.includes("[")) return tokens;
3391
+ function rewriteComponentTokens(source, tokens) {
3392
+ if (!source.includes("[")) return tokens;
3691
3393
  const syntax = candidates(source, tokens);
3692
3394
  if (syntax.roots.length === 0) return tokens;
3693
3395
  const result = [];
3694
3396
  emitRange(result, tokens, 0, source.length, syntax.roots, false, syntax.normalClosers);
3695
3397
  return result;
3696
3398
  }
3697
- const buildInlineComponent = (open, close, children, sourceSpan, accumulator) => {
3698
- const { context } = accumulator;
3699
- appendInline(accumulator, {
3700
- type: "inlineComponent",
3701
- name: normalizeComponentName(context.view.text.slice(inlineTokenStart(context.tokens, open) + 1, inlineTokenEnd(context.tokens, open))),
3702
- attributes: {},
3703
- children,
3704
- position: sourceSpan
3705
- });
3706
- };
3707
- const buildInlineSpan = (open, close, children, sourceSpan, accumulator) => {
3708
- appendInline(accumulator, {
3709
- type: "inlineComponent",
3710
- name: "span",
3711
- attributes: {},
3712
- children,
3713
- position: sourceSpan
3714
- });
3715
- };
3716
3399
  //#endregion
3717
3400
  //#region src/profile/features/component/index.ts
3718
3401
  const feature$11 = {
@@ -3721,30 +3404,38 @@ const feature$11 = {
3721
3404
  starts: blockStarts$2
3722
3405
  },
3723
3406
  inline: {
3407
+ lexical: inlineLexical,
3408
+ rewrite: rewriteComponentTokens,
3724
3409
  syntax: [{
3725
3410
  kind: "container",
3726
- token: 32,
3727
- contentOpen: 33,
3728
- close: 34,
3729
- build: buildInlineComponent
3411
+ isolateDelimiters: true,
3412
+ token: 35,
3413
+ contentOpen: 36,
3414
+ close: 37,
3415
+ build(open, close, sourceSpan, children, context) {
3416
+ return {
3417
+ type: "inlineComponent",
3418
+ name: normalizeComponentName(context.view.text.slice(inlineTokenStart(context.tokens, open) + 1, inlineTokenEnd(context.tokens, open))),
3419
+ attributes: {},
3420
+ children,
3421
+ position: sourceSpan
3422
+ };
3423
+ }
3730
3424
  }, {
3731
3425
  kind: "pair",
3732
- open: 36,
3733
- close: 37,
3734
- build: buildInlineSpan
3735
- }],
3736
- resolution: {
3737
- transform: transformComponentTokens,
3738
- pairs: [{
3739
- opener: 33,
3740
- closer: 34,
3741
- isolateDelimiters: true
3742
- }, {
3743
- opener: 36,
3744
- closer: 37,
3745
- isolateDelimiters: true
3746
- }]
3747
- }
3426
+ isolateDelimiters: true,
3427
+ open: 39,
3428
+ close: 40,
3429
+ build(open, close, sourceSpan, children) {
3430
+ return {
3431
+ type: "inlineComponent",
3432
+ name: "span",
3433
+ attributes: {},
3434
+ children,
3435
+ position: sourceSpan
3436
+ };
3437
+ }
3438
+ }]
3748
3439
  }
3749
3440
  };
3750
3441
  //#endregion
@@ -3758,10 +3449,10 @@ function splitReferenceTail(tokens, index) {
3758
3449
  const flags = inlineTokenFlags(tokens, index);
3759
3450
  const decodeFlags = flags & 16;
3760
3451
  const result = [];
3761
- appendInlineToken(result, 22, start, start + 1, flags & -17);
3762
- appendInlineToken(result, 18, start + 1, start + 2);
3452
+ appendInlineToken(result, 24, start, start + 1, flags & -17);
3453
+ appendInlineToken(result, 20, start + 1, start + 2);
3763
3454
  if (end > start + 3) appendInlineToken(result, 9, start + 2, end - 1, decodeFlags);
3764
- appendInlineToken(result, 21, end - 1, end);
3455
+ appendInlineToken(result, 23, end - 1, end);
3765
3456
  return result;
3766
3457
  }
3767
3458
  //#endregion
@@ -3834,17 +3525,17 @@ const blockRules$1 = [{
3834
3525
  open: 24,
3835
3526
  close: 25
3836
3527
  },
3837
- build(nodeId, offset, tokenBase, context) {
3838
- const token = blockToken(nodeId, tokenBase, 24, context);
3839
- const fields = definitionFields(context.arena.tokens, token);
3528
+ build(tokenStart, context) {
3529
+ const token = blockToken(tokenStart, 24, context);
3530
+ const fields = definitionFields(context.structure.tokens, token);
3840
3531
  return {
3841
3532
  type: "footnoteDefinition",
3842
3533
  identifier: fields.normalizedLabel.toLowerCase(),
3843
3534
  label: semanticText(fields.label),
3844
- children: buildBlockChildren(nodeId, offset, tokenBase, context),
3535
+ children: buildBlockChildren(tokenStart, context),
3845
3536
  position: {
3846
- start: context.arena.tokens.start(token),
3847
- end: blockEnd(nodeId, offset, context)
3537
+ start: context.structure.tokens.start(token),
3538
+ end: blockEnd(tokenStart, context)
3848
3539
  }
3849
3540
  };
3850
3541
  },
@@ -3911,62 +3602,50 @@ const blockStarts$1 = [{
3911
3602
  function closerIndex(tokens, start, end) {
3912
3603
  for (let index = start + 1; index < inlineTokenCount(tokens); index++) {
3913
3604
  if (inlineTokenStart(tokens, index) >= end) break;
3914
- if (inlineTokenEnd(tokens, index) === end && (inlineTokenKind(tokens, index) === 21 || inlineTokenKind(tokens, index) === 22)) return index;
3605
+ const kind = inlineTokenKind(tokens, index);
3606
+ if (inlineTokenEnd(tokens, index) === end && (kind === 23 || kind === 24) || kind === 22 && inlineTokenStart(tokens, index) + 1 === end) return index;
3915
3607
  }
3916
3608
  return -1;
3917
3609
  }
3918
- const splitFootnoteTails = (source, tokens, context) => {
3919
- let activeFootnoteEnd = -1;
3920
- let result;
3921
- for (let index = 0; index < inlineTokenCount(tokens); index++) {
3922
- const kind = inlineTokenKind(tokens, index);
3923
- const start = inlineTokenStart(tokens, index);
3924
- if (start >= activeFootnoteEnd) activeFootnoteEnd = -1;
3925
- if (kind === 18 || kind === 17) {
3926
- const label = footnoteLabelAt(source, kind === 17 ? start + 1 : start, source.length);
3927
- activeFootnoteEnd = label && context.hasDefinition(label.definitionKey) ? label.end : -1;
3928
- }
3929
- if (kind !== 20) {
3930
- if (result) copyInlineToken(result, tokens, index);
3931
- continue;
3932
- }
3933
- const embedded = footnoteLabelAt(source, start + 1, inlineTokenEnd(tokens, index));
3934
- if (activeFootnoteEnd !== start + 1 && (!embedded || !context.hasDefinition(embedded.definitionKey))) {
3935
- if (result) copyInlineToken(result, tokens, index);
3936
- continue;
3937
- }
3938
- if (!result) {
3939
- result = [];
3940
- for (let prefix = 0; prefix < index; prefix++) copyInlineToken(result, tokens, prefix);
3941
- }
3942
- result.push(...splitReferenceTail(tokens, index));
3943
- activeFootnoteEnd = -1;
3944
- }
3945
- return result ?? tokens;
3946
- };
3947
- const activateFootnoteReferences = (source, tokens, context) => {
3610
+ const rewriteFootnoteTokens = (source, tokens, context) => {
3948
3611
  let result;
3949
3612
  for (let index = 0; index < inlineTokenCount(tokens); index++) {
3950
3613
  const start = inlineTokenStart(tokens, index);
3951
3614
  const kind = inlineTokenKind(tokens, index);
3952
- const labelStart = kind === 17 ? start + 1 : start;
3953
- const label = kind === 18 || kind === 17 ? footnoteLabelAt(source, labelStart, source.length) : void 0;
3615
+ const labelStart = kind === 19 ? start + 1 : start;
3616
+ const label = kind === 20 || kind === 19 ? footnoteLabelAt(source, labelStart, source.length) : void 0;
3954
3617
  const close = label ? closerIndex(tokens, index, label.end) : -1;
3955
- if (!label || close < 0 || !context.hasDefinition(label.definitionKey)) {
3956
- if (result) copyInlineToken(result, tokens, index);
3618
+ if (label && close >= 0 && context.hasDefinition(label.definitionKey)) {
3619
+ if (!result) {
3620
+ result = [];
3621
+ for (let prefix = 0; prefix < index; prefix++) copyInlineToken(result, tokens, prefix);
3622
+ }
3623
+ if (kind === 19) appendInlineToken(result, 9, start, labelStart, inlineTokenFlags(tokens, index));
3624
+ appendInlineToken(result, 33, labelStart, label.end, inlineTokenFlags(tokens, index));
3625
+ if (inlineTokenKind(tokens, close) === 22) {
3626
+ const embedded = footnoteLabelAt(source, label.end, inlineTokenEnd(tokens, close));
3627
+ if (embedded && context.hasDefinition(embedded.definitionKey)) appendInlineToken(result, 33, label.end, embedded.end);
3628
+ else result.push(...splitReferenceTail(tokens, close).slice(4));
3629
+ }
3630
+ index = close;
3957
3631
  continue;
3958
3632
  }
3959
- if (!result) {
3960
- result = [];
3961
- for (let prefix = 0; prefix < index; prefix++) copyInlineToken(result, tokens, prefix);
3633
+ if (kind === 22) {
3634
+ const embedded = footnoteLabelAt(source, start + 1, inlineTokenEnd(tokens, index));
3635
+ if (embedded && context.hasDefinition(embedded.definitionKey)) {
3636
+ if (!result) {
3637
+ result = [];
3638
+ for (let prefix = 0; prefix < index; prefix++) copyInlineToken(result, tokens, prefix);
3639
+ }
3640
+ result.push(...splitReferenceTail(tokens, index).slice(0, 4));
3641
+ appendInlineToken(result, 33, start + 1, embedded.end);
3642
+ continue;
3643
+ }
3962
3644
  }
3963
- if (kind === 17) appendInlineToken(result, 9, start, labelStart, inlineTokenFlags(tokens, index));
3964
- appendInlineToken(result, 31, labelStart, label.end, inlineTokenFlags(tokens, index));
3965
- index = close;
3645
+ if (result) copyInlineToken(result, tokens, index);
3966
3646
  }
3967
3647
  return result ?? tokens;
3968
3648
  };
3969
- const transformFootnoteTokens = (source, tokens, context) => activateFootnoteReferences(source, splitFootnoteTails(source, tokens, context), context);
3970
3649
  //#endregion
3971
3650
  //#region src/profile/features/footnote/index.ts
3972
3651
  const feature$10 = {
@@ -3975,20 +3654,18 @@ const feature$10 = {
3975
3654
  starts: blockStarts$1
3976
3655
  },
3977
3656
  inline: {
3978
- resolution: { transform: transformFootnoteTokens },
3657
+ rewrite: rewriteFootnoteTokens,
3979
3658
  syntax: [{
3980
3659
  kind: "leaf",
3981
- token: 31,
3982
- build(tokenIndex, sourceSpan, accumulator) {
3983
- const { context } = accumulator;
3660
+ token: 33,
3661
+ build(tokenIndex, sourceSpan, context) {
3984
3662
  const label = inlineTokenText(context.view.text, context.tokens, tokenIndex).slice(2, -1);
3985
- appendInline(accumulator, {
3663
+ return {
3986
3664
  type: "footnoteReference",
3987
3665
  identifier: normalizeAssociationLabel(label).toLowerCase(),
3988
3666
  label: semanticText(label),
3989
3667
  position: sourceSpan
3990
- });
3991
- return true;
3668
+ };
3992
3669
  }
3993
3670
  }]
3994
3671
  }
@@ -3997,8 +3674,7 @@ const feature$10 = {
3997
3674
  //#region src/profile/features/formatting.ts
3998
3675
  const asteriskDelimiter = {
3999
3676
  token: 10,
4000
- marker: "*",
4001
- fallbackToken: 38,
3677
+ marker: 42,
4002
3678
  single: {
4003
3679
  open: 13,
4004
3680
  close: 14
@@ -4014,8 +3690,7 @@ const asteriskDelimiter = {
4014
3690
  };
4015
3691
  const underscoreDelimiter = {
4016
3692
  token: 11,
4017
- marker: "_",
4018
- fallbackToken: 38,
3693
+ marker: 95,
4019
3694
  single: {
4020
3695
  open: 13,
4021
3696
  close: 14
@@ -4032,61 +3707,83 @@ const underscoreDelimiter = {
4032
3707
  };
4033
3708
  const strikethroughDelimiter = {
4034
3709
  token: 12,
4035
- marker: "~",
4036
- fallbackToken: 38,
3710
+ marker: 126,
4037
3711
  double: {
4038
- open: 15,
4039
- close: 16
3712
+ open: 17,
3713
+ close: 18
4040
3714
  },
4041
3715
  pairing: { kind: "whole" }
4042
3716
  };
3717
+ const scanFormatting = (source, start, tokens) => {
3718
+ const code = source.charCodeAt(start);
3719
+ const end = inlineMarkerRunEnd(source, start);
3720
+ appendInlineToken(tokens, code === 42 ? 10 : code === 95 ? 11 : 12, start, end);
3721
+ return end;
3722
+ };
4043
3723
  function createBuildFormatting(type) {
4044
- return (openToken, closeToken, children, sourceSpan, accumulator) => {
4045
- appendInline(accumulator, {
4046
- type,
4047
- children,
4048
- position: sourceSpan
4049
- });
4050
- };
3724
+ return (openToken, closeToken, sourceSpan, children) => ({
3725
+ type,
3726
+ children,
3727
+ position: sourceSpan
3728
+ });
4051
3729
  }
4052
- const buildInlineEmphasis = createBuildFormatting("emphasis");
4053
- const buildInlineStrong = createBuildFormatting("strong");
4054
- const buildInlineDelete = createBuildFormatting("delete");
4055
- const buildInlineStrongOrDelete = (openToken, closeToken, children, sourceSpan, accumulator) => {
4056
- (accumulator.context.source[sourceSpan.start] === "~" ? buildInlineDelete : buildInlineStrong)(openToken, closeToken, children, sourceSpan, accumulator);
4057
- };
4058
3730
  function feature$9(strikethroughOptions) {
4059
3731
  const delimiters = [asteriskDelimiter, underscoreDelimiter];
4060
- if (strikethroughOptions) delimiters.push(typeof strikethroughOptions !== "object" || strikethroughOptions.singleTilde !== false ? {
4061
- ...strikethroughDelimiter,
4062
- single: strikethroughDelimiter.double
4063
- } : strikethroughDelimiter);
3732
+ const lexical = [{
3733
+ marker: 42,
3734
+ scan: scanFormatting
3735
+ }, {
3736
+ marker: 95,
3737
+ scan: scanFormatting
3738
+ }];
3739
+ const syntax = [
3740
+ {
3741
+ kind: "leaf",
3742
+ token: 10,
3743
+ build: buildInlineText
3744
+ },
3745
+ {
3746
+ kind: "leaf",
3747
+ token: 11,
3748
+ build: buildInlineText
3749
+ },
3750
+ {
3751
+ kind: "pair",
3752
+ open: 13,
3753
+ close: 14,
3754
+ build: createBuildFormatting("emphasis")
3755
+ },
3756
+ {
3757
+ kind: "pair",
3758
+ open: 15,
3759
+ close: 16,
3760
+ build: createBuildFormatting("strong")
3761
+ }
3762
+ ];
3763
+ if (strikethroughOptions) {
3764
+ lexical.push({
3765
+ marker: 126,
3766
+ scan: scanFormatting
3767
+ });
3768
+ delimiters.push(typeof strikethroughOptions !== "object" || strikethroughOptions.singleTilde !== false ? {
3769
+ ...strikethroughDelimiter,
3770
+ single: strikethroughDelimiter.double
3771
+ } : strikethroughDelimiter);
3772
+ syntax.push({
3773
+ kind: "leaf",
3774
+ token: 12,
3775
+ build: buildInlineText
3776
+ }, {
3777
+ kind: "pair",
3778
+ open: 17,
3779
+ close: 18,
3780
+ build: createBuildFormatting("delete")
3781
+ });
3782
+ }
4064
3783
  return { inline: {
4065
- resolution: { delimiters },
4066
- syntax: [
4067
- {
4068
- kind: "leaf",
4069
- token: 38,
4070
- build: buildInlineText
4071
- },
4072
- {
4073
- kind: "leaf",
4074
- token: 12,
4075
- build: buildInlineText
4076
- },
4077
- {
4078
- kind: "pair",
4079
- open: 13,
4080
- close: 14,
4081
- build: buildInlineEmphasis
4082
- },
4083
- {
4084
- kind: "pair",
4085
- open: 15,
4086
- close: 16,
4087
- build: strikethroughOptions ? buildInlineStrongOrDelete : buildInlineStrong
4088
- }
4089
- ]
3784
+ lexical,
3785
+ delimiters,
3786
+ syntax
4090
3787
  } };
4091
3788
  }
4092
3789
  //#endregion
@@ -4111,18 +3808,18 @@ function feature$8(marker) {
4111
3808
  kind: "leaf",
4112
3809
  token: 22
4113
3810
  },
4114
- build(nodeId, offset, tokenBase, context) {
4115
- const token = blockToken(nodeId, tokenBase, 22, context);
4116
- const rangeCount = context.arena.tokens.rangeCount(token);
3811
+ build(tokenStart, context) {
3812
+ const token = blockToken(tokenStart, 22, context);
3813
+ const rangeCount = context.structure.tokens.rangeCount(token);
4117
3814
  if (rangeCount < 2) throw new Error("FrontmatterToken does not contain two fences");
4118
- let value = normalizeLines(context.source.slice(context.arena.tokens.rangeEnd(token, 0), context.arena.tokens.rangeStart(token, rangeCount - 1)));
3815
+ let value = normalizeLines(context.source.slice(context.structure.tokens.rangeEnd(token, 0), context.structure.tokens.rangeStart(token, rangeCount - 1)));
4119
3816
  if (value.endsWith("\n")) value = value.slice(0, -1);
4120
3817
  return {
4121
3818
  type: "yaml",
4122
3819
  value,
4123
3820
  position: {
4124
- start: context.arena.tokens.start(token),
4125
- end: blockEnd(nodeId, offset, context)
3821
+ start: context.structure.tokens.start(token),
3822
+ end: blockEnd(tokenStart, context)
4126
3823
  }
4127
3824
  };
4128
3825
  }
@@ -4180,15 +3877,15 @@ const feature$7 = { block: {
4180
3877
  close: 8
4181
3878
  },
4182
3879
  inlineContent: true,
4183
- build(nodeId, offset, tokenBase, context) {
4184
- const marker = blockToken(nodeId, tokenBase, 5, context);
3880
+ build(tokenStart, context) {
3881
+ const marker = blockToken(tokenStart, 5, context);
4185
3882
  return {
4186
3883
  type: "heading",
4187
- depth: context.arena.tokens.end(marker) - context.arena.tokens.start(marker),
4188
- children: buildInlineChildren(nodeId, context, true),
3884
+ depth: context.structure.tokens.end(marker) - context.structure.tokens.start(marker),
3885
+ children: buildInlineChildren(tokenStart, context, true),
4189
3886
  position: {
4190
- start: context.arena.tokens.start(marker),
4191
- end: blockEnd(nodeId, offset, context)
3887
+ start: context.structure.tokens.start(marker),
3888
+ end: blockEnd(tokenStart, context)
4192
3889
  }
4193
3890
  };
4194
3891
  }
@@ -4200,17 +3897,17 @@ const feature$7 = { block: {
4200
3897
  close: 8
4201
3898
  },
4202
3899
  inlineContent: true,
4203
- build(nodeId, offset, tokenBase, context) {
4204
- const levelOne = directBlockToken(nodeId, tokenBase, 6, context);
4205
- if (levelOne === void 0) blockToken(nodeId, tokenBase, 7, context);
4206
- const children = buildInlineChildren(nodeId, context);
3900
+ build(tokenStart, context) {
3901
+ const levelOne = directBlockToken(tokenStart, 6, context);
3902
+ if (levelOne === void 0) blockToken(tokenStart, 7, context);
3903
+ const children = buildInlineChildren(tokenStart, context);
4207
3904
  return {
4208
3905
  type: "heading",
4209
3906
  depth: levelOne === void 0 ? 2 : 1,
4210
3907
  children,
4211
3908
  position: {
4212
3909
  start: firstChildStart(children),
4213
- end: context.arena.tokens.start(blockToken(nodeId, tokenBase, 8, context))
3910
+ end: context.structure.tokens.start(blockToken(tokenStart, 8, context))
4214
3911
  }
4215
3912
  };
4216
3913
  }
@@ -4299,6 +3996,9 @@ const htmlBlockTags = /* @__PURE__ */ new Set([
4299
3996
  ]);
4300
3997
  const htmlTagName = "[a-z][a-z0-9-]*";
4301
3998
  const completeHtmlTag = new RegExp(`^(?:<${htmlTagName}(?:\\s+[a-z_:][\\w.:-]*(?:\\s*=\\s*(?:[^\\s"'=<>\`]+|'[^']*'|"[^"]*"))?)*\\s*/?>|</${htmlTagName}\\s*>)[ \\t]*$`, "i");
3999
+ const htmlComment = /<!-->|<!--->|<!--[\s\S]*?(?:-->|$)/y;
4000
+ const autolink = /<(?:[A-Z][A-Z0-9+.\-]{1,31}:[^ \t\n\r<>]+|[\w!#$%&'*+\-/=?^`{|}~.]+@[A-Z0-9](?:[A-Z0-9]|-(?=[A-Z0-9]))*(?:\.[A-Z0-9](?:[A-Z0-9]|-(?=[A-Z0-9]))*)+)>/iy;
4001
+ const inlineHtml = /<[A-Za-z][A-Za-z0-9-]*(?:[ \t\n\r]+[A-Za-z_:][\w.:-]*(?:[ \t\n\r]*=[ \t\n\r]*(?:[^ \t\n\r"'=<>`]+|'[^']*'|"[^"]*"))?)*[ \t\n\r]*\/?>|<\/[A-Za-z][A-Za-z0-9-]*[ \t\n\r]*>|<\?[\s\S]*?\?>|<![A-Z][\s\S]*?>|<!\[CDATA\[[\s\S]*?\]\]>/y;
4302
4002
  function htmlStartAt(source, line) {
4303
4003
  const indent = lineIndent(source, line);
4304
4004
  if (!indent || source[indent.offset] !== "<") return;
@@ -4334,18 +4034,16 @@ function htmlStartAt(source, line) {
4334
4034
  if (completeHtmlTag.test(body)) return { interruptParagraph: false };
4335
4035
  }
4336
4036
  function htmlBlockUnterminated(token, context) {
4337
- const result = context.arena.tokens.value(token);
4338
- if (context.arena.tokens.kind(token) !== 23 || result === void 0) throw new Error("Expected HtmlBlockToken to contain its termination state");
4037
+ const result = context.structure.tokens.value(token);
4038
+ if (context.structure.tokens.kind(token) !== 23 || result === void 0) throw new Error("Expected HtmlBlockToken to contain its termination state");
4339
4039
  return result;
4340
4040
  }
4341
- const buildInlineHtml = (tokenIndex, sourceSpan, accumulator) => {
4342
- const { context } = accumulator;
4343
- appendInline(accumulator, {
4041
+ const buildInlineHtml = (tokenIndex, sourceSpan, context) => {
4042
+ return {
4344
4043
  type: "html",
4345
4044
  value: inlineTokenText(context.view.text, context.tokens, tokenIndex),
4346
4045
  position: sourceSpan
4347
- });
4348
- return true;
4046
+ };
4349
4047
  };
4350
4048
  const feature$6 = {
4351
4049
  block: {
@@ -4355,18 +4053,19 @@ const feature$6 = {
4355
4053
  kind: "leaf",
4356
4054
  token: 23
4357
4055
  },
4358
- build(nodeId, offset, tokenBase, context) {
4359
- const end = offset + context.arena.lenOf(nodeId);
4360
- const token = blockToken(nodeId, tokenBase, 23, context);
4056
+ build(tokenStart, context) {
4057
+ const offset = context.structure.tokens.start(tokenStart);
4058
+ const end = offset + context.structure.lenOf(tokenStart);
4059
+ const token = blockToken(tokenStart, 23, context);
4361
4060
  const unterminated = htmlBlockUnterminated(token, context);
4362
- let html = normalizeLines(context.arena.tokens.text(context.source, token));
4061
+ let html = normalizeLines(context.structure.tokens.text(context.source, token));
4363
4062
  if (!unterminated && html.endsWith("\n")) html = html.slice(0, -1);
4364
4063
  return {
4365
4064
  type: "html",
4366
4065
  value: html,
4367
4066
  position: {
4368
4067
  start: offset,
4369
- end: html.endsWith("\n") ? end : blockEnd(nodeId, offset, context)
4068
+ end: html.endsWith("\n") ? end : blockEnd(tokenStart, context)
4370
4069
  }
4371
4070
  };
4372
4071
  }
@@ -4392,55 +4091,175 @@ const feature$6 = {
4392
4091
  }
4393
4092
  }]
4394
4093
  },
4395
- inline: { syntax: [{
4396
- kind: "leaf",
4397
- token: 5,
4398
- build: buildInlineHtml
4399
- }, {
4400
- kind: "leaf",
4401
- token: 1,
4402
- build: buildInlineHtml
4403
- }] }
4094
+ inline: {
4095
+ lexical: [{
4096
+ marker: 60,
4097
+ scan(source, start, tokens) {
4098
+ let end = matchInlinePatternEnd(htmlComment, source, start);
4099
+ let kind = 1;
4100
+ if (end < 0) {
4101
+ end = matchInlinePatternEnd(autolink, source, start);
4102
+ kind = 4;
4103
+ }
4104
+ if (end < 0) {
4105
+ end = matchInlinePatternEnd(inlineHtml, source, start);
4106
+ kind = 5;
4107
+ }
4108
+ if (end < 0) {
4109
+ end = start + 1;
4110
+ kind = 41;
4111
+ }
4112
+ appendInlineToken(tokens, kind, start, end);
4113
+ return end;
4114
+ }
4115
+ }],
4116
+ syntax: [{
4117
+ kind: "leaf",
4118
+ token: 5,
4119
+ build: buildInlineHtml
4120
+ }, {
4121
+ kind: "leaf",
4122
+ token: 1,
4123
+ build: buildInlineHtml
4124
+ }]
4125
+ }
4404
4126
  };
4405
4127
  //#endregion
4406
4128
  //#region src/profile/features/link.ts
4407
- function trimLinkWhitespace(value) {
4408
- return value.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "");
4129
+ function skipWhitespace(source, start) {
4130
+ let end = start;
4131
+ while (end < source.length) {
4132
+ const code = source.charCodeAt(end);
4133
+ if (code !== 9 && code !== 10 && code !== 13 && code !== 32) break;
4134
+ end++;
4135
+ }
4136
+ return end;
4137
+ }
4138
+ function linkDestinationEnd(source, start) {
4139
+ if (source.charCodeAt(start) === 60) {
4140
+ let end = start + 1;
4141
+ while (end < source.length) {
4142
+ const code = source.charCodeAt(end);
4143
+ if (code === 62) return end + 1;
4144
+ if (code === 10 || code === 13 || code === 60) return -1;
4145
+ end += code === 92 && end + 1 < source.length ? 2 : 1;
4146
+ }
4147
+ return -1;
4148
+ }
4149
+ let depth = 0;
4150
+ let end = start;
4151
+ let consumed = false;
4152
+ while (end < source.length) {
4153
+ const code = source.charCodeAt(end);
4154
+ if (code === 92) {
4155
+ if (end + 1 >= source.length) break;
4156
+ consumed = true;
4157
+ end += 2;
4158
+ continue;
4159
+ }
4160
+ if (code === 40) {
4161
+ if (depth === 32) return -1;
4162
+ depth++;
4163
+ consumed = true;
4164
+ end++;
4165
+ continue;
4166
+ }
4167
+ if (code === 41) {
4168
+ if (depth === 0) break;
4169
+ depth--;
4170
+ consumed = true;
4171
+ end++;
4172
+ continue;
4173
+ }
4174
+ if (isLinkWhitespace(code)) break;
4175
+ consumed = true;
4176
+ end++;
4177
+ }
4178
+ return consumed && depth === 0 ? end : -1;
4179
+ }
4180
+ function linkTitleEnd(source, start) {
4181
+ const marker = source.charCodeAt(start);
4182
+ const close = marker === 40 ? 41 : marker;
4183
+ if (marker !== 34 && marker !== 39 && marker !== 40) return -1;
4184
+ let end = start + 1;
4185
+ while (end < source.length) {
4186
+ const code = source.charCodeAt(end);
4187
+ if (code === close) return end + 1;
4188
+ if (code === 10 || code === 13) return -1;
4189
+ end += code === 92 && end + 1 < source.length ? 2 : 1;
4190
+ }
4191
+ return -1;
4192
+ }
4193
+ function linkTailEnd(source, start) {
4194
+ if (source.charCodeAt(start + 1) !== 40) return -1;
4195
+ let offset = skipWhitespace(source, start + 2);
4196
+ if (source.charCodeAt(offset) === 41) return offset + 1;
4197
+ const destinationEnd = linkDestinationEnd(source, offset);
4198
+ if (destinationEnd < 0) return -1;
4199
+ offset = destinationEnd;
4200
+ const whitespaceEnd = skipWhitespace(source, offset);
4201
+ if (whitespaceEnd > offset && source.charCodeAt(whitespaceEnd) !== 41) {
4202
+ const titleEnd = linkTitleEnd(source, whitespaceEnd);
4203
+ if (titleEnd < 0) return -1;
4204
+ offset = titleEnd;
4205
+ } else offset = whitespaceEnd;
4206
+ offset = skipWhitespace(source, offset);
4207
+ return source.charCodeAt(offset) === 41 ? offset + 1 : -1;
4208
+ }
4209
+ function referenceTailEnd(source, start) {
4210
+ if (source.charCodeAt(start + 1) !== 91) return -1;
4211
+ let offset = start + 2;
4212
+ if (source.charCodeAt(offset) === 93) return offset + 1;
4213
+ let characters = 0;
4214
+ let hasContent = false;
4215
+ while (offset < source.length && characters < 999) {
4216
+ const code = source.charCodeAt(offset);
4217
+ if (code === 93) return hasContent ? offset + 1 : -1;
4218
+ if (code === 91) return -1;
4219
+ if (code === 92) {
4220
+ if (offset + 1 >= source.length) return -1;
4221
+ hasContent = true;
4222
+ offset += 2;
4223
+ } else {
4224
+ hasContent ||= !isLinkWhitespace(code);
4225
+ offset++;
4226
+ }
4227
+ characters++;
4228
+ }
4229
+ return -1;
4230
+ }
4231
+ function isLinkWhitespace(code) {
4232
+ return code === 9 || code === 10 || code === 13 || code === 32;
4409
4233
  }
4410
- function destinationTitle(bodySource) {
4411
- const body = trimLinkWhitespace(bodySource);
4412
- if (!body) return {
4234
+ function resourceAt(source, tokenStart, tokenEnd) {
4235
+ let start = tokenStart + 2;
4236
+ let end = tokenEnd - 1;
4237
+ while (start < end && isLinkWhitespace(source.charCodeAt(start))) start++;
4238
+ while (end > start && isLinkWhitespace(source.charCodeAt(end - 1))) end--;
4239
+ if (start === end) return {
4413
4240
  url: "",
4414
4241
  title: null
4415
4242
  };
4416
- let offset = 0;
4417
- let destination = "";
4418
- if (body[0] === "<") {
4419
- offset = 1;
4420
- while (offset < body.length) if (body[offset] === "\\") offset += 2;
4421
- else if (body[offset] === ">") break;
4422
- else offset++;
4423
- destination = body.slice(1, offset++);
4243
+ let destinationStart = start;
4244
+ let destinationEnd = start;
4245
+ if (source[start] === "<") {
4246
+ destinationStart++;
4247
+ destinationEnd = destinationStart;
4248
+ while (destinationEnd < end) if (source[destinationEnd] === "\\") destinationEnd += 2;
4249
+ else if (source[destinationEnd] === ">") break;
4250
+ else destinationEnd++;
4251
+ start = destinationEnd + 1;
4424
4252
  } else {
4425
- let depth = 0;
4426
- while (offset < body.length) if (body[offset] === "\\") offset += 2;
4427
- else if (body[offset] === "(") {
4428
- depth++;
4429
- offset++;
4430
- } else if (body[offset] === ")") {
4431
- depth--;
4432
- offset++;
4433
- } else if (depth === 0) {
4434
- const code = body.charCodeAt(offset);
4435
- if (code === 9 || code === 10 || code === 13 || code === 32) break;
4436
- offset++;
4437
- } else offset++;
4438
- destination = body.slice(0, offset);
4253
+ destinationEnd = start;
4254
+ while (destinationEnd < end) if (source[destinationEnd] === "\\") destinationEnd += 2;
4255
+ else if (isLinkWhitespace(source.charCodeAt(destinationEnd))) break;
4256
+ else destinationEnd++;
4257
+ start = destinationEnd;
4439
4258
  }
4440
- const titleSource = trimLinkWhitespace(body.slice(offset));
4259
+ while (start < end && isLinkWhitespace(source.charCodeAt(start))) start++;
4441
4260
  return {
4442
- url: semanticText(destination),
4443
- title: titleSource ? semanticText(titleSource.slice(1, -1)) : null
4261
+ url: semanticText(source.slice(destinationStart, destinationEnd)),
4262
+ title: start < end ? semanticText(source.slice(start + 1, end - 1)) : null
4444
4263
  };
4445
4264
  }
4446
4265
  function reference(close, syntaxStart, syntaxEnd, context, image) {
@@ -4466,11 +4285,10 @@ function phrasingText(children) {
4466
4285
  function createBuildMedia(media, resourceKind) {
4467
4286
  const image = media === "image";
4468
4287
  const referenceNode = resourceKind === "reference";
4469
- return (openToken, closeToken, children, sourceSpan, accumulator) => {
4470
- const context = accumulator.context;
4288
+ return (openToken, closeToken, sourceSpan, children, context) => {
4471
4289
  if (referenceNode) {
4472
4290
  const association = reference(closeToken, inlineTokenStart(context.tokens, openToken), inlineTokenEnd(context.tokens, closeToken), context, image);
4473
- appendInline(accumulator, image ? {
4291
+ return image ? {
4474
4292
  type: "imageReference",
4475
4293
  alt: phrasingText(children),
4476
4294
  ...association,
@@ -4480,106 +4298,141 @@ function createBuildMedia(media, resourceKind) {
4480
4298
  children,
4481
4299
  ...association,
4482
4300
  position: sourceSpan
4483
- });
4484
- } else {
4485
- const resource = destinationTitle(inlineTokenText(context.view.text, context.tokens, closeToken).slice(2, -1));
4486
- appendInline(accumulator, image ? {
4487
- type: "image",
4488
- alt: phrasingText(children),
4489
- ...resource,
4490
- position: sourceSpan
4491
- } : {
4492
- type: "link",
4493
- children,
4494
- ...resource,
4495
- position: sourceSpan
4496
- });
4301
+ };
4497
4302
  }
4303
+ const resource = resourceAt(context.view.text, inlineTokenStart(context.tokens, closeToken), inlineTokenEnd(context.tokens, closeToken));
4304
+ return image ? {
4305
+ type: "image",
4306
+ alt: phrasingText(children),
4307
+ ...resource,
4308
+ position: sourceSpan
4309
+ } : {
4310
+ type: "link",
4311
+ children,
4312
+ ...resource,
4313
+ position: sourceSpan
4314
+ };
4498
4315
  };
4499
4316
  }
4500
4317
  const buildInlineImage = createBuildMedia("image", "direct");
4501
4318
  const buildInlineReferenceImage = createBuildMedia("image", "reference");
4502
4319
  const buildInlineLink = createBuildMedia("link", "direct");
4503
4320
  const buildInlineReferenceLink = createBuildMedia("link", "reference");
4504
- const feature$5 = { inline: { syntax: [
4505
- {
4506
- kind: "pair",
4507
- open: 23,
4508
- close: 24,
4509
- build: buildInlineLink
4510
- },
4511
- {
4512
- kind: "pair",
4513
- open: 25,
4514
- close: 26,
4515
- build: buildInlineImage
4516
- },
4517
- {
4518
- kind: "pair",
4519
- open: 27,
4520
- close: 28,
4521
- build: buildInlineReferenceLink
4522
- },
4523
- {
4524
- kind: "pair",
4525
- open: 29,
4526
- close: 30,
4527
- build: buildInlineReferenceImage
4528
- },
4529
- {
4530
- kind: "leaf",
4531
- token: 4,
4532
- build(tokenIndex, sourceSpan, accumulator) {
4533
- const { context } = accumulator;
4534
- const label = inlineTokenText(context.view.text, context.tokens, tokenIndex).slice(1, -1);
4535
- appendInline(accumulator, {
4536
- type: "link",
4537
- url: label.includes(":") ? label : `mailto:${label}`,
4538
- title: null,
4539
- children: [{
4540
- type: "text",
4541
- value: label,
4542
- position: {
4543
- start: sourceSpan.start + 1,
4544
- end: sourceSpan.end - 1
4545
- }
4546
- }],
4547
- position: sourceSpan
4548
- });
4549
- return true;
4321
+ const feature$5 = { inline: {
4322
+ lexical: [
4323
+ {
4324
+ marker: 33,
4325
+ scan(source, start, tokens) {
4326
+ const image = source.charCodeAt(start + 1) === 91;
4327
+ const end = start + (image ? 2 : 1);
4328
+ appendInlineToken(tokens, image ? 19 : 41, start, end);
4329
+ return end;
4330
+ }
4331
+ },
4332
+ {
4333
+ marker: 91,
4334
+ scan(source, start, tokens) {
4335
+ const end = start + 1;
4336
+ appendInlineToken(tokens, 20, start, end);
4337
+ return end;
4338
+ }
4339
+ },
4340
+ {
4341
+ marker: 93,
4342
+ scan(source, start, tokens) {
4343
+ let end = linkTailEnd(source, start);
4344
+ let kind = 21;
4345
+ if (end < 0) {
4346
+ end = referenceTailEnd(source, start);
4347
+ kind = 22;
4348
+ }
4349
+ if (end < 0) {
4350
+ end = start + 1;
4351
+ kind = 23;
4352
+ }
4353
+ appendInlineToken(tokens, kind, start, end, kind === 23 ? 0 : 16);
4354
+ return end;
4355
+ }
4550
4356
  }
4551
- },
4552
- {
4553
- kind: "leaf",
4554
- token: 18,
4555
- build: buildInlineText
4556
- },
4557
- {
4558
- kind: "leaf",
4559
- token: 17,
4560
- build: buildInlineText
4561
- },
4562
- {
4563
- kind: "leaf",
4564
- token: 19,
4565
- build: buildInlineText
4566
- },
4567
- {
4568
- kind: "leaf",
4569
- token: 20,
4570
- build: buildInlineText
4571
- },
4572
- {
4573
- kind: "leaf",
4574
- token: 21,
4575
- build: buildInlineText
4576
- },
4577
- {
4578
- kind: "leaf",
4579
- token: 22,
4580
- build: buildInlineText
4581
- }
4582
- ] } };
4357
+ ],
4358
+ syntax: [
4359
+ {
4360
+ kind: "pair",
4361
+ open: 25,
4362
+ close: 26,
4363
+ build: buildInlineLink
4364
+ },
4365
+ {
4366
+ kind: "pair",
4367
+ open: 27,
4368
+ close: 28,
4369
+ build: buildInlineImage
4370
+ },
4371
+ {
4372
+ kind: "pair",
4373
+ open: 29,
4374
+ close: 30,
4375
+ build: buildInlineReferenceLink
4376
+ },
4377
+ {
4378
+ kind: "pair",
4379
+ open: 31,
4380
+ close: 32,
4381
+ build: buildInlineReferenceImage
4382
+ },
4383
+ {
4384
+ kind: "leaf",
4385
+ token: 4,
4386
+ build(tokenIndex, sourceSpan, context) {
4387
+ const label = inlineTokenText(context.view.text, context.tokens, tokenIndex).slice(1, -1);
4388
+ return {
4389
+ type: "link",
4390
+ url: label.includes(":") ? label : `mailto:${label}`,
4391
+ title: null,
4392
+ children: [{
4393
+ type: "text",
4394
+ value: label,
4395
+ position: {
4396
+ start: sourceSpan.start + 1,
4397
+ end: sourceSpan.end - 1
4398
+ }
4399
+ }],
4400
+ position: sourceSpan
4401
+ };
4402
+ }
4403
+ },
4404
+ {
4405
+ kind: "leaf",
4406
+ token: 20,
4407
+ build: buildInlineText
4408
+ },
4409
+ {
4410
+ kind: "leaf",
4411
+ token: 19,
4412
+ build: buildInlineText
4413
+ },
4414
+ {
4415
+ kind: "leaf",
4416
+ token: 21,
4417
+ build: buildInlineText
4418
+ },
4419
+ {
4420
+ kind: "leaf",
4421
+ token: 22,
4422
+ build: buildInlineText
4423
+ },
4424
+ {
4425
+ kind: "leaf",
4426
+ token: 23,
4427
+ build: buildInlineText
4428
+ },
4429
+ {
4430
+ kind: "leaf",
4431
+ token: 24,
4432
+ build: buildInlineText
4433
+ }
4434
+ ]
4435
+ } };
4583
4436
  //#endregion
4584
4437
  //#region src/profile/features/list.ts
4585
4438
  function listMarkerPadding(source, line, markerEnd, markerColumn) {
@@ -4678,55 +4531,48 @@ function hasBlankLineBetween(source, start, end, stripBlockQuotes) {
4678
4531
  }
4679
4532
  return false;
4680
4533
  }
4681
- function childrenSpread(nodeId, offset, tokenBase, stripBlockQuotes, context, nestedRule) {
4682
- const arena = context.arena;
4534
+ function childrenSpread(tokenStart, stripBlockQuotes, context, nestedRule) {
4535
+ const structure = context.structure;
4683
4536
  let previous;
4684
- const childCount = arena.childCount(nodeId);
4685
- for (let index = 0; index < childCount; index++) {
4686
- const childId = arena.childAt(nodeId, index);
4687
- if (childId < 0 || (nestedRule === void 0 ? !arena.isBlock(childId) : arena.ruleNameOf(childId) !== nestedRule)) continue;
4688
- const current = payloadBounds(childId, offset + arena.childRelAt(nodeId, index), tokenBase + arena.childTokRelAt(nodeId, index), context);
4537
+ for (let childId = structure.firstChild(tokenStart); childId !== noBlockEntry; childId = structure.nextChild(tokenStart, childId)) if (childId >= 0 && (nestedRule === void 0 ? structure.isBlock(childId) : structure.ruleNameOf(childId) === nestedRule)) {
4538
+ const current = payloadBounds(childId, context);
4689
4539
  if (previous && hasBlankLineBetween(context.source, previous.end, current.start, stripBlockQuotes)) return true;
4690
4540
  previous = current;
4691
4541
  }
4692
4542
  return false;
4693
4543
  }
4694
- const buildListItem = (nodeId, offset, tokenBase, context) => {
4695
- const rule = context.arena.ruleNameOf(nodeId);
4544
+ const buildListItem = (tokenStart, context) => {
4545
+ const rule = context.structure.ruleNameOf(tokenStart);
4696
4546
  if (rule !== "OrderedListItem" && rule !== "UnorderedListItem") throw new Error(`Expected list item syntax, received ${rule}`);
4697
- const marker = blockToken(nodeId, tokenBase, rule === "OrderedListItem" ? 17 : 13, context);
4698
- const children = buildBlockChildren(nodeId, offset, tokenBase, context);
4547
+ const marker = blockToken(tokenStart, rule === "OrderedListItem" ? 17 : 13, context);
4548
+ const children = buildBlockChildren(tokenStart, context);
4699
4549
  return {
4700
4550
  type: "listItem",
4701
- spread: childrenSpread(nodeId, offset, tokenBase, true, context),
4551
+ spread: childrenSpread(tokenStart, true, context),
4702
4552
  checked: null,
4703
4553
  children,
4704
4554
  position: {
4705
- start: context.arena.tokens.start(marker),
4706
- end: lastChildEnd(children, blockEnd(nodeId, offset, context))
4555
+ start: context.structure.tokens.start(marker),
4556
+ end: lastChildEnd(children, blockEnd(tokenStart, context))
4707
4557
  }
4708
4558
  };
4709
4559
  };
4710
4560
  function createBuildList(ordered) {
4711
- return (nodeId, offset, tokenBase, context) => {
4712
- const arena = context.arena;
4561
+ return (tokenStart, context) => {
4562
+ const structure = context.structure;
4713
4563
  const itemRule = ordered ? "OrderedListItem" : "UnorderedListItem";
4714
- const listMarker = blockToken(nodeId, tokenBase, ordered ? 15 : 11, context);
4564
+ const listMarker = blockToken(tokenStart, ordered ? 15 : 11, context);
4715
4565
  const items = [];
4716
- const childCount = arena.childCount(nodeId);
4717
- for (let index = 0; index < childCount; index++) {
4718
- const childId = arena.childAt(nodeId, index);
4719
- if (childId >= 0 && arena.ruleNameOf(childId) === itemRule) items.push(buildListItem(childId, offset + arena.childRelAt(nodeId, index), tokenBase + arena.childTokRelAt(nodeId, index), context));
4720
- }
4566
+ for (let childId = structure.firstChild(tokenStart); childId !== noBlockEntry; childId = structure.nextChild(tokenStart, childId)) if (childId >= 0 && structure.ruleNameOf(childId) === itemRule) items.push(buildListItem(childId, context));
4721
4567
  return {
4722
4568
  type: "list",
4723
4569
  ordered,
4724
- start: ordered ? Number.parseInt(context.arena.tokens.text(context.source, listMarker), 10) : null,
4725
- spread: childrenSpread(nodeId, offset, tokenBase, false, context, itemRule),
4570
+ start: ordered ? Number.parseInt(context.structure.tokens.text(context.source, listMarker), 10) : null,
4571
+ spread: childrenSpread(tokenStart, false, context, itemRule),
4726
4572
  children: items,
4727
4573
  position: {
4728
- start: context.arena.tokens.start(listMarker),
4729
- end: lastChildEnd(items, context.arena.tokens.end(listMarker))
4574
+ start: context.structure.tokens.start(listMarker),
4575
+ end: lastChildEnd(items, context.structure.tokens.end(listMarker))
4730
4576
  }
4731
4577
  };
4732
4578
  };
@@ -4874,17 +4720,20 @@ const blockRules = [{
4874
4720
  kind: "leaf",
4875
4721
  token: 21
4876
4722
  },
4877
- build(nodeId, offset, tokenBase, context) {
4878
- const end = offset + context.arena.lenOf(nodeId);
4879
- const value = context.arena.tokens.text(context.source, blockToken(nodeId, tokenBase, 21, context));
4880
- const block = readFencedBlock(value, mathFenceRule);
4723
+ build(tokenStart, context) {
4724
+ const offset = context.structure.tokens.start(tokenStart);
4725
+ const end = offset + context.structure.lenOf(tokenStart);
4726
+ const token = blockToken(tokenStart, 21, context);
4727
+ const value = context.structure.tokens.text(context.source, token);
4728
+ const block = context.structure.tokens.value(token);
4729
+ if (!block) throw new Error("MathBlockToken has no fence metadata");
4881
4730
  return {
4882
4731
  type: "math",
4883
4732
  meta: semanticText(block.info) || null,
4884
4733
  value: fencedBlockContent(value, block, "columns"),
4885
4734
  position: {
4886
4735
  start: firstNonspace(context.source, offset, lineEnd(context.source, offset)),
4887
- end: block.closed || end < context.source.length ? blockEnd(nodeId, offset, context) : end
4736
+ end: block.closed || end < context.source.length ? blockEnd(tokenStart, context) : end
4888
4737
  }
4889
4738
  };
4890
4739
  }
@@ -4899,81 +4748,38 @@ const blockStarts = [{
4899
4748
  if (!fence) return;
4900
4749
  let end = start + 1;
4901
4750
  while (end < lines.length && !closesFence(source, lines[end], fence)) end++;
4751
+ const closed = end < lines.length;
4902
4752
  if (end < lines.length) end++;
4903
- appendLogicalToken(out, 21, source, lines, start, end);
4753
+ appendLogicalToken(out, 21, source, lines, start, end, fencedBlock(source, lines[start], fence, closed));
4904
4754
  return end;
4905
4755
  }
4906
4756
  }];
4907
4757
  //#endregion
4908
4758
  //#region src/profile/features/math/inline.ts
4909
- function textTokenAt(tokens, offset) {
4910
- const index = firstInlineTokenEndingAfter(tokens, offset);
4911
- if (index < inlineTokenCount(tokens) && inlineTokenStart(tokens, index) <= offset && inlineTokenKind(tokens, index) === 9) return index;
4912
- }
4913
- function markerRunEnd(source, start) {
4914
- let end = start;
4915
- while (source[end] === "$") end++;
4916
- return end;
4917
- }
4918
- function firstMathSpan(source, tokens, minimum) {
4919
- let search = 0;
4920
- while (search < source.length) {
4921
- const start = source.indexOf("$", search);
4922
- if (start < 0) break;
4923
- const tokenIndex = textTokenAt(tokens, start);
4924
- if (tokenIndex === void 0 || source[start - 1] === "$" && textTokenAt(tokens, start - 1) !== void 0) {
4925
- search = start + 1;
4926
- continue;
4927
- }
4928
- const openerEnd = markerRunEnd(source, start);
4929
- const markerLength = openerEnd - start;
4930
- search = openerEnd;
4931
- if (markerLength < minimum) continue;
4932
- let close = openerEnd;
4933
- while (close < source.length) {
4934
- close = source.indexOf("$", close);
4935
- if (close < 0) break;
4936
- const closeEnd = markerRunEnd(source, close);
4937
- if (closeEnd - close === markerLength) return {
4938
- start,
4939
- end: closeEnd,
4940
- flags: inlineTokenFlags(tokens, tokenIndex)
4941
- };
4942
- close = closeEnd;
4943
- }
4944
- }
4945
- }
4946
- function copyRange(target, tokens, start, end, offset) {
4947
- for (let index = firstInlineTokenEndingAfter(tokens, start); index < inlineTokenCount(tokens); index++) {
4948
- const tokenStart = inlineTokenStart(tokens, index);
4949
- if (tokenStart >= end) break;
4950
- const tokenEnd = inlineTokenEnd(tokens, index);
4951
- const fragmentStart = Math.max(start, tokenStart);
4952
- const fragmentEnd = Math.min(end, tokenEnd);
4953
- appendInlineToken(target, inlineTokenKind(tokens, index), fragmentStart + offset, fragmentEnd + offset, fragmentStart === tokenStart ? inlineTokenFlags(tokens, index) : 0);
4954
- }
4955
- }
4956
- function createMathTokensTransform(singleDollarTextMath) {
4957
- return (source, tokens, context) => {
4958
- const minimum = singleDollarTextMath ? 1 : 2;
4959
- let segmentSource = source;
4960
- let segmentStart = 0;
4961
- let segmentTokens = tokens;
4962
- let result;
4963
- while (true) {
4964
- const span = firstMathSpan(segmentSource, segmentTokens, minimum);
4965
- if (!span) {
4966
- if (!result) return tokens;
4967
- copyRange(result, segmentTokens, 0, segmentSource.length, segmentStart);
4968
- return result;
4759
+ function createMathLexicalRule(singleDollarTextMath) {
4760
+ const minimum = singleDollarTextMath ? 1 : 2;
4761
+ return {
4762
+ marker: 36,
4763
+ scan(source, start, tokens) {
4764
+ const openEnd = inlineMarkerRunEnd(source, start);
4765
+ const markerLength = openEnd - start;
4766
+ let end = -1;
4767
+ if (markerLength >= minimum) {
4768
+ let close = openEnd;
4769
+ while (close < source.length) {
4770
+ close = source.indexOf("$", close);
4771
+ if (close < 0) break;
4772
+ const closeEnd = inlineMarkerRunEnd(source, close);
4773
+ if (closeEnd - close === markerLength) {
4774
+ end = closeEnd;
4775
+ break;
4776
+ }
4777
+ close = closeEnd;
4778
+ }
4969
4779
  }
4970
- result ??= [];
4971
- copyRange(result, segmentTokens, 0, span.start, segmentStart);
4972
- appendInlineToken(result, 3, segmentStart + span.start, segmentStart + span.end, span.flags);
4973
- segmentStart += span.end;
4974
- if (segmentStart === source.length) return result;
4975
- segmentSource = source.slice(segmentStart);
4976
- segmentTokens = context.tokenize(segmentSource);
4780
+ if (end < 0) end = openEnd;
4781
+ appendInlineToken(tokens, end === openEnd ? 9 : 3, start, end);
4782
+ return end;
4977
4783
  }
4978
4784
  };
4979
4785
  }
@@ -4990,14 +4796,12 @@ function mathTextValue(value) {
4990
4796
  const inlineSyntax = [{
4991
4797
  kind: "leaf",
4992
4798
  token: 3,
4993
- build(tokenIndex, sourceSpan, accumulator) {
4994
- const { context } = accumulator;
4995
- appendInline(accumulator, {
4799
+ build(tokenIndex, sourceSpan, context) {
4800
+ return {
4996
4801
  type: "inlineMath",
4997
4802
  value: mathTextValue(inlineTokenText(context.view.text, context.tokens, tokenIndex)),
4998
4803
  position: sourceSpan
4999
- });
5000
- return true;
4804
+ };
5001
4805
  }
5002
4806
  }];
5003
4807
  //#endregion
@@ -5010,7 +4814,7 @@ function feature$3(options) {
5010
4814
  starts: blockStarts
5011
4815
  },
5012
4816
  inline: {
5013
- resolution: { transform: createMathTokensTransform(singleDollarTextMath) },
4817
+ lexical: [createMathLexicalRule(singleDollarTextMath)],
5014
4818
  syntax: inlineSyntax
5015
4819
  }
5016
4820
  };
@@ -5065,14 +4869,14 @@ const feature$2 = { block: {
5065
4869
  close: 10
5066
4870
  },
5067
4871
  inlineContent: true,
5068
- build(nodeId, offset, tokenBase, context) {
5069
- const children = buildInlineChildren(nodeId, context);
4872
+ build(tokenStart, context) {
4873
+ const children = buildInlineChildren(tokenStart, context);
5070
4874
  return {
5071
4875
  type: "paragraph",
5072
4876
  children,
5073
4877
  position: {
5074
4878
  start: firstChildStart(children),
5075
- end: blockEnd(nodeId, offset, context)
4879
+ end: blockEnd(tokenStart, context)
5076
4880
  }
5077
4881
  };
5078
4882
  }
@@ -5085,37 +4889,35 @@ function linkDefinitionFields(tokens, token) {
5085
4889
  if (tokens.kind(token) !== 26 || !fields) throw new Error("Expected LinkDefinitionOpen token to contain parsed fields");
5086
4890
  return fields;
5087
4891
  }
5088
- function linkDefinitionAt(source, lines, startIndex) {
5089
- const indent = lineIndent(source, lines[startIndex]);
5090
- if (!indent || source[indent.offset] !== "[") return;
4892
+ function linkDefinitionAt(source, lines, startIndex, contentOffset) {
5091
4893
  let lineIndex = startIndex;
5092
- let offset = indent.offset + 1;
4894
+ let offset = contentOffset + 1;
5093
4895
  let label = "";
5094
4896
  let labelLength = 0;
5095
4897
  let labelHasContent = false;
5096
4898
  let labelStart = offset;
5097
- while (true) {
4899
+ scanLabel: while (true) {
5098
4900
  const line = lines[lineIndex];
5099
- if (!line || offset >= line.end) {
5100
- if (!line || lineIndex + 1 >= lines.length || isBlank(source, lines[lineIndex + 1])) return;
4901
+ while (offset < line.end) {
4902
+ const code = source.charCodeAt(offset);
4903
+ if (code === 92 && offset + 1 < line.end) {
4904
+ labelHasContent = true;
4905
+ labelLength += 2;
4906
+ offset += 2;
4907
+ continue;
4908
+ }
4909
+ if (code === 91) return;
4910
+ if (code === 93 && source.charCodeAt(offset + 1) === 58) break scanLabel;
4911
+ if (code !== 32 && code !== 9) labelHasContent = true;
5101
4912
  if (++labelLength > 999) return;
5102
- label += source.slice(labelStart, line.next);
5103
- lineIndex++;
5104
- offset = lines[lineIndex].start;
5105
- labelStart = offset;
5106
- continue;
5107
- }
5108
- if (source[offset] === "\\" && offset + 1 < line.end) {
5109
- labelHasContent = true;
5110
- labelLength += 2;
5111
- offset += 2;
5112
- continue;
4913
+ offset++;
5113
4914
  }
5114
- if (source[offset] === "[") return;
5115
- if (source[offset] === "]" && source[offset + 1] === ":") break;
5116
- if (source[offset] !== " " && source[offset] !== " ") labelHasContent = true;
4915
+ if (lineIndex + 1 >= lines.length || isBlank(source, lines[lineIndex + 1])) return;
5117
4916
  if (++labelLength > 999) return;
5118
- offset++;
4917
+ label += source.slice(labelStart, line.next);
4918
+ lineIndex++;
4919
+ offset = lines[lineIndex].start;
4920
+ labelStart = offset;
5119
4921
  }
5120
4922
  label += source.slice(labelStart, offset);
5121
4923
  if (!labelHasContent) return;
@@ -5162,7 +4964,7 @@ function linkDefinitionAt(source, lines, startIndex) {
5162
4964
  if (offset < lines[lineIndex].end && source[offset] !== " " && source[offset] !== " ") return;
5163
4965
  skipSpaces();
5164
4966
  let titleOnNextLine = false;
5165
- if (offset === lines[lineIndex].end && lineIndex + 1 < lines.length && !isBlank(source, lines[lineIndex + 1])) {
4967
+ if (offset === lines[lineIndex].end && lineIndex + 1 < lines.length) {
5166
4968
  lineIndex++;
5167
4969
  offset = lines[lineIndex].start;
5168
4970
  skipSpaces();
@@ -5173,7 +4975,7 @@ function linkDefinitionAt(source, lines, startIndex) {
5173
4975
  definitionKey: normalizeAssociationLabel(label),
5174
4976
  destination,
5175
4977
  label,
5176
- markerOffset: indent.offset - lines[startIndex].start,
4978
+ markerOffset: contentOffset - lines[startIndex].start,
5177
4979
  title: void 0
5178
4980
  };
5179
4981
  if (!closer) return {
@@ -5226,21 +5028,21 @@ const reassociateReferenceTails = (source, tokens, context) => {
5226
5028
  let result;
5227
5029
  for (let index = 0; index < count; index++) {
5228
5030
  const kind = inlineTokenKind(tokens, index);
5229
- const label = kind === 20 ? inlineTokenText(source, tokens, index).slice(2, -1) : "";
5230
- if (kind !== 20 || context.hasDefinition(normalizeAssociationLabel(label))) {
5031
+ const label = kind === 22 ? inlineTokenText(source, tokens, index).slice(2, -1) : "";
5032
+ if (kind !== 22 || context.hasDefinition(normalizeAssociationLabel(label))) {
5231
5033
  if (result) copyInlineToken(result, tokens, index);
5232
5034
  continue;
5233
5035
  }
5234
5036
  const openerIndex = index + 1;
5235
- if (openerIndex >= count || inlineTokenKind(tokens, openerIndex) !== 18 || inlineTokenStart(tokens, openerIndex) !== inlineTokenEnd(tokens, index)) {
5037
+ if (openerIndex >= count || inlineTokenKind(tokens, openerIndex) !== 20 || inlineTokenStart(tokens, openerIndex) !== inlineTokenEnd(tokens, index)) {
5236
5038
  if (result) copyInlineToken(result, tokens, index);
5237
5039
  continue;
5238
5040
  }
5239
5041
  let closerIndex = index + 2;
5240
5042
  let nested = false;
5241
- while (closerIndex < count && inlineTokenKind(tokens, closerIndex) !== 21) {
5043
+ while (closerIndex < count && inlineTokenKind(tokens, closerIndex) !== 23) {
5242
5044
  const closerKind = inlineTokenKind(tokens, closerIndex);
5243
- nested ||= closerKind === 18 || closerKind === 17;
5045
+ nested ||= closerKind === 20 || closerKind === 19;
5244
5046
  closerIndex++;
5245
5047
  }
5246
5048
  if (closerIndex === count || nested) {
@@ -5259,7 +5061,7 @@ const reassociateReferenceTails = (source, tokens, context) => {
5259
5061
  const split = splitReferenceTail(tokens, index);
5260
5062
  result.push(...split.slice(0, -4));
5261
5063
  const offset = inlineTokenEnd(tokens, index) - 1;
5262
- appendInlineToken(result, 20, offset, inlineTokenEnd(tokens, closerIndex), inlineTokenFlags(tokens, index));
5064
+ appendInlineToken(result, 22, offset, inlineTokenEnd(tokens, closerIndex), inlineTokenFlags(tokens, index));
5263
5065
  index = closerIndex;
5264
5066
  }
5265
5067
  return result ?? tokens;
@@ -5288,9 +5090,9 @@ const feature$1 = {
5288
5090
  open: 26,
5289
5091
  close: 28
5290
5092
  },
5291
- build(nodeId, offset, tokenBase, context) {
5292
- const token = blockToken(nodeId, tokenBase, 26, context);
5293
- const fields = linkDefinitionFields(context.arena.tokens, token);
5093
+ build(tokenStart, context) {
5094
+ const token = blockToken(tokenStart, 26, context);
5095
+ const fields = linkDefinitionFields(context.structure.tokens, token);
5294
5096
  return {
5295
5097
  type: "definition",
5296
5098
  identifier: fields.definitionKey.toLowerCase(),
@@ -5298,8 +5100,8 @@ const feature$1 = {
5298
5100
  url: semanticText(fields.destination),
5299
5101
  title: fields.title === void 0 ? null : semanticText(fields.title),
5300
5102
  position: {
5301
- start: context.arena.tokens.start(token) + fields.markerOffset,
5302
- end: blockEnd(nodeId, offset, context)
5103
+ start: context.structure.tokens.start(token) + fields.markerOffset,
5104
+ end: blockEnd(tokenStart, context)
5303
5105
  }
5304
5106
  };
5305
5107
  },
@@ -5309,8 +5111,8 @@ const feature$1 = {
5309
5111
  }],
5310
5112
  starts: [{
5311
5113
  codes: [91],
5312
- start(source, lines, start, out) {
5313
- const definition = linkDefinitionAt(source, lines, start);
5114
+ start(source, lines, start, out, contentOffset) {
5115
+ const definition = linkDefinitionAt(source, lines, start, contentOffset);
5314
5116
  if (!definition) return;
5315
5117
  const line = lines[start];
5316
5118
  out.push(26, line.start, line.start, { value: definition.fields });
@@ -5325,71 +5127,71 @@ const feature$1 = {
5325
5127
  }
5326
5128
  }]
5327
5129
  },
5328
- inline: { resolution: {
5329
- postTransform: reassociateReferenceTails,
5130
+ inline: {
5330
5131
  pairs: [
5331
5132
  {
5332
- opener: 18,
5333
- closer: 19,
5334
- open: 23,
5335
- close: 24,
5336
- deactivateEarlier: [18],
5133
+ opener: 20,
5134
+ closer: 21,
5135
+ open: 25,
5136
+ close: 26,
5137
+ deactivateEarlier: [20],
5337
5138
  isolateDelimiters: true
5338
5139
  },
5339
5140
  {
5340
- opener: 17,
5341
- closer: 19,
5342
- open: 25,
5343
- close: 26
5141
+ opener: 19,
5142
+ closer: 21,
5143
+ open: 27,
5144
+ close: 28
5344
5145
  },
5345
5146
  {
5346
- opener: 18,
5347
- closer: 20,
5348
- open: 27,
5349
- close: 28,
5350
- deactivateEarlier: [18],
5147
+ opener: 20,
5148
+ closer: 22,
5149
+ open: 29,
5150
+ close: 30,
5151
+ deactivateEarlier: [20],
5351
5152
  isolateDelimiters: true,
5352
5153
  activate: activateReference,
5353
5154
  splitUnmatchedCloser: splitReferenceTail
5354
5155
  },
5355
5156
  {
5356
- opener: 18,
5357
- closer: 21,
5358
- open: 27,
5359
- close: 28,
5360
- deactivateEarlier: [18],
5157
+ opener: 20,
5158
+ closer: 23,
5159
+ open: 29,
5160
+ close: 30,
5161
+ deactivateEarlier: [20],
5361
5162
  isolateDelimiters: true,
5362
5163
  activate: activateReference,
5363
5164
  content: {
5364
5165
  requireNonWhitespace: true,
5365
5166
  maxCharacters: 999,
5366
- forbidTokens: [18, 17]
5167
+ forbidTokens: [20, 19]
5367
5168
  }
5368
5169
  },
5369
5170
  {
5370
- opener: 17,
5371
- closer: 20,
5372
- open: 29,
5373
- close: 30,
5171
+ opener: 19,
5172
+ closer: 22,
5173
+ open: 31,
5174
+ close: 32,
5374
5175
  isolateDelimiters: true,
5375
5176
  activate: activateReference,
5376
5177
  splitUnmatchedCloser: splitReferenceTail
5377
5178
  },
5378
5179
  {
5379
- opener: 17,
5380
- closer: 21,
5381
- open: 29,
5382
- close: 30,
5180
+ opener: 19,
5181
+ closer: 23,
5182
+ open: 31,
5183
+ close: 32,
5383
5184
  isolateDelimiters: true,
5384
5185
  activate: activateReference,
5385
5186
  content: {
5386
5187
  requireNonWhitespace: true,
5387
5188
  maxCharacters: 999,
5388
- forbidTokens: [18, 17]
5189
+ forbidTokens: [20, 19]
5389
5190
  }
5390
5191
  }
5391
- ]
5392
- } }
5192
+ ],
5193
+ rewrite: reassociateReferenceTails
5194
+ }
5393
5195
  };
5394
5196
  //#endregion
5395
5197
  //#region src/profile/features/table.ts
@@ -5478,142 +5280,127 @@ function emitTableRow(line, cells, out) {
5478
5280
  }
5479
5281
  out.push(32, line.end, line.end);
5480
5282
  }
5481
- function childRules(nodeId, offset, tokenBase, rule, context) {
5482
- const arena = context.arena;
5283
+ function childRules(tokenStart, rule, context) {
5284
+ const structure = context.structure;
5483
5285
  const result = [];
5484
- for (let index = 0; index < arena.childCount(nodeId); index++) {
5485
- const child = arena.childAt(nodeId, index);
5486
- if (child >= 0 && arena.ruleNameOf(child) === rule) result.push({
5487
- id: child,
5488
- offset: offset + arena.childRelAt(nodeId, index),
5489
- tokenBase: tokenBase + arena.childTokRelAt(nodeId, index)
5490
- });
5491
- }
5286
+ for (let child = structure.firstChild(tokenStart); child !== noBlockEntry; child = structure.nextChild(tokenStart, child)) if (child >= 0 && structure.ruleNameOf(child) === rule) result.push(child);
5492
5287
  return result;
5493
5288
  }
5494
- const buildTableCell = (nodeId, offset, tokenBase, context) => {
5495
- const open = blockToken(nodeId, tokenBase, 33, context);
5496
- const close = blockToken(nodeId, tokenBase, 34, context);
5289
+ const buildTableCell = (tokenStart, context) => {
5290
+ const open = blockToken(tokenStart, 33, context);
5291
+ const close = blockToken(tokenStart, 34, context);
5497
5292
  return {
5498
5293
  type: "tableCell",
5499
- children: buildInlineChildren(nodeId, context, true),
5294
+ children: buildInlineChildren(tokenStart, context, true),
5500
5295
  position: {
5501
- start: context.arena.tokens.start(open),
5502
- end: context.arena.tokens.start(close)
5296
+ start: context.structure.tokens.start(open),
5297
+ end: context.structure.tokens.start(close)
5503
5298
  }
5504
5299
  };
5505
5300
  };
5506
- const buildTableRow = (nodeId, offset, tokenBase, context) => {
5507
- const open = blockToken(nodeId, tokenBase, 31, context);
5508
- const close = blockToken(nodeId, tokenBase, 32, context);
5301
+ const buildTableRow = (tokenStart, context) => {
5302
+ const open = blockToken(tokenStart, 31, context);
5303
+ const close = blockToken(tokenStart, 32, context);
5509
5304
  return {
5510
5305
  type: "tableRow",
5511
- children: childRules(nodeId, offset, tokenBase, "TableCell", context).map((cell) => buildTableCell(cell.id, cell.offset, cell.tokenBase, context)),
5306
+ children: childRules(tokenStart, "TableCell", context).map((cell) => buildTableCell(cell, context)),
5512
5307
  position: {
5513
- start: context.arena.tokens.start(open),
5514
- end: context.arena.tokens.start(close)
5308
+ start: context.structure.tokens.start(open),
5309
+ end: context.structure.tokens.start(close)
5515
5310
  }
5516
5311
  };
5517
5312
  };
5518
- function tableAlignment(nodeId, tokenBase, context) {
5519
- const arena = context.arena;
5313
+ function tableAlignment(tokenStart, context) {
5314
+ const structure = context.structure;
5520
5315
  const result = [];
5521
- for (let index = 0; index < arena.childCount(nodeId); index++) {
5522
- const child = arena.childAt(nodeId, index);
5316
+ for (let child = structure.firstChild(tokenStart); child !== noBlockEntry; child = structure.nextChild(tokenStart, child)) {
5523
5317
  if (child >= 0) continue;
5524
- const kind = arena.leafTokenKind(child, tokenBase);
5318
+ const kind = structure.tokens.kind(structure.leafToken(child));
5525
5319
  result.push(kind === 36 ? "left" : kind === 37 ? "right" : kind === 38 ? "center" : null);
5526
5320
  }
5527
5321
  return result;
5528
5322
  }
5529
- const feature = {
5530
- block: {
5531
- fallbacks: [(source, lines, start, out, context) => {
5532
- const delimiterLine = lines[start + 1];
5533
- if (!delimiterLine || context.startsInterruptingBlock(source, delimiterLine)) return;
5534
- const delimiter = delimiterAt(source, delimiterLine);
5535
- const header = delimiter ? tableCellsAt(source, lines[start], true) : void 0;
5536
- if (!header || !delimiter || header.length !== delimiter.cells.length) return;
5537
- out.push(29, header[0].start, header[0].start);
5538
- emitTableRow(lines[start], header, out);
5539
- for (let index = 0; index < delimiter.cells.length; index++) {
5540
- const cell = delimiter.cells[index];
5541
- out.push(delimiter.tokens[index], cell.start, cell.end);
5323
+ const feature = { block: {
5324
+ fallbacks: [(source, lines, start, out, context) => {
5325
+ const delimiterLine = lines[start + 1];
5326
+ if (!delimiterLine || context.startsInterruptingBlock(source, delimiterLine)) return;
5327
+ const delimiter = delimiterAt(source, delimiterLine);
5328
+ const header = delimiter ? tableCellsAt(source, lines[start], true) : void 0;
5329
+ if (!header || !delimiter || header.length !== delimiter.cells.length) return;
5330
+ out.push(29, header[0].start, header[0].start);
5331
+ emitTableRow(lines[start], header, out);
5332
+ for (let index = 0; index < delimiter.cells.length; index++) {
5333
+ const cell = delimiter.cells[index];
5334
+ out.push(delimiter.tokens[index], cell.start, cell.end);
5335
+ }
5336
+ let end = start + 2;
5337
+ while (end < lines.length) {
5338
+ const line = lines[end];
5339
+ if (isBlank(source, line) || !lineIndent(source, line) || context.startsInterruptingBlock(source, line)) break;
5340
+ const cells = tableCellsAt(source, line, false);
5341
+ if (!cells) break;
5342
+ emitTableRow(line, cells, out);
5343
+ end++;
5344
+ }
5345
+ out.push(30, lines[end - 1].end, lines[end - 1].end);
5346
+ return end;
5347
+ }],
5348
+ rules: [
5349
+ {
5350
+ rule: "TableCell",
5351
+ syntax: {
5352
+ kind: "frame",
5353
+ open: 33,
5354
+ close: 34
5355
+ },
5356
+ inlineContent: true
5357
+ },
5358
+ {
5359
+ rule: "TableRow",
5360
+ syntax: {
5361
+ kind: "frame",
5362
+ open: 31,
5363
+ close: 32
5542
5364
  }
5543
- let end = start + 2;
5544
- while (end < lines.length) {
5545
- const line = lines[end];
5546
- if (isBlank(source, line) || !lineIndent(source, line) || context.startsInterruptingBlock(source, line)) break;
5547
- const cells = tableCellsAt(source, line, false);
5548
- if (!cells) break;
5549
- emitTableRow(line, cells, out);
5550
- end++;
5365
+ },
5366
+ {
5367
+ rule: "TableDelimiter",
5368
+ syntax: {
5369
+ kind: "group",
5370
+ tokens: [
5371
+ 35,
5372
+ 36,
5373
+ 37,
5374
+ 38
5375
+ ]
5551
5376
  }
5552
- out.push(30, lines[end - 1].end, lines[end - 1].end);
5553
- return end;
5554
- }],
5555
- rules: [
5556
- {
5557
- rule: "TableCell",
5558
- syntax: {
5559
- kind: "frame",
5560
- open: 33,
5561
- close: 34
5562
- },
5563
- inlineContent: true
5564
- },
5565
- {
5566
- rule: "TableRow",
5567
- syntax: {
5568
- kind: "frame",
5569
- open: 31,
5570
- close: 32
5571
- }
5572
- },
5573
- {
5574
- rule: "TableDelimiter",
5575
- syntax: {
5576
- kind: "group",
5577
- tokens: [
5578
- 35,
5579
- 36,
5580
- 37,
5581
- 38
5582
- ]
5583
- }
5377
+ },
5378
+ {
5379
+ rule: "Table",
5380
+ syntax: {
5381
+ kind: "block",
5382
+ open: 29,
5383
+ close: 30
5584
5384
  },
5585
- {
5586
- rule: "Table",
5587
- syntax: {
5588
- kind: "block",
5589
- open: 29,
5590
- close: 30
5591
- },
5592
- build(nodeId, offset, tokenBase, context) {
5593
- const open = blockToken(nodeId, tokenBase, 29, context);
5594
- const close = blockToken(nodeId, tokenBase, 30, context);
5595
- const rows = childRules(nodeId, offset, tokenBase, "TableRow", context).map((row) => buildTableRow(row.id, row.offset, row.tokenBase, context));
5596
- const delimiter = childRules(nodeId, offset, tokenBase, "TableDelimiter", context)[0];
5597
- if (!delimiter) throw new Error("Table syntax does not contain a delimiter");
5598
- return {
5599
- type: "table",
5600
- align: tableAlignment(delimiter.id, delimiter.tokenBase, context),
5601
- children: rows,
5602
- position: {
5603
- start: context.arena.tokens.start(open),
5604
- end: context.arena.tokens.start(close)
5605
- }
5606
- };
5607
- }
5385
+ build(tokenStart, context) {
5386
+ const open = blockToken(tokenStart, 29, context);
5387
+ const close = blockToken(tokenStart, 30, context);
5388
+ const rows = childRules(tokenStart, "TableRow", context).map((row) => buildTableRow(row, context));
5389
+ const delimiter = childRules(tokenStart, "TableDelimiter", context)[0];
5390
+ if (!delimiter) throw new Error("Table syntax does not contain a delimiter");
5391
+ return {
5392
+ type: "table",
5393
+ align: tableAlignment(delimiter, context),
5394
+ children: rows,
5395
+ position: {
5396
+ start: context.structure.tokens.start(open),
5397
+ end: context.structure.tokens.start(close)
5398
+ }
5399
+ };
5608
5400
  }
5609
- ]
5610
- },
5611
- inline: { syntax: [{
5612
- kind: "leaf",
5613
- token: 2,
5614
- build: buildInlineCode
5615
- }] }
5616
- };
5401
+ }
5402
+ ]
5403
+ } };
5617
5404
  //#endregion
5618
5405
  //#region src/profile/index.ts
5619
5406
  function compileProfile(options = {}) {
@@ -5626,13 +5413,14 @@ function compileProfile(options = {}) {
5626
5413
  feature$6
5627
5414
  ];
5628
5415
  if (options.footnote) features.push(feature$10);
5629
- features.push(feature$1);
5630
5416
  if (options.frontmatter) features.unshift(feature$8(typeof options.frontmatter === "object" ? options.frontmatter.marker : "-"));
5631
5417
  if (options.table) features.push(feature);
5632
5418
  if (options.math) features.push(feature$3(options.math));
5633
5419
  features.push(feature$2, feature$13, feature$9(options.strikethrough), feature$5);
5420
+ if (options.attributes) features.push(feature$17);
5421
+ if (options.binding) features.push(feature$16);
5634
5422
  if (options.component) features.push(feature$11);
5635
- if (options.attributes) features.push(feature$16);
5423
+ features.push(feature$1);
5636
5424
  const blockFeatures = [];
5637
5425
  const inlineFeatures = [];
5638
5426
  for (const feature of features) {
@@ -5649,13 +5437,13 @@ function compileProfile(options = {}) {
5649
5437
  function createParser(options) {
5650
5438
  const profile = compileProfile(options?.features);
5651
5439
  let blockScanner;
5652
- let blockArena;
5440
+ let blockStructure;
5653
5441
  return {
5654
5442
  createDocument: (source) => new DocumentImpl(source, profile),
5655
5443
  parse: (source) => {
5656
5444
  blockScanner ??= new BlockScanner(profile.block);
5657
- blockArena ??= new BlockArena(profile.block.schema, blockScanner.tokens);
5658
- return DocumentImpl.parse(source, profile, blockScanner, blockArena);
5445
+ blockStructure ??= new BlockStructure(profile.block.schema, blockScanner.tokens);
5446
+ return DocumentImpl.parse(source, profile, blockScanner, blockStructure);
5659
5447
  }
5660
5448
  };
5661
5449
  }