satorigear 0.0.11 → 0.0.12

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.
Files changed (3) hide show
  1. package/README.md +13 -13
  2. package/dist/index.mjs +1064 -1019
  3. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -1,79 +1,182 @@
1
1
  import { decodeString } from "micromark-util-decode-string";
2
2
  import { htmlBlockNames, htmlRawNames } from "micromark-util-html-tag-name";
3
- // src/block/lines.ts
4
- var BlockLines = class BlockLines {
5
- #fields;
6
- #length = 0;
7
- #locatorField = 0;
8
- #sourceLength;
9
- #lineEndingsNormalized = true;
10
- constructor(sourceLength = 0, lineCapacity = 4) {
11
- this.#sourceLength = sourceLength;
12
- this.#fields = new Int32Array(lineCapacity * 4);
3
+ // src/block/scratch.ts
4
+ const allocate = globalThis.Buffer?.allocUnsafeSlow;
5
+ var ScratchView = class {
6
+ static bytes;
7
+ static words;
8
+ static supports = allocate !== void 0;
9
+ static prepare(sourceLength) {
10
+ const wordLimit = sourceLength + 31 & -32;
11
+ if (!this.bytes || this.bytes.length < wordLimit) {
12
+ this.bytes = allocate(wordLimit);
13
+ this.words = new Uint32Array(this.bytes.buffer, this.bytes.byteOffset, wordLimit >>> 2);
14
+ }
15
+ for (let i = 1; i <= 8; i++) this.words[(wordLimit >>> 2) - i] = 538976288;
16
+ return wordLimit;
13
17
  }
14
- static from(source, start, limit) {
15
- const sourceLength = source.length;
16
- if (start > 0 || limit < sourceLength) source = source.slice(start, limit);
17
- const lines = new BlockLines(sourceLength, Math.min(source.length + 15 >>> 4, 16384));
18
- lines.#length = BlockLines.#scan(source, lines);
19
- if (start !== 0) lines.#shiftPositions(0, start);
20
- return lines;
21
- }
22
- scan(source) {
23
- this.#sourceLength = source.length;
24
- this.#locatorField = 0;
25
- const lineCapacity = Math.min(source.length + 15 >>> 4, 16384);
26
- this.#ensureCapacity(lineCapacity * 4);
27
- this.#length = BlockLines.#scan(source, this);
28
- return this;
29
- }
30
- static #scan(source, lines) {
18
+ };
19
+ // src/block/lines.ts
20
+ function getLineCapacity(sourceLength) {
21
+ return Math.min(sourceLength + 15 >>> 4, 16384);
22
+ }
23
+ function growLineFields(fields, fieldLength) {
24
+ if (fieldLength <= fields.length) return fields;
25
+ let capacity = fields.length / 4;
26
+ while (capacity < fieldLength / 4) capacity += capacity < 16 ? Math.max(4, capacity) : capacity >>> 1;
27
+ const next = new Int32Array(capacity * 4);
28
+ next.set(fields);
29
+ return next;
30
+ }
31
+ var Latin1 = class {
32
+ static scan(source, fields) {
31
33
  const limit = source.length;
34
+ const wordLimit = ScratchView.prepare(limit);
35
+ const bytes = ScratchView.bytes;
36
+ const words = ScratchView.words;
37
+ bytes.write(source, 0, limit, "latin1");
38
+ let field = 0;
32
39
  let start = 0;
33
- let lineFeed = source.indexOf("\n");
34
- let carriageReturn = source.indexOf("\r");
35
- lines.#lineEndingsNormalized = carriageReturn < 0;
40
+ let cursor = 0;
41
+ let crnext = -1;
42
+ while (cursor < wordLimit) {
43
+ fields = growLineFields(fields, field + Math.min(32, limit - cursor) * 4);
44
+ const available = (fields.length - field) / 4;
45
+ const wordEnd = cursor > limit - available ? wordLimit : cursor + (available & -32);
46
+ for (;;) {
47
+ const offset = cursor >>> 2;
48
+ const word0 = words[offset];
49
+ const word1 = words[offset | 1];
50
+ const word2 = words[offset | 2];
51
+ const word3 = words[offset | 3];
52
+ const word4 = words[offset | 4];
53
+ const word5 = words[offset | 5];
54
+ const word6 = words[offset | 6];
55
+ const word7 = words[offset | 7];
56
+ const hits0 = word0 - 269488144 & ~word0 & 2155905152;
57
+ const hits1 = word1 - 269488144 & ~word1 & 2155905152;
58
+ const hits2 = word2 - 269488144 & ~word2 & 2155905152;
59
+ const hits3 = word3 - 269488144 & ~word3 & 2155905152;
60
+ const hits4 = word4 - 269488144 & ~word4 & 2155905152;
61
+ const hits5 = word5 - 269488144 & ~word5 & 2155905152;
62
+ const hits6 = word6 - 269488144 & ~word6 & 2155905152;
63
+ const hits7 = word7 - 269488144 & ~word7 & 2155905152;
64
+ if (hits0 | hits1 | hits2 | hits3 | hits4 | hits5 | hits6 | hits7) {
65
+ let events = Math.imul(hits0 >>> 4 | hits1, 2113665) >>> 24 | Math.imul(hits2 >>> 4 | hits3, 2113665) >>> 24 << 8 | Math.imul(hits4 >>> 4 | hits5, 2113665) >>> 24 << 16 | Math.imul(hits6 >>> 4 | hits7, 2113665) >>> 24 << 24;
66
+ const last = cursor + 31;
67
+ do {
68
+ const at = last - Math.clz32(events & -events);
69
+ const code = source.charCodeAt(at);
70
+ if (code === 10 || code === 13) {
71
+ let next = at + 1;
72
+ if (code === 13) {
73
+ crnext = next;
74
+ if (events >>> (at & 31) & 2 && source.charCodeAt(next) === 10) {
75
+ next++;
76
+ events &= events - 1;
77
+ }
78
+ } else if (at === crnext) {
79
+ fields[field - 4 | 2] = next;
80
+ start = next;
81
+ continue;
82
+ }
83
+ fields[field | 0] = start;
84
+ fields[field | 1] = at;
85
+ fields[field | 2] = next;
86
+ fields[field | 3] = 0;
87
+ field += 4;
88
+ start = next;
89
+ }
90
+ } while (events &= events - 1);
91
+ }
92
+ cursor += 32;
93
+ if (cursor >= wordEnd) break;
94
+ }
95
+ }
96
+ if (start < limit) {
97
+ fields = growLineFields(fields, field + 4);
98
+ fields[field | 0] = start;
99
+ fields[field | 1] = limit;
100
+ fields[field | 2] = limit;
101
+ fields[field | 3] = 0;
102
+ field += 4;
103
+ }
104
+ return {
105
+ fields,
106
+ length: field / 4,
107
+ normalized: crnext < 0
108
+ };
109
+ }
110
+ };
111
+ var Index0 = class {
112
+ static scan(source, fields) {
36
113
  let field = 0;
37
- let fields = lines.#fields;
114
+ let start = 0;
115
+ let lf = source.indexOf("\n");
116
+ let cr = source.indexOf("\r");
117
+ const normalized = cr < 0;
118
+ const limit = source.length;
38
119
  while (start < limit) {
39
- let end = lineFeed;
120
+ let end = lf;
40
121
  let next = end + 1;
41
- if (carriageReturn !== -1 && (end < 0 || carriageReturn < end)) {
42
- end = carriageReturn;
122
+ if (cr !== -1 && (end < 0 || cr < end)) {
123
+ end = cr;
43
124
  next = end + (source.charCodeAt(end + 1) === 10 ? 2 : 1);
44
- carriageReturn = source.indexOf("\r", next);
45
- if (lineFeed !== -1 && lineFeed < next) lineFeed = source.indexOf("\n", next);
46
- } else if (lineFeed < 0) end = next = limit;
47
- else lineFeed = source.indexOf("\n", next);
48
- if (field >= fields.length) {
49
- lines.#ensureCapacity(field + 4);
50
- fields = lines.#fields;
51
- }
52
- fields[field++] = start;
53
- fields[field++] = end;
54
- fields[field++] = next;
55
- fields[field++] = 0;
125
+ cr = source.indexOf("\r", next);
126
+ if (lf !== -1 && lf < next) lf = source.indexOf("\n", next);
127
+ } else if (lf < 0) end = next = limit;
128
+ else lf = source.indexOf("\n", next);
129
+ if (field >= fields.length) fields = growLineFields(fields, field + 4);
130
+ fields[field | 0] = start;
131
+ fields[field | 1] = end;
132
+ fields[field | 2] = next;
133
+ fields[field | 3] = 0;
134
+ field += 4;
56
135
  start = next;
57
136
  }
58
- return field / 4;
137
+ return {
138
+ fields,
139
+ length: field / 4,
140
+ normalized
141
+ };
142
+ }
143
+ };
144
+ var BlockLines = class BlockLines {
145
+ #fields;
146
+ #length = 0;
147
+ #locatorField = 0;
148
+ #normalized = true;
149
+ #sourceLength;
150
+ constructor(sourceLength = 0, capacity = 4) {
151
+ this.#sourceLength = sourceLength;
152
+ this.#fields = new Int32Array(capacity * 4);
153
+ }
154
+ scan(source, scratch = false) {
155
+ this.#locatorField = 0;
156
+ this.#sourceLength = source.length;
157
+ const fields = growLineFields(this.#fields, getLineCapacity(source.length) * 4);
158
+ const result = scratch && ScratchView.supports ? Latin1.scan(source, fields) : Index0.scan(source, fields);
159
+ this.#fields = result.fields;
160
+ this.#length = result.length;
161
+ this.#normalized = result.normalized;
59
162
  }
60
163
  get length() {
61
164
  return this.#length;
62
165
  }
63
166
  start(index) {
64
- return this.#fields[index * 4 + 0];
167
+ return this.#fields[index * 4 | 0];
65
168
  }
66
169
  end(index) {
67
- return this.#fields[index * 4 + 1];
170
+ return this.#fields[index * 4 | 1];
68
171
  }
69
172
  next(index) {
70
- return this.#fields[index * 4 + 2];
173
+ return this.#fields[index * 4 | 2];
71
174
  }
72
175
  prefixColumns(index) {
73
- return this.#fields[index * 4 + 3] >>> 1;
176
+ return this.#fields[index * 4 | 3] >>> 1;
74
177
  }
75
178
  lazy(index) {
76
- return (this.#fields[index * 4 + 3] & 1) !== 0;
179
+ return (this.#fields[index * 4 | 3] & 1) !== 0;
77
180
  }
78
181
  indexAtOrAfter(offset) {
79
182
  let low = 0;
@@ -90,34 +193,35 @@ var BlockLines = class BlockLines {
90
193
  return this;
91
194
  }
92
195
  normalizeLineEndings(value) {
93
- return this.#lineEndingsNormalized || !value.includes("\r") ? value : value.replace(/\r\n|\r/g, "\n");
196
+ return this.#normalized || !value.includes("\r") ? value : value.replace(/\r\n|\r/g, "\n");
94
197
  }
95
198
  locationAt(offset) {
96
- const fieldLength = this.#length * 4;
97
- if (fieldLength === 0) return {
199
+ const length = this.#length;
200
+ if (length === 0) return {
98
201
  line: 1,
99
202
  column: 1,
100
203
  offset
101
204
  };
102
205
  const fields = this.#fields;
103
- const sourceLength = this.#sourceLength;
104
- if (offset === sourceLength && fields[fieldLength - 4 + 1] < sourceLength) return {
105
- line: fieldLength / 4 + 1,
106
- column: 1,
107
- offset
108
- };
109
206
  let field = this.#locatorField;
110
- let start = fields[field + 0];
111
- while (field + 4 < fieldLength) {
112
- const nextStart = fields[field + 4 + 0];
113
- if (nextStart > offset) break;
114
- field += 4;
115
- start = nextStart;
207
+ if (fields[field | 2] <= offset) {
208
+ const sourceLength = this.#sourceLength;
209
+ if (offset < sourceLength) do
210
+ field += 4;
211
+ while (fields[field | 2] <= offset);
212
+ else {
213
+ field = (length - 1) * 4;
214
+ if (offset === sourceLength && fields[field | 1] < offset) return {
215
+ line: length + 1,
216
+ column: 1,
217
+ offset
218
+ };
219
+ }
220
+ this.#locatorField = field;
116
221
  }
117
- this.#locatorField = field;
118
222
  return {
119
223
  line: field / 4 + 1,
120
- column: offset - start + 1,
224
+ column: offset - fields[field | 0] + 1,
121
225
  offset
122
226
  };
123
227
  }
@@ -134,14 +238,14 @@ var BlockLines = class BlockLines {
134
238
  const field = this.#length * 4;
135
239
  let fields = this.#fields;
136
240
  if (field >= fields.length) {
137
- this.#ensureCapacity(field + 4);
241
+ this.#fields = growLineFields(this.#fields, field + 4);
138
242
  fields = this.#fields;
139
243
  }
140
244
  const state = prefixColumns * 2 + (lazy ? 1 : 0);
141
- fields[field + 0] = start;
142
- fields[field + 1] = end;
143
- fields[field + 2] = next;
144
- fields[field + 3] = state;
245
+ fields[field | 0] = start;
246
+ fields[field | 1] = end;
247
+ fields[field | 2] = next;
248
+ fields[field | 3] = state;
145
249
  this.#length++;
146
250
  }
147
251
  pushFrom(lines, index, start = lines.start(index), prefixColumns = lines.prefixColumns(index), lazy = lines.lazy(index)) {
@@ -181,28 +285,31 @@ var BlockLines = class BlockLines {
181
285
  const result = new BlockLines(this.#sourceLength, 0);
182
286
  result.#fields = this.#fields.subarray(start * 4, end * 4);
183
287
  result.#length = end - start;
184
- result.#lineEndingsNormalized = this.#lineEndingsNormalized;
288
+ result.#normalized = this.#normalized;
185
289
  return result;
186
290
  }
187
291
  #copy() {
188
292
  const next = new BlockLines(this.#sourceLength, 0);
189
293
  next.#fields = this.#fields.slice(0, this.#length * 4);
190
294
  next.#length = this.#length;
191
- next.#lineEndingsNormalized = this.#lineEndingsNormalized;
295
+ next.#normalized = this.#normalized;
192
296
  return next;
193
297
  }
194
- update(nextSource, restartOffset, oldDamageEnd, delta) {
195
- const suffix = Math.min(this.length, this.indexAtOrAfter(oldDamageEnd + 1) + 1);
298
+ update(nextSource, damageStart, oldDamageEnd, delta) {
299
+ const suffix = this.indexAtOrAfter(oldDamageEnd + 1);
196
300
  const newSuffixOffset = (suffix < this.length ? this.start(suffix) : nextSource.length - delta) + delta;
197
- let prefixEnd = this.indexAtOrAfter(restartOffset);
198
- if (nextSource.charCodeAt(restartOffset - 1) === 13 && nextSource.charCodeAt(restartOffset) === 10) restartOffset = this.start(--prefixEnd);
199
- const changed = BlockLines.from(nextSource, restartOffset, newSuffixOffset);
301
+ const prefixEnd = Math.max(0, this.indexAtOrAfter(damageStart) - 1);
302
+ const restartOffset = prefixEnd === 0 ? 0 : this.start(prefixEnd);
303
+ const changedSource = nextSource.slice(restartOffset, newSuffixOffset);
304
+ const changed = new BlockLines(0, getLineCapacity(changedSource.length));
305
+ changed.scan(changedSource);
306
+ if (restartOffset !== 0) changed.#shiftPositions(0, restartOffset);
200
307
  if (delta === 0 && changed.length === suffix - prefixEnd) {
201
308
  const next = this.#copy();
202
309
  const target = prefixEnd * 4;
203
310
  const fieldLength = changed.length * 4;
204
311
  for (let field = 0; field < fieldLength; field++) next.#fields[target + field] = changed.#fields[field];
205
- next.#lineEndingsNormalized &&= changed.#lineEndingsNormalized;
312
+ next.#normalized &&= changed.#normalized;
206
313
  return next;
207
314
  }
208
315
  const next = new BlockLines(nextSource.length, prefixEnd + changed.length + this.length - suffix);
@@ -211,7 +318,7 @@ var BlockLines = class BlockLines {
211
318
  const suffixStart = next.length;
212
319
  next.#appendRawRange(this, suffix, this.length);
213
320
  if (delta !== 0) next.#shiftPositions(suffixStart, delta);
214
- next.#lineEndingsNormalized = this.#lineEndingsNormalized && changed.#lineEndingsNormalized;
321
+ next.#normalized = this.#normalized && changed.#normalized;
215
322
  return next;
216
323
  }
217
324
  pop() {
@@ -223,42 +330,20 @@ var BlockLines = class BlockLines {
223
330
  const sourceEnd = end * 4;
224
331
  const targetStart = this.#length * 4;
225
332
  const targetEnd = targetStart + sourceEnd - sourceStart;
226
- this.#ensureCapacity(targetEnd);
333
+ this.#fields = growLineFields(this.#fields, targetEnd);
227
334
  this.#fields.set(lines.#fields.subarray(sourceStart, sourceEnd), targetStart);
228
335
  this.#length += end - start;
229
336
  }
230
- #ensureCapacity(fieldLength) {
231
- const fields = this.#fields;
232
- if (fieldLength <= fields.length) return;
233
- let lineCapacity = fields.length / 4;
234
- const requiredLines = fieldLength / 4;
235
- while (lineCapacity < requiredLines) lineCapacity += lineCapacity < 16 ? Math.max(4, lineCapacity) : lineCapacity >>> 1;
236
- const nextFields = new Int32Array(lineCapacity * 4);
237
- nextFields.set(fields);
238
- this.#fields = nextFields;
239
- }
240
337
  #shiftPositions(start, delta) {
241
338
  const fields = this.#fields;
242
339
  const fieldLength = this.#length * 4;
243
340
  for (let field = start * 4; field < fieldLength; field += 4) {
244
- fields[field + 0] += delta;
245
- fields[field + 1] += delta;
246
- fields[field + 2] += delta;
341
+ fields[field | 0] += delta;
342
+ fields[field | 1] += delta;
343
+ fields[field | 2] += delta;
247
344
  }
248
345
  }
249
346
  };
250
- function lineContentEnd(source, start, end) {
251
- if (end <= start) return end;
252
- if (source.charCodeAt(end - 1) === 10) return end - (end > start + 1 && source.charCodeAt(end - 2) === 13 ? 2 : 1);
253
- return source.charCodeAt(end - 1) === 13 ? end - 1 : end;
254
- }
255
- function indentOffset(source, lines, index) {
256
- let offset = lines.start(index);
257
- const limit = offset + 3 - lines.prefixColumns(index);
258
- const end = lines.end(index);
259
- while (offset < end && offset < limit && source.charCodeAt(offset) === 32) offset++;
260
- return offset;
261
- }
262
347
  function lineIndentOffset(source, lines, index) {
263
348
  let offset = lines.start(index);
264
349
  const limit = offset + 3 - lines.prefixColumns(index);
@@ -322,12 +407,17 @@ var BlockTokenStream = class {
322
407
  #fields = new Int32Array();
323
408
  #metadata;
324
409
  #opens = [];
325
- constructor() {}
326
410
  reset() {
327
411
  this.#definitionCounts?.clear();
328
412
  this.#definitionMembershipChanges?.clear();
413
+ const metadata = this.#metadata;
414
+ if (metadata) {
415
+ const used = Math.min(metadata.length, this.length);
416
+ for (let index = 0; index < used; index++) metadata[index] = void 0;
417
+ const reserve = Math.max(used, 16);
418
+ if (metadata.length > reserve * 2) metadata.length = reserve;
419
+ }
329
420
  this.#fieldLength = 0;
330
- this.#metadata = void 0;
331
421
  if (this.#opens.length) this.#opens.length = 0;
332
422
  }
333
423
  get length() {
@@ -342,14 +432,16 @@ var BlockTokenStream = class {
342
432
  push(kind, start, end, meta) {
343
433
  const field = this.#fieldLength;
344
434
  this.#ensureCapacity(field + 4);
345
- this.#fields[field + 0] = kind;
346
- this.#fields[field + 1] = start;
347
- this.#fields[field + 2] = end;
348
- this.#fields[field + 3] = kind < 64 ? 0 : this.#indexToken(field, kind);
435
+ this.#fields[field | 0] = kind;
436
+ this.#fields[field | 1] = start;
437
+ this.#fields[field | 2] = end;
438
+ this.#fields[field | 3] = kind < 64 ? 0 : this.#indexToken(field, kind);
349
439
  this.#fieldLength += 4;
350
440
  if (meta) {
351
- this.#metadata ??= [];
352
- this.#metadata[field / 4] = meta;
441
+ const index = field / 4;
442
+ const metadata = this.#metadata ??= new Array(Math.max(index + 1, 16));
443
+ if (index >= metadata.length) metadata.length = Math.max(index + 1, metadata.length * 2);
444
+ metadata[index] = meta;
353
445
  const definitionKey = meta.definitionKey;
354
446
  if (definitionKey !== void 0) this.#updateDefinitionCount(definitionKey, 1, false);
355
447
  }
@@ -357,12 +449,12 @@ var BlockTokenStream = class {
357
449
  closeRange(start, end) {
358
450
  const field = this.#fieldLength;
359
451
  this.#ensureCapacity(field + 4);
360
- this.#fields[field + 0] = start < end ? 1 : 0;
361
- this.#fields[field + 1] = start;
362
- this.#fields[field + 2] = end;
363
- this.#fields[field + 3] = 0;
452
+ this.#fields[field | 0] = start < end ? 1 : 0;
453
+ this.#fields[field | 1] = start;
454
+ this.#fields[field | 2] = end;
455
+ this.#fields[field | 3] = 0;
364
456
  const open = this.#opens.pop();
365
- this.#fields[open + 3] = (field - open) / 4 + 1;
457
+ this.#fields[open | 3] = (field - open) / 4 + 1;
366
458
  this.#fieldLength += 4;
367
459
  }
368
460
  #indexToken(field, kind) {
@@ -372,13 +464,13 @@ var BlockTokenStream = class {
372
464
  }
373
465
  if (kind < 256) {
374
466
  const open = this.#opens.pop();
375
- this.#fields[open + 3] = (field - open) / 4 + 1;
467
+ this.#fields[open | 3] = (field - open) / 4 + 1;
376
468
  return 0;
377
469
  }
378
470
  return 1;
379
471
  }
380
472
  nodeLength(index) {
381
- return this.#fields[index * 4 + 3];
473
+ return this.#fields[index * 4 | 3];
382
474
  }
383
475
  equalsAfterShift(index, next, nextIndex, delta, damageStart, damageEnd) {
384
476
  const start = this.start(index);
@@ -441,7 +533,7 @@ var BlockTokenStream = class {
441
533
  const metadata = this.#metadata;
442
534
  if (metadata) metadata.fill(void 0, start, end);
443
535
  const replacementMetadata = replacement.#metadata;
444
- if (replacementMetadata) copyMetadata(this.#metadata ??= [], replacementMetadata, 0, replacementMetadata.length, start);
536
+ if (replacementMetadata) copyMetadata(this.#metadata ??= [], replacementMetadata, 0, replacementLength, start);
445
537
  if (end === previousLength && this.#metadata) this.#metadata.length = Math.min(this.#metadata.length, start + replacementLength);
446
538
  } else {
447
539
  const metadata = [];
@@ -451,15 +543,15 @@ var BlockTokenStream = class {
451
543
  copyMetadata(metadata, previousMetadata, end, previousMetadata.length, end + indexDelta);
452
544
  }
453
545
  const replacementMetadata = replacement.#metadata;
454
- if (replacementMetadata) copyMetadata(metadata, replacementMetadata, 0, replacementMetadata.length, start);
546
+ if (replacementMetadata) copyMetadata(metadata, replacementMetadata, 0, replacementLength, start);
455
547
  this.#metadata = metadata.length > 0 ? metadata : void 0;
456
548
  }
457
549
  const newSuffixStart = start + replacementLength;
458
550
  if (sourceDelta !== 0 && newSuffixStart < this.length) {
459
551
  const suffixFieldStart = newSuffixStart * 4;
460
552
  for (let field = suffixFieldStart; field < this.#fieldLength; field += 4) {
461
- this.#fields[field + 1] += sourceDelta;
462
- this.#fields[field + 2] += sourceDelta;
553
+ this.#fields[field | 1] += sourceDelta;
554
+ this.#fields[field | 2] += sourceDelta;
463
555
  }
464
556
  }
465
557
  return change;
@@ -482,19 +574,23 @@ var BlockTokenStream = class {
482
574
  this.#fields[index * 4] = kind;
483
575
  }
484
576
  start(index) {
485
- return this.#fields[index * 4 + 1];
577
+ return this.#fields[index * 4 | 1];
486
578
  }
487
579
  setStart(index, start) {
488
- this.#fields[index * 4 + 1] = start;
580
+ this.#fields[index * 4 | 1] = start;
489
581
  }
490
582
  end(index) {
491
- return this.#fields[index * 4 + 2];
583
+ return this.#fields[index * 4 | 2];
492
584
  }
493
585
  setEnd(index, end) {
494
- this.#fields[index * 4 + 2] = end;
586
+ this.#fields[index * 4 | 2] = end;
495
587
  }
496
588
  contentEnd(source, index) {
497
- return lineContentEnd(source, this.start(index), this.end(index));
589
+ const start = this.start(index);
590
+ const end = this.end(index);
591
+ if (end <= start) return end;
592
+ const code = source.charCodeAt(end - 1);
593
+ return code === 10 ? end - (end > start + 1 && source.charCodeAt(end - 2) === 13 ? 2 : 1) : code === 13 ? end - 1 : end;
498
594
  }
499
595
  text(source, index) {
500
596
  return this.#metadata?.[index]?.text ?? source.slice(this.start(index), this.end(index));
@@ -571,8 +667,8 @@ function sameShiftedBlock(previous, record, next, tokenStart, tokenEnd, delta) {
571
667
  return true;
572
668
  }
573
669
  var BlockScanContext = class {
574
- #lineViewDepth = 0;
575
670
  #lineViews = [];
671
+ #scanDepth = 0;
576
672
  #lookaheadEnd = 0;
577
673
  #profile;
578
674
  #unwrappedLine = new BlockLines();
@@ -610,45 +706,38 @@ var BlockScanContext = class {
610
706
  return end;
611
707
  }
612
708
  createLineView(lines, index, start, prefixColumns) {
613
- const depth = this.#lineViewDepth++;
709
+ const depth = this.#scanDepth - 1;
614
710
  const view = this.#lineViews[depth] ??= new BlockLines();
615
711
  view.resetFrom(lines, index, start, prefixColumns);
616
712
  return view;
617
713
  }
618
714
  scanBlockLines(source, lines, tokens, visit) {
619
- const profile = this.#profile;
620
- for (let index = 0; index < lines.length;) {
621
- let contentOffset = lines.start(index);
622
- const lineEnd = lines.end(index);
623
- let columns = lines.prefixColumns(index);
624
- while (contentOffset < lineEnd) {
625
- const code = source.charCodeAt(contentOffset);
626
- if (code !== 32 && code !== 9) break;
627
- columns += code === 32 ? 1 : 4 - columns % 4;
628
- contentOffset++;
629
- }
630
- if (contentOffset === lineEnd) {
631
- index++;
632
- continue;
715
+ this.#scanDepth++;
716
+ try {
717
+ for (let index = 0; index < lines.length;) {
718
+ let contentOffset = lines.start(index);
719
+ const lineEnd = lines.end(index);
720
+ let columns = lines.prefixColumns(index);
721
+ while (contentOffset < lineEnd) {
722
+ const code = source.charCodeAt(contentOffset);
723
+ if (code !== 32 && code !== 9) break;
724
+ columns += code === 32 ? 1 : 4 - columns % 4;
725
+ contentOffset++;
726
+ }
727
+ if (contentOffset === lineEnd) {
728
+ index++;
729
+ continue;
730
+ }
731
+ if (columns > 3) contentOffset = -1;
732
+ const lineStart = index;
733
+ const tokenStart = tokens.length;
734
+ index = this.#profile.dispatch(source, lines, tokens, index, contentOffset, this);
735
+ if (visit?.(lineStart, index, tokenStart, tokens.length)) return;
633
736
  }
634
- if (columns > 3) contentOffset = -1;
635
- const lineStart = index;
636
- const tokenStart = tokens.length;
637
- index = profile.dispatch(source, lines, tokens, index, contentOffset, this);
638
- if (visit?.(lineStart, index, tokenStart, tokens.length)) return;
737
+ } finally {
738
+ this.#scanDepth--;
639
739
  }
640
740
  }
641
- scanLines(source, lines, tokens) {
642
- let blankSeparated = false;
643
- let previousContentEnd = -1;
644
- this.scanBlockLines(source, lines, tokens, (lineStart, lineEnd) => {
645
- blankSeparated ||= previousContentEnd >= 0 && lineStart > previousContentEnd;
646
- previousContentEnd = lineEnd;
647
- while (previousContentEnd > lineStart && isBlankLine(source, lines, previousContentEnd - 1)) previousContentEnd--;
648
- });
649
- if (lines === this.#lineViews[this.#lineViewDepth - 1]) this.#lineViewDepth--;
650
- return blankSeparated;
651
- }
652
741
  startsInterruptingBlock(source, lines, index, contentOffset = lineIndentOffset(source, lines, index)) {
653
742
  if (contentOffset < 0) return false;
654
743
  const interrupts = this.#profile.interrupts[source.charCodeAt(contentOffset)];
@@ -668,10 +757,11 @@ var BlockScanner = class {
668
757
  this.builds = profile.builds;
669
758
  }
670
759
  scan(source, indexed) {
671
- const lines = this.#lines.scan(source);
672
760
  const context = this.#context;
761
+ const lines = this.#lines;
673
762
  const tokens = this.#tokens;
674
763
  context.resetLookahead();
764
+ lines.scan(source, true);
675
765
  tokens.reset();
676
766
  if (!indexed) {
677
767
  context.scanBlockLines(source, lines, tokens);
@@ -717,7 +807,7 @@ var BlockScanner = class {
717
807
  const restartIndex = affectedIndex === 0 && affectedRecord?.start > changedSpan.start ? -1 : affectedRecord && affectedRecord.end < changedSpan.start ? affectedIndex : Math.max(0, affectedIndex - 1);
718
808
  const restartRecord = previousRecords[restartIndex];
719
809
  const restartOffset = restartRecord?.start ?? 0;
720
- const nextLines = this.#lines.update(nextSource, restartOffset, oldChangedEnd, offsetDelta);
810
+ const nextLines = this.#lines.update(nextSource, changedSpan.start, oldChangedEnd, offsetDelta);
721
811
  const scanLineStart = nextLines.indexAtOrAfter(restartOffset);
722
812
  const stableBlockCount = Math.max(0, restartIndex);
723
813
  const oldTokenStart = restartRecord?.tokenStart ?? 0;
@@ -793,17 +883,21 @@ function buildBlockNode(tokenStart, context) {
793
883
  function buildBlockChildren(tokenStart, context) {
794
884
  const builds = context.structure.builds;
795
885
  const tokens = context.structure.tokens;
796
- const children = [];
797
886
  const tokenEnd = tokenStart + tokens.nodeLength(tokenStart) - 1;
887
+ let children;
798
888
  for (let child = tokenStart + 1; child < tokenEnd;) {
799
889
  const length = tokens.nodeLength(child);
800
890
  if (length > 0) {
801
891
  const build = builds[tokens.kind(child)];
802
- if (build) children.push(build(child, context));
892
+ if (build) {
893
+ const node = build(child, context);
894
+ if (children) children.push(node);
895
+ else children = [node];
896
+ }
803
897
  }
804
898
  child += length || 1;
805
899
  }
806
- return children;
900
+ return children ?? [];
807
901
  }
808
902
  // src/source-view.ts
809
903
  function cloneLocation(location) {
@@ -826,13 +920,10 @@ function containingSegment(segments, offset) {
826
920
  var SourceView = class {
827
921
  #offset = 0;
828
922
  #segments;
923
+ #withBoundaryBreak = false;
829
924
  text = "";
830
- withBoundaryBreak = false;
831
- constructor(source, tokens, tokenStart, nodeLength) {
832
- this.reset(source, tokens, tokenStart, nodeLength);
833
- }
834
925
  reset(source, tokens, tokenStart, nodeLength) {
835
- this.withBoundaryBreak = tokens.value(tokenStart) === boundaryBreak;
926
+ this.#withBoundaryBreak = tokens.value(tokenStart) === boundaryBreak;
836
927
  let token = tokenStart + 1;
837
928
  const firstStart = tokens.start(token);
838
929
  const tokenEnd = tokenStart + nodeLength;
@@ -855,6 +946,9 @@ var SourceView = class {
855
946
  this.#segments = void 0;
856
947
  this.text = source.slice(firstStart, end);
857
948
  }
949
+ tokenize(tokenize, scratch = false) {
950
+ return tokenize(this.text, this.#withBoundaryBreak, !scratch || this.#segments || this.#withBoundaryBreak ? void 0 : this.#offset);
951
+ }
858
952
  mapOffset(offset) {
859
953
  const segments = this.#segments;
860
954
  if (!segments) return this.#offset + offset;
@@ -884,12 +978,15 @@ var TrackedDefinitionLookup = class {
884
978
  this.definitions = definitions;
885
979
  }
886
980
  hasDefinition(key) {
887
- (this.dependencies ??= new Set()).add(key);
981
+ if (this.dependencies !== definitionAny) {
982
+ this.dependencies ??= new Set();
983
+ this.dependencies.add(key);
984
+ }
888
985
  return this.definitions.hasDefinition(key);
889
986
  }
890
987
  hasDefinitions() {
891
988
  const hasAny = this.definitions.hasDefinitions();
892
- if (!hasAny) (this.dependencies ??= new Set()).add(definitionAny);
989
+ if (!hasAny) this.dependencies = definitionAny;
893
990
  return hasAny;
894
991
  }
895
992
  };
@@ -921,10 +1018,9 @@ var InlineRegion = class {
921
1018
  }
922
1019
  #dependenciesChanged(definitionMembershipChanges) {
923
1020
  const dependencies = this.#definitionDependencies;
924
- if (dependencies.has(definitionAny)) return definitionMembershipChanges.size > 0;
925
- const labels = dependencies;
926
- if (labels.size < definitionMembershipChanges.size) {
927
- for (const key of labels) if (definitionMembershipChanges.has(key)) return true;
1021
+ if (dependencies === definitionAny) return definitionMembershipChanges.size > 0;
1022
+ if (dependencies.size < definitionMembershipChanges.size) {
1023
+ for (const key of dependencies) if (definitionMembershipChanges.has(key)) return true;
928
1024
  } else for (const key of definitionMembershipChanges) if (dependencies.has(key)) return true;
929
1025
  return false;
930
1026
  }
@@ -933,7 +1029,7 @@ var InlineRegion = class {
933
1029
  const sourceUnchanged = previousTokens !== void 0 && view.text === this.view.text;
934
1030
  if (sourceUnchanged && !this.#dependenciesChanged(definitionMembershipChanges)) return false;
935
1031
  definitions.dependencies = void 0;
936
- const rawTokens = sourceUnchanged ? previousTokens : this.#profile.tokenize(view.text, view.withBoundaryBreak);
1032
+ const rawTokens = sourceUnchanged ? previousTokens : view.tokenize(this.#profile.tokenize);
937
1033
  const tokens = this.#profile.resolve(view.text, rawTokens, definitions);
938
1034
  this.#definitionDependencies = definitions.dependencies ?? emptySet;
939
1035
  this.#rawTokens = rawTokens;
@@ -986,7 +1082,8 @@ var InlineRegionState = class {
986
1082
  while (displacedIndex < displacedRegions.length && displacedRegions[displacedIndex].tokenStart < previousTokenStart) displacedIndex++;
987
1083
  if (displacedRegions[displacedIndex]?.tokenStart === previousTokenStart) candidate = displacedRegions[displacedIndex++];
988
1084
  }
989
- const view = new SourceView(source, tokens, token, nodeLength);
1085
+ const view = new SourceView();
1086
+ view.reset(source, tokens, token, nodeLength);
990
1087
  regions.push(candidate?.update(token, view, this.#lookup, definitionMembershipChanges) ?? new InlineRegion(this.#profile, token, view, this.#lookup));
991
1088
  token += nodeLength - 1;
992
1089
  }
@@ -1259,41 +1356,22 @@ function compileBlockProfile(features) {
1259
1356
  lazyContinuationUnwrappers
1260
1357
  };
1261
1358
  }
1262
- // src/inline/tokens.ts
1263
- function inlineTokenCount(tokens) {
1264
- return tokens.length / 4;
1265
- }
1266
- function inlineTokenKind(tokens, index) {
1267
- return tokens[index * 4 + 0];
1268
- }
1269
- function inlineTokenStart(tokens, index) {
1270
- return tokens[index * 4 + 1];
1271
- }
1272
- function inlineTokenEnd(tokens, index) {
1273
- return tokens[index * 4 + 2];
1274
- }
1275
- function inlineTokenData(tokens, index) {
1276
- return tokens[index * 4 + 3];
1277
- }
1278
- function setInlineTokenData(tokens, index, data) {
1279
- tokens[index * 4 + 3] = data;
1280
- }
1281
- function rewriteInlineTokenTail(tokens, kind, end) {
1282
- const offset = tokens.length - 4;
1283
- tokens[offset + 0] = kind;
1284
- tokens[offset + 2] = end;
1285
- }
1286
- function inlineTokenText(source, tokens, index, startPadding = 0, endPadding = startPadding) {
1287
- return source.slice(inlineTokenStart(tokens, index) + startPadding, inlineTokenEnd(tokens, index) - endPadding);
1288
- }
1289
- function appendInlineToken(target, kind, start, end, data = 0) {
1290
- target.push(kind, start, end, data);
1291
- }
1292
- function copyInlineToken(target, tokens, index) {
1293
- const offset = index * 4;
1294
- target.push(tokens[offset], tokens[offset + 1], tokens[offset + 2], tokens[offset + 3]);
1359
+ // src/inline/tokenizer.ts
1360
+ const cacheMasks = new Map();
1361
+ function createMasks(chars) {
1362
+ let masks = cacheMasks.get(chars);
1363
+ if (masks) return masks;
1364
+ masks = new Uint8Array(65536);
1365
+ for (let i = 0; i < chars.length; i++) {
1366
+ const marker = chars.charCodeAt(i);
1367
+ for (let byte = 0; byte < 256; byte++) {
1368
+ masks[byte << 8 | marker] |= 1;
1369
+ masks[marker << 8 | byte] |= 2;
1370
+ }
1371
+ }
1372
+ cacheMasks.set(chars, masks);
1373
+ return masks;
1295
1374
  }
1296
- // src/inline/tokenize.ts
1297
1375
  function matchInlinePatternEnd(pattern, source, start) {
1298
1376
  pattern.lastIndex = start;
1299
1377
  return pattern.test(source) ? pattern.lastIndex : -1;
@@ -1304,63 +1382,86 @@ function inlineMarkerRunEnd(source, start) {
1304
1382
  while (source.charCodeAt(end) === marker) end++;
1305
1383
  return end;
1306
1384
  }
1307
- function implicitSoftBreak(source, start, tokens) {
1308
- if (start <= (tokens.length ? inlineTokenEnd(tokens, inlineTokenCount(tokens) - 1) : 0)) return false;
1309
- const previous = source.charCodeAt(start - 1);
1310
- return previous !== 32 && previous !== 9;
1311
- }
1312
- function nextBoundaryOffset(source, boundary, offset) {
1313
- if (offset >= source.length) return -1;
1314
- boundary.lastIndex = offset;
1315
- return boundary.test(source) ? boundary.lastIndex - 1 : -1;
1316
- }
1317
- function tokenize(source, boundary, scanners) {
1318
- let offset = 0;
1319
- while (source.charCodeAt(offset) === 32) offset++;
1320
- offset = nextBoundaryOffset(source, boundary, offset);
1321
- if (offset < 0) return emptyArray;
1322
- const tokens = [];
1323
- for (; offset >= 0; offset = nextBoundaryOffset(source, boundary, offset)) {
1324
- const code = source.charCodeAt(offset);
1325
- if (code === 10 || code === 13) {
1326
- const spacesBreak = source.charCodeAt(offset - 1) === 32 && source.charCodeAt(offset - 2) === 32;
1327
- const endingStart = offset++;
1328
- if (code === 13 && source.charCodeAt(offset) === 10) offset++;
1329
- const lineStart = offset;
1330
- while (source.charCodeAt(offset) === 32) offset++;
1331
- if (spacesBreak) {
1332
- let spaces = endingStart - 2;
1333
- while (source.charCodeAt(spaces - 1) === 32) spaces--;
1334
- tokens.push(128, spaces, offset, lineStart);
1335
- } else if (code !== 10 || lineStart !== offset || !implicitSoftBreak(source, endingStart, tokens)) tokens.push(67, endingStart, offset, 0);
1336
- continue;
1337
- }
1338
- const scannedEnd = scanners[code]?.(source, offset, tokens) ?? offset;
1339
- offset = scannedEnd > offset ? scannedEnd : offset + 1;
1340
- }
1341
- return tokens;
1342
- }
1343
- function generateInlineTokenizer(boundaryFast, boundarySlow, boundaryCodes, scanners) {
1344
- const names = boundaryCodes.map((code, index) => `s${index}`);
1345
- const cases = boundaryCodes.map((code, index) => `case ${code}:d=s${index}(s,o,t);break;`).join("");
1346
- return Function("f", "b", "a", "i", "n", ...names, [
1347
- `return(s,u)=>{const x=u?b:f;let o=0;while(s.charCodeAt(o)===32)o++;`,
1348
- `o=n(s,x,o);if(o<0)return a;const t=[];for(;o>=0;o=n(s,x,o)){`,
1349
- `const c=s.charCodeAt(o);`,
1350
- `if(c===10||c===13){`,
1351
- `const k=s.charCodeAt(o-1)===32&&s.charCodeAt(o-2)===32;`,
1352
- `const r=o++;if(c===13&&s.charCodeAt(o)===10)o++;`,
1353
- `const l=o;while(s.charCodeAt(o)===32)o++;`,
1354
- `if(k){let h=r-2;while(s.charCodeAt(h-1)===32)h--;t.push(128,h,o,l)}`,
1355
- `else if(c!==10||l!==o||!i(s,r,t))t.push(67,r,o,0);continue}`,
1356
- `let d;switch(c){${cases}default:d=o}`,
1357
- `o=d>o?d:o+1}`,
1358
- `return t}`
1359
- ].join(""))(boundaryFast, boundarySlow, emptyArray, implicitSoftBreak, nextBoundaryOffset, ...boundaryCodes.map((code) => scanners[code]));
1385
+ function scanLineEnding(source, start, tokens) {
1386
+ let next = start + 1;
1387
+ if (source.charCodeAt(start) === 13 && source.charCodeAt(next) === 10) next++;
1388
+ const lineStart = next;
1389
+ while (source.charCodeAt(next) === 32) next++;
1390
+ if (source.charCodeAt(start - 1) === 32 && source.charCodeAt(start - 2) === 32) {
1391
+ let spaces = start - 2;
1392
+ while (source.charCodeAt(spaces - 1) === 32) spaces--;
1393
+ tokens.push(128, spaces, next, lineStart);
1394
+ } else tokens.push(67, start, next, 0);
1395
+ return next;
1396
+ }
1397
+ function createBitmap(words, masks, group) {
1398
+ const i = group >>> 2;
1399
+ const word0 = words[i];
1400
+ const word1 = words[i | 1];
1401
+ const word2 = words[i | 2];
1402
+ const word3 = words[i | 3];
1403
+ return masks[word0 & 65535] | masks[word0 >>> 16] << 2 | (masks[word1 & 65535] | masks[word1 >>> 16] << 2) << 4 | (masks[word2 & 65535] | masks[word2 >>> 16] << 2) << 8 | (masks[word3 & 65535] | masks[word3 >>> 16] << 2) << 12;
1404
+ }
1405
+ function tokenizeLatin1(source, base, masks, scanners) {
1406
+ const words = ScratchView.words;
1407
+ const limit = base + source.length;
1408
+ let group = -16;
1409
+ let bitmap = 0;
1410
+ let tokens;
1411
+ for (let cursor = 0; cursor < source.length;) {
1412
+ const absolute = base + cursor;
1413
+ const aligned = absolute & -16;
1414
+ if (group !== aligned) {
1415
+ group = aligned;
1416
+ bitmap = createBitmap(words, masks, group);
1417
+ }
1418
+ let bits = bitmap & -1 << (absolute & 15);
1419
+ while (!bits && (group += 16) < limit) bits = bitmap = createBitmap(words, masks, group);
1420
+ if (!bits) break;
1421
+ const offset = group + 31 - Math.clz32(bits & -bits) - base;
1422
+ if (offset >= source.length) break;
1423
+ const scan = scanners[source.charCodeAt(offset)];
1424
+ const end = scan?.(source, offset, tokens ??= []) ?? offset;
1425
+ cursor = end > offset ? end : offset + 1;
1426
+ }
1427
+ return tokens ?? emptyArray;
1428
+ }
1429
+ function createInlineTokenizer(boundaryFast, boundarySlow, masks, scanners) {
1430
+ return (source, withBoundaryBreak, base) => {
1431
+ if (masks && base !== void 0) return tokenizeLatin1(source, base, masks, scanners);
1432
+ const boundary = withBoundaryBreak ? boundarySlow : boundaryFast;
1433
+ let tokens;
1434
+ for (let cursor = 0; cursor < source.length;) {
1435
+ const offset = matchInlinePatternEnd(boundary, source, cursor) - 1;
1436
+ if (offset < 0) break;
1437
+ const scan = scanners[source.charCodeAt(offset)];
1438
+ const end = scan?.(source, offset, tokens ??= []) ?? offset;
1439
+ cursor = end > offset ? end : offset + 1;
1440
+ }
1441
+ return tokens ?? emptyArray;
1442
+ };
1443
+ }
1444
+ function generateInlineTokenizer(boundaryFast, boundarySlow, masks, codes, scanners) {
1445
+ const names = codes.map((code, index) => `s${index}`);
1446
+ const dispatch = `if(t===a)t=[];let d;switch(s.charCodeAt(o)){${codes.map((code, index) => `case ${code}:d=s${index}(s,o,t);break;`).join("")}default:d=o}p=d>o?d:o+1`;
1447
+ return Function("b1", "b2", "v", "a", "m", "c", ...names, [
1448
+ `return(s,u,z)=>{let t=a,p=0;`,
1449
+ masks && [
1450
+ `if(z!==void 0){`,
1451
+ `const w=v.words,e=z+s.length;let g=-16,h=0;`,
1452
+ `while(p<s.length){const k=z+p,q=k&~15;if(q!==g){g=q;h=c(w,m,g)}`,
1453
+ `let b=h&-1<<(k&15);while(!b&&(g+=16)<e)b=h=c(w,m,g);if(!b)break;`,
1454
+ `const o=g+31-Math.clz32(b&-b)-z;if(o>=s.length)break;`,
1455
+ `${dispatch}}return t}`
1456
+ ].join(""),
1457
+ `const x=u?b2:b1;while(p<s.length){x.lastIndex=p;if(!x.test(s))break;`,
1458
+ `const o=x.lastIndex-1;`,
1459
+ `${dispatch}}return t}`
1460
+ ].join(""))(boundaryFast, boundarySlow, ScratchView, emptyArray, masks, createBitmap, ...codes.map((code) => scanners[code]));
1360
1461
  }
1361
1462
  function compileInlineTokenizer(rules) {
1362
1463
  const scanners = [];
1363
- const boundaryCodes = [];
1464
+ const codes = [];
1364
1465
  for (const rule of rules) {
1365
1466
  const code = rule.marker;
1366
1467
  const previous = scanners[code];
@@ -1368,15 +1469,20 @@ function compileInlineTokenizer(rules) {
1368
1469
  const end = previous(source, start, tokens);
1369
1470
  return end > start ? end : rule.scan(source, start, tokens);
1370
1471
  };
1371
- if (previous === void 0) boundaryCodes.push(code);
1372
- }
1373
- const boundaries = String.fromCharCode(...boundaryCodes).replaceAll(/[\\\]-]/g, "\\$&");
1374
- const boundaryFast = new RegExp(`[\\r${boundaries}]`, "g");
1375
- const boundarySlow = new RegExp(`[\\r${boundaries}]|\\n(?:(?<=[ \\t]\\n)|(?= ))`, "g");
1472
+ if (previous === void 0) codes.push(code);
1473
+ }
1474
+ const markers = String.fromCharCode(...[13, ...codes].sort((a, b) => a - b));
1475
+ const masks = ScratchView.supports && codes.every((code) => code >= 0 && code <= 255) ? createMasks(markers) : void 0;
1476
+ const boundaries = markers.replaceAll(/[\\\]-]/g, "\\$&");
1477
+ const boundaryFast = new RegExp(`[${boundaries}]`, "g");
1478
+ const boundarySlow = new RegExp(`[${boundaries}]|\\n(?:(?<=[ \\t]\\n)|(?= ))`, "g");
1479
+ scanners[10] = scanLineEnding;
1480
+ scanners[13] = scanLineEnding;
1481
+ codes.push(10, 13);
1376
1482
  try {
1377
- return generateInlineTokenizer(boundaryFast, boundarySlow, boundaryCodes, scanners);
1483
+ return generateInlineTokenizer(boundaryFast, boundarySlow, masks, codes, scanners);
1378
1484
  } catch {
1379
- return (source, withBoundaryBreak) => tokenize(source, withBoundaryBreak ? boundarySlow : boundaryFast, scanners);
1485
+ return createInlineTokenizer(boundaryFast, boundarySlow, masks, scanners);
1380
1486
  }
1381
1487
  }
1382
1488
  // src/inline/profile.ts
@@ -1434,8 +1540,34 @@ const blockDecorators = [{
1434
1540
  };
1435
1541
  }
1436
1542
  }];
1543
+ // src/inline/tokens.ts
1544
+ function inlineTokenCount(tokens) {
1545
+ return tokens.length / 4;
1546
+ }
1547
+ function inlineTokenKind(tokens, index) {
1548
+ return tokens[index * 4 | 0];
1549
+ }
1550
+ function inlineTokenStart(tokens, index) {
1551
+ return tokens[index * 4 | 1];
1552
+ }
1553
+ function inlineTokenEnd(tokens, index) {
1554
+ return tokens[index * 4 | 2];
1555
+ }
1556
+ function inlineTokenData(tokens, index) {
1557
+ return tokens[index * 4 | 3];
1558
+ }
1559
+ function setInlineTokenData(tokens, index, data) {
1560
+ tokens[index * 4 | 3] = data;
1561
+ }
1562
+ function inlineTokenText(source, tokens, index, startPadding = 0, endPadding = startPadding) {
1563
+ return source.slice(inlineTokenStart(tokens, index) + startPadding, inlineTokenEnd(tokens, index) - endPadding);
1564
+ }
1565
+ function appendInlineToken(target, kind, start, end, data = 0) {
1566
+ target.push(kind, start, end, data);
1567
+ }
1437
1568
  // src/profile/utils.ts
1438
1569
  const inlineColonBoundary = /[ \t\n\r\p{sc=Han}\p{sc=Hira}\p{sc=Kana}\p{sc=Hang}\p{P}]/u;
1570
+ const nonSpaceAndLineEnding = /[^ \r\n]/;
1439
1571
  function isAsciiDigit(code) {
1440
1572
  return code >= 48 && code <= 57;
1441
1573
  }
@@ -1445,32 +1577,30 @@ function isAsciiLetter(code) {
1445
1577
  function isAsciiPunctuation(code) {
1446
1578
  return code >= 33 && code <= 47 || code >= 58 && code <= 64 || code >= 91 && code <= 96 || code >= 123 && code <= 126;
1447
1579
  }
1448
- function isMarkdownWhitespace(code) {
1449
- return code === 9 || code === 10 || code === 13 || code === 32;
1580
+ function isAsciiWhitespace(code) {
1581
+ return code === 32 || code >= 9 && code <= 13;
1582
+ }
1583
+ function isSpaceOrLineEnding(code) {
1584
+ return code === 32 || code === 10 || code === 13;
1585
+ }
1586
+ function isSpaceTabOrLineEnding(code) {
1587
+ return code === 32 || code === 10 || code === 13 || code === 9;
1450
1588
  }
1451
1589
  function hasInlineColonBoundary(source, start) {
1452
1590
  if (start <= 0) return true;
1453
1591
  const previous = source[start - 1];
1454
1592
  return previous !== ":" && inlineColonBoundary.test(previous);
1455
1593
  }
1594
+ function hasInlinePadding(value) {
1595
+ return isSpaceOrLineEnding(value.charCodeAt(0)) && isSpaceOrLineEnding(value.charCodeAt(value.length - 1)) && nonSpaceAndLineEnding.test(value);
1596
+ }
1456
1597
  function semanticText(value) {
1457
1598
  return value.includes("\\") || value.includes("&") ? decodeString(value) : value;
1458
1599
  }
1600
+ const identifierNeedsCleanup = /[^\x20-\x7E]|^ | $| {2}/;
1601
+ const identifierWhitespace = /[ \t\r\n]+/g;
1459
1602
  function normalizeIdentifier(identifier) {
1460
- scan: {
1461
- let hasUppercase = false;
1462
- for (let index = 0; index < identifier.length; index++) {
1463
- const code = identifier.charCodeAt(index);
1464
- if (code === 32) {
1465
- if (index === 0 || index + 1 === identifier.length || identifier.charCodeAt(index + 1) === 32) break scan;
1466
- continue;
1467
- }
1468
- if (code < 32 || code > 126) break scan;
1469
- if (code >= 65 && code <= 90) hasUppercase = true;
1470
- }
1471
- return hasUppercase ? identifier.toLowerCase() : identifier;
1472
- }
1473
- return identifier.trim().replace(/[ \t\r\n]+/g, " ").toLowerCase().toUpperCase().toLowerCase();
1603
+ return identifierNeedsCleanup.test(identifier) ? identifier.trim().replace(identifierWhitespace, " ").toLowerCase().toUpperCase().toLowerCase() : identifier.toLowerCase();
1474
1604
  }
1475
1605
  // src/profile/features/attributes/shared.ts
1476
1606
  const brackets = {
@@ -1595,7 +1725,7 @@ function parseAttributes(source, start) {
1595
1725
  }
1596
1726
  // src/profile/features/attributes/inline.ts
1597
1727
  function hasVisibleText(source, start, end) {
1598
- for (let offset = start; offset < end; offset++) if (!isMarkdownWhitespace(source.charCodeAt(offset))) return true;
1728
+ for (let offset = start; offset < end; offset++) if (!isSpaceTabOrLineEnding(source.charCodeAt(offset))) return true;
1599
1729
  return false;
1600
1730
  }
1601
1731
  function attributeEndAt(source, start) {
@@ -1616,8 +1746,7 @@ const feature$18 = {
1616
1746
  if (tokens.length === 0 && !hasVisibleText(source, 0, start)) {
1617
1747
  let textEnd = end;
1618
1748
  while (true) {
1619
- let next = textEnd;
1620
- while (source.charCodeAt(next) === 9 || source.charCodeAt(next) === 32) next++;
1749
+ const next = trimLineWhitespaceStart(source, textEnd, source.length);
1621
1750
  const nextEnd = source.charCodeAt(next) === 123 ? attributeEndAt(source, next) : void 0;
1622
1751
  if (nextEnd === void 0) break;
1623
1752
  textEnd = nextEnd;
@@ -1627,9 +1756,9 @@ const feature$18 = {
1627
1756
  const previous = inlineTokenCount(tokens) - 1;
1628
1757
  const previousEnd = previous >= 0 ? inlineTokenEnd(tokens, previous) : 0;
1629
1758
  if (previousEnd < start && (previous >= 0 && inlineTokenKind(tokens, previous) === 256 || hasVisibleText(source, previousEnd, start))) appendInlineToken(tokens, 64, previousEnd, start);
1630
- appendInlineToken(tokens, 256, start, end, start > 0 && isMarkdownWhitespace(source.charCodeAt(start - 1)) ? 1 : 0);
1759
+ appendInlineToken(tokens, 256, start, end, start > 0 && isSpaceTabOrLineEnding(source.charCodeAt(start - 1)) ? 1 : 0);
1631
1760
  let next = end;
1632
- while (isMarkdownWhitespace(source.charCodeAt(next))) next++;
1761
+ while (isSpaceTabOrLineEnding(source.charCodeAt(next))) next++;
1633
1762
  if (next === source.length) for (let index = inlineTokenCount(tokens) - 1; index >= 0; index--) {
1634
1763
  const kind = inlineTokenKind(tokens, index);
1635
1764
  if (kind === 64 && !hasVisibleText(source, inlineTokenStart(tokens, index), inlineTokenEnd(tokens, index))) continue;
@@ -1675,13 +1804,13 @@ const feature$17 = { inline: {
1675
1804
  const valueEnd = separator < 0 || separator > close ? close : separator;
1676
1805
  if (!source.slice(start + 2, valueEnd).trim()) return -1;
1677
1806
  const end = close + 2;
1678
- appendInlineToken(tokens, 135, start, end, valueEnd - start);
1807
+ appendInlineToken(tokens, 134, start, end, valueEnd - start);
1679
1808
  return end;
1680
1809
  }
1681
1810
  }],
1682
1811
  builds: [{
1683
1812
  kind: "leaf",
1684
- token: 135,
1813
+ token: 134,
1685
1814
  build(tokenIndex, position, context) {
1686
1815
  const source = context.view.text;
1687
1816
  const tokenStart = inlineTokenStart(context.tokens, tokenIndex);
@@ -1763,7 +1892,7 @@ const feature$16 = { block: {
1763
1892
  quoteLines.pushLazy(lines, index);
1764
1893
  }
1765
1894
  tokens.push(72, contentOffset, contentOffset + 1);
1766
- context.scanLines(source, quoteLines, tokens);
1895
+ context.scanBlockLines(source, quoteLines, tokens);
1767
1896
  const end = quoteLines.end(quoteLines.length - 1);
1768
1897
  tokens.push(192, end, end);
1769
1898
  return index;
@@ -1825,40 +1954,58 @@ const feature$15 = {
1825
1954
  }] }
1826
1955
  };
1827
1956
  // src/block/fence.ts
1828
- function fenceAt(source, lines, index, rule, markerOffset) {
1829
- const marker = source.charCodeAt(markerOffset);
1830
- if (marker !== rule.marker && marker !== rule.alternateMarker) return;
1831
- let offset = markerOffset + 1;
1832
- while (source.charCodeAt(offset) === marker) offset++;
1833
- const length = offset - markerOffset;
1834
- if (length < rule.minimumLength) return;
1835
- if (marker === rule.forbiddenInfoMarker) {
1836
- const lineEnd = lines.end(index);
1837
- while (offset < lineEnd) if (source.charCodeAt(offset++) === marker) return;
1957
+ function createFencedBlock(options) {
1958
+ const { marker, kind } = options;
1959
+ const normalizeEndings = kind === 258;
1960
+ const opening = createOpening(marker, options);
1961
+ const closing = createClosing(marker);
1962
+ function openingEnd(source, markerOffset) {
1963
+ if (source.charCodeAt(markerOffset + 1) === marker && (opening.lastIndex = markerOffset + 2, opening.test(source))) return opening.lastIndex;
1964
+ return -1;
1838
1965
  }
1839
1966
  return {
1840
- indent: lines.prefixColumns(index) + markerOffset - lines.start(index),
1841
- marker,
1842
- length,
1843
- offset: markerOffset
1967
+ codes: [marker],
1968
+ interrupt(source, lines, index, offset) {
1969
+ return openingEnd(source, offset) >= 0;
1970
+ },
1971
+ closingLineAt(source, lines, open, limit, offset) {
1972
+ const markerEnd = openingEnd(source, offset);
1973
+ if (markerEnd >= 0) return findFenceClose(source, lines, open, limit, marker, markerEnd - offset, closing);
1974
+ },
1975
+ start(source, lines, tokens, start, offset) {
1976
+ const markerEnd = openingEnd(source, offset);
1977
+ if (markerEnd >= 0) {
1978
+ const close = findFenceClose(source, lines, start, lines.length, marker, markerEnd - offset, closing);
1979
+ return emitFencedBlock(source, lines, tokens, kind, start, close, offset, markerEnd, normalizeEndings);
1980
+ }
1981
+ }
1844
1982
  };
1845
1983
  }
1846
- function closesFence(source, lines, index, fence) {
1847
- const lineStart = lines.start(index);
1848
- const first = source.charCodeAt(lineStart);
1849
- if (first !== fence.marker && first !== 32) return false;
1850
- let offset = lineStart;
1851
- if (first !== fence.marker) {
1852
- const contentOffset = lineIndentOffset(source, lines, index);
1853
- if (contentOffset < 0 || source.charCodeAt(contentOffset) !== fence.marker) return false;
1854
- offset = contentOffset;
1984
+ function createOpening(marker, options) {
1985
+ const escaped = "\\u" + marker.toString(16).padStart(4, "0");
1986
+ const pattern = escaped + "{" + (options.minimumLength - 2) + ",}" + (!options.allowMarkerInInfo ? "(?=[^" + escaped + "\\r\\n]*(?:\\r|\\n|$))" : "");
1987
+ return new RegExp(pattern, "y");
1988
+ }
1989
+ function createClosing(marker) {
1990
+ const escaped = "\\u" + marker.toString(16).padStart(4, "0");
1991
+ return new RegExp(escaped + "*[ ]*", "y");
1992
+ }
1993
+ function findFenceClose(source, lines, open, limit, marker, length, closing) {
1994
+ for (let line = open + 1; line < limit; line++) {
1995
+ let offset = lines.start(line);
1996
+ const end = lines.end(line);
1997
+ if (end - offset < length) continue;
1998
+ let first = source.charCodeAt(offset);
1999
+ if (first === 32) {
2000
+ const lastStart = Math.min(end - length, offset + 3 - lines.prefixColumns(line));
2001
+ while (first === 32 && offset < lastStart) first = source.charCodeAt(++offset);
2002
+ }
2003
+ if (first !== marker) continue;
2004
+ if (source.charCodeAt(offset + length - 1) !== marker) continue;
2005
+ closing.lastIndex = offset + 1;
2006
+ if (closing.test(source) && closing.lastIndex === end) return line;
1855
2007
  }
1856
- const markerEnd = offset + fence.length;
1857
- offset++;
1858
- while (source.charCodeAt(offset) === fence.marker) offset++;
1859
- if (offset < markerEnd) return false;
1860
- const lineEnd = lines.end(index);
1861
- return trimLineWhitespaceStart(source, offset, lineEnd) === lineEnd;
2008
+ return limit;
1862
2009
  }
1863
2010
  const fenceSpaceIndent = [
1864
2011
  /^(?<![\u2028\u2029]) /gm,
@@ -1870,15 +2017,19 @@ const fenceTabIndent = [
1870
2017
  /^(?<![\u2028\u2029]) ?\t/gm,
1871
2018
  /^(?<![\u2028\u2029]) {0,2}\t/gm
1872
2019
  ];
1873
- function appendFencedToken(source, lines, tokens, kind, start, end, fence, closed, normalizeEndings) {
1874
- const content = lines.physicallyContiguous() ? physicalFenceContent(source, lines, start, end - (closed ? 2 : 1), fence.indent, normalizeEndings) : logicalFenceContent(source, lines, start, end, closed, fence.indent, normalizeEndings);
2020
+ function emitFencedBlock(source, lines, tokens, kind, start, close, offset, markerEnd, normalizeEndings) {
2021
+ const indent = lines.prefixColumns(start) + offset - lines.start(start);
2022
+ const closed = close < lines.length;
2023
+ const end = closed ? close + 1 : close;
2024
+ const content = lines.physicallyContiguous() ? physicalFenceContent(source, lines, start, close - 1, indent, normalizeEndings) : logicalFenceContent(source, lines, start, close - 1, closed, indent, normalizeEndings);
1875
2025
  const lineEnd = lines.end(start);
1876
- const infoStart = trimLineWhitespaceStart(source, fence.offset + fence.length, lineEnd);
2026
+ const infoStart = trimLineWhitespaceStart(source, markerEnd, lineEnd);
1877
2027
  const consumedEnd = lines.next(end - 1);
1878
- tokens.push(kind, fence.offset, closed || consumedEnd < source.length ? lines.end(end - 1) : consumedEnd, {
2028
+ tokens.push(kind, offset, closed || consumedEnd < source.length ? lines.end(end - 1) : consumedEnd, {
1879
2029
  text: content,
1880
2030
  value: source.slice(infoStart, lineEnd)
1881
2031
  });
2032
+ return end;
1882
2033
  }
1883
2034
  function physicalFenceContent(source, lines, start, last, indent, normalizeEndings) {
1884
2035
  const firstOffset = lines.next(start);
@@ -1888,8 +2039,7 @@ function physicalFenceContent(source, lines, start, last, indent, normalizeEndin
1888
2039
  if (content.includes(" ")) content = content.replace(fenceTabIndent[indent - 1], " ");
1889
2040
  return content.replace(fenceSpaceIndent[indent - 1], "");
1890
2041
  }
1891
- function logicalFenceContent(source, lines, start, end, closed, indent, normalizeEndings) {
1892
- let last = end - (closed ? 2 : 1);
2042
+ function logicalFenceContent(source, lines, start, last, closed, indent, normalizeEndings) {
1893
2043
  if (!closed && last > start && lines.start(last) === lines.next(last) && !lines.prefixColumns(last)) last--;
1894
2044
  const parts = [];
1895
2045
  for (let index = start + 1; index <= last; index++) {
@@ -1908,14 +2058,24 @@ function logicalFenceContent(source, lines, start, end, closed, indent, normaliz
1908
2058
  return parts.join("");
1909
2059
  }
1910
2060
  // src/profile/features/code.ts
1911
- const codeFenceRule = {
1912
- alternateMarker: 126,
1913
- forbiddenInfoMarker: 96,
2061
+ const complexInfo = /[\\& \t]/;
2062
+ const separator = /[ \t]/;
2063
+ const backtickFence = createFencedBlock({
2064
+ kind: 258,
1914
2065
  marker: 96,
1915
- minimumLength: 3
1916
- };
1917
- function codeFenceAt(source, lines, index, markerOffset) {
1918
- return fenceAt(source, lines, index, codeFenceRule, markerOffset);
2066
+ minimumLength: 3,
2067
+ allowMarkerInInfo: false
2068
+ });
2069
+ const tildeFence = createFencedBlock({
2070
+ kind: 258,
2071
+ marker: 126,
2072
+ minimumLength: 3,
2073
+ allowMarkerInInfo: true
2074
+ });
2075
+ function codeFenceClosingLine(source, lines, open, limit, offset) {
2076
+ const marker = source.charCodeAt(offset);
2077
+ if (marker === 96) return backtickFence.closingLineAt(source, lines, open, limit, offset);
2078
+ if (marker === 126) return tildeFence.closingLineAt(source, lines, open, limit, offset);
1919
2079
  }
1920
2080
  function indentedContent(source, lines, start, end) {
1921
2081
  const parts = [];
@@ -1940,45 +2100,40 @@ const feature$14 = {
1940
2100
  token: [258, 259],
1941
2101
  build(tokenStart, context) {
1942
2102
  const tokens = context.structure.tokens;
1943
- const rawInfo = semanticText(tokens.value(tokenStart) ?? "");
1944
- const langEnd = rawInfo.search(/[ \t]/);
1945
- const lang = rawInfo ? langEnd < 0 ? rawInfo : rawInfo.slice(0, langEnd) : null;
1946
- const metaStart = langEnd < 0 ? -1 : rawInfo.slice(langEnd).search(/[^ \t]/);
2103
+ let info = tokens.value(tokenStart) ?? "";
2104
+ let langEnd = info.search(complexInfo);
2105
+ if (langEnd >= 0) {
2106
+ const code = info.charCodeAt(langEnd);
2107
+ if (code === 32 || code === 9) info = semanticText(info);
2108
+ else {
2109
+ info = decodeString(info);
2110
+ langEnd = info.search(separator);
2111
+ }
2112
+ }
1947
2113
  return {
1948
2114
  type: "code",
1949
- lang,
1950
- meta: metaStart < 0 ? null : rawInfo.slice(langEnd + metaStart),
2115
+ lang: langEnd >= 0 ? info.slice(0, langEnd) : info || null,
2116
+ meta: langEnd >= 0 && info.slice(trimLineWhitespaceStart(info, langEnd, info.length)) || null,
1951
2117
  value: tokens.text(context.source, tokenStart),
1952
2118
  position: context.locator.positionAt(tokens.start(tokenStart), tokens.end(tokenStart))
1953
2119
  };
1954
2120
  }
1955
2121
  }],
1956
- starts: [{
1957
- codes: [-1],
1958
- start(source, lines, tokens, start) {
1959
- let end = start + 1;
1960
- while (end < lines.length && lineIndentOffset(source, lines, end) < 0) end++;
1961
- let contentEnd = end;
1962
- while (contentEnd > start && isBlankLine(source, lines, contentEnd - 1)) contentEnd--;
1963
- tokens.push(259, lines.start(start), lines.end(contentEnd - 1), { text: indentedContent(source, lines, start, contentEnd) });
1964
- return end;
1965
- }
1966
- }, {
1967
- codes: [96, 126],
1968
- interrupt(source, lines, index, contentOffset) {
1969
- return !!codeFenceAt(source, lines, index, contentOffset);
2122
+ starts: [
2123
+ {
2124
+ codes: [-1],
2125
+ start(source, lines, tokens, start) {
2126
+ let end = start + 1;
2127
+ while (end < lines.length && lineIndentOffset(source, lines, end) < 0) end++;
2128
+ let contentEnd = end;
2129
+ while (contentEnd > start + 1 && isBlankLine(source, lines, contentEnd - 1)) contentEnd--;
2130
+ tokens.push(259, lines.start(start), lines.end(contentEnd - 1), { text: indentedContent(source, lines, start, contentEnd) });
2131
+ return end;
2132
+ }
1970
2133
  },
1971
- start(source, lines, tokens, start, contentOffset) {
1972
- const fence = codeFenceAt(source, lines, start, contentOffset);
1973
- if (!fence) return;
1974
- let end = start + 1;
1975
- while (end < lines.length && !closesFence(source, lines, end, fence)) end++;
1976
- const closed = end < lines.length;
1977
- if (end < lines.length) end++;
1978
- appendFencedToken(source, lines, tokens, 258, start, end, fence, closed, true);
1979
- return end;
1980
- }
1981
- }]
2134
+ backtickFence,
2135
+ tildeFence
2136
+ ]
1982
2137
  },
1983
2138
  inline: {
1984
2139
  scans: [{
@@ -2006,11 +2161,11 @@ const feature$14 = {
2006
2161
  token: 129,
2007
2162
  build: (tokenIndex, position, context) => {
2008
2163
  const markerLength = inlineTokenData(context.tokens, tokenIndex);
2009
- let value = context.locator.normalizeLineEndings(inlineTokenText(context.view.text, context.tokens, tokenIndex, markerLength));
2010
- if (/^[ \n]/.test(value) && /[ \n]$/.test(value) && /[^ \n]/.test(value)) value = value.slice(1, -1);
2164
+ const text = context.locator.normalizeLineEndings(inlineTokenText(context.view.text, context.tokens, tokenIndex, markerLength));
2165
+ const value = hasInlinePadding(text) ? text.slice(1, -1) : text;
2011
2166
  return {
2012
2167
  type: "inlineCode",
2013
- value: context.tableCell ? value.replace(/\\\|/g, "|") : value,
2168
+ value: context.tableCell ? value.replaceAll("\\|", "|") : value,
2014
2169
  position
2015
2170
  };
2016
2171
  }
@@ -2018,22 +2173,6 @@ const feature$14 = {
2018
2173
  }
2019
2174
  };
2020
2175
  // src/fragment/inline.ts
2021
- function createInlineOutput(children, cursor) {
2022
- return {
2023
- children,
2024
- cursor,
2025
- pendingText: void 0,
2026
- pendingEnd: 0,
2027
- pendingLiteral: ""
2028
- };
2029
- }
2030
- function createLiteralChildren(context, start, end) {
2031
- return start >= end ? [] : [{
2032
- type: "text",
2033
- value: context.view.text.slice(start, end),
2034
- position: context.locator.positionAt(context.view.mapOffset(start), context.view.mapEnd(end))
2035
- }];
2036
- }
2037
2176
  function appendText(context, output, value, start, end, literal) {
2038
2177
  if (output.pendingText === void 0 || "attributes" in output.pendingText) {
2039
2178
  const startLocation = context.locator.locationAt(context.view.mapOffset(start));
@@ -2072,47 +2211,27 @@ function trimTrailingText(context, output) {
2072
2211
  function appendGap(context, output, start, end) {
2073
2212
  appendText(context, output, context.view.text.slice(start, end), start, end, true);
2074
2213
  }
2075
- function appendLeaf(context, output, token, start, end, build) {
2076
- if (start > output.cursor) appendGap(context, output, output.cursor, start);
2077
- const sourceStart = context.view.mapOffset(start);
2078
- const sourceEnd = start === end ? sourceStart : context.view.mapEnd(end);
2079
- finishText(context, output);
2080
- output.children.push(build(token, context.locator.positionAt(sourceStart, sourceEnd), context));
2081
- output.pendingText = void 0;
2082
- }
2083
- function appendRange(profile, context, output, startToken, endToken) {
2084
- let index = startToken;
2085
- while (index < endToken) {
2214
+ function appendTextRange(context, output, trimEnd) {
2215
+ let index = context.tokenIndex;
2216
+ const end = inlineTokenCount(context.tokens);
2217
+ while (index < end) {
2086
2218
  const kind = inlineTokenKind(context.tokens, index);
2087
- if (kind === 67) {
2088
- const endingStart = inlineTokenStart(context.tokens, index);
2089
- const contentStart = inlineTokenEnd(context.tokens, index);
2090
- if (endingStart > output.cursor) appendGap(context, output, output.cursor, endingStart);
2091
- trimTrailingText(context, output);
2092
- appendText(context, output, "\n", endingStart, contentStart, false);
2093
- output.cursor = contentStart;
2094
- index++;
2095
- continue;
2096
- }
2097
- const build = profile.builders[kind];
2219
+ if (kind >= 128 && kind < 256) break;
2098
2220
  const childStart = inlineTokenStart(context.tokens, index);
2099
- if (kind < 192) {
2221
+ if (kind < 128) {
2222
+ if (childStart > output.cursor) appendGap(context, output, output.cursor, childStart);
2100
2223
  const childEnd = inlineTokenEnd(context.tokens, index);
2101
- if (kind < 128) {
2102
- if (childStart > output.cursor) appendGap(context, output, output.cursor, childStart);
2224
+ if (kind === 67) {
2225
+ trimTrailingText(context, output);
2226
+ appendText(context, output, "\n", childStart, childEnd, false);
2227
+ } else {
2228
+ const build = context.builders[kind];
2103
2229
  appendText(context, output, build(index, context), childStart, childEnd, kind === 64);
2104
- } else appendLeaf(context, output, index, childStart, childEnd, build);
2230
+ }
2105
2231
  output.cursor = childEnd;
2106
2232
  index++;
2107
2233
  continue;
2108
2234
  }
2109
- if (kind < 256) {
2110
- if (kind & 1) break;
2111
- if (childStart > output.cursor) appendGap(context, output, output.cursor, childStart);
2112
- finishText(context, output);
2113
- index = appendPair(profile, context, output, index, endToken, build);
2114
- continue;
2115
- }
2116
2235
  const childEnd = inlineTokenEnd(context.tokens, index);
2117
2236
  finishText(context, output);
2118
2237
  const sourceStart = context.view.mapOffset(childStart);
@@ -2120,7 +2239,8 @@ function appendRange(profile, context, output, startToken, endToken) {
2120
2239
  start: sourceStart,
2121
2240
  end: childStart === childEnd ? sourceStart : context.view.mapEnd(childEnd)
2122
2241
  };
2123
- if (build(index, sourceSpan, context, output)) {
2242
+ const decorate = context.builders[kind];
2243
+ if (decorate(index, sourceSpan, context, output)) {
2124
2244
  const previous = output.children.at(-1);
2125
2245
  output.pendingText = previous?.type === "text" ? previous : void 0;
2126
2246
  output.pendingEnd = childEnd;
@@ -2128,35 +2248,77 @@ function appendRange(profile, context, output, startToken, endToken) {
2128
2248
  }
2129
2249
  index++;
2130
2250
  }
2131
- const contentEnd = index < endToken ? inlineTokenStart(context.tokens, index) : context.view.text.length;
2251
+ const contentEnd = index < end ? inlineTokenStart(context.tokens, index) : context.view.text.length;
2132
2252
  if (contentEnd > output.cursor) appendGap(context, output, output.cursor, contentEnd);
2133
- if (startToken === 0) {
2253
+ if (trimEnd && index === end) {
2134
2254
  trimTrailingText(context, output);
2135
2255
  if (output.pendingText && !output.pendingText.value) output.children.pop();
2136
2256
  }
2137
2257
  finishText(context, output);
2138
- return index;
2258
+ output.cursor = contentEnd;
2259
+ context.tokenIndex = index;
2139
2260
  }
2140
- function appendPair(profile, context, output, openToken, endToken, build) {
2141
- const childStart = inlineTokenStart(context.tokens, openToken);
2142
- const positionStart = context.locator.locationAt(context.view.mapOffset(childStart));
2143
- const nextToken = openToken + 1;
2144
- const nextKind = inlineTokenKind(context.tokens, nextToken);
2145
- let children;
2146
- let closeToken;
2147
- if (nextKind >= 192 && nextKind < 256 && nextKind & 1) {
2148
- children = createLiteralChildren(context, inlineTokenEnd(context.tokens, openToken), inlineTokenStart(context.tokens, nextToken));
2149
- closeToken = nextToken;
2150
- } else closeToken = appendRange(profile, context, createInlineOutput(children = [], inlineTokenEnd(context.tokens, openToken)), nextToken, endToken);
2151
- const childEnd = inlineTokenEnd(context.tokens, closeToken);
2152
- const value = build(openToken, closeToken, {
2153
- start: positionStart,
2154
- end: context.locator.locationAt(context.view.mapEnd(childEnd))
2155
- }, children, context);
2156
- output.children.push(value);
2157
- output.pendingText = void 0;
2158
- output.cursor = childEnd;
2159
- return closeToken + 1;
2261
+ function buildLiteral(context, start, end) {
2262
+ return {
2263
+ type: "text",
2264
+ value: context.view.text.slice(start, end),
2265
+ position: context.locator.positionAt(context.view.mapOffset(start), context.view.mapEnd(end))
2266
+ };
2267
+ }
2268
+ function buildFragment(context, cursor, closer) {
2269
+ const trimEnd = closer === 0;
2270
+ const children = [];
2271
+ const tokens = context.tokens;
2272
+ const end = inlineTokenCount(tokens);
2273
+ let output;
2274
+ while (context.tokenIndex < end) {
2275
+ const index = context.tokenIndex;
2276
+ const kind = inlineTokenKind(tokens, index);
2277
+ if (kind === closer) break;
2278
+ if (kind < 128 || kind >= 256) {
2279
+ if (output) {
2280
+ output.cursor = cursor;
2281
+ output.pendingText = void 0;
2282
+ } else output = {
2283
+ children,
2284
+ cursor,
2285
+ pendingText: void 0,
2286
+ pendingEnd: 0,
2287
+ pendingLiteral: ""
2288
+ };
2289
+ appendTextRange(context, output, trimEnd);
2290
+ cursor = output.cursor;
2291
+ continue;
2292
+ }
2293
+ const startOffset = inlineTokenStart(tokens, index);
2294
+ if (cursor < startOffset) children.push(buildLiteral(context, cursor, startOffset));
2295
+ const sourceStart = context.view.mapOffset(startOffset);
2296
+ const start = context.locator.locationAt(sourceStart);
2297
+ let nested;
2298
+ if (kind >= 192) {
2299
+ const start = inlineTokenEnd(tokens, index);
2300
+ const next = index + 1;
2301
+ context.tokenIndex = next;
2302
+ if (inlineTokenKind(tokens, next) === kind + 1) {
2303
+ const end = inlineTokenStart(tokens, next);
2304
+ nested = end <= start ? [] : [buildLiteral(context, start, end)];
2305
+ } else nested = buildFragment(context, start, kind + 1).children;
2306
+ }
2307
+ const close = context.tokenIndex++;
2308
+ const endOffset = inlineTokenEnd(tokens, close);
2309
+ const position = {
2310
+ start,
2311
+ end: context.locator.locationAt(startOffset === endOffset ? sourceStart : context.view.mapEnd(endOffset))
2312
+ };
2313
+ const build = context.builders[kind];
2314
+ const node = nested ? build(index, close, position, nested, context) : build(index, position, context);
2315
+ children.push(node);
2316
+ cursor = endOffset;
2317
+ }
2318
+ let tail = context.tokenIndex < end ? inlineTokenStart(tokens, context.tokenIndex) : context.view.text.length;
2319
+ if (cursor < tail && trimEnd) tail = trimLineWhitespaceEnd(context.view.text, cursor, tail);
2320
+ if (cursor < tail) children.push(buildLiteral(context, cursor, tail));
2321
+ return output ?? { children };
2160
2322
  }
2161
2323
  function buildInlineFragment(tokenStart, tableCell, context) {
2162
2324
  let tokens;
@@ -2170,12 +2332,14 @@ function buildInlineFragment(tokenStart, tableCell, context) {
2170
2332
  const blockTokens = context.structure.tokens;
2171
2333
  const nodeLength = blockTokens.nodeLength(tokenStart);
2172
2334
  if (nodeLength <= 1 || blockTokens.kind(tokenStart + 1) !== 1) return { children: [] };
2173
- const previousView = context.inlineContext?.view;
2174
- view = (previousView?.reset(context.source, blockTokens, tokenStart, nodeLength), previousView) ?? new SourceView(context.source, blockTokens, tokenStart, nodeLength);
2175
- tokens = context.profile.tokenize(view.text, view.withBoundaryBreak);
2335
+ view = context.inlineContext?.view ?? new SourceView();
2336
+ view.reset(context.source, blockTokens, tokenStart, nodeLength);
2337
+ tokens = view.tokenize(context.profile.tokenize, ScratchView.supports);
2176
2338
  if (tokens.length) tokens = context.profile.resolve(view.text, tokens, blockTokens);
2177
2339
  }
2178
2340
  const inlineContext = context.inlineContext ??= {
2341
+ builders: context.profile.builders,
2342
+ tokenIndex: 0,
2179
2343
  locator: context.locator,
2180
2344
  tableCell,
2181
2345
  tokens,
@@ -2184,10 +2348,12 @@ function buildInlineFragment(tokenStart, tableCell, context) {
2184
2348
  inlineContext.tableCell = tableCell;
2185
2349
  inlineContext.tokens = tokens;
2186
2350
  inlineContext.view = view;
2187
- if (tokens.length === 0) return { children: createLiteralChildren(inlineContext, 0, trimLineWhitespaceEnd(view.text, 0, view.text.length)) };
2188
- const output = createInlineOutput([], 0);
2189
- appendRange(context.profile, inlineContext, output, 0, inlineTokenCount(tokens));
2190
- return output;
2351
+ if (tokens.length === 0) {
2352
+ const end = trimLineWhitespaceEnd(view.text, 0, view.text.length);
2353
+ return { children: end <= 0 ? [] : [buildLiteral(inlineContext, 0, end)] };
2354
+ }
2355
+ inlineContext.tokenIndex = 0;
2356
+ return buildFragment(inlineContext, 0, 0);
2191
2357
  }
2192
2358
  // src/profile/features/frontmatter.ts
2193
2359
  function frontmatterFenceAt(source, lines, index, marker) {
@@ -2353,17 +2519,15 @@ function blockOpeningAt(source, lines, index, contentOffset, shorthand) {
2353
2519
  };
2354
2520
  }
2355
2521
  function blockClose(source, lines, start, opening) {
2356
- let codeFence;
2357
2522
  let depth = 0;
2358
2523
  for (let index = start + 1; index < lines.length; index++) {
2359
- if (codeFence) {
2360
- if (closesFence(source, lines, index, codeFence)) codeFence = void 0;
2361
- continue;
2362
- }
2363
2524
  const contentOffset = lineIndentOffset(source, lines, index);
2364
2525
  if (contentOffset < 0) continue;
2365
- codeFence = codeFenceAt(source, lines, index, contentOffset);
2366
- if (codeFence) continue;
2526
+ const close = codeFenceClosingLine(source, lines, index, lines.length, contentOffset);
2527
+ if (close !== void 0) {
2528
+ index = close;
2529
+ continue;
2530
+ }
2367
2531
  let markerEnd = contentOffset;
2368
2532
  while (source.charCodeAt(markerEnd) === 58) markerEnd++;
2369
2533
  if (markerEnd - contentOffset === opening.fenceSize && trimLineWhitespaceStart(source, markerEnd, lines.end(index)) === lines.end(index)) {
@@ -2378,16 +2542,14 @@ function blockClose(source, lines, start, opening) {
2378
2542
  }
2379
2543
  }
2380
2544
  function nextSlot(source, lines, start, end) {
2381
- let codeFence;
2382
2545
  for (let index = start; index < end; index++) {
2383
- if (codeFence) {
2384
- if (closesFence(source, lines, index, codeFence)) codeFence = void 0;
2385
- continue;
2386
- }
2387
2546
  const contentOffset = lineIndentOffset(source, lines, index);
2388
2547
  if (contentOffset < 0) continue;
2389
- codeFence = codeFenceAt(source, lines, index, contentOffset);
2390
- if (codeFence) continue;
2548
+ const close = codeFenceClosingLine(source, lines, index, end, contentOffset);
2549
+ if (close !== void 0) {
2550
+ index = close;
2551
+ continue;
2552
+ }
2391
2553
  const slot = slotOpeningAt(source, lines, index, contentOffset);
2392
2554
  if (slot) return {
2393
2555
  index,
@@ -2406,17 +2568,17 @@ function emitComponentBody(source, lines, tokens, start, end, closingOffset, con
2406
2568
  }
2407
2569
  let slot = nextSlot(source, lines, cursor, end);
2408
2570
  if (!slot) {
2409
- context.scanLines(source, lines.view(cursor, end), tokens);
2571
+ context.scanBlockLines(source, lines.view(cursor, end), tokens);
2410
2572
  return;
2411
2573
  }
2412
- if (slot.index > cursor) context.scanLines(source, lines.view(cursor, slot.index), tokens);
2574
+ if (slot.index > cursor) context.scanBlockLines(source, lines.view(cursor, slot.index), tokens);
2413
2575
  while (slot) {
2414
2576
  const following = nextSlot(source, lines, slot.index + 1, end);
2415
2577
  const next = following?.index ?? end;
2416
2578
  const opening = slot.opening;
2417
2579
  tokens.push(84, opening.offset, opening.nameEnd);
2418
2580
  if (opening.attributesStart !== void 0 && opening.attributesEnd !== void 0) tokens.push(2, opening.attributesStart, opening.attributesEnd);
2419
- context.scanLines(source, lines.view(slot.index + 1, next), tokens);
2581
+ context.scanBlockLines(source, lines.view(slot.index + 1, next), tokens);
2420
2582
  const slotEnd = following ? following.opening.offset : closingOffset;
2421
2583
  tokens.push(202, slotEnd, slotEnd);
2422
2584
  slot = following;
@@ -2541,7 +2703,7 @@ const inlineScans = [{
2541
2703
  scan(source, start, tokens) {
2542
2704
  const end = hasInlineColonBoundary(source, start) ? componentNameEnd(source, start + 1, false) : void 0;
2543
2705
  if (end === void 0) return -1;
2544
- appendInlineToken(tokens, 137, start, end);
2706
+ appendInlineToken(tokens, 136, start, end);
2545
2707
  return end;
2546
2708
  }
2547
2709
  }];
@@ -2566,7 +2728,7 @@ const feature$12 = {
2566
2728
  builds: [
2567
2729
  {
2568
2730
  kind: "leaf",
2569
- token: 137,
2731
+ token: 136,
2570
2732
  build(token, position, context) {
2571
2733
  return buildInlineComponent(token, token, position, [], context);
2572
2734
  }
@@ -2605,13 +2767,13 @@ const feature$11 = { inline: {
2605
2767
  while (isShortcodeCharacter(source.charCodeAt(end))) end++;
2606
2768
  if (end === start + 1 || source.charCodeAt(end) !== 58) return -1;
2607
2769
  end++;
2608
- appendInlineToken(tokens, 136, start, end);
2770
+ appendInlineToken(tokens, 135, start, end);
2609
2771
  return end;
2610
2772
  }
2611
2773
  }],
2612
2774
  builds: [{
2613
2775
  kind: "leaf",
2614
- token: 136,
2776
+ token: 135,
2615
2777
  build(tokenIndex, position, context) {
2616
2778
  return {
2617
2779
  type: "emoji",
@@ -2630,9 +2792,9 @@ function footnoteLabelAt(source, start, limit) {
2630
2792
  let length = 0;
2631
2793
  while (offset < limit) {
2632
2794
  const code = source.charCodeAt(offset);
2633
- if (isMarkdownWhitespace(code)) return;
2795
+ if (isSpaceTabOrLineEnding(code)) return;
2634
2796
  if (code === 92 && offset + 1 < limit) {
2635
- if (isMarkdownWhitespace(source.charCodeAt(offset + 1))) return;
2797
+ if (isSpaceTabOrLineEnding(source.charCodeAt(offset + 1))) return;
2636
2798
  offset += 2;
2637
2799
  length += 2;
2638
2800
  } else if (code === 93) {
@@ -2732,7 +2894,7 @@ const feature$10 = {
2732
2894
  normalizedLabel: match.normalizedLabel
2733
2895
  }
2734
2896
  });
2735
- context.scanLines(source, definitionLines, tokens);
2897
+ context.scanBlockLines(source, definitionLines, tokens);
2736
2898
  const end = definitionLines.length > 0 ? definitionLines.next(definitionLines.length - 1) : match.markerEnd;
2737
2899
  tokens.push(197, end, end);
2738
2900
  return index;
@@ -2741,7 +2903,7 @@ const feature$10 = {
2741
2903
  },
2742
2904
  inline: { builds: [{
2743
2905
  kind: "leaf",
2744
- token: 133,
2906
+ token: 132,
2745
2907
  build(tokenIndex, position, context) {
2746
2908
  const label = inlineTokenText(context.view.text, context.tokens, tokenIndex, 2, 1);
2747
2909
  return {
@@ -2852,6 +3014,25 @@ function setextMarkerAt(source, lines, index, markerOffset) {
2852
3014
  offset = trimLineWhitespaceStart(source, offset, lineEnd);
2853
3015
  return offset === lineEnd ? marker : void 0;
2854
3016
  }
3017
+ function createBuildHeading(openKind) {
3018
+ return (tokenStart, context) => {
3019
+ const tokens = context.structure.tokens;
3020
+ const kind = tokens.kind(tokenStart);
3021
+ const start = context.locator.locationAt(tokens.start(tokenStart));
3022
+ const inline = buildInlineFragment(tokenStart, false, context);
3023
+ const result = {
3024
+ type: "heading",
3025
+ depth: kind - openKind + 1,
3026
+ children: inline.children,
3027
+ position: {
3028
+ start,
3029
+ end: context.locator.locationAt(tokens.end(tokenStart))
3030
+ }
3031
+ };
3032
+ if (inline.attributes) result.attributes = inline.attributes;
3033
+ return result;
3034
+ };
3035
+ }
2855
3036
  const feature$8 = { block: {
2856
3037
  builds: [{
2857
3038
  token: [
@@ -2862,42 +3043,10 @@ const feature$8 = { block: {
2862
3043
  68,
2863
3044
  69
2864
3045
  ],
2865
- build(tokenStart, context) {
2866
- const tokens = context.structure.tokens;
2867
- const kind = tokens.kind(tokenStart);
2868
- const start = context.locator.locationAt(tokens.start(tokenStart));
2869
- const inline = buildInlineFragment(tokenStart, false, context);
2870
- const result = {
2871
- type: "heading",
2872
- depth: kind - 64 + 1,
2873
- children: inline.children,
2874
- position: {
2875
- start,
2876
- end: context.locator.locationAt(tokens.end(tokenStart))
2877
- }
2878
- };
2879
- if (inline.attributes) result.attributes = inline.attributes;
2880
- return result;
2881
- }
3046
+ build: createBuildHeading(64)
2882
3047
  }, {
2883
3048
  token: [70, 71],
2884
- build(tokenStart, context) {
2885
- const tokens = context.structure.tokens;
2886
- const kind = tokens.kind(tokenStart);
2887
- const start = context.locator.locationAt(tokens.start(tokenStart));
2888
- const inline = buildInlineFragment(tokenStart, false, context);
2889
- const result = {
2890
- type: "heading",
2891
- depth: kind - 70 + 1,
2892
- children: inline.children,
2893
- position: {
2894
- start,
2895
- end: context.locator.locationAt(tokens.end(tokenStart))
2896
- }
2897
- };
2898
- if (inline.attributes) result.attributes = inline.attributes;
2899
- return result;
2900
- }
3049
+ build: createBuildHeading(70)
2901
3050
  }],
2902
3051
  starts: [{
2903
3052
  codes: [35],
@@ -2925,9 +3074,12 @@ const feature$8 = { block: {
2925
3074
  } };
2926
3075
  // src/profile/features/html.ts
2927
3076
  const htmlBlockTags = new Set(htmlBlockNames);
3077
+ const htmlRawTags = new Set(htmlRawNames);
2928
3078
  const htmlTagName = "[a-z][a-z0-9-]*";
2929
- const completeHtmlTag = new RegExp(`^(?:<${htmlTagName}(?:\\s+[a-z_:][\\w.:-]*(?:\\s*=\\s*(?:[^\\s"'=<>\`]+|'[^']*'|"[^"]*"))?)*\\s*/?>|</${htmlTagName}\\s*>)[ \\t]*$`, "i");
2930
- const htmlComment = /<!-->|<!--->|<!--[\s\S]*?(?:-->|$)/y;
3079
+ const htmlAttribute = `\\s+[a-z_:][\\w.:-]*(?:\\s*=\\s*(?:[^\\s"'=<>\`]+|'[^']*'|"[^"]*"))?`;
3080
+ const htmlTag = /^<\/?([a-z][a-z0-9-]*)(?=[ \t\n\r/>]|$)/i;
3081
+ const completeHtmlTag = new RegExp(`^(?:<${htmlTagName}(?:${htmlAttribute})*\\s*/?>|</${htmlTagName}\\s*>)[ \\t]*$`, "i");
3082
+ const htmlComment = /<!--(?:-?>|.*?-->|.*)/sy;
2931
3083
  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;
2932
3084
  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;
2933
3085
  function htmlStartAt(source, lines, index, contentOffset) {
@@ -2951,22 +3103,14 @@ function htmlStartAt(source, lines, index, contentOffset) {
2951
3103
  terminator: ">"
2952
3104
  };
2953
3105
  }
2954
- const lower = body.toLowerCase();
2955
- for (const tag of htmlRawNames) if (lower.startsWith(`<${tag}`) && (lower.length === tag.length + 1 || /[ \t>]/.test(lower[tag.length + 1]))) return {
3106
+ const tag = htmlTag.exec(body)?.[1].toLowerCase();
3107
+ if (tag && body[1] !== "/" && htmlRawTags.has(tag) && (body.length === tag.length + 1 || /[ \t>]/.test(body[tag.length + 1]))) return {
2956
3108
  interruptParagraph: true,
2957
3109
  terminator: `</${tag}>`
2958
3110
  };
2959
- const tag = /^<\/?([a-z][a-z0-9-]*)(?=[ \t\n\r/>]|$)/i.exec(body)?.[1].toLowerCase();
2960
3111
  if (tag && htmlBlockTags.has(tag)) return { interruptParagraph: true };
2961
3112
  if (completeHtmlTag.test(body)) return { interruptParagraph: false };
2962
3113
  }
2963
- const buildInlineHtml = (tokenIndex, position, context) => {
2964
- return {
2965
- type: "html",
2966
- value: inlineTokenText(context.view.text, context.tokens, tokenIndex),
2967
- position
2968
- };
2969
- };
2970
3114
  const feature$7 = {
2971
3115
  block: {
2972
3116
  builds: [{
@@ -3016,11 +3160,11 @@ const feature$7 = {
3016
3160
  let kind = 130;
3017
3161
  if (end < 0) {
3018
3162
  end = matchInlinePatternEnd(autolink, source, start);
3019
- kind = 132;
3163
+ kind = 131;
3020
3164
  }
3021
3165
  if (end < 0) {
3022
3166
  end = matchInlinePatternEnd(inlineHtml, source, start);
3023
- kind = 131;
3167
+ kind = 130;
3024
3168
  }
3025
3169
  if (end < 0) return start + 1;
3026
3170
  appendInlineToken(tokens, kind, start, end);
@@ -3028,13 +3172,15 @@ const feature$7 = {
3028
3172
  }
3029
3173
  }],
3030
3174
  builds: [{
3031
- kind: "leaf",
3032
- token: 131,
3033
- build: buildInlineHtml
3034
- }, {
3035
3175
  kind: "leaf",
3036
3176
  token: 130,
3037
- build: buildInlineHtml
3177
+ build(tokenIndex, position, context) {
3178
+ return {
3179
+ type: "html",
3180
+ value: inlineTokenText(context.view.text, context.tokens, tokenIndex),
3181
+ position
3182
+ };
3183
+ }
3038
3184
  }]
3039
3185
  }
3040
3186
  };
@@ -3047,7 +3193,8 @@ const buildEscapedInlineText = (tokenIndex, context) => {
3047
3193
  return inlineTokenText(context.view.text, context.tokens, tokenIndex, 1, 0);
3048
3194
  };
3049
3195
  const buildDecodedInlineText = (tokenIndex, context) => {
3050
- return semanticText(inlineTokenText(context.view.text, context.tokens, tokenIndex));
3196
+ const text = inlineTokenText(context.view.text, context.tokens, tokenIndex);
3197
+ return decodeString(text);
3051
3198
  };
3052
3199
  const feature$6 = { inline: {
3053
3200
  scans: [{
@@ -3099,7 +3246,7 @@ const feature$6 = { inline: {
3099
3246
  // src/profile/features/link.ts
3100
3247
  function skipWhitespace(source, start, end = source.length) {
3101
3248
  while (start < end) {
3102
- if (!isMarkdownWhitespace(source.charCodeAt(start))) break;
3249
+ if (!isSpaceTabOrLineEnding(source.charCodeAt(start))) break;
3103
3250
  start++;
3104
3251
  }
3105
3252
  return start;
@@ -3140,7 +3287,7 @@ function linkDestinationEnd(source, start) {
3140
3287
  end++;
3141
3288
  continue;
3142
3289
  }
3143
- if (isMarkdownWhitespace(code)) break;
3290
+ if (isSpaceTabOrLineEnding(code)) break;
3144
3291
  consumed = true;
3145
3292
  end++;
3146
3293
  }
@@ -3159,15 +3306,15 @@ function linkTitleEnd(source, start) {
3159
3306
  }
3160
3307
  return -1;
3161
3308
  }
3162
- const simpleLinkTail = /\([^<\\()\t\n\r ]+\)/y;
3309
+ const simpleLinkTail = /[^<\\()\t\n\r ]+\)/y;
3163
3310
  function scanLinkTail(source, start, tokens) {
3164
- simpleLinkTail.lastIndex = start + 1;
3311
+ if (source.charCodeAt(start + 1) !== 40) return -1;
3312
+ simpleLinkTail.lastIndex = start + 2;
3165
3313
  if (simpleLinkTail.test(source)) {
3166
3314
  const end = simpleLinkTail.lastIndex;
3167
3315
  appendInlineToken(tokens, 68, start, end, end - start - 1);
3168
3316
  return end;
3169
3317
  }
3170
- if (source.charCodeAt(start + 1) !== 40) return -1;
3171
3318
  let offset = skipWhitespace(source, start + 2);
3172
3319
  let destinationEnd = offset;
3173
3320
  if (source.charCodeAt(offset) !== 41) {
@@ -3236,7 +3383,7 @@ function createBuildMedia(media, resourceKind) {
3236
3383
  url = semanticText(context.view.text.slice(start, destinationEnd));
3237
3384
  start = skipWhitespace(context.view.text, resourceEnd, end);
3238
3385
  if (start < end) {
3239
- while (end > start && isMarkdownWhitespace(context.view.text.charCodeAt(end - 1))) end--;
3386
+ while (end > start && isSpaceTabOrLineEnding(context.view.text.charCodeAt(end - 1))) end--;
3240
3387
  title = semanticText(context.view.text.slice(start + 1, end - 1));
3241
3388
  }
3242
3389
  }
@@ -3311,7 +3458,7 @@ const feature$5 = { inline: {
3311
3458
  },
3312
3459
  {
3313
3460
  kind: "leaf",
3314
- token: 132,
3461
+ token: 131,
3315
3462
  build(tokenIndex, position, context) {
3316
3463
  const label = inlineTokenText(context.view.text, context.tokens, tokenIndex, 1);
3317
3464
  return {
@@ -3346,6 +3493,7 @@ const feature$5 = { inline: {
3346
3493
  ]
3347
3494
  } };
3348
3495
  // src/profile/features/list.ts
3496
+ const interruptingMarker = /(?:[*+-]|0{0,8}1[.)])[ \t]+[^ \t\r\n]/y;
3349
3497
  function listMarkerAt(source, lines, index, contentOffset) {
3350
3498
  if (contentOffset < 0) return;
3351
3499
  const indent = lines.prefixColumns(index) + contentOffset - lines.start(index);
@@ -3353,20 +3501,14 @@ function listMarkerAt(source, lines, index, contentOffset) {
3353
3501
  const lineEnd = lines.end(index);
3354
3502
  let delimiter;
3355
3503
  let markerEnd = contentOffset + 1;
3356
- let startNumber = 0;
3357
3504
  if (marker === 45 || marker === 43 || marker === 42) {
3358
3505
  const following = source.charCodeAt(markerEnd);
3359
- if (markerEnd !== lineEnd && following !== 32 && following !== 9 || isThematicBreak(source, lines, index, contentOffset)) return;
3506
+ if (markerEnd !== lineEnd && following !== 32 && following !== 9) return;
3360
3507
  delimiter = marker;
3361
3508
  } else {
3362
- if (marker < 48 || marker > 57) return;
3363
- let orderedEnd = contentOffset;
3364
- while (orderedEnd < lineEnd && orderedEnd - contentOffset < 9) {
3365
- const digit = source.charCodeAt(orderedEnd) - 48;
3366
- if (digit < 0 || digit > 9) break;
3367
- startNumber = startNumber * 10 + digit;
3368
- orderedEnd++;
3369
- }
3509
+ if (!isAsciiDigit(marker)) return;
3510
+ let orderedEnd = contentOffset + 1;
3511
+ while (orderedEnd < lineEnd && orderedEnd - contentOffset < 9 && isAsciiDigit(source.charCodeAt(orderedEnd))) orderedEnd++;
3370
3512
  delimiter = source.charCodeAt(orderedEnd);
3371
3513
  if (delimiter !== 46 && delimiter !== 41) return;
3372
3514
  if (orderedEnd + 1 < lineEnd) {
@@ -3386,7 +3528,10 @@ function listMarkerAt(source, lines, index, contentOffset) {
3386
3528
  const code = source.charCodeAt(contentStart);
3387
3529
  if (code === 32) column++;
3388
3530
  else if (code === 9) column += 4 - column % 4;
3389
- else break;
3531
+ else {
3532
+ if (code === marker && isThematicBreak(source, lines, index, contentOffset)) return;
3533
+ break;
3534
+ }
3390
3535
  contentStart++;
3391
3536
  }
3392
3537
  hasContent = contentStart < lineEnd;
@@ -3404,23 +3549,19 @@ function listMarkerAt(source, lines, index, contentOffset) {
3404
3549
  contentOffset: contentStart,
3405
3550
  contentIndent,
3406
3551
  contentPrefixColumns: prefixColumns,
3407
- delimiter,
3408
- startNumber
3552
+ delimiter
3409
3553
  };
3410
3554
  }
3411
- function isTaskListWhitespace(code) {
3412
- return code === 32 || code >= 9 && code <= 13;
3413
- }
3414
3555
  function taskListMarkerAt(source, start, end) {
3415
3556
  while (source.charCodeAt(start) === 32) start++;
3416
3557
  if (source.charCodeAt(start++) !== 91) return;
3417
3558
  const state = source.charCodeAt(start++);
3418
3559
  const checked = state === 88 || state === 120;
3419
- if (!checked && !isTaskListWhitespace(state)) return;
3560
+ if (!checked && !isAsciiWhitespace(state)) return;
3420
3561
  if (source.charCodeAt(start++) !== 93) return;
3421
3562
  if (start >= end) return;
3422
3563
  const whitespace = source.charCodeAt(start++);
3423
- if (!isTaskListWhitespace(whitespace)) return;
3564
+ if (!isAsciiWhitespace(whitespace)) return;
3424
3565
  if (whitespace === 13 && source.charCodeAt(start) === 10) start++;
3425
3566
  return {
3426
3567
  checked,
@@ -3432,7 +3573,7 @@ function hasTaskListContent(source, tokens, firstChunk, contentStart) {
3432
3573
  for (let token = firstChunk; token < tokenEnd; token++) {
3433
3574
  const start = token === firstChunk ? contentStart : tokens.start(token);
3434
3575
  const end = tokens.end(token);
3435
- for (let offset = start; offset < end; offset++) if (!isTaskListWhitespace(source.charCodeAt(offset))) return true;
3576
+ for (let offset = start; offset < end; offset++) if (!isAsciiWhitespace(source.charCodeAt(offset))) return true;
3436
3577
  }
3437
3578
  return false;
3438
3579
  }
@@ -3528,7 +3669,12 @@ const listStart = (source, lines, tokens, start, contentOffset, context) => {
3528
3669
  itemLines.pushLazy(lines, index);
3529
3670
  index++;
3530
3671
  }
3531
- const itemSpread = context.scanLines(source, itemLines, tokens);
3672
+ let itemSpread = false;
3673
+ let previousEnd = -1;
3674
+ context.scanBlockLines(source, itemLines, tokens, (lineStart, lineEnd) => {
3675
+ itemSpread ||= previousEnd >= 0 && (lineStart > previousEnd || isBlankLine(source, itemLines, previousEnd - 1));
3676
+ previousEnd = lineEnd;
3677
+ });
3532
3678
  const itemEnd = itemLines.length ? itemLines.end(itemLines.length - 1) : marker.end;
3533
3679
  tokens.push(itemSpread ? 194 : 193, itemEnd, itemEnd);
3534
3680
  listEnd = itemEnd;
@@ -3600,19 +3746,20 @@ function feature$4(taskList = false) {
3600
3746
  return true;
3601
3747
  },
3602
3748
  interrupt(source, lines, index, contentOffset) {
3603
- const marker = listMarkerAt(source, lines, index, contentOffset);
3604
- return marker?.hasContent === true && (marker.startNumber === 1 || marker.delimiter !== 46 && marker.delimiter !== 41);
3749
+ interruptingMarker.lastIndex = contentOffset;
3750
+ return interruptingMarker.test(source) && interruptingMarker.lastIndex <= lines.end(index);
3605
3751
  },
3606
3752
  start: taskList ? taskListStart : listStart
3607
3753
  }]
3608
3754
  } };
3609
3755
  }
3610
3756
  // src/profile/features/math/block.ts
3611
- const mathFenceRule = {
3612
- forbiddenInfoMarker: 36,
3757
+ const mathFence = createFencedBlock({
3758
+ kind: 262,
3613
3759
  marker: 36,
3614
- minimumLength: 2
3615
- };
3760
+ minimumLength: 2,
3761
+ allowMarkerInInfo: false
3762
+ });
3616
3763
  const blockBuilds = [{
3617
3764
  token: 262,
3618
3765
  build(tokenStart, context) {
@@ -3625,22 +3772,7 @@ const blockBuilds = [{
3625
3772
  };
3626
3773
  }
3627
3774
  }];
3628
- const blockStarts = [{
3629
- codes: [36],
3630
- interrupt(source, lines, index, contentOffset) {
3631
- return fenceAt(source, lines, index, mathFenceRule, contentOffset) !== void 0;
3632
- },
3633
- start(source, lines, tokens, start, contentOffset) {
3634
- const fence = fenceAt(source, lines, start, mathFenceRule, contentOffset);
3635
- if (!fence) return;
3636
- let end = start + 1;
3637
- while (end < lines.length && !closesFence(source, lines, end, fence)) end++;
3638
- const closed = end < lines.length;
3639
- if (end < lines.length) end++;
3640
- appendFencedToken(source, lines, tokens, 262, start, end, fence, closed, false);
3641
- return end;
3642
- }
3643
- }];
3775
+ const blockStarts = [mathFence];
3644
3776
  // src/profile/features/math/inline.ts
3645
3777
  function createMathScanRule(singleDollarTextMath) {
3646
3778
  const minimum = singleDollarTextMath ? 1 : 2;
@@ -3664,23 +3796,20 @@ function createMathScanRule(singleDollarTextMath) {
3664
3796
  }
3665
3797
  }
3666
3798
  if (end < 0) return openEnd;
3667
- appendInlineToken(tokens, 134, start, end, markerLength);
3799
+ appendInlineToken(tokens, 133, start, end, markerLength);
3668
3800
  return end;
3669
3801
  }
3670
3802
  };
3671
3803
  }
3672
3804
  const inlineBuilds = [{
3673
3805
  kind: "leaf",
3674
- token: 134,
3806
+ token: 133,
3675
3807
  build(tokenIndex, position, context) {
3676
3808
  const markerLength = inlineTokenData(context.tokens, tokenIndex);
3677
- let value = inlineTokenText(context.view.text, context.tokens, tokenIndex, markerLength);
3678
- const startPadding = value.startsWith("\r\n") ? 2 : /^[ \r\n]/.test(value) ? 1 : 0;
3679
- const endPadding = value.endsWith("\r\n") ? 2 : /[ \r\n]$/.test(value) ? 1 : 0;
3680
- if (startPadding > 0 && endPadding > 0 && /[^ \r\n]/.test(value)) value = value.slice(startPadding, -endPadding);
3809
+ const text = inlineTokenText(context.view.text, context.tokens, tokenIndex, markerLength);
3681
3810
  return {
3682
3811
  type: "inlineMath",
3683
- value,
3812
+ value: hasInlinePadding(text) ? text.slice(text.startsWith("\r\n") ? 2 : 1, text.endsWith("\r\n") ? -2 : -1) : text,
3684
3813
  position
3685
3814
  };
3686
3815
  }
@@ -3705,28 +3834,32 @@ const feature$2 = { block: {
3705
3834
  const openToken = tokens.length;
3706
3835
  tokens.push(79, contentOffset, contentOffset);
3707
3836
  let chunkOffset = contentOffset;
3837
+ let chunkEnd = lines.end(start);
3708
3838
  let withBoundaryBreak = false;
3709
3839
  let index = start + 1;
3710
3840
  while (index < lines.length) {
3841
+ const lineStart = lines.start(index);
3711
3842
  const lineEnd = lines.end(index);
3712
- const contentStart = trimLineWhitespaceStart(source, lines.start(index), lineEnd);
3843
+ let spaceEnd = lineStart;
3844
+ let first = source.charCodeAt(spaceEnd);
3845
+ while (spaceEnd < lineEnd && first === 32) first = source.charCodeAt(++spaceEnd);
3846
+ const contentStart = spaceEnd < lineEnd && first === 9 ? trimLineWhitespaceStart(source, spaceEnd + 1, lineEnd) : spaceEnd;
3713
3847
  if (contentStart === lineEnd) break;
3714
- const chunkStart = indentOffset(source, lines, index);
3848
+ const chunkStart = Math.min(spaceEnd, lineStart + Math.max(0, 3 - lines.prefixColumns(index)));
3715
3849
  if (!lines.lazy(index) && chunkStart === contentStart) {
3716
3850
  const setext = setextMarkerAt(source, lines, index, contentStart);
3717
3851
  if (setext) {
3718
3852
  tokens.setKind(openToken, setext === 61 ? 70 : 71);
3719
3853
  tokens.setEnd(openToken, lineEnd);
3720
- tokens.closeRange(chunkOffset, lines.end(index - 1));
3854
+ tokens.closeRange(chunkOffset, chunkEnd);
3721
3855
  return index + 1;
3722
3856
  }
3723
3857
  if (context.startsInterruptingBlock(source, lines, index, contentStart)) break;
3724
3858
  }
3725
3859
  const previousEnd = lines.next(index - 1);
3726
- const newline = lines.end(index - 1);
3727
- if (!withBoundaryBreak && source.charCodeAt(newline) === 10) {
3728
- const previous = source.charCodeAt(newline - 1);
3729
- if (previous === 32 || previous === 9 || source.charCodeAt(chunkStart) === 32) {
3860
+ if (!withBoundaryBreak) {
3861
+ const previous = source.charCodeAt(chunkEnd - 1);
3862
+ if ((previous === 32 || previous === 9 || chunkStart < contentStart && source.charCodeAt(chunkStart) === 32) && source.charCodeAt(chunkEnd) === 10) {
3730
3863
  withBoundaryBreak = true;
3731
3864
  tokens.setValue(openToken, boundaryBreak);
3732
3865
  }
@@ -3735,10 +3868,10 @@ const feature$2 = { block: {
3735
3868
  tokens.push(1, chunkOffset, previousEnd);
3736
3869
  chunkOffset = chunkStart;
3737
3870
  }
3871
+ chunkEnd = lineEnd;
3738
3872
  index++;
3739
3873
  }
3740
- const close = lines.end(index - 1);
3741
- tokens.closeRange(chunkOffset, close);
3874
+ tokens.closeRange(chunkOffset, chunkEnd);
3742
3875
  return index;
3743
3876
  }],
3744
3877
  builds: [{
@@ -4023,31 +4156,33 @@ function emitTableRow(lines, tokens, index, cells) {
4023
4156
  const lineEnd = lines.end(index);
4024
4157
  tokens.push(199, lineEnd, lineEnd);
4025
4158
  }
4026
- const buildTableCell = (tokenStart, context) => {
4027
- const tokens = context.structure.tokens;
4028
- const start = context.locator.locationAt(tokens.start(tokenStart));
4029
- return {
4030
- type: "tableCell",
4031
- children: buildInlineFragment(tokenStart, true, context).children,
4032
- position: {
4033
- start,
4034
- end: context.locator.locationAt(tokens.start(tokenStart + tokens.nodeLength(tokenStart)))
4035
- }
4036
- };
4037
- };
4038
- const buildTableRow = (tokenStart, context) => {
4159
+ function buildTableRow(tokenStart, context) {
4039
4160
  const tokens = context.structure.tokens;
4040
4161
  const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
4041
4162
  const start = context.locator.locationAt(tokens.start(tokenStart));
4163
+ const children = [];
4164
+ for (let cell = tokenStart + 1; cell < close;) {
4165
+ const next = cell + tokens.nodeLength(cell);
4166
+ const start = context.locator.locationAt(tokens.start(cell));
4167
+ children.push({
4168
+ type: "tableCell",
4169
+ children: buildInlineFragment(cell, true, context).children,
4170
+ position: {
4171
+ start,
4172
+ end: context.locator.locationAt(tokens.start(next))
4173
+ }
4174
+ });
4175
+ cell = next;
4176
+ }
4042
4177
  return {
4043
4178
  type: "tableRow",
4044
- children: buildBlockChildren(tokenStart, context),
4179
+ children,
4045
4180
  position: {
4046
4181
  start,
4047
4182
  end: context.locator.locationAt(tokens.start(close))
4048
4183
  }
4049
4184
  };
4050
- };
4185
+ }
4051
4186
  function tableAlignment(tokenStart, context) {
4052
4187
  const tokens = context.structure.tokens;
4053
4188
  const result = [];
@@ -4084,147 +4219,168 @@ const feature = { block: {
4084
4219
  tokens.push(198, tableEnd, tableEnd);
4085
4220
  return end;
4086
4221
  }],
4087
- builds: [
4088
- {
4089
- token: 82,
4090
- build: buildTableCell
4091
- },
4092
- {
4093
- token: 81,
4094
- build: buildTableRow
4095
- },
4096
- {
4097
- token: 80,
4098
- build(tokenStart, context) {
4099
- const tokens = context.structure.tokens;
4100
- const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
4101
- const start = context.locator.locationAt(tokens.start(tokenStart));
4102
- const header = tokenStart + 1;
4103
- return {
4104
- type: "table",
4105
- align: tableAlignment(header + tokens.nodeLength(header), context),
4106
- children: buildBlockChildren(tokenStart, context),
4107
- position: {
4108
- start,
4109
- end: context.locator.locationAt(tokens.start(close))
4110
- }
4111
- };
4112
- }
4222
+ builds: [{
4223
+ token: 80,
4224
+ build(tokenStart, context) {
4225
+ const tokens = context.structure.tokens;
4226
+ const close = tokenStart + tokens.nodeLength(tokenStart) - 1;
4227
+ const start = context.locator.locationAt(tokens.start(tokenStart));
4228
+ const header = tokenStart + 1;
4229
+ const delimiter = header + tokens.nodeLength(header);
4230
+ const align = tableAlignment(delimiter, context);
4231
+ const rows = [buildTableRow(header, context)];
4232
+ for (let row = delimiter + align.length; row < close; row += tokens.nodeLength(row)) rows.push(buildTableRow(row, context));
4233
+ return {
4234
+ type: "table",
4235
+ align,
4236
+ children: rows,
4237
+ position: {
4238
+ start,
4239
+ end: context.locator.locationAt(tokens.start(close))
4240
+ }
4241
+ };
4113
4242
  }
4114
- ]
4243
+ }]
4115
4244
  } };
4116
4245
  // src/inline/delimiter.ts
4117
- const whitespace = /\s/u;
4118
- const punctuation = /[\p{P}\p{S}]/u;
4119
- function codePointBefore(source, offset) {
4120
- if (offset <= 0) return 10;
4121
- const trailing = source.charCodeAt(offset - 1);
4122
- if (trailing >= 56320 && trailing <= 57343 && offset > 1) {
4123
- const leading = source.charCodeAt(offset - 2);
4124
- if (leading >= 55296 && leading <= 56319) return source.codePointAt(offset - 2);
4125
- }
4126
- return trailing;
4127
- }
4128
- function delimiterCharacterClass(code) {
4129
- if (code <= 127) {
4130
- if (code === 32 || code >= 9 && code <= 13) return 1;
4131
- return isAsciiPunctuation(code) ? 2 : 0;
4132
- }
4133
- const character = String.fromCodePoint(code);
4134
- return whitespace.test(character) ? 1 : punctuation.test(character) ? 2 : 0;
4246
+ const whitespace = /\s/uy;
4247
+ const punctuation = /[\p{P}\p{S}]/uy;
4248
+ const delimiterClasses = Uint8Array.from({ length: 128 }, (value, code) => isAsciiWhitespace(code) ? 0 : isAsciiPunctuation(code) ? 1 : 2);
4249
+ function delimiterCharacterClass(source, index) {
4250
+ if (index < 0 || index >= source.length) return 0;
4251
+ const code = source.charCodeAt(index);
4252
+ if (code <= 127) return delimiterClasses[code];
4253
+ if (code >= 56320 && code <= 57343) {
4254
+ const leading = source.charCodeAt(index - 1);
4255
+ if (leading >= 55296 && leading <= 56319) index--;
4256
+ }
4257
+ if (whitespace.lastIndex = index, whitespace.test(source)) return 0;
4258
+ if (punctuation.lastIndex = index, punctuation.test(source)) return 1;
4259
+ return 2;
4135
4260
  }
4136
4261
  function flanking(source, start, end, config) {
4137
- const before = delimiterCharacterClass(codePointBefore(source, start));
4138
- const after = delimiterCharacterClass(source.codePointAt(end) ?? 10);
4139
- const left = after === 0 || after === 2 && before !== 0;
4140
- const right = before === 0 || before === 2 && after !== 0;
4141
- if (config.bits & 1) return (left ? 1 : 0) | (right ? 2 : 0);
4142
- return (left && (!right || before === 2) ? 1 : 0) | (right && (!left || after === 2) ? 2 : 0);
4262
+ const before = delimiterCharacterClass(source, start - 1);
4263
+ const after = delimiterCharacterClass(source, end);
4264
+ if (before !== after) return before < after ? 1 : 2;
4265
+ return before === 1 || before === 2 && config.bits & 1 ? 3 : 0;
4143
4266
  }
4144
4267
  function canPair(opener, closer) {
4145
- if (!(opener.state & 1)) return false;
4146
4268
  if (closer.config.bits & 2 && (opener.state & 12) !== (closer.state & 12)) return false;
4147
- if (!(closer.config.bits & 4) || !(opener.state & 2) && !(closer.state & 1)) return true;
4148
- return (opener.state & 12) + (closer.state & 12) !== 12;
4149
- }
4150
- function unlinkRun(run) {
4151
- if (run.previous) run.previous.next = run.next;
4152
- if (run.next) run.next.previous = run.previous;
4153
- }
4154
- function resolveDelimiterScope(first, replacements = []) {
4155
- const openersBottom = [];
4156
- let closer = first;
4157
- while (closer) {
4158
- if (!(closer.state & 2)) {
4159
- closer = closer.next;
4160
- continue;
4161
- }
4162
- const bottomSlot = (closer.config.bits >>> 3) + (closer.state & 1 ? 3 : 0) + (closer.state >> 2);
4163
- const bottom = openersBottom[bottomSlot] ?? 0;
4164
- let opener = closer.previous;
4165
- while (opener && opener.tokenIndex >= bottom) {
4166
- if (opener.config === closer.config && canPair(opener, closer)) break;
4167
- opener = opener.previous;
4269
+ if (closer.config.bits & 4 && (opener.state & 2 || closer.state & 1)) return (opener.state & 12) + (closer.state & 12) !== 12;
4270
+ return true;
4271
+ }
4272
+ var DelimiterStack = class {
4273
+ #replacements = [];
4274
+ #runs = [];
4275
+ #length = 0;
4276
+ add(source, tokens, tokenIndex, config) {
4277
+ const offset = inlineTokenStart(tokens, tokenIndex);
4278
+ const end = inlineTokenEnd(tokens, tokenIndex);
4279
+ const length = end - offset;
4280
+ if (config.bits & 2 && (length > 2 || !(length === 1 ? config.single : config.double))) return;
4281
+ const delimiterFlanking = flanking(source, offset, end, config);
4282
+ if (delimiterFlanking !== 0) this.#runs[this.#length++] = {
4283
+ tokenIndex,
4284
+ config,
4285
+ start: offset,
4286
+ remaining: length,
4287
+ state: delimiterFlanking | length % 3 << 2
4288
+ };
4289
+ }
4290
+ close(tokens, afterToken = -1) {
4291
+ const runs = this.#runs;
4292
+ const end = this.#length;
4293
+ let start = 0;
4294
+ if (afterToken >= 0) {
4295
+ start = end;
4296
+ while (start && runs[start - 1].tokenIndex > afterToken) start--;
4297
+ }
4298
+ if (start === end) return;
4299
+ const openersBottom = [];
4300
+ let top = start;
4301
+ for (let index = start; index < end; index++) {
4302
+ const closer = runs[index];
4303
+ if (!(closer.state & 2)) {
4304
+ runs[top++] = closer;
4305
+ continue;
4306
+ }
4307
+ const bottomSlot = (closer.config.bits >>> 3) + (closer.state & 1 ? 3 : 0) + (closer.state >> 2);
4308
+ const bottom = openersBottom[bottomSlot] ?? 0;
4309
+ while (closer.remaining) {
4310
+ let candidate = top - 1;
4311
+ while (candidate >= start && runs[candidate].tokenIndex >= bottom) {
4312
+ const opener = runs[candidate];
4313
+ if (opener.config === closer.config && canPair(opener, closer)) break;
4314
+ candidate--;
4315
+ }
4316
+ if (candidate < start || runs[candidate].tokenIndex < bottom) {
4317
+ openersBottom[bottomSlot] = closer.tokenIndex;
4318
+ if (closer.state & 1) runs[top++] = closer;
4319
+ break;
4320
+ }
4321
+ const opener = runs[candidate];
4322
+ const use = opener.remaining >= 2 && closer.remaining >= 2 && closer.config.double ? 2 : 1;
4323
+ const openEnd = opener.start + opener.remaining;
4324
+ const openStart = openEnd - use;
4325
+ const closeStart = closer.start;
4326
+ const closeEnd = closeStart + use;
4327
+ const pair = use === 2 ? closer.config.double : closer.config.single;
4328
+ this.#record(tokens, opener.tokenIndex, openStart, openEnd, pair.open);
4329
+ this.#record(tokens, closer.tokenIndex, closeStart, closeEnd, pair.close);
4330
+ opener.remaining -= use;
4331
+ closer.start += use;
4332
+ closer.remaining -= use;
4333
+ top = candidate + (opener.remaining ? 1 : 0);
4334
+ }
4168
4335
  }
4169
- if (!opener || opener.tokenIndex < bottom) {
4170
- openersBottom[bottomSlot] = closer.tokenIndex;
4171
- if (!(closer.state & 1)) unlinkRun(closer);
4172
- closer = closer.next;
4173
- continue;
4336
+ this.#length = start;
4337
+ }
4338
+ #record(tokens, index, start, end, kind) {
4339
+ if (start === inlineTokenStart(tokens, index) && end === inlineTokenEnd(tokens, index)) {
4340
+ tokens[index * 4 | 0] = kind;
4341
+ return;
4174
4342
  }
4175
- const use = opener.remaining >= 2 && closer.remaining >= 2 && closer.config.double ? 2 : 1;
4176
- const openEnd = opener.start + opener.remaining;
4177
- const openStart = openEnd - use;
4178
- const closeStart = closer.start;
4179
- const closeEnd = closeStart + use;
4180
- const pair = use === 2 ? closer.config.double : closer.config.single;
4181
- (replacements[opener.tokenIndex] ??= []).push({
4182
- start: openStart,
4183
- end: openEnd,
4184
- kind: pair.open
4185
- });
4186
- (replacements[closer.tokenIndex] ??= []).push({
4187
- start: closeStart,
4188
- end: closeEnd,
4189
- kind: pair.close
4343
+ this.#replacements[index] ??= [];
4344
+ this.#replacements[index].push({
4345
+ start,
4346
+ end,
4347
+ kind
4190
4348
  });
4191
- opener.remaining -= use;
4192
- closer.start += use;
4193
- closer.remaining -= use;
4194
- opener.next = closer;
4195
- closer.previous = opener;
4196
- if (opener.remaining === 0) unlinkRun(opener);
4197
- if (closer.remaining === 0) {
4198
- unlinkRun(closer);
4199
- closer = closer.next;
4349
+ }
4350
+ emitResolvedTokens(tokens) {
4351
+ let end = inlineTokenCount(tokens);
4352
+ let index = 0;
4353
+ while (index < end && inlineTokenKind(tokens, index) >= 64) index++;
4354
+ if (index === end) return tokens;
4355
+ const limit = Math.max(index, this.#replacements.length);
4356
+ while (end > limit && inlineTokenKind(tokens, end - 1) < 64) end--;
4357
+ const result = tokens.slice(0, index * 4);
4358
+ for (; index < end; index++) {
4359
+ const kind = inlineTokenKind(tokens, index);
4360
+ if (kind >= 64) {
4361
+ const field = index * 4;
4362
+ result.push(kind, tokens[field + 1], tokens[field + 2], tokens[field + 3]);
4363
+ } else if (kind !== 0) this.#appendMatches(result, tokens, index);
4200
4364
  }
4365
+ return result;
4201
4366
  }
4202
- return replacements;
4203
- }
4204
- function delimiterRunAt(source, tokens, tokenIndex, config) {
4205
- const offset = inlineTokenStart(tokens, tokenIndex);
4206
- const end = inlineTokenEnd(tokens, tokenIndex);
4207
- const length = end - offset;
4208
- if (config.bits & 2 && (length > 2 || (length === 1 ? !config.single : !config.double))) return;
4209
- const delimiterFlanking = flanking(source, offset, end, config);
4210
- if (delimiterFlanking !== 0) return {
4211
- tokenIndex,
4212
- config,
4213
- start: offset,
4214
- remaining: length,
4215
- previous: void 0,
4216
- next: void 0,
4217
- state: delimiterFlanking | length % 3 << 2
4218
- };
4219
- }
4220
- function appendResolvedRawToken(result, tokens, tokenIndex, replacements) {
4221
- const matched = replacements[tokenIndex];
4222
- if (matched) {
4223
- if (matched.length > 1) matched.sort((left, right) => left.start - right.start);
4224
- for (const replacement of matched) appendInlineToken(result, replacement.kind, replacement.start, replacement.end);
4367
+ #appendMatches(result, tokens, tokenIndex) {
4368
+ const matched = this.#replacements[tokenIndex];
4369
+ let last;
4370
+ if (matched) {
4371
+ let left = 0;
4372
+ let right = matched.length;
4373
+ while (left < right) {
4374
+ last = matched[left].kind & 1 ? matched[left++] : matched[--right];
4375
+ appendInlineToken(result, last.kind, last.start, last.end);
4376
+ }
4377
+ }
4378
+ appendMarkerText(result, tokens, tokenIndex, last?.end);
4225
4379
  }
4380
+ };
4381
+ function appendMarkerText(result, tokens, tokenIndex, afterMatch) {
4226
4382
  if (inlineTokenKind(tokens, tokenIndex + 1) >= 256) {
4227
- const start = matched ? matched[matched.length - 1].end : inlineTokenStart(tokens, tokenIndex);
4383
+ const start = afterMatch ?? inlineTokenStart(tokens, tokenIndex);
4228
4384
  const end = inlineTokenEnd(tokens, tokenIndex);
4229
4385
  if (start < end) appendInlineToken(result, 64, start, end);
4230
4386
  }
@@ -4241,22 +4397,6 @@ function compileDelimiterConfigs(delimiterConfigs) {
4241
4397
  return delimiterByKind;
4242
4398
  }
4243
4399
  // src/profile/resolve.ts
4244
- function rewriteLinkCandidates(top, bottom) {
4245
- let candidate = top;
4246
- top = bottom;
4247
- while (candidate && candidate !== bottom) {
4248
- const next = candidate.candidateNext;
4249
- const state = candidate.state;
4250
- if (state === 3) candidate.state = 0;
4251
- else {
4252
- candidate.state = state | 16;
4253
- candidate.candidateNext = top;
4254
- top = candidate;
4255
- }
4256
- candidate = next;
4257
- }
4258
- return top;
4259
- }
4260
4400
  function referenceLabelEnd(source, start) {
4261
4401
  if (source.charCodeAt(start) !== 91) return -1;
4262
4402
  let offset = start + 1;
@@ -4272,292 +4412,197 @@ function referenceLabelEnd(source, start) {
4272
4412
  hasContent = true;
4273
4413
  offset += 2;
4274
4414
  } else {
4275
- hasContent ||= !isMarkdownWhitespace(code);
4415
+ hasContent ||= !isSpaceTabOrLineEnding(code);
4276
4416
  offset++;
4277
4417
  }
4278
4418
  characters++;
4279
4419
  }
4280
4420
  return -1;
4281
4421
  }
4282
- function acceptsShortcutLabel(source, start, end) {
4283
- let hasContent = false;
4284
- let characters = 0;
4285
- for (let offset = start; offset < end; offset++) {
4286
- const code = source.charCodeAt(offset);
4287
- hasContent ||= !isMarkdownWhitespace(code);
4288
- if (code >= 55296 && code <= 56319 && offset + 1 < end) {
4289
- const trailing = source.charCodeAt(offset + 1);
4290
- if (trailing >= 56320 && trailing <= 57343) offset++;
4291
- }
4292
- if (++characters > 999) return false;
4293
- }
4294
- return hasContent;
4295
- }
4296
- function analyzeBrackets(source, tokens, definitions, options, start, end) {
4297
- let first;
4298
- let last;
4299
- let candidateTop;
4300
- let stackTop;
4301
- for (let tokenIndex = start; tokenIndex < end; tokenIndex++) {
4302
- const kind = inlineTokenKind(tokens, tokenIndex);
4303
- if (kind >= 4 && kind <= 5) {
4304
- const frame = {
4305
- open: tokenIndex,
4306
- close: -1,
4307
- parent: stackTop,
4308
- next: void 0,
4309
- state: kind === 4 ? 8 : 0,
4310
- candidateBottom: candidateTop,
4311
- candidateNext: void 0,
4312
- delimiterBottom: void 0,
4313
- resolvedEnd: 0,
4314
- logicalParent: void 0,
4315
- closingLink: void 0
4316
- };
4317
- if (last) last.next = frame;
4318
- else first = frame;
4319
- last = frame;
4320
- stackTop = frame;
4321
- continue;
4322
- }
4323
- if (kind !== 68 && kind !== 6) continue;
4324
- if (!stackTop) continue;
4325
- const frame = stackTop;
4326
- stackTop = frame.parent;
4327
- frame.close = tokenIndex;
4328
- const image = (frame.state & 8) !== 0;
4329
- if (options.footnote && kind === 6) {
4330
- const openToken = frame.open;
4331
- const label = footnoteLabelAt(source, inlineTokenStart(tokens, openToken) + (image ? 1 : 0), source.length);
4332
- if (label?.end === inlineTokenEnd(tokens, tokenIndex) && definitions.hasDefinition(label.definitionKey)) {
4333
- frame.state = 1;
4334
- candidateTop = frame.candidateBottom;
4335
- continue;
4422
+ const withinLabelLimit = /^.{1,999}$/su;
4423
+ const acceptedLabel = /[^ \t\r\n]/;
4424
+ function acceptsShortcutLabel(label) {
4425
+ return label.length > 999 ? label.length <= 1998 && withinLabelLimit.test(label) : acceptedLabel.test(label);
4426
+ }
4427
+ function opensComponentLabel(tokens, open) {
4428
+ return open > 0 && inlineTokenKind(tokens, open - 1) === 136 && inlineTokenEnd(tokens, open - 1) === inlineTokenStart(tokens, open);
4429
+ }
4430
+ function pairBrackets(tokens, start, end) {
4431
+ const mates = new Array(end);
4432
+ const openers = [];
4433
+ for (let index = start; index < end; index++) {
4434
+ const kind = inlineTokenKind(tokens, index);
4435
+ if (kind === 5 || kind === 4) openers.push(index);
4436
+ else if (kind === 6 || kind === 68) {
4437
+ const open = openers.pop();
4438
+ if (open !== void 0) {
4439
+ mates[open] = index;
4440
+ mates[index] = open;
4336
4441
  }
4337
4442
  }
4338
- if (!options.component || image) continue;
4339
- if (kind === 68) candidateTop = rewriteLinkCandidates(candidateTop, frame.candidateBottom);
4340
- const openToken = frame.open;
4341
- const componentToken = openToken - 1;
4342
- if (componentToken >= 0 && inlineTokenKind(tokens, componentToken) === 137 && inlineTokenEnd(tokens, componentToken) === inlineTokenStart(tokens, openToken)) {
4343
- frame.state = 2;
4344
- frame.candidateNext = candidateTop;
4345
- candidateTop = frame;
4346
- continue;
4347
- }
4348
- const suffixStart = inlineTokenStart(tokens, tokenIndex) + 1;
4349
- const trailing = source.charCodeAt(suffixStart);
4350
- if (trailing === 40 || trailing === 91) continue;
4351
- frame.state = trailing === 123 && attributesEnd(source, suffixStart) !== void 0 ? 4 : 3;
4352
- frame.candidateNext = candidateTop;
4353
- candidateTop = frame;
4354
- }
4355
- return first;
4356
- }
4357
- function resolvePairedSyntax(source, tokens, nextFrame, definitions, delimiterByKind, start, end) {
4358
- const hasDefinitions = nextFrame !== void 0 && definitions.hasDefinitions();
4359
- let firstRun;
4360
- let lastRun;
4361
- let replacements;
4362
- let rawTop;
4363
- let logicalTop;
4364
- let consumedEnd = -1;
4365
- let consumeFrames = false;
4366
- let inactiveBefore = 0;
4443
+ }
4444
+ return mates;
4445
+ }
4446
+ function componentOpening(source, tokens, open, close, insideLink) {
4447
+ if (opensComponentLabel(tokens, open)) return 208;
4448
+ const suffix = inlineTokenStart(tokens, close) + 1;
4449
+ const trailing = source.charCodeAt(suffix);
4450
+ if (trailing === 40 || trailing === 91) return 0;
4451
+ if (insideLink && (trailing !== 123 || attributesEnd(source, suffix) === void 0)) return 0;
4452
+ return 206;
4453
+ }
4454
+ function matchedLinkEnd(source, tokens, definitions, open, close, lastVisibleOpener) {
4455
+ const end = inlineTokenEnd(tokens, close);
4456
+ if (inlineTokenKind(tokens, close) === 68) return end;
4457
+ if (!definitions.hasDefinitions()) return -1;
4458
+ const contentStart = inlineTokenEnd(tokens, open);
4459
+ const contentEnd = inlineTokenStart(tokens, close);
4460
+ const labelEnd = referenceLabelEnd(source, end);
4461
+ if (labelEnd > 0) {
4462
+ const label = labelEnd === end + 2 ? source.slice(contentStart, contentEnd) : source.slice(end + 1, labelEnd - 1);
4463
+ return definitions.hasDefinition(normalizeIdentifier(label)) ? labelEnd : -1;
4464
+ }
4465
+ if (lastVisibleOpener > open) return -1;
4466
+ const label = source.slice(contentStart, contentEnd);
4467
+ return acceptsShortcutLabel(label) && definitions.hasDefinition(normalizeIdentifier(label)) ? end : -1;
4468
+ }
4469
+ function setBoundary(tokens, index, kind, end) {
4470
+ const field = index * 4;
4471
+ tokens[field | 0] = kind;
4472
+ tokens[field | 2] = end;
4473
+ }
4474
+ function resolvePairs(source, tokens, definitions, options, delimiters, start) {
4475
+ const end = inlineTokenCount(tokens);
4476
+ const openers = [];
4477
+ const scopes = new DelimiterStack();
4478
+ let mates;
4367
4479
  let lastVisibleOpener = -1;
4368
- let literalDepth = 0;
4369
- for (let tokenIndex = start; tokenIndex < end; tokenIndex++) {
4370
- const kind = inlineTokenKind(tokens, tokenIndex);
4371
- const tokenStart = inlineTokenStart(tokens, tokenIndex);
4372
- if (consumedEnd >= 0 && tokenStart >= consumedEnd) {
4373
- consumedEnd = -1;
4374
- consumeFrames = false;
4375
- }
4376
- const consuming = consumedEnd >= 0;
4377
- const delimiter = delimiterByKind[kind];
4378
- if (!consuming && delimiter) {
4379
- const run = delimiterRunAt(source, tokens, tokenIndex, delimiter);
4380
- if (run) {
4381
- run.previous = lastRun;
4382
- if (lastRun) lastRun.next = run;
4383
- else firstRun = run;
4384
- lastRun = run;
4385
- }
4386
- }
4387
- if (kind >= 4 && kind <= 5) {
4388
- const frame = nextFrame;
4389
- nextFrame = frame.next;
4390
- rawTop = frame;
4391
- if (consuming) {
4392
- if (consumeFrames) frame.state = 7;
4393
- continue;
4394
- }
4395
- frame.delimiterBottom = lastRun;
4396
- const state = frame.state;
4397
- const claim = state & 7;
4398
- if (claim === 1) {
4399
- const closeToken = frame.close;
4400
- consumedEnd = inlineTokenEnd(tokens, closeToken);
4401
- continue;
4402
- }
4403
- if (claim >= 2 && claim <= 4) {
4404
- if (state & 16) literalDepth++;
4405
- continue;
4406
- }
4407
- if (literalDepth > 0 && !(kind === 4)) {
4408
- frame.state = state | 32;
4409
- continue;
4410
- }
4411
- frame.logicalParent = logicalTop;
4412
- logicalTop = frame;
4413
- lastVisibleOpener = tokenIndex;
4480
+ let lastLinkOpener = -1;
4481
+ let literalLabelEnd = -1;
4482
+ let resourceLabelEnd = -1;
4483
+ let consumedEnd = -1;
4484
+ for (let index = start; index < end; index++) {
4485
+ const kind = inlineTokenKind(tokens, index);
4486
+ const opener = kind === 5 || kind === 4;
4487
+ if (opener && (options.component || options.footnote && source.charCodeAt(inlineTokenEnd(tokens, index)) === 94)) mates ??= pairBrackets(tokens, index, end);
4488
+ const close = opener ? mates?.[index] : void 0;
4489
+ const component = options.component && kind === 5 && close !== void 0;
4490
+ const insideLink = index < resourceLabelEnd;
4491
+ if (component && inlineTokenKind(tokens, close) === 68) resourceLabelEnd = Math.max(resourceLabelEnd, close);
4492
+ if (consumedEnd >= 0 && inlineTokenStart(tokens, index) < consumedEnd) {
4493
+ tokens[index * 4 | 0] = 0;
4414
4494
  continue;
4415
4495
  }
4416
- if (kind !== 68 && kind !== 6) continue;
4417
- const rawFrame = rawTop;
4418
- if (rawFrame !== void 0) rawTop = rawFrame.parent;
4419
- if (consuming) {
4420
- if (consumeFrames && rawFrame !== void 0) rawFrame.state = 7;
4496
+ const delimiter = delimiters[kind];
4497
+ if (delimiter) {
4498
+ scopes.add(source, tokens, index, delimiter);
4421
4499
  continue;
4422
4500
  }
4423
- const rawState = rawFrame === void 0 ? 0 : rawFrame.state;
4424
- const rawClaim = rawState & 7;
4425
- let scopeFrame = rawClaim >= 1 && rawClaim <= 4 ? rawFrame : void 0;
4426
- if (scopeFrame === void 0 && (literalDepth <= 0 || rawFrame === void 0 || (rawState & 8) !== 0)) {
4427
- const frame = logicalTop;
4428
- if (frame !== void 0) logicalTop = frame.logicalParent;
4429
- if (frame !== void 0 && (kind === 68 || hasDefinitions)) {
4430
- const openToken = frame.open;
4431
- const image = (frame.state & 8) !== 0;
4432
- if (image || openToken + 1 >= inactiveBefore) {
4433
- const contentStart = inlineTokenEnd(tokens, openToken);
4434
- const contentEnd = tokenStart;
4435
- const tokenEnd = inlineTokenEnd(tokens, tokenIndex);
4436
- let closeEnd = tokenEnd;
4437
- let reference = false;
4438
- let matched = kind === 68;
4439
- if (!matched) {
4440
- const labelEnd = referenceLabelEnd(source, closeEnd);
4441
- if (labelEnd > 0) {
4442
- const label = source.slice(closeEnd + 1, labelEnd - 1) || source.slice(contentStart, contentEnd);
4443
- matched = definitions.hasDefinition(normalizeIdentifier(label));
4444
- if (matched) {
4445
- closeEnd = labelEnd;
4446
- reference = true;
4447
- }
4448
- } else if (lastVisibleOpener <= openToken && acceptsShortcutLabel(source, contentStart, contentEnd) && definitions.hasDefinition(normalizeIdentifier(source.slice(contentStart, contentEnd)))) {
4449
- matched = true;
4450
- reference = true;
4451
- }
4452
- }
4453
- if (matched) {
4454
- frame.state = (reference ? 6 : 5) | (image ? 8 : 0);
4455
- frame.close = tokenIndex;
4456
- frame.resolvedEnd = closeEnd;
4457
- rawFrame.closingLink = frame;
4458
- scopeFrame = frame;
4459
- if (!image) inactiveBefore = openToken + 1;
4460
- if (closeEnd > tokenEnd) {
4461
- consumedEnd = closeEnd;
4462
- consumeFrames = true;
4463
- }
4464
- }
4501
+ if (opener) {
4502
+ if (options.footnote && close !== void 0 && inlineTokenKind(tokens, close) === 6) {
4503
+ const label = footnoteLabelAt(source, inlineTokenEnd(tokens, index) - 1, source.length);
4504
+ if (label && label.end === inlineTokenEnd(tokens, close) && definitions.hasDefinition(label.definitionKey)) {
4505
+ consumedEnd = label.end;
4506
+ setBoundary(tokens, index, 132, consumedEnd);
4507
+ if (kind === 4) tokens[index * 4 | 1]++;
4508
+ continue;
4465
4509
  }
4466
4510
  }
4467
- }
4468
- if (rawState & 16) literalDepth--;
4469
- if (scopeFrame && lastRun !== scopeFrame.delimiterBottom) {
4470
- const bottom = scopeFrame.delimiterBottom;
4471
- const first = bottom ? bottom.next : firstRun;
4472
- if (bottom) bottom.next = void 0;
4473
- else firstRun = void 0;
4474
- first.previous = void 0;
4475
- replacements = resolveDelimiterScope(first, replacements);
4476
- lastRun = bottom;
4477
- }
4478
- }
4479
- if (firstRun) replacements = resolveDelimiterScope(firstRun, replacements);
4480
- return replacements ?? emptyArray;
4481
- }
4482
- function appendReferenceBoundary(result, tokens, frame, close) {
4483
- const token = close ? frame.close : frame.open;
4484
- const state = frame.state;
4485
- const kind = ((state & 7) === 6 ? 202 : 198) + (state & 8 ? 2 : 0) + (close ? 1 : 0);
4486
- const end = close ? frame.resolvedEnd : inlineTokenEnd(tokens, token);
4487
- appendInlineToken(result, kind, inlineTokenStart(tokens, token), end, inlineTokenData(tokens, token));
4488
- return end;
4489
- }
4490
- function emitResolvedTokens(tokens, nextFrame, replacements, start, end) {
4491
- const result = start ? tokens.slice(0, start * 4) : [];
4492
- let rawTop;
4493
- let skipEnd = -1;
4494
- for (let tokenIndex = start; tokenIndex < end; tokenIndex++) {
4495
- const kind = inlineTokenKind(tokens, tokenIndex);
4496
- const tokenStart = inlineTokenStart(tokens, tokenIndex);
4497
- if (skipEnd >= 0 && tokenStart >= skipEnd) skipEnd = -1;
4498
- const skipping = skipEnd >= 0;
4499
- if (kind >= 4 && kind <= 5) {
4500
- const frame = nextFrame;
4501
- nextFrame = frame.next;
4502
- rawTop = frame;
4503
- const state = frame.state;
4504
- const claim = state & 7;
4505
- if (!skipping) {
4506
- if (claim === 1) {
4507
- const closeToken = frame.close;
4508
- const start = inlineTokenStart(tokens, tokenIndex) + (kind === 4 ? 1 : 0);
4509
- skipEnd = inlineTokenEnd(tokens, closeToken);
4510
- appendInlineToken(result, 133, start, skipEnd);
4511
- } else if (claim === 2) {
4512
- const closeToken = frame.close;
4513
- const openEnd = inlineTokenEnd(tokens, tokenIndex);
4514
- if (openEnd < inlineTokenStart(tokens, closeToken)) rewriteInlineTokenTail(result, 208, openEnd);
4515
- } else if (claim >= 3 && claim <= 4) appendInlineToken(result, 206, inlineTokenStart(tokens, tokenIndex), inlineTokenEnd(tokens, tokenIndex));
4516
- else if (claim === 5 || claim === 6) appendReferenceBoundary(result, tokens, frame, false);
4517
- else if (claim === 7) skipEnd = inlineTokenEnd(tokens, frame.close);
4518
- else if (!(state & 32)) appendResolvedRawToken(result, tokens, tokenIndex, replacements);
4519
- }
4520
- continue;
4521
- }
4522
- if (kind === 68 || kind === 6) {
4523
- const rawFrame = rawTop;
4524
- if (rawFrame !== void 0) rawTop = rawFrame.parent;
4525
- const closingLink = rawFrame?.closingLink;
4526
- if (skipping) continue;
4527
- if (closingLink) {
4528
- skipEnd = appendReferenceBoundary(result, tokens, closingLink, true);
4529
- continue;
4530
- }
4531
- const state = rawFrame === void 0 ? 0 : rawFrame.state;
4532
- const claim = state & 7;
4533
- if (claim === 2) {
4534
- const openToken = rawFrame.open;
4535
- if (inlineTokenEnd(tokens, openToken) < tokenStart) appendInlineToken(result, 209, tokenStart, tokenStart + 1);
4536
- continue;
4511
+ if (component) {
4512
+ const opening = componentOpening(source, tokens, index, close, insideLink);
4513
+ if (opening !== 0) {
4514
+ tokens[index * 4 | 0] = opening;
4515
+ if (insideLink) literalLabelEnd = Math.max(literalLabelEnd, close);
4516
+ continue;
4517
+ }
4537
4518
  }
4538
- if (claim >= 3 && claim <= 4) {
4539
- appendInlineToken(result, 207, tokenStart, tokenStart + 1);
4519
+ if (index < literalLabelEnd && kind !== 4) {
4520
+ tokens[index * 4 | 0] = 0;
4521
+ if (close !== void 0) tokens[close * 4 | 0] = 0;
4540
4522
  continue;
4541
4523
  }
4542
- if (claim === 1 || state & 32) continue;
4543
- }
4544
- if (!skipping) {
4545
- if (kind >= 64) copyInlineToken(result, tokens, tokenIndex);
4546
- else appendResolvedRawToken(result, tokens, tokenIndex, replacements);
4524
+ openers.push(index);
4525
+ lastVisibleOpener = index;
4526
+ continue;
4547
4527
  }
4548
- }
4549
- return result;
4528
+ if (kind !== 6 && kind !== 68) continue;
4529
+ const mate = mates?.[index];
4530
+ const claim = mate === void 0 ? void 0 : inlineTokenKind(tokens, mate);
4531
+ let open;
4532
+ if (claim === 208 || claim === 206) {
4533
+ open = mate;
4534
+ const at = inlineTokenStart(tokens, index);
4535
+ const openingEnd = inlineTokenEnd(tokens, open);
4536
+ if (claim === 208) {
4537
+ tokens[open * 4 | 0] = 0;
4538
+ if (openingEnd >= at) tokens[index * 4 | 0] = 0;
4539
+ else {
4540
+ setBoundary(tokens, open - 1, claim, openingEnd);
4541
+ setBoundary(tokens, index, claim + 1, at + 1);
4542
+ }
4543
+ } else setBoundary(tokens, index, claim + 1, at + 1);
4544
+ } else {
4545
+ if (index < literalLabelEnd && mate !== void 0 && inlineTokenKind(tokens, mate) !== 4) continue;
4546
+ const label = openers.pop();
4547
+ if (label === void 0) continue;
4548
+ const image = inlineTokenKind(tokens, label) === 4;
4549
+ if (!image && label < lastLinkOpener) continue;
4550
+ const closeEnd = matchedLinkEnd(source, tokens, definitions, label, index, lastVisibleOpener);
4551
+ if (closeEnd < 0) continue;
4552
+ open = label;
4553
+ const opening = (kind === 68 ? 198 : 202) + (image ? 2 : 0);
4554
+ const tokenEnd = inlineTokenEnd(tokens, index);
4555
+ tokens[open * 4 | 0] = opening;
4556
+ setBoundary(tokens, index, opening + 1, closeEnd);
4557
+ if (!image) lastLinkOpener = open;
4558
+ if (closeEnd > tokenEnd) consumedEnd = closeEnd;
4559
+ }
4560
+ scopes.close(tokens, open);
4561
+ }
4562
+ scopes.close(tokens);
4563
+ return scopes.emitResolvedTokens(tokens);
4550
4564
  }
4551
4565
  function compileInlineResolver(options) {
4552
4566
  return (delimiterConfigs) => {
4553
4567
  const delimiterByKind = compileDelimiterConfigs(delimiterConfigs);
4554
4568
  return (source, tokens, definitions) => {
4555
4569
  let start = 0;
4570
+ let resolved;
4571
+ let hasLiteralBrackets = false;
4556
4572
  const end = inlineTokenCount(tokens);
4557
- while (start < end && inlineTokenKind(tokens, start) >= 64) start++;
4558
- if (start === end) return tokens;
4559
- const frames = analyzeBrackets(source, tokens, definitions, options, start, end);
4560
- return emitResolvedTokens(tokens, frames, resolvePairedSyntax(source, tokens, frames, definitions, delimiterByKind, start, end), start, end);
4573
+ while (start < end) {
4574
+ const kind = inlineTokenKind(tokens, start);
4575
+ if (kind >= 64) {
4576
+ start++;
4577
+ continue;
4578
+ }
4579
+ if (kind !== 5 && kind !== 4) break;
4580
+ if (kind === 5 && options.component && opensComponentLabel(tokens, start)) break;
4581
+ let close = start + 1;
4582
+ while (close < end) {
4583
+ const inner = inlineTokenKind(tokens, close);
4584
+ if (inner < 64 || inner === 68) break;
4585
+ close++;
4586
+ }
4587
+ const closing = close === end ? 0 : inlineTokenKind(tokens, close);
4588
+ const resource = closing === 68;
4589
+ if (closing !== 0 && !resource && (closing !== 6 || options.component || options.footnote && source.charCodeAt(inlineTokenEnd(tokens, start)) === 94 || definitions.hasDefinitions())) break;
4590
+ if (!resource) {
4591
+ hasLiteralBrackets = true;
4592
+ start = close < end ? close + 1 : end;
4593
+ continue;
4594
+ }
4595
+ resolved ??= tokens.slice();
4596
+ const opening = kind === 4 ? 200 : 198;
4597
+ resolved[start * 4 | 0] = opening;
4598
+ resolved[close * 4 | 0] = opening + 1;
4599
+ start = close + 1;
4600
+ }
4601
+ if (start === end) {
4602
+ const settled = resolved ?? tokens;
4603
+ return hasLiteralBrackets ? new DelimiterStack().emitResolvedTokens(settled) : settled;
4604
+ }
4605
+ return resolvePairs(source, resolved ?? tokens.slice(), definitions, options, delimiterByKind, start);
4561
4606
  };
4562
4607
  };
4563
4608
  }