@vscode/markdown-editor 0.0.2-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/commands/cursorCommands.d.ts +13 -0
  2. package/dist/commands/editCommands.d.ts +14 -0
  3. package/dist/commands/index.d.ts +4 -0
  4. package/dist/commands/selectionCommands.d.ts +6 -0
  5. package/dist/commands/types.d.ts +26 -0
  6. package/dist/core/geometry.d.ts +37 -0
  7. package/dist/core/index.d.ts +8 -0
  8. package/dist/core/lengthEdit.d.ts +38 -0
  9. package/dist/core/offsetRange.d.ts +26 -0
  10. package/dist/core/selection.d.ts +13 -0
  11. package/dist/core/sourceOffset.d.ts +1 -0
  12. package/dist/core/stringEdit.d.ts +27 -0
  13. package/dist/core/stringValue.d.ts +8 -0
  14. package/dist/core/wordUtils.d.ts +6 -0
  15. package/dist/highlighter/defaultMonacoSyntaxHighlighter.d.ts +20 -0
  16. package/dist/highlighter/index.d.ts +3 -0
  17. package/dist/highlighter/monacoSyntaxHighlighter.d.ts +38 -0
  18. package/dist/highlighter/syntaxHighlighter.d.ts +67 -0
  19. package/dist/index.d.ts +6 -0
  20. package/dist/index.js +4274 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/model/cursorNavigation.d.ts +7 -0
  23. package/dist/model/editorModel.d.ts +54 -0
  24. package/dist/model/index.d.ts +4 -0
  25. package/dist/model/measuredLayoutModel.d.ts +50 -0
  26. package/dist/observables.d.ts +1 -0
  27. package/dist/observables.js +463 -0
  28. package/dist/observables.js.map +1 -0
  29. package/dist/parser/_micromarkAdapter.d.ts +7 -0
  30. package/dist/parser/ast.d.ts +274 -0
  31. package/dist/parser/index.d.ts +4 -0
  32. package/dist/parser/parse.d.ts +2 -0
  33. package/dist/parser/parser.d.ts +14 -0
  34. package/dist/parser/reconcile.d.ts +33 -0
  35. package/dist/parser/test/getAnnotatedSource.d.ts +2 -0
  36. package/dist/parser/test/snapshot.d.ts +9 -0
  37. package/dist/parser/visualizeAst.d.ts +33 -0
  38. package/dist/runOnChange-owE1SMC0.js +1514 -0
  39. package/dist/runOnChange-owE1SMC0.js.map +1 -0
  40. package/dist/test/random.d.ts +16 -0
  41. package/dist/view/content/blockView.d.ts +179 -0
  42. package/dist/view/content/documentView.d.ts +41 -0
  43. package/dist/view/content/dom.d.ts +16 -0
  44. package/dist/view/content/katexEditableIdentifiers.d.ts +36 -0
  45. package/dist/view/content/viewNode.d.ts +93 -0
  46. package/dist/view/editorController.d.ts +34 -0
  47. package/dist/view/editorView.d.ts +100 -0
  48. package/dist/view/fixture/astViewerView.d.ts +27 -0
  49. package/dist/view/fixture/cyclingTsHighlighter.d.ts +17 -0
  50. package/dist/view/fixture/debugColors.d.ts +26 -0
  51. package/dist/view/fixture/monacoDebugPanel.d.ts +7 -0
  52. package/dist/view/index.d.ts +9 -0
  53. package/dist/view/measuredLayoutDebugView.d.ts +70 -0
  54. package/dist/view/parts/cursorView.d.ts +29 -0
  55. package/dist/view/parts/selectionView.d.ts +57 -0
  56. package/dist/view/viewData.d.ts +251 -0
  57. package/dist/view/visualLineMap.d.ts +157 -0
  58. package/dist/view/visualizeViewTree.d.ts +5 -0
  59. package/package.json +68 -0
  60. package/src/view/editor.css +517 -0
  61. package/src/view/themes/default.css +235 -0
  62. package/src/view/themes/github.css +308 -0
package/dist/index.js ADDED
@@ -0,0 +1,4274 @@
1
+ import { parse as en, preprocess as tn, postprocess as nn } from "micromark";
2
+ import { math as sn } from "micromark-extension-math";
3
+ import { gfmTable as rn } from "micromark-extension-gfm-table";
4
+ import { gfmTaskListItem as on } from "micromark-extension-gfm-task-list-item";
5
+ import { gfmStrikethrough as cn } from "micromark-extension-gfm-strikethrough";
6
+ import { o as G, d as A, D as te, t as ln, r as an, a as Z } from "./runOnChange-owE1SMC0.js";
7
+ import Nt from "katex";
8
+ class _ {
9
+ constructor(e, t) {
10
+ if (this.start = e, this.endExclusive = t, e > t)
11
+ throw new Error(`Invalid range: [${e}, ${t})`);
12
+ }
13
+ static fromTo(e, t) {
14
+ return new _(e, t);
15
+ }
16
+ static ofLength(e) {
17
+ return new _(0, e);
18
+ }
19
+ static ofStartAndLength(e, t) {
20
+ return new _(e, e + t);
21
+ }
22
+ static emptyAt(e) {
23
+ return new _(e, e);
24
+ }
25
+ get isEmpty() {
26
+ return this.start === this.endExclusive;
27
+ }
28
+ get length() {
29
+ return this.endExclusive - this.start;
30
+ }
31
+ delta(e) {
32
+ return new _(this.start + e, this.endExclusive + e);
33
+ }
34
+ deltaStart(e) {
35
+ return new _(this.start + e, this.endExclusive);
36
+ }
37
+ deltaEnd(e) {
38
+ return new _(this.start, this.endExclusive + e);
39
+ }
40
+ contains(e) {
41
+ return this.start <= e && e < this.endExclusive;
42
+ }
43
+ containsRange(e) {
44
+ return this.start <= e.start && e.endExclusive <= this.endExclusive;
45
+ }
46
+ intersects(e) {
47
+ return Math.max(this.start, e.start) < Math.min(this.endExclusive, e.endExclusive);
48
+ }
49
+ intersectsOrTouches(e) {
50
+ return Math.max(this.start, e.start) <= Math.min(this.endExclusive, e.endExclusive);
51
+ }
52
+ intersect(e) {
53
+ const t = Math.max(this.start, e.start), s = Math.min(this.endExclusive, e.endExclusive);
54
+ if (t <= s)
55
+ return new _(t, s);
56
+ }
57
+ join(e) {
58
+ return new _(
59
+ Math.min(this.start, e.start),
60
+ Math.max(this.endExclusive, e.endExclusive)
61
+ );
62
+ }
63
+ isBefore(e) {
64
+ return this.endExclusive <= e.start;
65
+ }
66
+ isAfter(e) {
67
+ return this.start >= e.endExclusive;
68
+ }
69
+ substring(e) {
70
+ return e.substring(this.start, this.endExclusive);
71
+ }
72
+ slice(e) {
73
+ return e.slice(this.start, this.endExclusive);
74
+ }
75
+ equals(e) {
76
+ return this.start === e.start && this.endExclusive === e.endExclusive;
77
+ }
78
+ toString() {
79
+ return `[${this.start}, ${this.endExclusive})`;
80
+ }
81
+ }
82
+ class V {
83
+ constructor(e, t) {
84
+ this.replaceRange = e, this.newText = t;
85
+ }
86
+ static insert(e, t) {
87
+ return new V(_.emptyAt(e), t);
88
+ }
89
+ static replace(e, t) {
90
+ return new V(e, t);
91
+ }
92
+ static delete(e) {
93
+ return new V(e, "");
94
+ }
95
+ get isEmpty() {
96
+ return this.replaceRange.isEmpty && this.newText.length === 0;
97
+ }
98
+ equals(e) {
99
+ return this.replaceRange.equals(e.replaceRange) && this.newText === e.newText;
100
+ }
101
+ toString() {
102
+ return `${this.replaceRange} -> ${JSON.stringify(this.newText)}`;
103
+ }
104
+ }
105
+ class y {
106
+ static empty = new y([]);
107
+ static single(e) {
108
+ return new y([e]);
109
+ }
110
+ static replace(e, t) {
111
+ return new y([V.replace(e, t)]);
112
+ }
113
+ static insert(e, t) {
114
+ return new y([V.insert(e, t)]);
115
+ }
116
+ static delete(e) {
117
+ return new y([V.delete(e)]);
118
+ }
119
+ replacements;
120
+ constructor(e) {
121
+ let t = -1;
122
+ for (const s of e) {
123
+ if (s.replaceRange.start < t)
124
+ throw new Error(
125
+ `Edits must be disjoint and sorted. Found ${s} after ${t}`
126
+ );
127
+ t = s.replaceRange.endExclusive;
128
+ }
129
+ this.replacements = e;
130
+ }
131
+ get isEmpty() {
132
+ return this.replacements.length === 0;
133
+ }
134
+ apply(e) {
135
+ const t = [];
136
+ let s = 0;
137
+ for (const i of this.replacements)
138
+ t.push(e.substring(s, i.replaceRange.start)), t.push(i.newText), s = i.replaceRange.endExclusive;
139
+ return t.push(e.substring(s)), t.join("");
140
+ }
141
+ inverse(e) {
142
+ const t = [];
143
+ let s = 0;
144
+ for (const i of this.replacements) {
145
+ const r = e.substring(i.replaceRange.start, i.replaceRange.endExclusive);
146
+ t.push(
147
+ V.replace(
148
+ _.ofStartAndLength(i.replaceRange.start + s, i.newText.length),
149
+ r
150
+ )
151
+ ), s += i.newText.length - i.replaceRange.length;
152
+ }
153
+ return new y(t);
154
+ }
155
+ equals(e) {
156
+ if (this.replacements.length !== e.replacements.length)
157
+ return !1;
158
+ for (let t = 0; t < this.replacements.length; t++)
159
+ if (!this.replacements[t].equals(e.replacements[t]))
160
+ return !1;
161
+ return !0;
162
+ }
163
+ mapOffset(e) {
164
+ let t = 0;
165
+ for (const s of this.replacements) {
166
+ if (s.replaceRange.start > e)
167
+ break;
168
+ if (s.replaceRange.endExclusive <= e)
169
+ t += s.newText.length - s.replaceRange.length;
170
+ else
171
+ return s.replaceRange.start + t + s.newText.length;
172
+ }
173
+ return e + t;
174
+ }
175
+ toString() {
176
+ return `[${this.replacements.map((e) => e.toString()).join(", ")}]`;
177
+ }
178
+ }
179
+ class Ie {
180
+ constructor(e, t) {
181
+ if (this.replaceRange = e, this.newLength = t, t < 0)
182
+ throw new Error(`newLength must be non-negative, got ${t}`);
183
+ }
184
+ static replace(e, t) {
185
+ return new Ie(e, t);
186
+ }
187
+ get lengthDelta() {
188
+ return this.newLength - this.replaceRange.length;
189
+ }
190
+ equals(e) {
191
+ return this.replaceRange.equals(e.replaceRange) && this.newLength === e.newLength;
192
+ }
193
+ toString() {
194
+ return `${this.replaceRange} -> +${this.newLength}`;
195
+ }
196
+ }
197
+ class J {
198
+ static empty = new J([]);
199
+ static single(e) {
200
+ return new J([e]);
201
+ }
202
+ static replace(e, t) {
203
+ return new J([Ie.replace(e, t)]);
204
+ }
205
+ replacements;
206
+ constructor(e) {
207
+ let t = -1;
208
+ for (const s of e) {
209
+ if (s.replaceRange.start < t)
210
+ throw new Error(`Edits must be disjoint and sorted. Found ${s} after end ${t}`);
211
+ t = s.replaceRange.endExclusive;
212
+ }
213
+ this.replacements = e;
214
+ }
215
+ get isEmpty() {
216
+ return this.replacements.length === 0;
217
+ }
218
+ equals(e) {
219
+ if (this.replacements.length !== e.replacements.length)
220
+ return !1;
221
+ for (let t = 0; t < this.replacements.length; t++)
222
+ if (!this.replacements[t].equals(e.replacements[t]))
223
+ return !1;
224
+ return !0;
225
+ }
226
+ toString() {
227
+ return this.isEmpty ? "LengthEdit.empty" : this.replacements.join(", ");
228
+ }
229
+ }
230
+ class Oe {
231
+ constructor(e) {
232
+ this.value = e;
233
+ }
234
+ get length() {
235
+ return this.value.length;
236
+ }
237
+ substring(e) {
238
+ return e.substring(this.value);
239
+ }
240
+ toString() {
241
+ return this.value;
242
+ }
243
+ }
244
+ class x {
245
+ constructor(e, t) {
246
+ this.anchor = e, this.active = t;
247
+ }
248
+ static collapsed(e) {
249
+ return new x(e, e);
250
+ }
251
+ get isCollapsed() {
252
+ return this.anchor === this.active;
253
+ }
254
+ get isForward() {
255
+ return this.active >= this.anchor;
256
+ }
257
+ get range() {
258
+ return this.isForward ? new _(this.anchor, this.active) : new _(this.active, this.anchor);
259
+ }
260
+ collapseToActive() {
261
+ return x.collapsed(this.active);
262
+ }
263
+ withActive(e) {
264
+ return new x(this.anchor, e);
265
+ }
266
+ }
267
+ class ee {
268
+ constructor(e, t) {
269
+ this.x = e, this.y = t;
270
+ }
271
+ static ZERO = new ee(0, 0);
272
+ translate(e, t) {
273
+ return new ee(this.x + e, this.y + t);
274
+ }
275
+ }
276
+ class R {
277
+ constructor(e, t, s, i) {
278
+ this.x = e, this.y = t, this.width = s, this.height = i;
279
+ }
280
+ static EMPTY = new R(0, 0, 0, 0);
281
+ static fromPointPoint(e, t, s, i) {
282
+ return new R(e, t, s - e, i - t);
283
+ }
284
+ static fromPointSize(e, t, s, i) {
285
+ return new R(e, t, s, i);
286
+ }
287
+ get left() {
288
+ return this.x;
289
+ }
290
+ get top() {
291
+ return this.y;
292
+ }
293
+ get right() {
294
+ return this.x + this.width;
295
+ }
296
+ get bottom() {
297
+ return this.y + this.height;
298
+ }
299
+ get topLeft() {
300
+ return new ee(this.x, this.y);
301
+ }
302
+ containsX(e) {
303
+ return e >= this.left && e < this.right;
304
+ }
305
+ containsY(e) {
306
+ return e >= this.top && e < this.bottom;
307
+ }
308
+ containsPoint(e) {
309
+ return this.containsX(e.x) && this.containsY(e.y);
310
+ }
311
+ /** Same y/height, zero-width band at `x = this.left`. Useful for caret rects derived from a line. */
312
+ withZeroWidthAt(e) {
313
+ return new R(e, this.y, 0, this.height);
314
+ }
315
+ translate(e, t) {
316
+ return new R(this.x + e, this.y + t, this.width, this.height);
317
+ }
318
+ }
319
+ function It(n, e) {
320
+ if (e <= 0)
321
+ return 0;
322
+ let t = e - 1;
323
+ for (; t > 0 && /\s/.test(n[t]); )
324
+ t--;
325
+ if (t >= 0 && /\w/.test(n[t]))
326
+ for (; t > 0 && /\w/.test(n[t - 1]); )
327
+ t--;
328
+ else if (t >= 0)
329
+ for (; t > 0 && !/\w/.test(n[t - 1]) && !/\s/.test(n[t - 1]); )
330
+ t--;
331
+ return t;
332
+ }
333
+ function Bt(n, e) {
334
+ const t = n.length;
335
+ if (e >= t)
336
+ return t;
337
+ let s = e;
338
+ for (; s < t && /\s/.test(n[s]); )
339
+ s++;
340
+ if (s < t && /\w/.test(n[s]))
341
+ for (; s < t && /\w/.test(n[s]); )
342
+ s++;
343
+ else if (s < t)
344
+ for (; s < t && !/\w/.test(n[s]) && !/\s/.test(n[s]); )
345
+ s++;
346
+ return s;
347
+ }
348
+ function hn(n, e) {
349
+ if (e >= n.length)
350
+ return { start: n.length, end: n.length };
351
+ const t = n[e];
352
+ if (/\w/.test(t)) {
353
+ let r = e, o = e;
354
+ for (; r > 0 && /\w/.test(n[r - 1]); )
355
+ r--;
356
+ for (; o < n.length && /\w/.test(n[o]); )
357
+ o++;
358
+ return { start: r, end: o };
359
+ }
360
+ if (/\s/.test(t)) {
361
+ let r = e, o = e;
362
+ for (; r > 0 && /\s/.test(n[r - 1]); )
363
+ r--;
364
+ for (; o < n.length && /\s/.test(n[o]); )
365
+ o++;
366
+ return { start: r, end: o };
367
+ }
368
+ let s = e, i = e;
369
+ for (; s > 0 && !/\w/.test(n[s - 1]) && !/\s/.test(n[s - 1]); )
370
+ s--;
371
+ for (; i < n.length && !/\w/.test(n[i]) && !/\s/.test(n[i]); )
372
+ i++;
373
+ return { start: s, end: i };
374
+ }
375
+ let un = 1;
376
+ class E {
377
+ /**
378
+ * A stable identity. Every node has one: it is minted on construction and
379
+ * carried across edits by reconciliation, so a node that survives an edit
380
+ * (even with changed content) keeps the same id.
381
+ */
382
+ id = un++;
383
+ _length = -1;
384
+ get length() {
385
+ if (this._length < 0) {
386
+ let e = 0;
387
+ for (const t of this.children)
388
+ e += t.length;
389
+ this._length = e;
390
+ }
391
+ return this._length;
392
+ }
393
+ /**
394
+ * True when `other` has the same content. Containers compare children *by
395
+ * identity* (`===`): bottom-up reconciliation substitutes reused old
396
+ * instances into the fresh tree first, so equal children already share
397
+ * instances — keeping this O(children), not O(subtree). Leaves have no
398
+ * children, so {@link _localEquals} is their whole comparison.
399
+ */
400
+ structurallyEqual(e) {
401
+ if (this === e)
402
+ return !0;
403
+ if (this.kind !== e.kind || this.length !== e.length || !this._localEquals(e))
404
+ return !1;
405
+ const t = this.children, s = e.children;
406
+ if (t.length !== s.length)
407
+ return !1;
408
+ for (let i = 0; i < t.length; i++)
409
+ if (t[i] !== s[i])
410
+ return !1;
411
+ return !0;
412
+ }
413
+ /** Compares only this node's own scalar fields (kind/length already match). */
414
+ _localEquals(e) {
415
+ return !0;
416
+ }
417
+ /**
418
+ * A copy of this node that adopts `id`. Reconciliation uses this to carry an
419
+ * old identity onto a node whose content changed. Nodes are immutable value
420
+ * holders, so a shallow prototype copy with `id` overridden is sound.
421
+ */
422
+ cloneWithId(e) {
423
+ const t = Object.create(Object.getPrototypeOf(this));
424
+ return Object.assign(t, this), t.id = e, t;
425
+ }
426
+ }
427
+ const dn = [];
428
+ function O(n, e) {
429
+ let t;
430
+ for (let s = 0; s < e.length; s++) {
431
+ const i = n.get(e[s]);
432
+ i && i !== e[s] && ((t ??= e.slice())[s] = i);
433
+ }
434
+ return t ?? e;
435
+ }
436
+ function P(n, e) {
437
+ const t = n.get(e);
438
+ return t && t !== e ? t : e;
439
+ }
440
+ class Ee extends E {
441
+ get children() {
442
+ return dn;
443
+ }
444
+ get length() {
445
+ return this.content.length;
446
+ }
447
+ mapChildren() {
448
+ return this;
449
+ }
450
+ }
451
+ class ve extends Ee {
452
+ constructor(e) {
453
+ super(), this.content = e;
454
+ }
455
+ kind = "text";
456
+ _localEquals(e) {
457
+ return this.content === e.content;
458
+ }
459
+ }
460
+ class k extends Ee {
461
+ constructor(e, t) {
462
+ super(), this.markerKind = e, this.content = t;
463
+ }
464
+ kind = "marker";
465
+ _localEquals(e) {
466
+ return this.markerKind === e.markerKind && this.content === e.content;
467
+ }
468
+ }
469
+ class N extends Ee {
470
+ constructor(e, t) {
471
+ super(), this.content = e, this.glueKind = t;
472
+ }
473
+ kind = "glue";
474
+ _localEquals(e) {
475
+ return this.content === e.content && this.glueKind === e.glueKind;
476
+ }
477
+ }
478
+ class ne extends E {
479
+ constructor(e) {
480
+ super(), this.content = e;
481
+ }
482
+ kind = "thematicBreak";
483
+ get children() {
484
+ return this.content;
485
+ }
486
+ get marker() {
487
+ return le(this.content, "content");
488
+ }
489
+ mapChildren(e) {
490
+ return new ne(O(e, this.content));
491
+ }
492
+ }
493
+ function le(n, e) {
494
+ return n.find((t) => t instanceof k && t.markerKind === e);
495
+ }
496
+ class Be extends E {
497
+ constructor(e, t, s) {
498
+ super(), this.openMarker = e, this.content = t, this.closeMarker = s;
499
+ }
500
+ kind = "strong";
501
+ get children() {
502
+ return [this.openMarker, ...this.content, this.closeMarker];
503
+ }
504
+ mapChildren(e) {
505
+ return new Be(P(e, this.openMarker), O(e, this.content), P(e, this.closeMarker));
506
+ }
507
+ }
508
+ class Re extends E {
509
+ constructor(e, t, s) {
510
+ super(), this.openMarker = e, this.content = t, this.closeMarker = s;
511
+ }
512
+ kind = "emphasis";
513
+ get children() {
514
+ return [this.openMarker, ...this.content, this.closeMarker];
515
+ }
516
+ mapChildren(e) {
517
+ return new Re(P(e, this.openMarker), O(e, this.content), P(e, this.closeMarker));
518
+ }
519
+ }
520
+ class De extends E {
521
+ constructor(e, t, s) {
522
+ super(), this.openMarker = e, this.content = t, this.closeMarker = s;
523
+ }
524
+ kind = "strikethrough";
525
+ get children() {
526
+ return [this.openMarker, ...this.content, this.closeMarker];
527
+ }
528
+ mapChildren(e) {
529
+ return new De(P(e, this.openMarker), O(e, this.content), P(e, this.closeMarker));
530
+ }
531
+ }
532
+ class Ae extends E {
533
+ constructor(e) {
534
+ super(), this.content = e;
535
+ }
536
+ kind = "inlineCode";
537
+ get children() {
538
+ return this.content;
539
+ }
540
+ mapChildren(e) {
541
+ return new Ae(O(e, this.content));
542
+ }
543
+ }
544
+ class $e extends E {
545
+ constructor(e) {
546
+ super(), this.content = e;
547
+ }
548
+ kind = "inlineMath";
549
+ get children() {
550
+ return this.content;
551
+ }
552
+ mapChildren(e) {
553
+ return new $e(O(e, this.content));
554
+ }
555
+ }
556
+ class fe extends E {
557
+ constructor(e, t) {
558
+ super(), this.url = e, this.content = t;
559
+ }
560
+ kind = "link";
561
+ get children() {
562
+ return this.content;
563
+ }
564
+ mapChildren(e) {
565
+ return new fe(this.url, O(e, this.content));
566
+ }
567
+ _localEquals(e) {
568
+ return this.url === e.url;
569
+ }
570
+ }
571
+ class ge extends E {
572
+ constructor(e, t, s) {
573
+ super(), this.alt = e, this.url = t, this.content = s;
574
+ }
575
+ kind = "image";
576
+ get children() {
577
+ return this.content;
578
+ }
579
+ mapChildren(e) {
580
+ return new ge(this.alt, this.url, O(e, this.content));
581
+ }
582
+ _localEquals(e) {
583
+ return this.alt === e.alt && this.url === e.url;
584
+ }
585
+ }
586
+ class U extends E {
587
+ constructor(e, t, s) {
588
+ super(), this.level = e, this.marker = t, this.content = s;
589
+ }
590
+ kind = "heading";
591
+ get children() {
592
+ return [this.marker, ...this.content];
593
+ }
594
+ mapChildren(e) {
595
+ return new U(this.level, P(e, this.marker), O(e, this.content));
596
+ }
597
+ _localEquals(e) {
598
+ return this.level === e.level;
599
+ }
600
+ }
601
+ class z extends E {
602
+ constructor(e) {
603
+ super(), this.content = e;
604
+ }
605
+ kind = "paragraph";
606
+ get children() {
607
+ return this.content;
608
+ }
609
+ mapChildren(e) {
610
+ return new z(O(e, this.content));
611
+ }
612
+ }
613
+ class K extends E {
614
+ constructor(e, t) {
615
+ super(), this.language = e, this.content = t;
616
+ }
617
+ kind = "codeBlock";
618
+ _previous;
619
+ _contentEdit;
620
+ get children() {
621
+ return this.content;
622
+ }
623
+ get openFence() {
624
+ return le(this.content, "openFence");
625
+ }
626
+ get closeFence() {
627
+ return le(this.content, "closeFence");
628
+ }
629
+ get code() {
630
+ return le(this.content, "content");
631
+ }
632
+ /** Relative start offset of the {@link code} marker within this block. */
633
+ get codeOffset() {
634
+ let e = 0;
635
+ for (const t of this.content) {
636
+ if (t.kind === "marker" && t.markerKind === "content")
637
+ return e;
638
+ e += t.length;
639
+ }
640
+ return e;
641
+ }
642
+ mapChildren(e) {
643
+ return new K(this.language, O(e, this.content));
644
+ }
645
+ _localEquals(e) {
646
+ return this.language === e.language;
647
+ }
648
+ /**
649
+ * A copy of this block carrying an incremental link to `previous`:
650
+ * `contentEdit` (in the block's *content* coordinates) turns `previous`'s
651
+ * content into this one. Uses a weak reference so the previous tree can be
652
+ * garbage-collected.
653
+ */
654
+ withCodeDiff(e, t) {
655
+ const s = this.cloneWithId(this.id);
656
+ return s._previous = new WeakRef(e), s._contentEdit = t, s;
657
+ }
658
+ /**
659
+ * When this block was incrementally derived from `previous` (same
660
+ * fences/language, edit entirely within the content), returns the
661
+ * content-coordinate edit; otherwise `undefined`.
662
+ */
663
+ getDiff(e) {
664
+ if (this._contentEdit && this._previous?.deref() === e)
665
+ return { stringEdit: this._contentEdit };
666
+ }
667
+ }
668
+ class se extends E {
669
+ constructor(e) {
670
+ super(), this.content = e;
671
+ }
672
+ kind = "mathBlock";
673
+ get children() {
674
+ return this.content;
675
+ }
676
+ get code() {
677
+ return le(this.content, "content");
678
+ }
679
+ mapChildren(e) {
680
+ return new se(O(e, this.content));
681
+ }
682
+ }
683
+ class pe extends E {
684
+ constructor(e) {
685
+ super(), this.content = e;
686
+ }
687
+ kind = "blockQuote";
688
+ get children() {
689
+ return this.content;
690
+ }
691
+ get blocks() {
692
+ return this.content.filter(Ve);
693
+ }
694
+ mapChildren(e) {
695
+ return new pe(O(e, this.content));
696
+ }
697
+ }
698
+ class H extends E {
699
+ constructor(e, t) {
700
+ super(), this.ordered = e, this.content = t;
701
+ }
702
+ kind = "list";
703
+ get children() {
704
+ return this.content;
705
+ }
706
+ get items() {
707
+ return this.content.filter((e) => e instanceof $);
708
+ }
709
+ mapChildren(e) {
710
+ return new H(this.ordered, O(e, this.content));
711
+ }
712
+ _localEquals(e) {
713
+ return this.ordered === e.ordered;
714
+ }
715
+ }
716
+ class $ extends E {
717
+ constructor(e, t, s, i) {
718
+ super(), this.marker = e, this.content = t, this.checked = s, this.leadingTrivia = i;
719
+ }
720
+ kind = "listItem";
721
+ get children() {
722
+ return this.leadingTrivia ? [this.leadingTrivia, this.marker, ...this.content] : [this.marker, ...this.content];
723
+ }
724
+ get blocks() {
725
+ return this.content.filter(Ve);
726
+ }
727
+ mapChildren(e) {
728
+ return new $(
729
+ P(e, this.marker),
730
+ O(e, this.content),
731
+ this.checked,
732
+ this.leadingTrivia ? P(e, this.leadingTrivia) : void 0
733
+ );
734
+ }
735
+ _localEquals(e) {
736
+ return this.checked === e.checked;
737
+ }
738
+ }
739
+ class me extends E {
740
+ constructor(e) {
741
+ super(), this.content = e;
742
+ }
743
+ kind = "table";
744
+ get children() {
745
+ return this.content;
746
+ }
747
+ get _rows() {
748
+ return this.content.filter((e) => e instanceof he);
749
+ }
750
+ get headerRow() {
751
+ return this._rows[0];
752
+ }
753
+ get delimiterRow() {
754
+ return this._rows[1];
755
+ }
756
+ get bodyRows() {
757
+ return this._rows.slice(2);
758
+ }
759
+ mapChildren(e) {
760
+ return new me(O(e, this.content));
761
+ }
762
+ }
763
+ class he extends E {
764
+ constructor(e) {
765
+ super(), this.content = e;
766
+ }
767
+ kind = "tableRow";
768
+ get children() {
769
+ return this.content;
770
+ }
771
+ get cells() {
772
+ return this.content.filter((e) => e instanceof ue);
773
+ }
774
+ mapChildren(e) {
775
+ return new he(O(e, this.content));
776
+ }
777
+ }
778
+ class ue extends E {
779
+ constructor(e) {
780
+ super(), this.content = e;
781
+ }
782
+ kind = "tableCell";
783
+ get children() {
784
+ return this.content;
785
+ }
786
+ mapChildren(e) {
787
+ return new ue(O(e, this.content));
788
+ }
789
+ }
790
+ class Fe extends E {
791
+ constructor(e) {
792
+ super(), this.content = e;
793
+ }
794
+ kind = "document";
795
+ get children() {
796
+ return this.content;
797
+ }
798
+ get blocks() {
799
+ return this.content.filter(Ve);
800
+ }
801
+ mapChildren(e) {
802
+ return new Fe(O(e, this.content));
803
+ }
804
+ }
805
+ function Ve(n) {
806
+ return n instanceof U || n instanceof z || n instanceof K || n instanceof se || n instanceof ne || n instanceof pe || n instanceof H || n instanceof me;
807
+ }
808
+ function fn(n, e) {
809
+ if (n.id === e.id)
810
+ return 0;
811
+ let t = 0;
812
+ for (const s of n.children) {
813
+ const i = fn(s, e);
814
+ if (i !== void 0)
815
+ return t + i;
816
+ t += s.length;
817
+ }
818
+ }
819
+ const gn = /\[[ xX]\]/;
820
+ function Si(n) {
821
+ if (n.checked === void 0)
822
+ return;
823
+ const e = gn.exec(Rt(n));
824
+ if (e)
825
+ return _.ofStartAndLength(e.index, e[0].length);
826
+ }
827
+ function Rt(n) {
828
+ if (n instanceof Ee)
829
+ return n.content;
830
+ let e = "";
831
+ for (const t of n.children)
832
+ e += Rt(t);
833
+ return e;
834
+ }
835
+ function pn(n) {
836
+ const e = en({ extensions: [sn(), rn(), on(), cn()] }), t = tn()(n, void 0, !0);
837
+ return nn(e.document().write(t)).map(([i, r]) => ({
838
+ type: i,
839
+ tokenType: r.type,
840
+ startOffset: r.start.offset,
841
+ endOffset: r.end.offset
842
+ }));
843
+ }
844
+ function mn(n) {
845
+ return new wn(pn(n), n).build();
846
+ }
847
+ function Ue(n) {
848
+ const e = [];
849
+ for (let t = 0; t < n.length; t++) {
850
+ const s = n[t], i = n[t + 1];
851
+ if (s instanceof N && s.glueKind === "indent") {
852
+ if (i instanceof H) {
853
+ const o = i.content.findIndex((c) => c instanceof $);
854
+ if (o >= 0) {
855
+ const c = i.content.map((l, a) => a === o ? Ye(l, s) : l);
856
+ e.push(new H(i.ordered, c)), t++;
857
+ continue;
858
+ }
859
+ } else if (i instanceof $) {
860
+ e.push(Ye(i, s)), t++;
861
+ continue;
862
+ }
863
+ const r = e[e.length - 1];
864
+ if (r instanceof N && r.glueKind === void 0) {
865
+ e[e.length - 1] = new N(r.content + s.content);
866
+ continue;
867
+ }
868
+ e.push(new N(s.content));
869
+ continue;
870
+ }
871
+ e.push(s);
872
+ }
873
+ return e;
874
+ }
875
+ function Ye(n, e) {
876
+ return new $(n.marker, n.content, n.checked, e);
877
+ }
878
+ function ke(n) {
879
+ const e = [];
880
+ for (let t = 0; t < n.length; t++) {
881
+ const s = n[t];
882
+ if (s instanceof N && s.glueKind === void 0) {
883
+ const i = e[e.length - 1], r = n[t + 1], o = i instanceof z && r instanceof z, c = new N(s.content, o ? "blockBreak" : "blockGap"), l = i !== void 0 ? Dt(i, c) : void 0;
884
+ l ? e[e.length - 1] = l : e.push(c);
885
+ continue;
886
+ }
887
+ e.push(s);
888
+ }
889
+ return e;
890
+ }
891
+ function Dt(n, e) {
892
+ switch (n.kind) {
893
+ case "paragraph":
894
+ return new z([...n.content, e]);
895
+ case "heading": {
896
+ const t = n;
897
+ return new U(t.level, t.marker, [...t.content, e]);
898
+ }
899
+ case "codeBlock": {
900
+ const t = n;
901
+ return new K(t.language, [...t.content, e]);
902
+ }
903
+ case "mathBlock":
904
+ return new se([...n.content, e]);
905
+ case "thematicBreak":
906
+ return new ne([...n.content, e]);
907
+ case "table":
908
+ return new me([...n.content, e]);
909
+ case "blockQuote":
910
+ return new pe(Me(n.content, e));
911
+ case "list": {
912
+ const t = n;
913
+ return new H(t.ordered, Me(t.content, e));
914
+ }
915
+ case "listItem": {
916
+ const t = n;
917
+ return new $(t.marker, Me(t.content, e), t.checked, t.leadingTrivia);
918
+ }
919
+ default:
920
+ return;
921
+ }
922
+ }
923
+ function Me(n, e) {
924
+ const t = n[n.length - 1], s = t !== void 0 ? Dt(t, e) : void 0;
925
+ if (s) {
926
+ const i = n.slice();
927
+ return i[i.length - 1] = s, i;
928
+ }
929
+ return [...n, e];
930
+ }
931
+ function _n(n) {
932
+ return n.some((e) => !(e instanceof N)) ? n : [new z(n)];
933
+ }
934
+ class C {
935
+ constructor(e, t) {
936
+ this._parentStart = e, this._source = t;
937
+ }
938
+ _entries = [];
939
+ add(e, t) {
940
+ this._entries.push({ node: e, start: t });
941
+ }
942
+ build(e, t) {
943
+ this._entries.sort((c, l) => c.start - l.start);
944
+ const s = [];
945
+ let i = this._parentStart;
946
+ const r = this._parentStart + e, o = (c, l) => {
947
+ const a = this._source.substring(c, l), h = t ? null : /\n[^\S\n]+$/.exec(a);
948
+ if (!h) {
949
+ s.push(new N(a, t));
950
+ return;
951
+ }
952
+ const g = a.slice(h.index + 1);
953
+ s.push(new N(a.slice(0, a.length - g.length))), s.push(new N(g, "indent"));
954
+ };
955
+ for (const { node: c, start: l } of this._entries)
956
+ c.length !== 0 && (l > i && o(i, l), s.push(c), i = l + c.length);
957
+ return i < r && o(i, r), s;
958
+ }
959
+ }
960
+ class wn {
961
+ constructor(e, t) {
962
+ this._events = e, this._source = t;
963
+ }
964
+ _idx = 0;
965
+ _checkChecked;
966
+ build() {
967
+ const e = new C(0, this._source);
968
+ for (; this._idx < this._events.length; ) {
969
+ const t = this._events[this._idx];
970
+ if (t.type === "enter") {
971
+ const s = t.startOffset, i = this._tryParseBlock();
972
+ i ? e.add(i, s) : this._idx++;
973
+ } else
974
+ this._idx++;
975
+ }
976
+ return new Fe(_n(ke(e.build(this._source.length))));
977
+ }
978
+ _tryParseBlock() {
979
+ switch (this._events[this._idx].tokenType) {
980
+ case "atxHeading":
981
+ return this._parseHeading();
982
+ case "paragraph":
983
+ return this._parseParagraph();
984
+ case "codeFenced":
985
+ return this._parseCodeFenced();
986
+ case "mathFlow":
987
+ return this._parseMathFlow();
988
+ case "thematicBreak":
989
+ return this._parseThematicBreak();
990
+ case "blockQuote":
991
+ return this._parseBlockQuote();
992
+ case "listUnordered":
993
+ case "listOrdered":
994
+ return this._parseList();
995
+ case "table":
996
+ return this._parseTable();
997
+ default:
998
+ return;
999
+ }
1000
+ }
1001
+ _parseHeading() {
1002
+ const e = this._consume("enter", "atxHeading");
1003
+ let t = 1, s = e.startOffset, i = e.startOffset;
1004
+ const r = [];
1005
+ for (; this._notExit("atxHeading"); ) {
1006
+ const h = this._events[this._idx];
1007
+ if (h.type === "enter" && h.tokenType === "atxHeadingSequence") {
1008
+ const g = this._consume("enter", "atxHeadingSequence"), u = this._consume("exit", "atxHeadingSequence");
1009
+ i === e.startOffset && (t = Math.min(6, Math.max(1, u.endOffset - g.startOffset)), s = g.startOffset, i = u.endOffset);
1010
+ } else h.type === "enter" && h.tokenType === "atxHeadingText" ? (this._consume("enter", "atxHeadingText"), this._parseInlines(r, "atxHeadingText"), this._consume("exit", "atxHeadingText")) : this._idx++;
1011
+ }
1012
+ const o = this._consume("exit", "atxHeading");
1013
+ i > s && r.length > 0 && (i = r[0].start);
1014
+ const c = new k("headingMarker", this._source.substring(e.startOffset, i)), l = new C(i, this._source);
1015
+ for (const h of r)
1016
+ l.add(h.node, h.start);
1017
+ const a = l.build(o.endOffset - i);
1018
+ return new U(t, c, a);
1019
+ }
1020
+ _parseParagraph() {
1021
+ const e = this._consume("enter", "paragraph"), t = [];
1022
+ for (; this._notExit("paragraph"); )
1023
+ this._parseInlineEvent(t);
1024
+ const s = this._consume("exit", "paragraph"), i = new C(e.startOffset, this._source);
1025
+ for (const r of t)
1026
+ i.add(r.node, r.start);
1027
+ return new z(i.build(s.endOffset - e.startOffset));
1028
+ }
1029
+ _parseCodeFenced() {
1030
+ const e = this._consume("enter", "codeFenced");
1031
+ let t = "";
1032
+ const s = new C(e.startOffset, this._source);
1033
+ let i = !1, r, o;
1034
+ for (; this._notExit("codeFenced"); ) {
1035
+ const l = this._events[this._idx];
1036
+ if (l.type === "enter" && l.tokenType === "codeFencedFence") {
1037
+ const a = this._consume("enter", "codeFencedFence");
1038
+ for (; this._notExit("codeFencedFence"); ) {
1039
+ const g = this._events[this._idx];
1040
+ if (g.type === "enter" && g.tokenType === "codeFencedFenceInfo") {
1041
+ for (this._consume("enter", "codeFencedFenceInfo"); this._notExit("codeFencedFenceInfo"); ) {
1042
+ const u = this._events[this._idx];
1043
+ u.tokenType === "data" && (t = this._source.substring(u.startOffset, u.endOffset)), this._idx++;
1044
+ }
1045
+ this._consume("exit", "codeFencedFenceInfo");
1046
+ } else
1047
+ this._idx++;
1048
+ }
1049
+ const h = this._consume("exit", "codeFencedFence");
1050
+ s.add(new k(
1051
+ i ? "closeFence" : "openFence",
1052
+ this._source.substring(a.startOffset, h.endOffset)
1053
+ ), a.startOffset), i = !0;
1054
+ } else l.tokenType === "codeFlowValue" || l.tokenType === "lineEnding" ? (r === void 0 && (r = l.startOffset), o = l.endOffset, this._idx++) : this._idx++;
1055
+ }
1056
+ const c = this._consume("exit", "codeFenced");
1057
+ return r !== void 0 && s.add(new k("content", this._source.substring(r, o)), r), new K(t, s.build(c.endOffset - e.startOffset));
1058
+ }
1059
+ _parseMathFlow() {
1060
+ const e = this._consume("enter", "mathFlow"), t = new C(e.startOffset, this._source);
1061
+ let s = !1, i, r;
1062
+ for (; this._notExit("mathFlow"); ) {
1063
+ const c = this._events[this._idx];
1064
+ if (c.type === "enter" && c.tokenType === "mathFlowFence") {
1065
+ const l = this._consume("enter", "mathFlowFence");
1066
+ for (; this._notExit("mathFlowFence"); )
1067
+ this._idx++;
1068
+ const a = this._consume("exit", "mathFlowFence");
1069
+ t.add(new k(
1070
+ s ? "closeFence" : "openFence",
1071
+ this._source.substring(l.startOffset, a.endOffset)
1072
+ ), l.startOffset), s = !0;
1073
+ } else c.tokenType === "mathFlowValue" || c.tokenType === "lineEnding" ? (i === void 0 && (i = c.startOffset), r = c.endOffset, this._idx++) : this._idx++;
1074
+ }
1075
+ const o = this._consume("exit", "mathFlow");
1076
+ return i !== void 0 && t.add(new k("content", this._source.substring(i, r)), i), new se(t.build(o.endOffset - e.startOffset));
1077
+ }
1078
+ _parseThematicBreak() {
1079
+ const e = this._consume("enter", "thematicBreak");
1080
+ for (; this._notExit("thematicBreak"); )
1081
+ this._idx++;
1082
+ const t = this._consume("exit", "thematicBreak"), s = new k("content", this._source.substring(e.startOffset, t.endOffset));
1083
+ return new ne([s]);
1084
+ }
1085
+ _parseBlockQuote() {
1086
+ const e = this._consume("enter", "blockQuote"), t = new C(e.startOffset, this._source);
1087
+ let s = !1;
1088
+ for (; this._notExit("blockQuote"); ) {
1089
+ const r = this._events[this._idx];
1090
+ if (r.type === "enter" && r.tokenType === "blockQuotePrefix") {
1091
+ const o = this._consume("enter", "blockQuotePrefix");
1092
+ for (; this._notExit("blockQuotePrefix"); )
1093
+ this._idx++;
1094
+ const c = this._consume("exit", "blockQuotePrefix");
1095
+ s || t.add(new k("blockQuoteMarker", this._source.substring(o.startOffset, c.endOffset)), o.startOffset);
1096
+ } else if (r.type === "enter") {
1097
+ const o = r.startOffset, c = this._tryParseBlock();
1098
+ c ? (t.add(c, o), s = !0) : this._idx++;
1099
+ } else
1100
+ this._idx++;
1101
+ }
1102
+ const i = this._consume("exit", "blockQuote");
1103
+ return new pe(ke(t.build(i.endOffset - e.startOffset)));
1104
+ }
1105
+ _parseList() {
1106
+ const e = this._events[this._idx].tokenType, t = e === "listOrdered", s = this._consume("enter", e), i = new C(s.startOffset, this._source);
1107
+ let r, o, c, l, a, h;
1108
+ const g = () => {
1109
+ if (r === void 0 || o === void 0 || l === void 0)
1110
+ return;
1111
+ const d = new k("listItemMarker", this._source.substring(o, c)), f = h ?? c, p = l.build(f - c);
1112
+ i.add(new $(d, ke(Ue(p)), a), r);
1113
+ };
1114
+ for (; this._notExit(e); ) {
1115
+ const d = this._events[this._idx];
1116
+ if (d.type === "enter" && d.tokenType === "listItemPrefix") {
1117
+ for (g(), this._consume("enter", "listItemPrefix"), r = d.startOffset, o = d.startOffset, a = void 0, h = void 0, this._checkChecked = void 0; this._notExit("listItemPrefix"); )
1118
+ this._idx++;
1119
+ c = this._events[this._idx].endOffset, this._consume("exit", "listItemPrefix"), l = new C(c, this._source);
1120
+ } else if (d.type === "enter") {
1121
+ const f = d.startOffset, p = this._tryParseBlock();
1122
+ p && l ? (l.add(p, f), h = f + p.length, a === void 0 && this._checkChecked !== void 0 && (a = this._checkChecked)) : p || this._idx++;
1123
+ } else
1124
+ this._idx++;
1125
+ }
1126
+ g();
1127
+ const u = this._consume("exit", e);
1128
+ return new H(t, ke(Ue(i.build(u.endOffset - s.startOffset))));
1129
+ }
1130
+ _parseTable() {
1131
+ const e = this._consume("enter", "table"), t = new C(e.startOffset, this._source);
1132
+ let s = 0;
1133
+ for (; this._notExit("table"); ) {
1134
+ const r = this._events[this._idx];
1135
+ if (r.type === "enter" && r.tokenType === "tableHead") {
1136
+ for (this._consume("enter", "tableHead"); this._notExit("tableHead"); ) {
1137
+ const o = this._events[this._idx];
1138
+ if (o.type === "enter" && o.tokenType === "tableRow") {
1139
+ const c = this._parseTableRow("tableHeader");
1140
+ s = c.cells.length, t.add(c, o.startOffset);
1141
+ } else if (o.type === "enter" && o.tokenType === "tableDelimiterRow") {
1142
+ const c = o.startOffset;
1143
+ for (; this._notExit("tableDelimiterRow"); )
1144
+ this._idx++;
1145
+ const l = this._events[this._idx].endOffset;
1146
+ this._idx++, t.add(this._buildDelimiterRow(c, l, s), c);
1147
+ } else
1148
+ this._idx++;
1149
+ }
1150
+ this._consume("exit", "tableHead");
1151
+ } else if (r.type === "enter" && r.tokenType === "tableBody") {
1152
+ for (this._consume("enter", "tableBody"); this._notExit("tableBody"); ) {
1153
+ const o = this._events[this._idx];
1154
+ o.type === "enter" && o.tokenType === "tableRow" ? t.add(this._parseTableRow("tableData"), o.startOffset) : this._idx++;
1155
+ }
1156
+ this._consume("exit", "tableBody");
1157
+ } else
1158
+ this._idx++;
1159
+ }
1160
+ const i = this._consume("exit", "table");
1161
+ return new me(t.build(i.endOffset - e.startOffset));
1162
+ }
1163
+ _buildDelimiterRow(e, t, s) {
1164
+ const i = this._source.substring(e, t), r = [];
1165
+ for (let a = 0; a < i.length; a++)
1166
+ i[a] === "|" && r.push(a);
1167
+ const o = [], c = Math.max(1, s);
1168
+ if (i[0] === "|")
1169
+ for (let a = 0; a < c && a < r.length; a++)
1170
+ o.push(r[a]);
1171
+ else {
1172
+ o.push(0);
1173
+ for (let a = 0; a < c - 1 && a < r.length; a++)
1174
+ o.push(r[a]);
1175
+ }
1176
+ const l = new C(e, this._source);
1177
+ for (let a = 0; a < o.length; a++) {
1178
+ const h = o[a], g = a + 1 < o.length ? o[a + 1] : i.length, u = new C(e + h, this._source), d = i.substring(h, g);
1179
+ a === o.length - 1 && d.length > 1 && d.endsWith("|") ? (u.add(new k("tableDelimiter", d.slice(0, -1)), e + h), u.add(new k("tableDelimiterClose", "|"), e + h + d.length - 1)) : u.add(new k("tableDelimiter", d), e + h), l.add(new ue(u.build(g - h)), e + h);
1180
+ }
1181
+ return new he(l.build(t - e));
1182
+ }
1183
+ _parseTableRow(e) {
1184
+ const t = this._consume("enter", "tableRow"), s = new C(t.startOffset, this._source);
1185
+ for (; this._notExit("tableRow"); ) {
1186
+ const r = this._events[this._idx];
1187
+ if (r.type === "enter" && r.tokenType === e) {
1188
+ const o = this._consume("enter", e), c = [];
1189
+ for (; this._notExit(e); ) {
1190
+ const h = this._events[this._idx];
1191
+ if (h.type === "enter" && h.tokenType === "tableContent") {
1192
+ for (this._consume("enter", "tableContent"); this._notExit("tableContent"); )
1193
+ this._parseInlineEvent(c);
1194
+ this._consume("exit", "tableContent");
1195
+ } else
1196
+ this._idx++;
1197
+ }
1198
+ const l = this._consume("exit", e), a = new C(o.startOffset, this._source);
1199
+ for (const h of c)
1200
+ a.add(h.node, h.start);
1201
+ s.add(new ue(a.build(l.endOffset - o.startOffset, "tableCellGlue")), o.startOffset);
1202
+ } else
1203
+ this._idx++;
1204
+ }
1205
+ const i = this._consume("exit", "tableRow");
1206
+ return new he(s.build(i.endOffset - t.startOffset));
1207
+ }
1208
+ _parseInlines(e, t) {
1209
+ for (; this._idx < this._events.length; ) {
1210
+ const s = this._events[this._idx];
1211
+ if (s.type === "exit" && s.tokenType === t)
1212
+ return;
1213
+ this._parseInlineEvent(e);
1214
+ }
1215
+ }
1216
+ _parseInlineEvent(e) {
1217
+ const t = this._events[this._idx];
1218
+ if (t.type === "enter")
1219
+ switch (t.tokenType) {
1220
+ case "strongSequence":
1221
+ case "emphasisSequence":
1222
+ this._parseEmphasisOrStrong(e);
1223
+ return;
1224
+ case "codeText":
1225
+ e.push(this._parseInlineCode());
1226
+ return;
1227
+ case "mathText":
1228
+ e.push(this._parseInlineMath());
1229
+ return;
1230
+ case "link":
1231
+ e.push(this._parseLink());
1232
+ return;
1233
+ case "image":
1234
+ e.push(this._parseImage());
1235
+ return;
1236
+ case "strikethrough":
1237
+ e.push(this._parseStrikethrough());
1238
+ return;
1239
+ case "hardBreakTrailing":
1240
+ case "hardBreakEscape":
1241
+ e.push(this._parseHardBreak());
1242
+ return;
1243
+ }
1244
+ t.type === "exit" && (t.tokenType === "data" || t.tokenType === "codeTextData") && e.push({ node: new ve(this._source.substring(t.startOffset, t.endOffset)), start: t.startOffset }), t.type === "exit" && t.tokenType === "taskListCheckValueChecked" ? this._checkChecked = !0 : t.type === "exit" && t.tokenType === "taskListCheckValueUnchecked" && (this._checkChecked = !1), this._idx++;
1245
+ }
1246
+ /**
1247
+ * A GFM hard line break — either two-or-more trailing spaces
1248
+ * (`hardBreakTrailing`) or a backslash (`hardBreakEscape`) — followed by the
1249
+ * line ending it forces. Both halves are absorbed into a single
1250
+ * `hardBreak` marker, so the node *is* the whole break: a bare `lineEnding`
1251
+ * (a soft break) is never matched here and stays glue that collapses to a
1252
+ * space. Whether the line ending breaks is thus micromark's call, not ours.
1253
+ */
1254
+ _parseHardBreak() {
1255
+ const e = this._events[this._idx], t = e.tokenType;
1256
+ for (this._consume("enter", t); this._notExit(t); )
1257
+ this._idx++;
1258
+ let s = this._consume("exit", t).endOffset;
1259
+ const i = this._events[this._idx];
1260
+ return i && i.type === "enter" && i.tokenType === "lineEnding" && (this._consume("enter", "lineEnding"), s = this._consume("exit", "lineEnding").endOffset), { node: new k("hardBreak", this._source.substring(e.startOffset, s)), start: e.startOffset };
1261
+ }
1262
+ _parseEmphasisOrStrong(e) {
1263
+ const t = this._events[this._idx].tokenType, s = t === "strongSequence", i = this._consume("enter", t), r = this._consume("exit", t), o = [];
1264
+ for (; this._idx < this._events.length; ) {
1265
+ const c = this._events[this._idx];
1266
+ if (c.type === "enter" && c.tokenType === t) {
1267
+ const l = this._consume("enter", t), a = this._consume("exit", t), h = new k("openMarker", this._source.substring(i.startOffset, r.endOffset)), g = new k("closeMarker", this._source.substring(l.startOffset, a.endOffset)), u = new C(r.endOffset, this._source);
1268
+ for (const p of o)
1269
+ u.add(p.node, p.start);
1270
+ const d = u.build(l.startOffset - r.endOffset), f = s ? new Be(h, d, g) : new Re(h, d, g);
1271
+ e.push({ node: f, start: i.startOffset });
1272
+ return;
1273
+ }
1274
+ this._parseInlineEvent(o);
1275
+ }
1276
+ e.push({ node: new ve(this._source.substring(i.startOffset, r.endOffset)), start: i.startOffset });
1277
+ }
1278
+ _parseInlineCode() {
1279
+ const e = this._consume("enter", "codeText"), t = new C(e.startOffset, this._source);
1280
+ let s = !1, i, r;
1281
+ for (; this._notExit("codeText"); ) {
1282
+ const c = this._events[this._idx];
1283
+ c.type === "enter" && c.tokenType === "codeTextSequence" ? (t.add(new k(s ? "closeMarker" : "openMarker", this._source.substring(c.startOffset, c.endOffset)), c.startOffset), s = !0) : c.type === "enter" && c.tokenType === "codeTextData" && (i === void 0 && (i = c.startOffset), r = c.endOffset), this._idx++;
1284
+ }
1285
+ const o = this._consume("exit", "codeText");
1286
+ return i !== void 0 && t.add(new k("content", this._source.substring(i, r)), i), { node: new Ae(t.build(o.endOffset - e.startOffset)), start: e.startOffset };
1287
+ }
1288
+ _parseInlineMath() {
1289
+ const e = this._consume("enter", "mathText"), t = new C(e.startOffset, this._source);
1290
+ let s = !1, i, r;
1291
+ for (; this._notExit("mathText"); ) {
1292
+ const c = this._events[this._idx];
1293
+ c.type === "enter" && c.tokenType === "mathTextSequence" ? (t.add(new k(s ? "closeMarker" : "openMarker", this._source.substring(c.startOffset, c.endOffset)), c.startOffset), s = !0) : c.type === "enter" && c.tokenType === "mathTextData" && (i === void 0 && (i = c.startOffset), r = c.endOffset), this._idx++;
1294
+ }
1295
+ const o = this._consume("exit", "mathText");
1296
+ return i !== void 0 && t.add(new k("content", this._source.substring(i, r)), i), { node: new $e(t.build(o.endOffset - e.startOffset)), start: e.startOffset };
1297
+ }
1298
+ _parseStrikethrough() {
1299
+ const e = this._consume("enter", "strikethrough");
1300
+ let t, s, i = e.startOffset, r = e.startOffset;
1301
+ const o = [];
1302
+ for (; this._notExit("strikethrough"); ) {
1303
+ const a = this._events[this._idx];
1304
+ if (a.type === "enter" && a.tokenType === "strikethroughSequence") {
1305
+ const h = this._source.substring(a.startOffset, a.endOffset);
1306
+ t ? (s = new k("closeMarker", h), r = a.startOffset) : (t = new k("openMarker", h), i = a.endOffset), this._idx++;
1307
+ } else a.type === "enter" && a.tokenType === "strikethroughText" ? (this._consume("enter", "strikethroughText"), this._parseInlines(o, "strikethroughText"), this._consume("exit", "strikethroughText")) : this._idx++;
1308
+ }
1309
+ this._consume("exit", "strikethrough");
1310
+ const c = new C(i, this._source);
1311
+ for (const a of o)
1312
+ c.add(a.node, a.start);
1313
+ const l = c.build(r - i);
1314
+ return { node: new De(t, l, s), start: e.startOffset };
1315
+ }
1316
+ _parseLink() {
1317
+ const e = this._consume("enter", "link"), t = new C(e.startOffset, this._source), s = [];
1318
+ let i = "", r = !1;
1319
+ for (; this._notExit("link"); ) {
1320
+ const c = this._events[this._idx];
1321
+ if (c.type === "enter" && c.tokenType === "label") {
1322
+ for (this._consume("enter", "label"); this._notExit("label"); ) {
1323
+ const l = this._events[this._idx];
1324
+ if (l.type === "enter" && l.tokenType === "labelMarker")
1325
+ t.add(new k(r ? "closeBracket" : "openBracket", this._source.substring(l.startOffset, l.endOffset)), l.startOffset), r = !0;
1326
+ else if (l.type === "enter" && l.tokenType === "labelText") {
1327
+ this._consume("enter", "labelText"), this._parseInlines(s, "labelText"), this._consume("exit", "labelText");
1328
+ continue;
1329
+ }
1330
+ this._idx++;
1331
+ }
1332
+ this._consume("exit", "label");
1333
+ } else if (c.type === "enter" && c.tokenType === "resource") {
1334
+ this._consume("enter", "resource");
1335
+ let l = !1;
1336
+ for (; this._notExit("resource"); ) {
1337
+ const a = this._events[this._idx];
1338
+ a.type === "enter" && a.tokenType === "resourceMarker" ? (t.add(new k(l ? "closeParen" : "openParen", this._source.substring(a.startOffset, a.endOffset)), a.startOffset), l = !0) : a.type === "enter" && a.tokenType === "resourceDestinationString" && (i = this._source.substring(a.startOffset, a.endOffset), t.add(new k("url", i), a.startOffset)), this._idx++;
1339
+ }
1340
+ this._consume("exit", "resource");
1341
+ } else
1342
+ this._idx++;
1343
+ }
1344
+ const o = this._consume("exit", "link");
1345
+ for (const c of s)
1346
+ t.add(c.node, c.start);
1347
+ return { node: new fe(i, t.build(o.endOffset - e.startOffset)), start: e.startOffset };
1348
+ }
1349
+ _parseImage() {
1350
+ const e = this._consume("enter", "image"), t = new C(e.startOffset, this._source);
1351
+ let s = "", i = "", r = !1;
1352
+ for (; this._notExit("image"); ) {
1353
+ const c = this._events[this._idx];
1354
+ if (c.type === "enter" && c.tokenType === "label") {
1355
+ for (this._consume("enter", "label"); this._notExit("label"); ) {
1356
+ const l = this._events[this._idx];
1357
+ l.type === "enter" && l.tokenType === "labelImageMarker" ? t.add(new k("bangBracket", this._source.substring(l.startOffset, l.endOffset)), l.startOffset) : l.type === "enter" && l.tokenType === "labelMarker" ? (t.add(new k(r ? "closeBracket" : "openBracket", this._source.substring(l.startOffset, l.endOffset)), l.startOffset), r = !0) : l.type === "enter" && l.tokenType === "labelText" && (s = this._source.substring(l.startOffset, l.endOffset)), this._idx++;
1358
+ }
1359
+ this._consume("exit", "label");
1360
+ } else if (c.type === "enter" && c.tokenType === "resource") {
1361
+ this._consume("enter", "resource");
1362
+ let l = !1;
1363
+ for (; this._notExit("resource"); ) {
1364
+ const a = this._events[this._idx];
1365
+ a.type === "enter" && a.tokenType === "resourceMarker" ? (t.add(new k(l ? "closeParen" : "openParen", this._source.substring(a.startOffset, a.endOffset)), a.startOffset), l = !0) : a.type === "enter" && a.tokenType === "resourceDestinationString" && (i = this._source.substring(a.startOffset, a.endOffset)), this._idx++;
1366
+ }
1367
+ this._consume("exit", "resource");
1368
+ } else
1369
+ this._idx++;
1370
+ }
1371
+ const o = this._consume("exit", "image");
1372
+ return { node: new ge(s, i, t.build(o.endOffset - e.startOffset)), start: e.startOffset };
1373
+ }
1374
+ _notExit(e) {
1375
+ if (this._idx >= this._events.length)
1376
+ return !1;
1377
+ const t = this._events[this._idx];
1378
+ return !(t.type === "exit" && t.tokenType === e);
1379
+ }
1380
+ _consume(e, t) {
1381
+ const s = this._events[this._idx];
1382
+ if (!s || s.type !== e || s.tokenType !== t)
1383
+ throw new Error(`Expected ${e}:${t} at ${this._idx}, got ${s?.type}:${s?.tokenType}`);
1384
+ return this._idx++, s;
1385
+ }
1386
+ }
1387
+ class kn {
1388
+ constructor(e) {
1389
+ this._edit = e;
1390
+ }
1391
+ /**
1392
+ * The original range corresponding to `mod`, or `undefined` if `mod`
1393
+ * overlaps any replaced/inserted text (i.e. is not provably unchanged).
1394
+ */
1395
+ getOriginalRange(e) {
1396
+ let t = 0;
1397
+ for (const s of this._edit.replacements) {
1398
+ const i = s.replaceRange.start + t;
1399
+ if (i >= e.endExclusive)
1400
+ break;
1401
+ const r = i + s.newText.length;
1402
+ if (Math.max(i, e.start) < Math.min(r, e.endExclusive))
1403
+ return;
1404
+ t += s.newText.length - s.replaceRange.length;
1405
+ }
1406
+ return e.delta(-t);
1407
+ }
1408
+ /** The original offset for `mod`, or `undefined` if it falls inside inserted text. */
1409
+ getOriginalOffset(e) {
1410
+ let t = 0;
1411
+ for (const s of this._edit.replacements) {
1412
+ const i = s.replaceRange.start + t;
1413
+ if (e < i)
1414
+ break;
1415
+ if (e < i + s.newText.length)
1416
+ return;
1417
+ t += s.newText.length - s.replaceRange.length;
1418
+ }
1419
+ return e - t;
1420
+ }
1421
+ }
1422
+ class xn {
1423
+ _byRange = /* @__PURE__ */ new Map();
1424
+ _byId = /* @__PURE__ */ new Map();
1425
+ constructor(e) {
1426
+ this._walk(e, 0);
1427
+ }
1428
+ _walk(e, t) {
1429
+ const s = `${t}:${t + e.length}`;
1430
+ let i = this._byRange.get(s);
1431
+ i || (i = [], this._byRange.set(s, i)), i.push(e), this._byId.set(`${t}:${e.kind}`, e);
1432
+ let r = t;
1433
+ for (const o of e.children)
1434
+ this._walk(o, r), r += o.length;
1435
+ }
1436
+ /** The old node spanning exactly `range` with the given `kind`, if any. */
1437
+ lookupExact(e, t) {
1438
+ return this._byRange.get(`${e.start}:${e.endExclusive}`)?.find((s) => s.kind === t);
1439
+ }
1440
+ /** The old node that began at `originalStart` with the given `kind`. */
1441
+ lookupId(e, t) {
1442
+ return this._byId.get(`${e}:${t}`);
1443
+ }
1444
+ }
1445
+ function At(n, e, t, s, i) {
1446
+ let r, o = e;
1447
+ for (const g of n.children) {
1448
+ const u = At(g, o, t, s, i);
1449
+ u !== g && (r ??= /* @__PURE__ */ new Map()).set(g, u), o += g.length;
1450
+ }
1451
+ let c = r ? n.mapChildren(r) : n;
1452
+ const l = t.getOriginalRange(_.ofStartAndLength(e, n.length));
1453
+ if (l) {
1454
+ const g = s.lookupExact(l, c.kind);
1455
+ if (g && c.structurallyEqual(g))
1456
+ return g;
1457
+ }
1458
+ const a = t.getOriginalOffset(e), h = a !== void 0 ? s.lookupId(a, c.kind) : void 0;
1459
+ if (h && h.id !== c.id && (c = c.cloneWithId(h.id)), c instanceof K && h instanceof K && a !== void 0) {
1460
+ const g = vn(c, h, a, i);
1461
+ if (g)
1462
+ return g;
1463
+ }
1464
+ return c;
1465
+ }
1466
+ function vn(n, e, t, s) {
1467
+ const i = e.code, r = n.code;
1468
+ if (!i || !r || n.language !== e.language || n.openFence?.content !== e.openFence?.content || n.closeFence?.content !== e.closeFence?.content)
1469
+ return;
1470
+ const o = t + e.codeOffset, c = o + i.length;
1471
+ if (!bn(s, o, c))
1472
+ return;
1473
+ const l = yn(s, -o);
1474
+ if (l.apply(i.content) === r.content)
1475
+ return n.withCodeDiff(e, l);
1476
+ }
1477
+ function bn(n, e, t) {
1478
+ for (const s of n.replacements)
1479
+ if (s.replaceRange.start < e || s.replaceRange.endExclusive > t)
1480
+ return !1;
1481
+ return !0;
1482
+ }
1483
+ function yn(n, e) {
1484
+ return new y(n.replacements.map((t) => V.replace(t.replaceRange.delta(e), t.newText)));
1485
+ }
1486
+ function En(n, e, t) {
1487
+ return At(n, 0, new kn(t), new xn(e), t);
1488
+ }
1489
+ function Cn(n, e, t) {
1490
+ const s = mn(n);
1491
+ return !e || !t ? s : En(s, e, t);
1492
+ }
1493
+ class On {
1494
+ parse(e, t, s) {
1495
+ return Cn(e.value, t, s);
1496
+ }
1497
+ }
1498
+ function Mn(n, e, t = 0) {
1499
+ if (n.children.length === 0) {
1500
+ const r = $t(e.substring(t, t + n.length));
1501
+ if (n instanceof ve || n instanceof N)
1502
+ return r;
1503
+ const o = n instanceof k ? n.markerKind : n.kind;
1504
+ return `<${o}${Je(n)}>${r}</${o}>`;
1505
+ }
1506
+ let s = "", i = t;
1507
+ for (const r of n.children)
1508
+ s += Mn(r, e, i), i += r.length;
1509
+ return `<${n.kind}${Je(n)}>${s}</${n.kind}>`;
1510
+ }
1511
+ function Je(n) {
1512
+ const e = {};
1513
+ return n instanceof U ? e.level = String(n.level) : n instanceof H ? e.ordered = String(n.ordered) : n instanceof K ? n.language && (e.language = n.language) : n instanceof fe ? e.url = n.url : n instanceof ge ? (e.alt = n.alt, e.url = n.url) : n instanceof $ && n.checked !== void 0 && (e.checked = String(n.checked)), Object.entries(e).map(([t, s]) => ` ${t}="${$t(s)}"`).join("");
1514
+ }
1515
+ function $t(n) {
1516
+ return n.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
1517
+ }
1518
+ function Tn(n) {
1519
+ return n instanceof ve ? `text ${JSON.stringify(n.content)}` : n instanceof N ? `glue${n.glueKind ? `(${n.glueKind})` : ""} ${JSON.stringify(n.content)}` : n instanceof k ? `marker(${n.markerKind}) ${JSON.stringify(n.content)}` : n instanceof ne ? `thematicBreak ${JSON.stringify(n.content)}` : n instanceof U ? `heading(level=${n.level})` : n instanceof H ? `list(ordered=${n.ordered})` : n instanceof $ ? n.checked === void 0 ? "listItem" : `listItem(checked=${n.checked})` : n instanceof K ? `codeBlock(language=${JSON.stringify(n.language)})` : n instanceof se ? "mathBlock" : n instanceof fe ? `link(url=${JSON.stringify(n.url)})` : n instanceof ge ? `image(alt=${JSON.stringify(n.alt)}, url=${JSON.stringify(n.url)})` : n.kind;
1520
+ }
1521
+ const Ze = /* @__PURE__ */ new WeakMap();
1522
+ let Sn = 0;
1523
+ function Ln(n) {
1524
+ let e = Ze.get(n);
1525
+ return e === void 0 && (e = Sn++, Ze.set(n, e)), e;
1526
+ }
1527
+ function Li(n, e) {
1528
+ let t = 0;
1529
+ function s(i) {
1530
+ const r = t, o = i.children;
1531
+ let c;
1532
+ return o.length === 0 ? t += i.length : c = o.map(s), { label: `${Tn(i)} #${Ln(i)} nid=${i.id}`, range: [r, t], children: c };
1533
+ }
1534
+ return { $fileExtension: "ast.w", source: e, root: s(n) };
1535
+ }
1536
+ const Nn = Symbol("NO_ACTIVE_BLOCKS");
1537
+ class Ni {
1538
+ _parser = new On();
1539
+ /**
1540
+ * The most recent edit applied to {@link sourceText}, used by
1541
+ * {@link document} to let the parser link incrementally edited code
1542
+ * blocks. Only trusted when it exactly bridges the previous and current
1543
+ * source text (see {@link document}).
1544
+ */
1545
+ _pendingEdit;
1546
+ sourceText = G(this, new Oe(""));
1547
+ /**
1548
+ * The current selection, or `undefined` when the editor has no caret
1549
+ * (e.g. an inactive/unfocused rendering).
1550
+ */
1551
+ selection = G(this, x.collapsed(0));
1552
+ /**
1553
+ * Forces the rendered active-block set. `undefined` (the default)
1554
+ * derives the set from the current selection range (see
1555
+ * {@link activeBlocks}). The sentinel {@link NO_ACTIVE_BLOCKS} forces
1556
+ * "no active block" — useful in fixtures that always want the
1557
+ * collapsed/inactive rendering.
1558
+ */
1559
+ activeBlocksOverride = G(this, void 0);
1560
+ cursorOffset = A(
1561
+ this,
1562
+ (e) => e.readObservable(this.selection)?.active
1563
+ );
1564
+ /**
1565
+ * The parsed document. Threads the previous document into the parser so
1566
+ * unchanged blocks keep their object identity across reparses (see
1567
+ * {@link MarkdownParser.parse}). Writing `previous` inside the compute is
1568
+ * a safe optimization: `derived` only recomputes when `sourceText`
1569
+ * changes, and the result is structurally identical to a full reparse.
1570
+ */
1571
+ document = (() => {
1572
+ let e, t;
1573
+ return A(this, (s) => {
1574
+ const i = s.readObservable(this.sourceText), r = this._pendingEdit, o = r && r.baseText === t && r.newText === i.value ? r.edit : void 0, c = this._parser.parse(i, e, o);
1575
+ return e = c, t = i.value, c;
1576
+ });
1577
+ })();
1578
+ /**
1579
+ * Block that contains the cursor (selection's active end). Used by
1580
+ * cursor navigation to know which block's marker ranges count as
1581
+ * visible. Unaffected by {@link activeBlocksOverride} because
1582
+ * navigation is independent of rendering.
1583
+ */
1584
+ activeBlock = A(this, (e) => {
1585
+ const t = e.readObservable(this.document), s = e.readObservable(this.cursorOffset);
1586
+ if (s !== void 0)
1587
+ return Ft(t, s);
1588
+ });
1589
+ /**
1590
+ * All blocks whose source range intersects the current selection.
1591
+ * The rendering side uses this to decide which blocks render in
1592
+ * their expanded (markers-visible) form. When the selection is
1593
+ * collapsed this is a one-element set holding {@link activeBlock}.
1594
+ */
1595
+ activeBlocks = A(this, (e) => {
1596
+ const t = e.readObservable(this.activeBlocksOverride);
1597
+ if (t === Nn)
1598
+ return /* @__PURE__ */ new Set();
1599
+ if (t !== void 0)
1600
+ return new Set(t);
1601
+ const s = e.readObservable(this.document), i = e.readObservable(this.selection);
1602
+ return i === void 0 ? /* @__PURE__ */ new Set() : new Set(In(s, i.range.start, i.range.endExclusive));
1603
+ });
1604
+ applyEdit(e) {
1605
+ const t = this.sourceText.get(), s = new Oe(e.apply(t.value)), i = this.selection.get() ?? x.collapsed(0), r = e.mapOffset(i.active);
1606
+ this._pendingEdit = { baseText: t.value, newText: s.value, edit: e }, this.sourceText.set(s, void 0), this.selection.set(x.collapsed(r), void 0);
1607
+ }
1608
+ applyEditForSelection(e) {
1609
+ const t = this.sourceText.get(), s = new Oe(e.apply(t.value)), i = this.selection.get() ?? x.collapsed(0), r = e.mapOffset(i.range.endExclusive);
1610
+ this._pendingEdit = { baseText: t.value, newText: s.value, edit: e }, this.sourceText.set(s, void 0), this.selection.set(x.collapsed(r), void 0);
1611
+ }
1612
+ }
1613
+ function Ft(n, e) {
1614
+ let t = 0, s;
1615
+ for (const i of n.children) {
1616
+ const r = t + i.length;
1617
+ if (n.blocks.includes(i)) {
1618
+ if (t <= e && e < r)
1619
+ return i;
1620
+ r === e && (s = i);
1621
+ }
1622
+ t = r;
1623
+ }
1624
+ return s;
1625
+ }
1626
+ function In(n, e, t) {
1627
+ if (e === t) {
1628
+ const r = Ft(n, e);
1629
+ return r ? [r] : [];
1630
+ }
1631
+ const s = [];
1632
+ let i = 0;
1633
+ for (const r of n.children) {
1634
+ const o = i + r.length;
1635
+ n.blocks.includes(r) && i < t && o > e && s.push(r), i = o;
1636
+ }
1637
+ return s;
1638
+ }
1639
+ class _e {
1640
+ constructor(e) {
1641
+ this.lines = e;
1642
+ }
1643
+ static EMPTY = new _e([]);
1644
+ static measure(e) {
1645
+ return An(e);
1646
+ }
1647
+ get lineCount() {
1648
+ return this.lines.length;
1649
+ }
1650
+ get isEmpty() {
1651
+ return this.lines.length === 0;
1652
+ }
1653
+ lineRect(e) {
1654
+ return this.lines[e].rect;
1655
+ }
1656
+ // ---- SourceOffset → ... -------------------------------------------
1657
+ /**
1658
+ * Line whose runs cover the offset, or the nearest line by source
1659
+ * distance if no run covers it.
1660
+ *
1661
+ * An offset that is only a run's *trailing* boundary (`offset ===
1662
+ * endExclusive`) — most notably the source offset just past a
1663
+ * line-breaking `\n`, which a zero-width run reports as its end on the line
1664
+ * it terminates — belongs to the START of the NEXT line instead. Preferring
1665
+ * the line that actually *starts* the offset makes the caret advance past a
1666
+ * newline to the next line rather than collapsing onto the previous line's
1667
+ * end (which would render two distinct offsets at the same caret position).
1668
+ * The first such trailing-boundary line is remembered as a fallback for the
1669
+ * document's very last offset, where no later line starts it.
1670
+ */
1671
+ lineIndexOfOffset(e) {
1672
+ let t = -1;
1673
+ for (let r = 0; r < this.lines.length; r++) {
1674
+ const o = this.lines[r].offsetMembership(e);
1675
+ if (o === "covers")
1676
+ return r;
1677
+ o === "end" && t < 0 && (t = r);
1678
+ }
1679
+ if (t >= 0)
1680
+ return t;
1681
+ let s = 0, i = 1 / 0;
1682
+ for (let r = 0; r < this.lines.length; r++) {
1683
+ const o = this.lines[r].sourceDistanceTo(e);
1684
+ o < i && (i = o, s = r);
1685
+ }
1686
+ return s;
1687
+ }
1688
+ /**
1689
+ * x of the caret position before `offset`, on the line returned by
1690
+ * {@link lineIndexOfOffset}. Returns `0` when the map is empty.
1691
+ */
1692
+ xAtOffset(e) {
1693
+ return this.lines.length === 0 ? 0 : this.lines[this.lineIndexOfOffset(e)].xAtOffset(e);
1694
+ }
1695
+ // ---- Point2D → ... -------------------------------------------------
1696
+ /**
1697
+ * Line whose vertical band contains `y`, clamped to the first/last
1698
+ * line when `y` is outside the document.
1699
+ */
1700
+ lineIndexAtY(e) {
1701
+ if (this.lines.length === 0)
1702
+ return 0;
1703
+ for (let t = 0; t < this.lines.length; t++)
1704
+ if (e < this.lines[t].rect.bottom)
1705
+ return t;
1706
+ return this.lines.length - 1;
1707
+ }
1708
+ /**
1709
+ * Snap a 2D point to the nearest source offset. Uses `y` to pick a
1710
+ * line, then `x` to pick an offset within it. Up/down navigation
1711
+ * uses {@link offsetInLineAtX} directly to preserve desired column.
1712
+ */
1713
+ offsetAtPoint(e) {
1714
+ return this.offsetInLineAtX(this.lineIndexAtY(e.y), e.x);
1715
+ }
1716
+ /** Snap `x` to the nearest offset on a specific line. */
1717
+ offsetInLineAtX(e, t) {
1718
+ return e < 0 || e >= this.lines.length ? 0 : this.lines[e].offsetAtX(t);
1719
+ }
1720
+ }
1721
+ class Bn {
1722
+ constructor(e, t) {
1723
+ this.rect = e, this.runs = t;
1724
+ }
1725
+ containsOffset(e) {
1726
+ for (const t of this.runs)
1727
+ if (t.containsOffset(e))
1728
+ return !0;
1729
+ return !1;
1730
+ }
1731
+ /**
1732
+ * How `offset` relates to this line's runs:
1733
+ * - `'covers'`: a run starts at or strictly contains the offset
1734
+ * (`start <= offset < endExclusive`) — the caret belongs on this line.
1735
+ * - `'end'`: the offset is only some run's trailing boundary
1736
+ * (`offset === endExclusive`) with no run covering it — a line-break
1737
+ * boundary the caret should leave for the next line.
1738
+ * - `'none'`: no run touches the offset.
1739
+ */
1740
+ offsetMembership(e) {
1741
+ let t = !1;
1742
+ for (const s of this.runs) {
1743
+ if (e >= s.sourceStart && e < s.sourceEndExclusive)
1744
+ return "covers";
1745
+ e === s.sourceEndExclusive && (t = !0);
1746
+ }
1747
+ return t ? "end" : "none";
1748
+ }
1749
+ /**
1750
+ * Min `|offset - r|` over offsets `r` in any of this line's runs. Used
1751
+ * to pick the nearest line when no run actually covers the offset.
1752
+ */
1753
+ sourceDistanceTo(e) {
1754
+ let t = 1 / 0;
1755
+ for (const s of this.runs) {
1756
+ const i = s.sourceDistanceTo(e);
1757
+ i < t && (t = i);
1758
+ }
1759
+ return t;
1760
+ }
1761
+ /**
1762
+ * x of the caret position before `offset` on this line. When `offset`
1763
+ * is past all runs (trailing whitespace / blank line), returns the
1764
+ * right edge of the last run; when before all runs, returns the left
1765
+ * edge of the first run.
1766
+ */
1767
+ xAtOffset(e) {
1768
+ for (const s of this.runs)
1769
+ if (s.containsOffset(e))
1770
+ return s.xAtOffset(e);
1771
+ const t = this.runs[0];
1772
+ return e <= t.sourceStart ? t.rect.left : this.runs[this.runs.length - 1].rect.right;
1773
+ }
1774
+ /**
1775
+ * Snap `x` to the nearest offset on this line. If `x` falls inside a
1776
+ * run, the offset is interpolated by character fraction; otherwise it
1777
+ * snaps to the closer edge of the nearest run.
1778
+ */
1779
+ offsetAtX(e) {
1780
+ if (this.runs.length === 0)
1781
+ return 0;
1782
+ let t = this.runs[0], s = 1 / 0;
1783
+ for (const i of this.runs) {
1784
+ if (i.rect.containsX(e) || e === i.rect.right)
1785
+ return i.offsetAtX(e);
1786
+ const r = Math.min(Math.abs(e - i.rect.left), Math.abs(e - i.rect.right));
1787
+ r < s && (s = r, t = i);
1788
+ }
1789
+ return e <= t.rect.left ? t.sourceRange.start : t.sourceRange.endExclusive;
1790
+ }
1791
+ }
1792
+ class Le {
1793
+ constructor(e, t, s) {
1794
+ this.sourceRange = e, this.rect = t, this.source = s;
1795
+ }
1796
+ get sourceStart() {
1797
+ return this.sourceRange.start;
1798
+ }
1799
+ get sourceEndExclusive() {
1800
+ return this.sourceRange.endExclusive;
1801
+ }
1802
+ get sourceLength() {
1803
+ return this.sourceRange.length;
1804
+ }
1805
+ containsOffset(e) {
1806
+ return e >= this.sourceStart && e <= this.sourceEndExclusive;
1807
+ }
1808
+ sourceDistanceTo(e) {
1809
+ return e < this.sourceStart ? this.sourceStart - e : e > this.sourceEndExclusive ? e - this.sourceEndExclusive : 0;
1810
+ }
1811
+ xAtOffset(e) {
1812
+ if (this.sourceLength === 0)
1813
+ return this.rect.left;
1814
+ if (this.source)
1815
+ return Rn(this.source.textNode, this.source.textNodeStart + (e - this.sourceStart), this.rect.left);
1816
+ const t = (e - this.sourceStart) / this.sourceLength;
1817
+ return this.rect.left + t * this.rect.width;
1818
+ }
1819
+ offsetAtX(e) {
1820
+ if (this.rect.width <= 0)
1821
+ return this.sourceStart;
1822
+ if (this.source) {
1823
+ const s = Dn(this.source.textNode, this.source.textNodeStart, this.source.textNodeStart + this.sourceLength, e);
1824
+ return this.sourceStart + (s - this.source.textNodeStart);
1825
+ }
1826
+ const t = (e - this.rect.left) / this.rect.width;
1827
+ return this.sourceStart + Math.round(t * this.sourceLength);
1828
+ }
1829
+ }
1830
+ function Rn(n, e, t) {
1831
+ if (e <= 0)
1832
+ return t;
1833
+ const s = document.createRange();
1834
+ s.setStart(n, e - 1), s.setEnd(n, e);
1835
+ const i = s.getBoundingClientRect();
1836
+ return i.width === 0 && i.height === 0 ? t : i.right;
1837
+ }
1838
+ function Dn(n, e, t, s) {
1839
+ const i = document.createRange();
1840
+ let r = e, o = 1 / 0;
1841
+ for (let c = e; c < t; c++) {
1842
+ i.setStart(n, c), i.setEnd(n, c + 1);
1843
+ const l = i.getBoundingClientRect();
1844
+ if (l.width === 0 && l.height === 0)
1845
+ continue;
1846
+ const a = (l.left + l.right) / 2, h = Math.abs(s - l.left), g = Math.abs(s - l.right);
1847
+ if (h < o && (o = h, r = c), g < o && (o = g, r = c + 1), s >= l.left && s <= l.right)
1848
+ return s < a ? c : c + 1;
1849
+ }
1850
+ return r;
1851
+ }
1852
+ function An(n) {
1853
+ const e = [];
1854
+ for (const a of n) {
1855
+ const h = e.length;
1856
+ a.viewNode.forEachTextLeaf(a.absoluteStart, (g, u) => {
1857
+ const d = g.dom;
1858
+ if (d.length === 0)
1859
+ return;
1860
+ const f = document.createRange();
1861
+ f.selectNodeContents(d);
1862
+ const p = f.getClientRects();
1863
+ if (p.length === 0)
1864
+ return;
1865
+ const m = Fn(d);
1866
+ if (p.length === 1)
1867
+ e.push(new Le(
1868
+ _.fromTo(u, u + d.length),
1869
+ et(p[0], m),
1870
+ { textNode: d, textNodeStart: 0 }
1871
+ ));
1872
+ else {
1873
+ const w = $n(d, p);
1874
+ for (let v = 0; v < p.length; v++) {
1875
+ const I = v === 0 ? 0 : w[v - 1], X = v < w.length ? w[v] : d.length;
1876
+ e.push(new Le(
1877
+ _.fromTo(u + I, u + X),
1878
+ et(p[v], m),
1879
+ { textNode: d, textNodeStart: I }
1880
+ ));
1881
+ }
1882
+ }
1883
+ }), e.length === h && Vn(e, a.viewNode, a.absoluteStart);
1884
+ }
1885
+ e.sort((a, h) => a.rect.y - h.rect.y || a.rect.x - h.rect.x);
1886
+ const t = [];
1887
+ let s = [], i = -1 / 0, r = 0, o = 1 / 0, c = -1 / 0;
1888
+ const l = () => {
1889
+ s.length !== 0 && t.push(new Bn(
1890
+ R.fromPointPoint(o, i, c, i + r),
1891
+ s
1892
+ ));
1893
+ };
1894
+ for (const a of e) {
1895
+ const h = a.rect, g = Math.min(i + r, h.y + h.height) - Math.max(i, h.y);
1896
+ s.length > 0 && g > Math.min(r, h.height) / 2 ? (s.push(a), r = Math.max(r, h.y + h.height - i), o = Math.min(o, h.left), c = Math.max(c, h.right)) : (l(), s = [a], i = h.y, r = h.height, o = h.left, c = h.right);
1897
+ }
1898
+ return l(), new _e(t);
1899
+ }
1900
+ function $n(n, e) {
1901
+ const t = [], s = document.createRange();
1902
+ for (let i = 0; i < e.length - 1; i++) {
1903
+ const r = e[i + 1].y;
1904
+ let o = i === 0 ? 0 : t[i - 1], c = n.length;
1905
+ for (; o < c; ) {
1906
+ const l = o + c >>> 1;
1907
+ s.setStart(n, l), s.setEnd(n, Math.min(l + 1, n.length)), s.getBoundingClientRect().y < r - 1 ? o = l + 1 : c = l;
1908
+ }
1909
+ t.push(o);
1910
+ }
1911
+ return t;
1912
+ }
1913
+ function Fn(n) {
1914
+ const e = n.parentElement;
1915
+ if (!e)
1916
+ return 0;
1917
+ const t = getComputedStyle(e);
1918
+ let s = parseFloat(t.lineHeight);
1919
+ return isFinite(s) || (s = parseFloat(t.fontSize) * 1.2), s;
1920
+ }
1921
+ function et(n, e) {
1922
+ if (e <= n.height)
1923
+ return R.fromPointSize(n.x, n.y, n.width, n.height);
1924
+ const t = (e - n.height) / 2;
1925
+ return R.fromPointSize(n.x, n.y - t, n.width, e);
1926
+ }
1927
+ function Vn(n, e, t) {
1928
+ const s = e.dom;
1929
+ if (s.nodeType !== 1)
1930
+ return;
1931
+ const i = s.getBoundingClientRect();
1932
+ i.width === 0 && i.height === 0 || n.push(new Le(
1933
+ _.fromTo(t, t + e.sourceLength),
1934
+ R.fromPointSize(i.x, i.y, i.width, i.height)
1935
+ ));
1936
+ }
1937
+ class Pn {
1938
+ measurements = G(this, []);
1939
+ /**
1940
+ * Concatenated visual line map across all mounted blocks. Lines are
1941
+ * left in DOM (client-coordinate) y order — each per-block map already
1942
+ * uses absolute client coordinates from `getClientRects()`, so the
1943
+ * concatenation is well-formed without re-sorting.
1944
+ */
1945
+ visualLineMap = A(this, (e) => {
1946
+ const s = e.readObservable(this.measurements).flatMap((i) => i.visualLineMap?.lines ?? []);
1947
+ return new _e(s);
1948
+ });
1949
+ }
1950
+ function Vt(n, e, t, s) {
1951
+ let i = s === "right" ? t + 1 : t - 1, r = 0;
1952
+ for (const o of n.children) {
1953
+ if (n.blocks.includes(o)) {
1954
+ const l = o, a = Kn(l, l === e, t - r);
1955
+ i = Hn(i, r, a, s);
1956
+ }
1957
+ r += o.length;
1958
+ }
1959
+ return s === "right" ? Math.min(i, n.length) : Math.max(i, 0);
1960
+ }
1961
+ function Kn(n, e, t) {
1962
+ if (!e)
1963
+ return qn(n);
1964
+ if (n.kind === "list") {
1965
+ const s = Pt(n, t);
1966
+ return Wn(n, s);
1967
+ }
1968
+ return [];
1969
+ }
1970
+ function Hn(n, e, t, s) {
1971
+ if (s === "right")
1972
+ for (const i of t) {
1973
+ const r = n - e;
1974
+ (i.contains(r) || i.start === r) && (n = e + i.endExclusive);
1975
+ }
1976
+ else
1977
+ for (let i = t.length - 1; i >= 0; i--) {
1978
+ const r = t[i], o = n - e;
1979
+ (r.contains(o) || r.endExclusive === o) && (n = e + r.start);
1980
+ }
1981
+ return n;
1982
+ }
1983
+ function Pt(n, e) {
1984
+ let t = 0, s;
1985
+ for (let i = 0; i < n.children.length; i++) {
1986
+ const r = n.children[i], o = t + r.length, c = n.items.indexOf(r);
1987
+ if (c >= 0) {
1988
+ if (t <= e && e < o)
1989
+ return c;
1990
+ o === e && (s = c);
1991
+ }
1992
+ t = o;
1993
+ }
1994
+ return s;
1995
+ }
1996
+ function Wn(n, e) {
1997
+ const t = [];
1998
+ let s = 0;
1999
+ for (const i of n.children) {
2000
+ const r = n.items.indexOf(i);
2001
+ if (r >= 0 && r !== e) {
2002
+ const o = n.items[r];
2003
+ Pe(o, s, t);
2004
+ }
2005
+ s += i.length;
2006
+ }
2007
+ return t.sort((i, r) => i.start - r.start), t;
2008
+ }
2009
+ function qn(n) {
2010
+ const e = [];
2011
+ return Pe(n, 0, e), e.sort((t, s) => t.start - s.start), e;
2012
+ }
2013
+ function Pe(n, e, t) {
2014
+ if (n.children.length === 0) {
2015
+ n instanceof k && t.push(_.ofStartAndLength(e, n.length));
2016
+ return;
2017
+ }
2018
+ switch (n.kind) {
2019
+ case "codeBlock":
2020
+ case "mathBlock": {
2021
+ let i = e;
2022
+ for (const r of n.children)
2023
+ r instanceof k && (r.markerKind === "openFence" || r.markerKind === "closeFence") && t.push(_.ofStartAndLength(i, r.length)), i += r.length;
2024
+ return;
2025
+ }
2026
+ case "inlineCode":
2027
+ case "inlineMath": {
2028
+ let i = e;
2029
+ for (const r of n.children)
2030
+ r instanceof k && (r.markerKind === "openMarker" || r.markerKind === "closeMarker") && t.push(_.ofStartAndLength(i, r.length)), i += r.length;
2031
+ return;
2032
+ }
2033
+ case "thematicBreak":
2034
+ return;
2035
+ case "image": {
2036
+ t.push(_.ofStartAndLength(e, n.length));
2037
+ return;
2038
+ }
2039
+ }
2040
+ let s = e;
2041
+ for (const i of n.children)
2042
+ Pe(i, s, t), s += i.length;
2043
+ }
2044
+ const jn = (n) => n.selection.isCollapsed ? Math.min(n.selection.active + 1, n.text.length) : n.selection.range.endExclusive, Gn = (n) => n.selection.isCollapsed ? Math.max(n.selection.active - 1, 0) : n.selection.range.start, zn = (n) => Math.min(n.selection.active + 1, n.text.length), Xn = (n) => Math.max(n.selection.active - 1, 0), Qn = (n) => Bt(n.text, n.selection.active), tt = (n) => It(n.text, n.selection.active), Un = (n) => n.text.lastIndexOf(`
2045
+ `, n.selection.active - 1) + 1, Yn = (n) => {
2046
+ const e = n.text.indexOf(`
2047
+ `, n.selection.active);
2048
+ return e === -1 ? n.text.length : e;
2049
+ }, Jn = () => 0, Zn = (n) => n.text.length, nt = (n) => {
2050
+ const e = n.lineMap.lineIndexOfOffset(n.selection.active), t = n.desiredColumn ?? n.lineMap.xAtOffset(n.selection.active);
2051
+ return e >= n.lineMap.lineCount - 1 ? { offset: n.selection.active, desiredColumn: t } : { offset: n.lineMap.offsetInLineAtX(e + 1, t), desiredColumn: t };
2052
+ }, st = (n) => {
2053
+ const e = n.lineMap.lineIndexOfOffset(n.selection.active), t = n.desiredColumn ?? n.lineMap.xAtOffset(n.selection.active);
2054
+ return e <= 0 ? { offset: n.selection.active, desiredColumn: t } : { offset: n.lineMap.offsetInLineAtX(e - 1, t), desiredColumn: t };
2055
+ }, it = (n) => {
2056
+ const e = n.selection;
2057
+ if (!e.isCollapsed)
2058
+ return {
2059
+ edit: y.delete(e.range),
2060
+ selection: x.collapsed(e.range.start)
2061
+ };
2062
+ if (e.active === 0)
2063
+ return;
2064
+ const t = new _(Vt(n.document, n.activeBlock, e.active, "left"), e.active);
2065
+ return {
2066
+ edit: y.delete(t),
2067
+ selection: x.collapsed(t.start)
2068
+ };
2069
+ }, es = (n) => {
2070
+ const e = n.selection;
2071
+ if (!e.isCollapsed)
2072
+ return {
2073
+ edit: y.delete(e.range),
2074
+ selection: x.collapsed(e.range.start)
2075
+ };
2076
+ if (e.active >= n.text.length)
2077
+ return;
2078
+ const t = new _(e.active, Vt(n.document, n.activeBlock, e.active, "right"));
2079
+ return {
2080
+ edit: y.delete(t),
2081
+ selection: x.collapsed(t.start)
2082
+ };
2083
+ }, ts = (n) => {
2084
+ const e = n.selection;
2085
+ if (!e.isCollapsed)
2086
+ return {
2087
+ edit: y.delete(e.range),
2088
+ selection: x.collapsed(e.range.start)
2089
+ };
2090
+ if (e.active === 0)
2091
+ return;
2092
+ const t = It(n.text, e.active), s = new _(t, e.active);
2093
+ return {
2094
+ edit: y.delete(s),
2095
+ selection: x.collapsed(t)
2096
+ };
2097
+ }, ns = (n) => {
2098
+ const e = n.selection;
2099
+ if (!e.isCollapsed)
2100
+ return {
2101
+ edit: y.delete(e.range),
2102
+ selection: x.collapsed(e.range.start)
2103
+ };
2104
+ if (e.active >= n.text.length)
2105
+ return;
2106
+ const t = Bt(n.text, e.active), s = new _(e.active, t);
2107
+ return {
2108
+ edit: y.delete(s),
2109
+ selection: x.collapsed(e.active)
2110
+ };
2111
+ };
2112
+ function ss(n) {
2113
+ return (e) => {
2114
+ const t = y.replace(e.selection.range, n), s = e.selection.range.start + n.length;
2115
+ return {
2116
+ edit: t,
2117
+ selection: x.collapsed(s)
2118
+ };
2119
+ };
2120
+ }
2121
+ const is = (n) => {
2122
+ const e = y.replace(n.selection.range, `
2123
+
2124
+ `), t = n.selection.range.start + 2;
2125
+ return {
2126
+ edit: e,
2127
+ selection: x.collapsed(t)
2128
+ };
2129
+ }, rs = (n) => {
2130
+ const e = y.replace(n.selection.range, `
2131
+ `), t = n.selection.range.start + 1;
2132
+ return {
2133
+ edit: e,
2134
+ selection: x.collapsed(t)
2135
+ };
2136
+ }, os = (n) => {
2137
+ const e = n.selection.range.start;
2138
+ let t = 0;
2139
+ for (; t < 2 && n.text[e - 1 - t] === " "; )
2140
+ t++;
2141
+ const i = " ".repeat(2 - t) + `
2142
+ `, r = y.replace(n.selection.range, i), o = e + i.length;
2143
+ return {
2144
+ edit: r,
2145
+ selection: x.collapsed(o)
2146
+ };
2147
+ }, cs = (n) => new x(0, n.text.length), ls = (n, e) => {
2148
+ const t = hn(n.text, e);
2149
+ return new x(t.start, t.end);
2150
+ };
2151
+ function as(n, e) {
2152
+ return new x(e.start, e.endExclusive);
2153
+ }
2154
+ const rt = /* @__PURE__ */ new WeakMap(), Te = /* @__PURE__ */ new WeakMap();
2155
+ class ie extends te {
2156
+ constructor(e, t, s = hs) {
2157
+ super(), this.ast = e, this.dom = t, this._children = s, rt.set(t, this);
2158
+ for (const i of s)
2159
+ Te.set(i, this);
2160
+ }
2161
+ _children;
2162
+ /** This node's view children (a mirror of `ast.children`). */
2163
+ get children() {
2164
+ return this._children;
2165
+ }
2166
+ /**
2167
+ * Replace this node's children in place, disposing the old ones and
2168
+ * re-pointing the new ones' parent to this node. The node value is still
2169
+ * conceptually immutable with respect to its `ast`/`dom` *identity*; this
2170
+ * is used only when a node patches its own DOM subtree in place (a code
2171
+ * block re-tokenising on a highlighter recolour), where the source-mapping
2172
+ * leaves must follow the new DOM text nodes without rebuilding the node
2173
+ * itself.
2174
+ */
2175
+ _replaceChildren(e) {
2176
+ for (const t of this._children)
2177
+ t.dispose();
2178
+ this._children = e;
2179
+ for (const t of e)
2180
+ Te.set(t, this);
2181
+ }
2182
+ dispose() {
2183
+ for (const e of this._children)
2184
+ e.dispose();
2185
+ }
2186
+ /**
2187
+ * The number of source characters this node spans. Defaults to the length
2188
+ * of its {@link ast}; a synthetic leaf that subdivides one ast node (a
2189
+ * decorated-whitespace character, a code-block token span) shares that ast
2190
+ * for identity but overrides this with the length of its own slice, so the
2191
+ * renderer never has to fabricate an AST node just to carry a length.
2192
+ */
2193
+ get sourceLength() {
2194
+ return this.ast.length;
2195
+ }
2196
+ /**
2197
+ * The DOM node a parent mounts for this child. It is {@link dom} for almost
2198
+ * everything; a marker is the exception — its `dom` is the inner Text node
2199
+ * (so source ↔ DOM mapping lands on it) while the node it mounts is the
2200
+ * wrapping `<span>`.
2201
+ */
2202
+ get mountNode() {
2203
+ return this.dom;
2204
+ }
2205
+ /** The view node that rendered this node's parent, or `undefined` for a root. */
2206
+ get parent() {
2207
+ return Te.get(this);
2208
+ }
2209
+ /**
2210
+ * Closest view node owning `domNode`: the node itself if registered, else
2211
+ * the nearest registered ancestor. Returns `undefined` if the DOM node is
2212
+ * outside any view tree.
2213
+ */
2214
+ static forDom(e) {
2215
+ for (let t = e; t; t = t.parentNode) {
2216
+ const s = rt.get(t);
2217
+ if (s)
2218
+ return s;
2219
+ }
2220
+ }
2221
+ /**
2222
+ * This node's start offset within its parent's local source space: the sum
2223
+ * of the `ast.length` of the siblings before it. Polymorphic via
2224
+ * {@link _localOffsetOfChild} so a parent whose children do not map
2225
+ * linearly (e.g. it hides or reorders some) can override how its children
2226
+ * are placed.
2227
+ */
2228
+ localOffsetInParent() {
2229
+ const e = this.parent;
2230
+ return e ? e._localOffsetOfChild(this) : 0;
2231
+ }
2232
+ /** Start offset of `child` within this node's local source space. */
2233
+ _localOffsetOfChild(e) {
2234
+ let t = 0;
2235
+ for (const s of this.children) {
2236
+ if (s === e)
2237
+ return t;
2238
+ t += s.sourceLength;
2239
+ }
2240
+ return t;
2241
+ }
2242
+ /**
2243
+ * Map a DOM hit that lands on THIS node's own representation into a source
2244
+ * range in this node's local space `[0, ast.length)`. Polymorphic: a text
2245
+ * leaf maps the caret offset 1:1; an element-only node (KaTeX math, `<hr>`,
2246
+ * an image, a hidden marker) has no internal text mapping and snaps to its
2247
+ * start by default — subclasses may override (e.g. to snap to the nearer
2248
+ * edge by x).
2249
+ */
2250
+ getLocalSourceRange(e) {
2251
+ return this.dom === e.node && this.dom.nodeType === 3 ? _.emptyAt(Math.max(0, Math.min(e.offset, this.sourceLength))) : _.emptyAt(0);
2252
+ }
2253
+ /**
2254
+ * DOM hit (any node + offset within it) → source offset relative to THIS
2255
+ * node, or `undefined` when the hit is outside this node's subtree. Enters
2256
+ * the tree at the closest owning view node ({@link forDom}), maps the hit
2257
+ * into that node's local space ({@link getLocalSourceRange}), then lifts the
2258
+ * range up the parent chain — adding each node's {@link localOffsetInParent} —
2259
+ * until it reaches this node.
2260
+ */
2261
+ resolveSource(e) {
2262
+ let t = ie.forDom(e.node);
2263
+ if (!t)
2264
+ return;
2265
+ let s = t.getLocalSourceRange(e);
2266
+ for (; t !== this; ) {
2267
+ const i = t.parent;
2268
+ if (!i)
2269
+ return;
2270
+ s = s.delta(t.localOffsetInParent()), t = i;
2271
+ }
2272
+ return s.start;
2273
+ }
2274
+ /**
2275
+ * Source offset → DOM position. `nodeOffset` is the absolute source
2276
+ * offset of THIS node's start. Returns a position into a DOM Text node,
2277
+ * descending into children based on accumulated lengths.
2278
+ */
2279
+ sourceToDom(e, t = 0) {
2280
+ if (e < t || e > t + this.sourceLength)
2281
+ return;
2282
+ if (this.children.length === 0)
2283
+ return this.dom.nodeType === 3 ? { node: this.dom, offset: e - t } : void 0;
2284
+ let s = t;
2285
+ for (const i of this.children) {
2286
+ const r = s + i.sourceLength;
2287
+ if (e >= s && e <= r) {
2288
+ const o = i.sourceToDom(e, s);
2289
+ if (o)
2290
+ return o;
2291
+ }
2292
+ s = r;
2293
+ }
2294
+ }
2295
+ /** Visit every text-bearing leaf in this subtree with its absolute offset. */
2296
+ forEachTextLeaf(e, t) {
2297
+ if (this.children.length === 0) {
2298
+ this.dom.nodeType === 3 && t(this, e);
2299
+ return;
2300
+ }
2301
+ let s = e;
2302
+ for (const i of this.children)
2303
+ i.forEachTextLeaf(s, t), s += i.sourceLength;
2304
+ }
2305
+ }
2306
+ const hs = [];
2307
+ class M extends ie {
2308
+ constructor(e, t, s) {
2309
+ super(e.ast, t, s), this.data = e;
2310
+ }
2311
+ get block() {
2312
+ return this.data.ast;
2313
+ }
2314
+ get element() {
2315
+ return this.dom;
2316
+ }
2317
+ /**
2318
+ * Whether this already-built node can stand in for `data` unchanged. The
2319
+ * builder preserves view-data identity for any subtree whose ast and
2320
+ * selection-derived flags are unchanged (see `buildDocumentViewData`), so a
2321
+ * single identity check captures "nothing in my subtree changed" — and its
2322
+ * whole subtree, and any session it owns, are kept as-is.
2323
+ */
2324
+ canReuse(e) {
2325
+ return this.data === e;
2326
+ }
2327
+ /**
2328
+ * Called by the view after this block is mounted and measured, with the
2329
+ * block's rendered height in px. The default is a no-op; subclasses whose
2330
+ * active/inactive renderings have different intrinsic heights (e.g. a math
2331
+ * block) override this to remember a height to reserve across the toggle.
2332
+ */
2333
+ recordMeasuredHeight(e) {
2334
+ }
2335
+ }
2336
+ function re(n, e, t) {
2337
+ if (t instanceof M) {
2338
+ if (t.canReuse(n))
2339
+ return t;
2340
+ } else if (t?.ast === n.ast)
2341
+ return t;
2342
+ switch (n.kind) {
2343
+ case "text":
2344
+ return gs(n, S(t, He));
2345
+ case "marker":
2346
+ return n.ast.markerKind === "hardBreak" ? new lt(n, S(t, lt)) : new ct(n, S(t, ct));
2347
+ case "glue":
2348
+ return new be(n, S(t, be));
2349
+ case "heading":
2350
+ return new at(n, e, S(t, at));
2351
+ case "paragraph":
2352
+ return new F(n, "p", "md-block md-paragraph", e, j(t));
2353
+ case "codeBlock":
2354
+ return new We(n, e, t);
2355
+ case "mathBlock":
2356
+ return new dt(n, e, S(t, dt));
2357
+ case "thematicBreak":
2358
+ return new ht(n, e, S(t, ht));
2359
+ case "blockQuote":
2360
+ return new F(n, "blockquote", "md-block md-blockquote", e, j(t));
2361
+ case "list":
2362
+ return new ft(n, e, S(t, ft));
2363
+ case "listItem":
2364
+ return new gt(n, e, S(t, gt));
2365
+ case "table":
2366
+ return new F(n, "table", "md-block md-table", e, j(t));
2367
+ case "tableRow":
2368
+ return new pt(n, e, S(t, pt));
2369
+ case "tableCell":
2370
+ return new F(n, "td", "", e, j(t));
2371
+ case "strong":
2372
+ return new F(n, "strong", "", e, j(t));
2373
+ case "emphasis":
2374
+ return new F(n, "em", "", e, j(t));
2375
+ case "strikethrough":
2376
+ return new F(n, "del", "", e, j(t));
2377
+ case "inlineCode":
2378
+ return new mt(n, e, S(t, mt));
2379
+ case "inlineMath":
2380
+ return new _t(n, e, S(t, _t));
2381
+ case "link":
2382
+ return new wt(n, e, S(t, wt));
2383
+ case "image":
2384
+ return new kt(n, e, S(t, kt));
2385
+ case "document":
2386
+ return new F(n, "div", "", e, j(t));
2387
+ }
2388
+ }
2389
+ function S(n, e) {
2390
+ return n instanceof e ? n : void 0;
2391
+ }
2392
+ function j(n) {
2393
+ return n instanceof F ? n : void 0;
2394
+ }
2395
+ function W(n, e, t, s) {
2396
+ const { paired: i, unused: r } = qe(e, t ?? T), o = e.map((c) => s(c, i.get(c)));
2397
+ for (const c of r)
2398
+ c.dispose();
2399
+ return us(n, o), o;
2400
+ }
2401
+ function us(n, e) {
2402
+ ae(n, e.map((t) => t.mountNode));
2403
+ }
2404
+ function ae(n, e) {
2405
+ let t = n.firstChild, s = 0;
2406
+ for (; s < e.length && t === e[s]; s++)
2407
+ t = t.nextSibling;
2408
+ if (!(s === e.length && t === null)) {
2409
+ for (; s < e.length; s++) {
2410
+ const i = e[s];
2411
+ t === i ? t = i.nextSibling : n.insertBefore(i, t);
2412
+ }
2413
+ for (; t; ) {
2414
+ const i = t;
2415
+ t = t.nextSibling, n.removeChild(i);
2416
+ }
2417
+ }
2418
+ }
2419
+ function Y(n) {
2420
+ return (e, t) => re(e, n, t);
2421
+ }
2422
+ function Kt(n) {
2423
+ return (e, t) => e.kind === "marker" && e.ast.markerKind === "content" ? new He(e, document.createTextNode(e.ast.content)) : re(e, n, t);
2424
+ }
2425
+ const T = [];
2426
+ function ds(n) {
2427
+ return "content" in n ? n.content : fs;
2428
+ }
2429
+ const fs = [];
2430
+ function gs(n, e) {
2431
+ const t = n.ast.content, s = {
2432
+ leftBoundary: n.leftWordBoundary,
2433
+ rightBoundary: n.rightWordBoundary,
2434
+ decorateNewline: !0
2435
+ };
2436
+ if (n.showWhitespace && Wt(t, s)) {
2437
+ const o = document.createElement("span");
2438
+ o.className = "md-text";
2439
+ const c = Ke(o, t, s, n.ast);
2440
+ return new M(n, o, c);
2441
+ }
2442
+ const i = e?.dom, r = i instanceof globalThis.Text && i.data === t ? i : document.createTextNode(t);
2443
+ return new He(n, r);
2444
+ }
2445
+ function ot(n) {
2446
+ return n === " " || n === " " || n === `
2447
+ ` || n === "\r";
2448
+ }
2449
+ const ps = { leftBoundary: !1, rightBoundary: !1, decorateNewline: !1 };
2450
+ function ms(n, e, t) {
2451
+ const s = e > 0 ? !ot(n[e - 1]) : t.leftBoundary, i = e < n.length - 1 ? !ot(n[e + 1]) : t.rightBoundary;
2452
+ return s && i;
2453
+ }
2454
+ function Ht(n, e, t) {
2455
+ const s = n[e];
2456
+ if (s === " ")
2457
+ return "md-ws-tab";
2458
+ if (s === `
2459
+ ` || s === "\r")
2460
+ return t.decorateNewline ? "md-ws-newline" : void 0;
2461
+ if (s === " " && !ms(n, e, t))
2462
+ return "md-ws-space";
2463
+ }
2464
+ function Wt(n, e) {
2465
+ for (let t = 0; t < n.length; t++)
2466
+ if (Ht(n, t, e) !== void 0)
2467
+ return !0;
2468
+ return !1;
2469
+ }
2470
+ function _s(n, e) {
2471
+ const t = [];
2472
+ let s = -1;
2473
+ const i = (c) => {
2474
+ s >= 0 && (t.push({ text: n.slice(s, c), cls: void 0 }), s = -1);
2475
+ }, r = e.breakGlyphClass ? n.indexOf(`
2476
+ `) : -1, o = e.newlineGlyph ? n.lastIndexOf(`
2477
+ `) : -1;
2478
+ for (let c = 0; c < n.length; c++) {
2479
+ const l = n[c];
2480
+ if (e.newlineGlyph && (l === `
2481
+ ` || l === "\r")) {
2482
+ const h = c === r;
2483
+ if (c !== o || h) {
2484
+ i(c), t.push({ text: l, cls: h ? e.breakGlyphClass : "md-ws-newline-glyph", display: "↵" });
2485
+ continue;
2486
+ }
2487
+ }
2488
+ const a = e.newlineGlyph && (l === `
2489
+ ` || l === "\r") ? void 0 : Ht(n, c, e);
2490
+ if (a === void 0) {
2491
+ s < 0 && (s = c);
2492
+ continue;
2493
+ }
2494
+ i(c), t.push({ text: l, cls: a });
2495
+ }
2496
+ return i(n.length), t;
2497
+ }
2498
+ function Ke(n, e, t, s) {
2499
+ const i = [];
2500
+ for (const r of _s(e, t)) {
2501
+ const o = document.createTextNode(r.display ?? r.text);
2502
+ if (r.cls) {
2503
+ const c = document.createElement("span");
2504
+ c.className = r.cls, c.appendChild(o), n.appendChild(c);
2505
+ } else
2506
+ n.appendChild(o);
2507
+ i.push(new Q(s, o, T, r.text.length));
2508
+ }
2509
+ return i;
2510
+ }
2511
+ class He extends M {
2512
+ constructor(e, t) {
2513
+ super(e, t, T);
2514
+ }
2515
+ }
2516
+ class Q extends ie {
2517
+ _sourceLength;
2518
+ constructor(e, t, s = T, i = e.length) {
2519
+ super(e, t, s), this._sourceLength = i;
2520
+ }
2521
+ get sourceLength() {
2522
+ return this._sourceLength;
2523
+ }
2524
+ }
2525
+ class ct extends M {
2526
+ _span;
2527
+ constructor(e, t) {
2528
+ const s = e.ast, i = `md-marker md-marker-${s.markerKind}`, r = t && t.dom instanceof globalThis.Text && t.dom.data === s.content, o = r ? t._span : document.createElement("span");
2529
+ o.className = e.visible ? i : `${i} md-marker-hidden`;
2530
+ const c = r ? t.dom : document.createTextNode(s.content);
2531
+ r || o.appendChild(c), super(e, c, T), this._span = o;
2532
+ }
2533
+ get mountNode() {
2534
+ return this._span;
2535
+ }
2536
+ }
2537
+ class be extends M {
2538
+ _span;
2539
+ constructor(e, t) {
2540
+ const s = be._build(e, t);
2541
+ super(e, s.dom, s.children), this._span = s.span;
2542
+ }
2543
+ get mountNode() {
2544
+ return this._span;
2545
+ }
2546
+ static _build(e, t) {
2547
+ const s = e.ast, i = s.glueKind ? `md-glue md-glue-${s.glueKind}` : "md-glue", r = s.glueKind === "blockBreak", o = {
2548
+ leftBoundary: !1,
2549
+ rightBoundary: !1,
2550
+ decorateNewline: e.decorateNewline,
2551
+ // A blank-line `↵` glyph only belongs where the gap is hosted at the end
2552
+ // of a block's last line (inline flow, `decorateNewline`). The hostless
2553
+ // document-level gap (trailing a leaf thematic break, or leading the
2554
+ // first block) sits between block-level siblings where the break is
2555
+ // already visible, so it draws no glyph and simply collapses — making
2556
+ // e.g. `---\n\n…` and `---\n…` render identically.
2557
+ newlineGlyph: (s.glueKind === "blockGap" || r) && e.decorateNewline,
2558
+ breakGlyphClass: r ? "md-ws-blockbreak-glyph" : void 0
2559
+ };
2560
+ if (e.visible && Wt(s.content, o)) {
2561
+ const g = document.createElement("span");
2562
+ g.className = i;
2563
+ const u = Ke(g, s.content, o, s);
2564
+ return { span: g, dom: g, children: u };
2565
+ }
2566
+ const l = t && t.dom instanceof globalThis.Text && t.dom.data === s.content && t.children.length === 0, a = l ? t._span : document.createElement("span");
2567
+ a.className = e.visible ? i : `${i} md-glue-hidden`;
2568
+ const h = l ? t.dom : document.createTextNode(s.content);
2569
+ return l || a.appendChild(h), { span: a, dom: h, children: T };
2570
+ }
2571
+ }
2572
+ class lt extends M {
2573
+ _span;
2574
+ constructor(e, t) {
2575
+ const s = e.ast.content, i = document.createElement("span");
2576
+ i.className = "md-hardbreak";
2577
+ const r = document.createElement("span");
2578
+ r.className = e.visible ? "md-hardbreak-src" : "md-hardbreak-src md-hardbreak-src-hidden";
2579
+ const o = e.visible ? Ke(r, s, ps, e.ast) : ws(r, s, e.ast);
2580
+ i.appendChild(r), i.appendChild(document.createElement("br")), super(e, i, o), this._span = i;
2581
+ }
2582
+ get mountNode() {
2583
+ return this._span;
2584
+ }
2585
+ }
2586
+ function ws(n, e, t) {
2587
+ const s = document.createTextNode(e);
2588
+ return n.appendChild(s), [new Q(t, s)];
2589
+ }
2590
+ class F extends M {
2591
+ constructor(e, t, s, i, r) {
2592
+ const o = r?.element ?? document.createElement(t);
2593
+ !r && s && (o.className = s);
2594
+ const c = W(o, ds(e), r?.children, Y(i));
2595
+ super(e, o, c);
2596
+ }
2597
+ }
2598
+ class at extends M {
2599
+ constructor(e, t, s) {
2600
+ const i = s && s.element.tagName === `H${e.ast.level}`, r = i ? s.element : document.createElement(`h${e.ast.level}`);
2601
+ i || (r.className = "md-block md-heading");
2602
+ const o = W(r, e.content, s?.children, Y(t));
2603
+ super(e, r, o);
2604
+ }
2605
+ }
2606
+ class ht extends M {
2607
+ constructor(e, t, s) {
2608
+ if (e.showMarkup) {
2609
+ const r = s?.dom instanceof HTMLDivElement ? s : void 0, o = r?.element ?? document.createElement("div");
2610
+ r || (o.className = "md-block md-thematic-break-source");
2611
+ const c = W(o, e.content, r?.children, Y(t));
2612
+ super(e, o, c);
2613
+ return;
2614
+ }
2615
+ const i = document.createElement("hr");
2616
+ i.className = "md-block md-thematic-break", super(e, i, T);
2617
+ }
2618
+ }
2619
+ class We extends M {
2620
+ _session;
2621
+ /**
2622
+ * Subscription that re-tokenises the rendered `<code>` in place whenever the
2623
+ * session advances its {@link ISyntaxHighlighterDocument.snapshot} *without*
2624
+ * a source edit (an async grammar finishing, a live recolour). It is tied to
2625
+ * this node's lifetime, but like {@link _session} it must be disposed
2626
+ * manually: a node reused as `previous` for a rebuild is never `dispose`d
2627
+ * (see {@link reconcileDomChildren}), so the rebuilding constructor disposes
2628
+ * its predecessor's subscription explicitly.
2629
+ */
2630
+ _snapshotSub;
2631
+ constructor(e, t, s) {
2632
+ const i = e.ast, r = i.code?.content ?? "", o = !e.showMarkup && i.language && t?.renderCustomCodeBlock ? t.renderCustomCodeBlock(i.language, r) ?? void 0 : void 0, c = t?.syntaxHighlighter, l = s instanceof We ? s : void 0;
2633
+ let a;
2634
+ if (!o && c && i.language) {
2635
+ if (l?._session && i === l.ast)
2636
+ a = l._session, l._session = void 0;
2637
+ else if (l?._session) {
2638
+ const f = i.getDiff(l.ast);
2639
+ f && (a = l._session, l._session = void 0, ln((p) => a.update(f.stringEdit, p)));
2640
+ }
2641
+ a || (a = c.create(i.language, r));
2642
+ }
2643
+ l?._session && (l._session.dispose(), l._session = void 0), l?._snapshotSub?.dispose(), l && (l._snapshotSub = void 0);
2644
+ const h = a ? a.snapshot.get().getTokens(_.ofLength(r.length)).tokens : void 0;
2645
+ let g, u, d;
2646
+ if (o)
2647
+ o.classList.add("md-block", "md-code-block"), g = o, u = T;
2648
+ else {
2649
+ const f = document.createElement("pre");
2650
+ f.className = "md-block md-code-block";
2651
+ const p = [];
2652
+ for (const m of e.content)
2653
+ if (m.ast === i.code) {
2654
+ const w = document.createElement("code");
2655
+ i.language && (w.className = `language-${CSS.escape(i.language)}`);
2656
+ const v = ks(i.code, r, w, h);
2657
+ v instanceof qt && (d = v), p.push(v), f.appendChild(w);
2658
+ } else {
2659
+ const w = re(m, t, void 0);
2660
+ f.appendChild(w.mountNode), p.push(w);
2661
+ }
2662
+ g = f, u = p;
2663
+ }
2664
+ super(e, g, u), this._session = a, a && d && (this._snapshotSub = an(a.snapshot, () => {
2665
+ const f = a.snapshot.get();
2666
+ d.refresh(r, f.getTokens(_.ofLength(r.length)).tokens);
2667
+ }));
2668
+ }
2669
+ dispose() {
2670
+ this._snapshotSub?.dispose(), this._snapshotSub = void 0, this._session?.dispose(), this._session = void 0, super.dispose();
2671
+ }
2672
+ }
2673
+ function ks(n, e, t, s) {
2674
+ if (!s) {
2675
+ const i = document.createTextNode(e);
2676
+ return t.appendChild(i), new Q(n, i);
2677
+ }
2678
+ return new qt(n, t, e, s);
2679
+ }
2680
+ class qt extends ie {
2681
+ constructor(e, t, s, i) {
2682
+ super(e, t, ut(e, t, s, i));
2683
+ }
2684
+ /** Re-render the token spans for a new colouring, in place. */
2685
+ refresh(e, t) {
2686
+ const s = this.dom;
2687
+ s.replaceChildren(), this._replaceChildren(ut(this.ast, s, e, t));
2688
+ }
2689
+ }
2690
+ function ut(n, e, t, s) {
2691
+ const i = [];
2692
+ let r = 0;
2693
+ for (const o of s) {
2694
+ const c = t.slice(r, r + o.length);
2695
+ r += o.length;
2696
+ const l = document.createTextNode(c);
2697
+ if (o.className) {
2698
+ const a = document.createElement("span");
2699
+ for (const h of o.className.split("."))
2700
+ h && a.classList.add(`tok-${h}`);
2701
+ a.appendChild(l), e.appendChild(a);
2702
+ } else
2703
+ e.appendChild(l);
2704
+ i.push(new Q(n, l, T, c.length));
2705
+ }
2706
+ return i;
2707
+ }
2708
+ function jt(n) {
2709
+ let e = 0;
2710
+ for (const t of n) {
2711
+ if (t.kind === "marker" && t.markerKind === "content")
2712
+ return e;
2713
+ e += t.length;
2714
+ }
2715
+ return e;
2716
+ }
2717
+ function Gt(n, e, t) {
2718
+ const s = e.filter((c) => c.length > 0 && c.start >= 0 && c.start + c.length <= t).slice().sort((c, l) => c.start - l.start), i = [];
2719
+ let r = 0;
2720
+ const o = (c) => {
2721
+ c > 0 && i.push(new Q(n, document.createTextNode(""), T, c));
2722
+ };
2723
+ for (const c of s)
2724
+ c.start < r || (o(c.start - r), i.push(new Q(n, c.dom, T, c.length)), r = c.start + c.length);
2725
+ return o(t - r), i;
2726
+ }
2727
+ class dt extends M {
2728
+ /**
2729
+ * The rendered (inactive, KaTeX) height in px, measured after mount and
2730
+ * carried forward across rebuilds via `previous`. When the block becomes
2731
+ * active (source markers shown) this height is reserved as a `min-height`
2732
+ * so the editor does not collapse below the rendered size, keeping the
2733
+ * surrounding layout stable as the caret enters and leaves the block.
2734
+ */
2735
+ _renderedHeight;
2736
+ constructor(e, t, s) {
2737
+ const i = e.ast, r = s?._renderedHeight;
2738
+ if (e.showMarkup) {
2739
+ const a = document.createElement("pre");
2740
+ a.className = "md-block md-math-block", r !== void 0 && (a.style.boxSizing = "border-box", a.style.minHeight = `${r}px`);
2741
+ const h = [];
2742
+ for (const g of e.content)
2743
+ if (g.ast === i.code) {
2744
+ const u = document.createElement("code"), d = document.createTextNode(i.code.content);
2745
+ u.appendChild(d), a.appendChild(u), h.push(new Q(i.code, d));
2746
+ } else {
2747
+ const u = re(g, t, void 0);
2748
+ a.appendChild(u.mountNode), h.push(u);
2749
+ }
2750
+ super(e, a, h), this._renderedHeight = r;
2751
+ return;
2752
+ }
2753
+ const o = i.code?.content ?? "", c = t?.renderMath?.({
2754
+ latex: o,
2755
+ displayMode: !0,
2756
+ className: "md-block md-math-block",
2757
+ nodeLength: i.length,
2758
+ contentStart: jt(i.content)
2759
+ });
2760
+ if (c) {
2761
+ super(e, c.dom, Gt(i, c.segments, i.length)), this._renderedHeight = r;
2762
+ return;
2763
+ }
2764
+ const l = document.createElement("div");
2765
+ l.className = "md-block md-math-block";
2766
+ try {
2767
+ Nt.render(o, l, { displayMode: !0, throwOnError: !1 });
2768
+ } catch {
2769
+ l.textContent = o;
2770
+ }
2771
+ super(e, l, T), this._renderedHeight = r;
2772
+ }
2773
+ recordMeasuredHeight(e) {
2774
+ this.data.showMarkup || (this._renderedHeight = e);
2775
+ }
2776
+ }
2777
+ class ft extends M {
2778
+ constructor(e, t, s) {
2779
+ const i = e.ast.ordered ? "ol" : "ul", r = s && s.element.tagName === i.toUpperCase(), o = r ? s.element : document.createElement(i);
2780
+ r || (o.className = "md-block md-list");
2781
+ const c = W(o, e.content, s?.children, Y(t));
2782
+ super(e, o, c);
2783
+ }
2784
+ }
2785
+ class gt extends M {
2786
+ constructor(e, t, s) {
2787
+ const i = e.ast, r = document.createElement("li");
2788
+ e.isActive && r.classList.add("md-list-item-active"), i.checked !== void 0 && r.classList.add("md-task-list-item"), e.isActive || r.classList.add("md-markers-hidden"), r.style.setProperty("--md-list-level", String(e.level));
2789
+ const o = xs(r, e.content, s, t);
2790
+ if (!e.isActive && i.checked !== void 0) {
2791
+ const c = document.createElement("input");
2792
+ c.type = "checkbox", c.checked = i.checked, c.className = "md-checkbox";
2793
+ const l = t?.onToggleCheckbox;
2794
+ if (l) {
2795
+ const a = i.checked;
2796
+ c.addEventListener("mousedown", (h) => {
2797
+ h.stopPropagation(), h.preventDefault(), l(i, !a);
2798
+ });
2799
+ } else
2800
+ c.disabled = !0;
2801
+ r.insertBefore(c, r.firstChild);
2802
+ }
2803
+ super(e, r, o);
2804
+ }
2805
+ }
2806
+ function xs(n, e, t, s) {
2807
+ const { paired: i, unused: r } = qe(e, t?.children ?? T), o = e.map((h) => re(h, s, i.get(h)));
2808
+ for (const h of r)
2809
+ h.dispose();
2810
+ if (!(e.length >= 2 && e[0].kind === "glue" && e[0].ast.glueKind === "indent" && e[1].kind === "marker" && e[1].ast.markerKind === "listItemMarker"))
2811
+ return ae(n, o.map((h) => h.mountNode)), o;
2812
+ const l = n.firstElementChild, a = l && l.classList.contains("md-list-gutter") ? l : document.createElement("span");
2813
+ return a.className = "md-list-gutter", ae(a, [o[0].mountNode, o[1].mountNode]), ae(n, [a, ...o.slice(2).map((h) => h.mountNode)]), o;
2814
+ }
2815
+ class pt extends M {
2816
+ constructor(e, t, s) {
2817
+ const i = s?.element ?? document.createElement("tr");
2818
+ e.isDelimiter && i.classList.add("md-table-delimiter-row");
2819
+ const r = W(i, e.content, s?.children, Y(t));
2820
+ e.isDelimiter || e.content.forEach((o, c) => {
2821
+ o.kind === "tableCell" && o.isActive && r[c].element.classList.add("md-table-cell-active");
2822
+ }), super(e, i, r);
2823
+ }
2824
+ }
2825
+ class mt extends M {
2826
+ constructor(e, t, s) {
2827
+ const i = s?.element ?? document.createElement("code"), r = W(i, e.content, s?.children, Kt(t));
2828
+ super(e, i, r);
2829
+ }
2830
+ }
2831
+ class _t extends M {
2832
+ constructor(e, t, s) {
2833
+ if (e.showMarkup) {
2834
+ const l = document.createElement("span");
2835
+ l.className = "md-inline-math";
2836
+ const a = W(l, e.content, s?.children, Kt(t));
2837
+ super(e, l, a);
2838
+ return;
2839
+ }
2840
+ const r = e.content.find(
2841
+ (l) => l.kind === "marker" && l.ast.markerKind === "content"
2842
+ )?.ast.content ?? "", o = t?.renderMath?.({
2843
+ latex: r,
2844
+ displayMode: !1,
2845
+ className: "md-inline-math",
2846
+ nodeLength: e.ast.length,
2847
+ contentStart: jt(e.ast.content)
2848
+ });
2849
+ if (o) {
2850
+ super(e, o.dom, Gt(e.ast, o.segments, e.ast.length));
2851
+ return;
2852
+ }
2853
+ const c = document.createElement("span");
2854
+ c.className = "md-inline-math";
2855
+ try {
2856
+ Nt.render(r, c, { throwOnError: !1 });
2857
+ } catch {
2858
+ c.textContent = r;
2859
+ }
2860
+ super(e, c, T);
2861
+ }
2862
+ }
2863
+ class wt extends M {
2864
+ constructor(e, t, s) {
2865
+ const i = s?.element ?? document.createElement("a");
2866
+ zt(e.ast.url) ? (i.href = e.ast.url, i.dataset.mdUrl = e.ast.url) : (i.removeAttribute("href"), delete i.dataset.mdUrl), s || i.addEventListener("mousedown", (o) => {
2867
+ const c = i.dataset.mdUrl;
2868
+ !c || i.closest(".md-block-active") !== null && !(o.ctrlKey || o.metaKey) || (o.preventDefault(), o.stopPropagation(), t?.onOpenLink ? t.onOpenLink(c, o) : window.open(c, "_blank", "noopener"));
2869
+ });
2870
+ const r = W(i, e.content, s?.children, Y(t));
2871
+ super(e, i, r);
2872
+ }
2873
+ }
2874
+ class kt extends M {
2875
+ constructor(e, t, s) {
2876
+ const i = e.ast;
2877
+ if (e.showMarkup) {
2878
+ const o = document.createElement("span");
2879
+ o.className = "md-image-source";
2880
+ const c = W(o, e.content, s?.children, Y(t));
2881
+ super(e, o, c);
2882
+ return;
2883
+ }
2884
+ const r = document.createElement("img");
2885
+ zt(i.url) && (r.src = i.url), r.alt = i.alt, super(e, r, T);
2886
+ }
2887
+ }
2888
+ function qe(n, e) {
2889
+ const t = /* @__PURE__ */ new Map();
2890
+ for (const i of e)
2891
+ t.set(i.ast.id, i);
2892
+ const s = /* @__PURE__ */ new Map();
2893
+ for (const i of n) {
2894
+ const r = t.get(i.ast.id);
2895
+ r && (s.set(i, r), t.delete(i.ast.id));
2896
+ }
2897
+ return { paired: s, unused: [...t.values()] };
2898
+ }
2899
+ function zt(n) {
2900
+ return !n.trim().toLowerCase().startsWith("javascript:");
2901
+ }
2902
+ class je extends ie {
2903
+ constructor(e, t, s, i) {
2904
+ super(e, t, i), this.blocks = s;
2905
+ }
2906
+ static create(e, t, s) {
2907
+ const i = s?.contentDomNode ?? document.createElement("div"), r = new Map(
2908
+ e.children.filter((u) => u.kind === "block").map((u) => [u.view, u.isActive])
2909
+ ), o = s?.children, c = e.children.map((u) => u.view), { paired: l, unused: a } = qe(c, o ?? vs), h = c.map((u, d) => {
2910
+ const f = l.get(u), p = re(u, t, f);
2911
+ if (e.children[d].kind === "block") {
2912
+ const m = r.get(u);
2913
+ m !== void 0 && (p.element.classList.toggle("md-block-active", m), p.element.classList.toggle("md-markers-hidden", !m));
2914
+ }
2915
+ return p;
2916
+ });
2917
+ for (const u of a)
2918
+ u.dispose();
2919
+ ae(i, h.map((u) => u.mountNode));
2920
+ const g = [];
2921
+ return e.children.forEach((u, d) => {
2922
+ u.kind === "block" && g.push({ node: h[d], absoluteStart: u.absoluteStart });
2923
+ }), new je(e.ast, i, g, h);
2924
+ }
2925
+ /** The stable content element this document mounts its children into. */
2926
+ get contentDomNode() {
2927
+ return this.dom;
2928
+ }
2929
+ }
2930
+ const vs = [];
2931
+ function bs(n) {
2932
+ const e = document;
2933
+ if (e.caretPositionFromPoint) {
2934
+ const s = e.caretPositionFromPoint(n.x, n.y);
2935
+ return s ? { node: s.offsetNode, offset: s.offset } : void 0;
2936
+ }
2937
+ const t = e.caretRangeFromPoint?.(n.x, n.y);
2938
+ if (t)
2939
+ return { node: t.startContainer, offset: t.startOffset };
2940
+ }
2941
+ class ys {
2942
+ constructor(e, t, s) {
2943
+ this.ast = e, this.blocks = t, this.children = s;
2944
+ }
2945
+ kind = "document";
2946
+ }
2947
+ class Es {
2948
+ constructor(e, t) {
2949
+ this.ast = e, this.content = t;
2950
+ }
2951
+ kind = "heading";
2952
+ }
2953
+ class Cs {
2954
+ constructor(e, t) {
2955
+ this.ast = e, this.content = t;
2956
+ }
2957
+ kind = "paragraph";
2958
+ }
2959
+ class Os {
2960
+ constructor(e, t, s) {
2961
+ this.ast = e, this.showMarkup = t, this.content = s;
2962
+ }
2963
+ kind = "codeBlock";
2964
+ }
2965
+ class Ms {
2966
+ constructor(e, t, s) {
2967
+ this.ast = e, this.showMarkup = t, this.content = s;
2968
+ }
2969
+ kind = "mathBlock";
2970
+ }
2971
+ class Ts {
2972
+ constructor(e, t) {
2973
+ this.ast = e, this.content = t;
2974
+ }
2975
+ kind = "blockQuote";
2976
+ }
2977
+ class Ss {
2978
+ constructor(e, t) {
2979
+ this.ast = e, this.content = t;
2980
+ }
2981
+ kind = "list";
2982
+ }
2983
+ class Ls {
2984
+ constructor(e, t) {
2985
+ this.ast = e, this.content = t;
2986
+ }
2987
+ kind = "table";
2988
+ }
2989
+ class Ns {
2990
+ constructor(e, t) {
2991
+ this.ast = e, this.content = t;
2992
+ }
2993
+ kind = "strong";
2994
+ }
2995
+ class Is {
2996
+ constructor(e, t) {
2997
+ this.ast = e, this.content = t;
2998
+ }
2999
+ kind = "emphasis";
3000
+ }
3001
+ class Bs {
3002
+ constructor(e, t) {
3003
+ this.ast = e, this.content = t;
3004
+ }
3005
+ kind = "strikethrough";
3006
+ }
3007
+ class Rs {
3008
+ constructor(e, t) {
3009
+ this.ast = e, this.content = t;
3010
+ }
3011
+ kind = "inlineCode";
3012
+ }
3013
+ class Ds {
3014
+ constructor(e, t, s) {
3015
+ this.ast = e, this.showMarkup = t, this.content = s;
3016
+ }
3017
+ kind = "inlineMath";
3018
+ }
3019
+ class As {
3020
+ constructor(e, t) {
3021
+ this.ast = e, this.content = t;
3022
+ }
3023
+ kind = "link";
3024
+ }
3025
+ class $s {
3026
+ constructor(e, t, s) {
3027
+ this.ast = e, this.showMarkup = t, this.content = s;
3028
+ }
3029
+ kind = "image";
3030
+ }
3031
+ class Fs {
3032
+ constructor(e, t, s, i) {
3033
+ this.ast = e, this.isActive = t, this.content = s, this.level = i;
3034
+ }
3035
+ kind = "listItem";
3036
+ }
3037
+ class Vs {
3038
+ constructor(e, t, s) {
3039
+ this.ast = e, this.isDelimiter = t, this.content = s;
3040
+ }
3041
+ kind = "tableRow";
3042
+ }
3043
+ class Ps {
3044
+ constructor(e, t, s, i) {
3045
+ this.ast = e, this.isActive = t, this.showTableGlue = s, this.content = i;
3046
+ }
3047
+ kind = "tableCell";
3048
+ }
3049
+ class Ks {
3050
+ /**
3051
+ * Whether non-obvious whitespace in this text is revealed (block is active).
3052
+ * `leftWordBoundary`/`rightWordBoundary` say whether the inline sibling on
3053
+ * that side ends/starts with visible word content (e.g. inline code, a link,
3054
+ * emphasis); a single space touching such a sibling is obvious and stays
3055
+ * undecorated, just like a space between two words within this leaf.
3056
+ */
3057
+ constructor(e, t, s = !1, i = !1) {
3058
+ this.ast = e, this.showWhitespace = t, this.leftWordBoundary = s, this.rightWordBoundary = i;
3059
+ }
3060
+ kind = "text";
3061
+ }
3062
+ class Hs {
3063
+ constructor(e, t, s) {
3064
+ this.ast = e, this.showMarkup = t, this.content = s;
3065
+ }
3066
+ kind = "thematicBreak";
3067
+ }
3068
+ class Ws {
3069
+ constructor(e, t) {
3070
+ this.ast = e, this.visible = t;
3071
+ }
3072
+ kind = "marker";
3073
+ }
3074
+ class Xt {
3075
+ constructor(e, t, s) {
3076
+ this.ast = e, this.visible = t, this.decorateNewline = s;
3077
+ }
3078
+ kind = "glue";
3079
+ }
3080
+ const qs = { showMarkup: !1, showTableGlue: !1, selectionInNode: void 0 };
3081
+ function B(n) {
3082
+ return n.inlineFlow ? n : { ...n, inlineFlow: !0 };
3083
+ }
3084
+ function Qt(n, e, t, s) {
3085
+ const i = /* @__PURE__ */ new Map();
3086
+ if (s)
3087
+ for (const u of s.children)
3088
+ i.set(u.view.ast, u);
3089
+ const r = n.content, o = new Set(n.blocks), c = (u) => u >= 0 && u < r.length && o.has(r[u]), l = (u) => c(u) && e.has(r[u]), a = [], h = [];
3090
+ let g = 0;
3091
+ for (let u = 0; u < r.length; u++) {
3092
+ const d = r[u];
3093
+ if (o.has(d)) {
3094
+ const f = d, p = e.has(f), m = i.get(f);
3095
+ let w;
3096
+ if (!p)
3097
+ w = m && !m.isActive ? m.view : vt(f, qs, m?.view);
3098
+ else {
3099
+ const v = t ? new _(
3100
+ Math.max(0, t.start - g),
3101
+ Math.min(f.length, t.endExclusive - g)
3102
+ ) : void 0;
3103
+ w = vt(f, { showMarkup: !0, showTableGlue: !1, selectionInNode: v }, m?.view);
3104
+ }
3105
+ a.push({ ast: f, absoluteStart: g, isActive: p, view: w }), h.push({ absoluteStart: g, isActive: p, view: w, kind: "block" });
3106
+ } else if (d instanceof N && (d.glueKind === "blockGap" || d.glueKind === "blockBreak")) {
3107
+ const f = xt(c, l, r.length, u, -1), p = f || xt(c, l, r.length, u, 1), m = i.get(d), w = b(m?.view, new Xt(d, p, f));
3108
+ h.push({ absoluteStart: g, isActive: p, view: w, kind: "glue" });
3109
+ }
3110
+ g += d.length;
3111
+ }
3112
+ return new ys(n, a, h);
3113
+ }
3114
+ function xt(n, e, t, s, i) {
3115
+ for (let r = s + i; r >= 0 && r < t; r += i)
3116
+ if (n(r))
3117
+ return e(r);
3118
+ return !1;
3119
+ }
3120
+ function vt(n, e, t) {
3121
+ return oe(n, e, t);
3122
+ }
3123
+ function oe(n, e, t, s) {
3124
+ const i = n;
3125
+ switch (i.kind) {
3126
+ case "text":
3127
+ return b(t, new Ks(i, e.showMarkup, s?.left ?? !1, s?.right ?? !1));
3128
+ // The trailing gap the parser absorbs into the break is inline flow, so its
3129
+ // revealed newlines get the `↵` glyph (each its own width) — without it the
3130
+ // raw `\n`s are zero-width and the caret can't tell them apart.
3131
+ case "thematicBreak":
3132
+ return b(t, new Hs(i, e.showMarkup, L(i.children, B(e), t)));
3133
+ case "marker":
3134
+ return b(t, new Ws(i, zs(i.markerKind, e)));
3135
+ case "glue":
3136
+ return b(t, new Xt(i, Xs(i.glueKind, e), e.inlineFlow ?? !1));
3137
+ case "heading":
3138
+ return b(t, new Es(i, L(i.children, B(e), t)));
3139
+ case "paragraph":
3140
+ return b(t, new Cs(i, L(i.children, B(e), t)));
3141
+ case "codeBlock":
3142
+ return b(t, new Os(i, e.showMarkup, L(i.children, e, t)));
3143
+ case "mathBlock":
3144
+ return b(t, new Ms(i, e.showMarkup, L(i.children, e, t)));
3145
+ case "blockQuote":
3146
+ return b(t, new Ts(i, L(i.children, e, t)));
3147
+ case "list":
3148
+ return b(t, Qs(i, e, t));
3149
+ case "listItem":
3150
+ return b(t, Ut(i, e, t));
3151
+ case "table":
3152
+ return b(t, Us(i, e, t));
3153
+ case "tableRow":
3154
+ return b(t, Yt(i, e, !1, t));
3155
+ case "tableCell":
3156
+ return b(t, Jt(i, e.showMarkup, e.showTableGlue, e.selectionInNode, t));
3157
+ case "strong":
3158
+ return b(t, new Ns(i, L(i.children, B(e), t)));
3159
+ case "emphasis":
3160
+ return b(t, new Is(i, L(i.children, B(e), t)));
3161
+ case "strikethrough":
3162
+ return b(t, new Bs(i, L(i.children, B(e), t)));
3163
+ case "inlineCode":
3164
+ return b(t, new Rs(i, L(i.children, B(e), t)));
3165
+ case "inlineMath":
3166
+ return b(t, new Ds(i, e.showMarkup, L(i.children, B(e), t)));
3167
+ case "link":
3168
+ return b(t, new As(i, L(i.children, B(e), t)));
3169
+ case "image":
3170
+ return b(t, new $s(i, e.showMarkup, L(i.children, B(e), t)));
3171
+ case "document":
3172
+ return Qt(i, /* @__PURE__ */ new Set(), _.ofLength(0), void 0);
3173
+ }
3174
+ }
3175
+ function Ne(n) {
3176
+ return n.kind === "document" ? n.blocks.map((e) => e.view) : "content" in n ? n.content : js;
3177
+ }
3178
+ const js = [];
3179
+ function we(n) {
3180
+ if (!n)
3181
+ return;
3182
+ const e = /* @__PURE__ */ new Map();
3183
+ for (const t of Ne(n))
3184
+ e.set(t.ast, t);
3185
+ return e;
3186
+ }
3187
+ function b(n, e) {
3188
+ if (n && n.kind === e.kind && n.ast === e.ast && Gs(n, e)) {
3189
+ const t = Ne(n), s = Ne(e);
3190
+ if (t.length === s.length && t.every((i, r) => i === s[r]))
3191
+ return n;
3192
+ }
3193
+ return e;
3194
+ }
3195
+ function Gs(n, e) {
3196
+ switch (n.kind) {
3197
+ case "text":
3198
+ return n.showWhitespace === e.showWhitespace && n.leftWordBoundary === e.leftWordBoundary && n.rightWordBoundary === e.rightWordBoundary;
3199
+ case "marker":
3200
+ return n.visible === e.visible;
3201
+ case "glue":
3202
+ return n.visible === e.visible && n.decorateNewline === e.decorateNewline;
3203
+ case "thematicBreak":
3204
+ return n.showMarkup === e.showMarkup;
3205
+ case "codeBlock":
3206
+ return n.showMarkup === e.showMarkup;
3207
+ case "mathBlock":
3208
+ return n.showMarkup === e.showMarkup;
3209
+ case "inlineMath":
3210
+ return n.showMarkup === e.showMarkup;
3211
+ case "image":
3212
+ return n.showMarkup === e.showMarkup;
3213
+ case "listItem":
3214
+ return n.isActive === e.isActive;
3215
+ case "tableCell": {
3216
+ const t = e;
3217
+ return n.isActive === t.isActive && n.showTableGlue === t.showTableGlue;
3218
+ }
3219
+ case "tableRow":
3220
+ return n.isDelimiter === e.isDelimiter;
3221
+ default:
3222
+ return !0;
3223
+ }
3224
+ }
3225
+ function L(n, e, t) {
3226
+ const s = we(t);
3227
+ return n.map((i, r) => {
3228
+ const o = i.kind === "text" ? {
3229
+ left: r > 0 && bt(n[r - 1], "end"),
3230
+ right: r < n.length - 1 && bt(n[r + 1], "start")
3231
+ } : void 0;
3232
+ return oe(i, e, s?.get(i), o);
3233
+ });
3234
+ }
3235
+ function bt(n, e) {
3236
+ switch (n.kind) {
3237
+ case "text": {
3238
+ const t = n.content, s = e === "end" ? t[t.length - 1] : t[0];
3239
+ return s !== void 0 && s !== " " && s !== " " && s !== `
3240
+ ` && s !== "\r";
3241
+ }
3242
+ case "inlineCode":
3243
+ case "inlineMath":
3244
+ case "link":
3245
+ case "image":
3246
+ case "strong":
3247
+ case "emphasis":
3248
+ case "strikethrough":
3249
+ return !0;
3250
+ default:
3251
+ return !1;
3252
+ }
3253
+ }
3254
+ function zs(n, e) {
3255
+ return e.showMarkup || n === "tableCellGlue" && e.showTableGlue;
3256
+ }
3257
+ function Xs(n, e) {
3258
+ return e.showMarkup || n === "tableCellGlue" && e.showTableGlue;
3259
+ }
3260
+ function Qs(n, e, t) {
3261
+ const s = we(t), i = Ys(n, e), r = new Set(n.items), o = (e.listLevel ?? 0) + 1, c = [];
3262
+ let l = 0;
3263
+ for (const a of n.children) {
3264
+ if (r.has(a)) {
3265
+ const h = a, g = i.has(n.items.indexOf(h)), u = {
3266
+ showMarkup: g,
3267
+ showTableGlue: !1,
3268
+ selectionInNode: g && e.selectionInNode ? e.selectionInNode.delta(-l) : void 0,
3269
+ listLevel: o
3270
+ };
3271
+ c.push(b(s?.get(h), Ut(h, u, s?.get(h))));
3272
+ } else
3273
+ c.push(oe(a, e, s?.get(a)));
3274
+ l += a.length;
3275
+ }
3276
+ return new Ss(n, c);
3277
+ }
3278
+ function Ut(n, e, t) {
3279
+ const s = we(t), i = [];
3280
+ let r = 0;
3281
+ for (const o of n.children) {
3282
+ const c = e.selectionInNode ? { ...e, selectionInNode: e.selectionInNode.delta(-r) } : e;
3283
+ i.push(oe(o, c, s?.get(o))), r += o.length;
3284
+ }
3285
+ return new Fs(n, e.showMarkup, i, e.listLevel ?? 1);
3286
+ }
3287
+ function Us(n, e, t) {
3288
+ const s = we(t), i = e.showMarkup, r = n.delimiterRow, o = new Set(
3289
+ [n.headerRow, n.delimiterRow, ...n.bodyRows].filter((a) => a !== void 0)
3290
+ ), c = [];
3291
+ let l = 0;
3292
+ for (const a of n.children) {
3293
+ if (o.has(a)) {
3294
+ const h = a, g = h === r, u = {
3295
+ showMarkup: i,
3296
+ showTableGlue: i,
3297
+ selectionInNode: !g && i && e.selectionInNode ? e.selectionInNode.delta(-l) : void 0
3298
+ };
3299
+ c.push(b(s?.get(h), Yt(h, u, g, s?.get(h))));
3300
+ } else
3301
+ c.push(oe(a, e, s?.get(a)));
3302
+ l += a.length;
3303
+ }
3304
+ return new Ls(n, c);
3305
+ }
3306
+ function Yt(n, e, t, s) {
3307
+ const i = we(s), r = e.showMarkup, o = t ? void 0 : Js(n, e), c = new Set(n.cells), l = [];
3308
+ let a = 0;
3309
+ for (const h of n.children) {
3310
+ if (c.has(h)) {
3311
+ const g = h, u = t ? r : o.has(n.cells.indexOf(g)), d = u && e.selectionInNode ? e.selectionInNode.delta(-a) : void 0;
3312
+ l.push(Jt(g, u, r, d, i?.get(g)));
3313
+ } else
3314
+ l.push(oe(h, e, i?.get(h)));
3315
+ a += h.length;
3316
+ }
3317
+ return new Vs(n, t, l);
3318
+ }
3319
+ function Jt(n, e, t, s, i) {
3320
+ const r = { showMarkup: e, showTableGlue: t, selectionInNode: s };
3321
+ return new Ps(n, e, t, L(n.children, B(r), i));
3322
+ }
3323
+ const ye = /* @__PURE__ */ new Set();
3324
+ function Ys(n, e) {
3325
+ if (!e.showMarkup || !e.selectionInNode)
3326
+ return ye;
3327
+ const t = e.selectionInNode;
3328
+ if (t.isEmpty) {
3329
+ const r = Pt(n, t.start);
3330
+ return r === void 0 ? ye : /* @__PURE__ */ new Set([r]);
3331
+ }
3332
+ const s = /* @__PURE__ */ new Set();
3333
+ let i = 0;
3334
+ for (const r of n.children) {
3335
+ const o = n.items.indexOf(r);
3336
+ o >= 0 && i < t.endExclusive && i + r.length > t.start && s.add(o), i += r.length;
3337
+ }
3338
+ return s;
3339
+ }
3340
+ function Js(n, e) {
3341
+ if (!e.showMarkup || !e.selectionInNode)
3342
+ return ye;
3343
+ const t = e.selectionInNode;
3344
+ if (t.isEmpty) {
3345
+ const r = Zs(n, t.start);
3346
+ return r === void 0 ? ye : /* @__PURE__ */ new Set([r]);
3347
+ }
3348
+ const s = /* @__PURE__ */ new Set();
3349
+ let i = 0;
3350
+ for (const r of n.children) {
3351
+ const o = n.cells.indexOf(r);
3352
+ o >= 0 && i < t.endExclusive && i + r.length > t.start && s.add(o), i += r.length;
3353
+ }
3354
+ return s;
3355
+ }
3356
+ function Zs(n, e) {
3357
+ let t = 0;
3358
+ for (const s of n.children) {
3359
+ const i = t + s.length, r = n.cells.indexOf(s);
3360
+ if (r >= 0 && (t <= e && e < i || i === e))
3361
+ return r;
3362
+ t = i;
3363
+ }
3364
+ }
3365
+ class ei extends te {
3366
+ constructor(e, t) {
3367
+ super(), this._parent = e, this.element = document.createElement("div"), this.element.className = "md-cursor", this.rendering = A(this, (s) => {
3368
+ const i = s.readObservable(t.offset), r = s.readObservable(t.visualLineMap);
3369
+ if (i === void 0 || r.isEmpty)
3370
+ return this.element.style.display = "none", new yt(i ?? 0, !1, R.EMPTY);
3371
+ const o = r.lineIndexOfOffset(i), c = r.lineRect(o).withZeroWidthAt(r.xAtOffset(i)), l = this._parent.getBoundingClientRect(), a = c.translate(-l.left, -l.top);
3372
+ return this.element.style.left = `${a.x}px`, this.element.style.top = `${a.y}px`, this.element.style.height = `${a.height}px`, this.element.style.display = "", new yt(i, !0, a);
3373
+ }), this._register(Z((s) => {
3374
+ s.readObservable(this.rendering);
3375
+ })), this._register(Z((s) => {
3376
+ s.readObservable(t.offset), this.element.style.animation = "none", this.element.offsetWidth, this.element.style.animation = "";
3377
+ }));
3378
+ }
3379
+ element;
3380
+ rendering;
3381
+ }
3382
+ class yt {
3383
+ constructor(e, t, s) {
3384
+ this.offset = e, this.visible = t, this.rect = s;
3385
+ }
3386
+ }
3387
+ class ti extends te {
3388
+ constructor(e, t) {
3389
+ super(), this._parent = e, this.element = document.createElementNS("http://www.w3.org/2000/svg", "svg"), this.element.setAttribute("class", "md-selection-layer"), this._path = document.createElementNS("http://www.w3.org/2000/svg", "path"), this._path.setAttribute("class", "md-selection-path"), this.element.appendChild(this._path), this.rendering = A(this, (s) => {
3390
+ const i = s.readObservable(t.selection), r = s.readObservable(t.visualLineMap), o = s.readObservable(t.blocks);
3391
+ return ni(this._path, this._parent, i, r, o);
3392
+ }), this._register(Z((s) => {
3393
+ s.readObservable(this.rendering);
3394
+ }));
3395
+ }
3396
+ element;
3397
+ rendering;
3398
+ _path;
3399
+ }
3400
+ class Et {
3401
+ constructor(e) {
3402
+ this.rects = e;
3403
+ }
3404
+ }
3405
+ function ni(n, e, t, s, i) {
3406
+ if (!t || t.isCollapsed)
3407
+ return n.setAttribute("d", ""), new Et([]);
3408
+ const r = t.range, o = e.getBoundingClientRect(), c = [];
3409
+ for (const d of s.lines) {
3410
+ const f = Zt(d);
3411
+ if (!f || !r.intersects(f))
3412
+ continue;
3413
+ const p = i.find((m) => _.ofStartAndLength(m.absoluteStart, m.block.length).containsRange(f));
3414
+ c.push({ line: d, lineRange: f, block: p });
3415
+ }
3416
+ const l = [], a = [];
3417
+ for (let d = 0; d < c.length; d++) {
3418
+ const { line: f, lineRange: p } = c[d], m = d === 0, w = d === c.length - 1, v = m ? f.xAtOffset(Math.max(r.start, p.start)) : f.rect.left, I = w ? f.xAtOffset(Math.min(r.endExclusive, p.endExclusive)) : f.rect.right;
3419
+ l.push({
3420
+ x: v - o.left,
3421
+ y: f.rect.top - o.top,
3422
+ width: Math.max(0, I - v),
3423
+ height: f.rect.height
3424
+ }), a.push({ left: v, right: I, top: f.rect.top, bottom: f.rect.top + f.rect.height });
3425
+ }
3426
+ for (let d = 0; d < a.length - 1; d++) {
3427
+ const f = a[d], p = a[d + 1];
3428
+ if (p.top <= f.bottom)
3429
+ continue;
3430
+ const m = Math.max(f.left, p.left), w = Math.min(f.right, p.right);
3431
+ w <= m || l.push({
3432
+ x: m - o.left,
3433
+ y: f.bottom - o.top,
3434
+ width: w - m,
3435
+ height: p.top - f.bottom
3436
+ });
3437
+ }
3438
+ const h = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Map();
3439
+ for (let d = 0; d < c.length; d++) {
3440
+ const f = c[d].block;
3441
+ f && (h.has(f) || h.set(f, d), g.set(f, d));
3442
+ }
3443
+ const u = i.map(
3444
+ (d) => r.intersects(_.ofStartAndLength(d.absoluteStart, d.block.length))
3445
+ );
3446
+ for (let d = 0; d < i.length - 1; d++) {
3447
+ const f = i[d], p = i[d + 1], m = f.absoluteStart + f.block.length, w = p.absoluteStart;
3448
+ if (m < w && !r.containsRange(_.fromTo(m, w)) || m >= w && !(u[d] && u[d + 1]))
3449
+ continue;
3450
+ const v = f.element.getBoundingClientRect(), I = p.element.getBoundingClientRect(), X = si(s, p, I.top), q = v.bottom;
3451
+ if (X <= q)
3452
+ continue;
3453
+ const ce = g.get(f), Ge = h.get(p), ze = ce !== void 0 ? a[ce] : { left: v.left, right: v.right }, Xe = Ge !== void 0 ? a[Ge] : { left: I.left, right: I.right }, Ce = Math.max(ze.left, Xe.left), Qe = Math.min(ze.right, Xe.right);
3454
+ Qe <= Ce || l.push({
3455
+ x: Ce - o.left,
3456
+ y: q - o.top,
3457
+ width: Qe - Ce,
3458
+ height: X - q
3459
+ });
3460
+ }
3461
+ for (const d of i) {
3462
+ const f = _.ofStartAndLength(d.absoluteStart, d.block.length);
3463
+ if (!r.intersects(f))
3464
+ continue;
3465
+ let p = !1;
3466
+ for (const { lineRange: w } of c)
3467
+ if (w.intersects(f)) {
3468
+ p = !0;
3469
+ break;
3470
+ }
3471
+ if (p)
3472
+ continue;
3473
+ const m = d.element.getBoundingClientRect();
3474
+ l.push({
3475
+ x: m.left - o.left,
3476
+ y: m.top - o.top,
3477
+ width: m.width,
3478
+ height: m.height
3479
+ });
3480
+ }
3481
+ return n.setAttribute("d", ii(l, 4)), new Et(l);
3482
+ }
3483
+ function Zt(n) {
3484
+ if (n.runs.length === 0)
3485
+ return;
3486
+ let e = 1 / 0, t = -1 / 0;
3487
+ for (const s of n.runs)
3488
+ s.sourceStart < e && (e = s.sourceStart), s.sourceEndExclusive > t && (t = s.sourceEndExclusive);
3489
+ return _.fromTo(e, t);
3490
+ }
3491
+ function si(n, e, t) {
3492
+ const s = _.ofStartAndLength(e.absoluteStart, e.block.length);
3493
+ for (const i of n.lines) {
3494
+ const r = Zt(i);
3495
+ if (r && s.containsRange(r))
3496
+ return i.rect.top;
3497
+ }
3498
+ return t;
3499
+ }
3500
+ function ii(n, e) {
3501
+ if (n.length === 0)
3502
+ return "";
3503
+ const t = n.slice().sort((r, o) => r.y - o.y || r.x - o.x), s = [];
3504
+ let i = [];
3505
+ for (const r of t) {
3506
+ const o = i[i.length - 1], c = o !== void 0 && r.y <= o.y + o.height + 0.5, l = o !== void 0 && Math.max(o.x, r.x) < Math.min(o.x + o.width, r.x + r.width);
3507
+ c && l ? i.push(r) : (i.length > 0 && s.push(i), i = [r]);
3508
+ }
3509
+ return i.length > 0 && s.push(i), s.map((r) => ri(r, e)).filter(Boolean).join(" ");
3510
+ }
3511
+ function ri(n, e) {
3512
+ const t = [];
3513
+ t.push({ x: n[0].x + n[0].width, y: n[0].y, convex: !0 });
3514
+ for (let i = 0; i < n.length - 1; i++) {
3515
+ const r = n[i], o = n[i + 1], c = r.x + r.width, l = o.x + o.width;
3516
+ if (Math.abs(c - l) > 0.5) {
3517
+ const a = r.y + r.height, h = c > l;
3518
+ t.push({ x: c, y: a, convex: h }), t.push({ x: l, y: a, convex: !h });
3519
+ }
3520
+ }
3521
+ const s = n[n.length - 1];
3522
+ t.push({ x: s.x + s.width, y: s.y + s.height, convex: !0 }), t.push({ x: s.x, y: s.y + s.height, convex: !0 });
3523
+ for (let i = n.length - 1; i > 0; i--) {
3524
+ const r = n[i], o = n[i - 1];
3525
+ if (Math.abs(r.x - o.x) > 0.5) {
3526
+ const c = r.y, l = r.x < o.x;
3527
+ t.push({ x: r.x, y: c, convex: l }), t.push({ x: o.x, y: c, convex: !l });
3528
+ }
3529
+ }
3530
+ return t.push({ x: n[0].x, y: n[0].y, convex: !0 }), oi(t, e);
3531
+ }
3532
+ function oi(n, e) {
3533
+ const t = n.length;
3534
+ if (t < 3)
3535
+ return "";
3536
+ const s = new Array(t);
3537
+ for (let o = 0; o < t; o++) {
3538
+ const c = n[(o + t - 1) % t], l = n[(o + 1) % t], a = xe(n[o], c), h = xe(n[o], l);
3539
+ s[o] = Math.min(e, a / 2, h / 2);
3540
+ }
3541
+ const i = Se(n[t - 1], n[0], xe(n[t - 1], n[0]) - s[0]), r = [`M${D(i.x)},${D(i.y)}`];
3542
+ for (let o = 0; o < t; o++) {
3543
+ const c = n[o], l = n[(o + 1) % t], a = s[o], h = Se(c, l, a);
3544
+ a > 0 ? r.push(`A${D(a)},${D(a)} 0 0 ${c.convex ? 1 : 0} ${D(h.x)},${D(h.y)}`) : r.push(`L${D(h.x)},${D(h.y)}`);
3545
+ const g = s[(o + 1) % t], u = Se(c, l, xe(c, l) - g);
3546
+ r.push(`L${D(u.x)},${D(u.y)}`);
3547
+ }
3548
+ return r.push("Z"), r.join(" ");
3549
+ }
3550
+ function xe(n, e) {
3551
+ return Math.hypot(n.x - e.x, n.y - e.y);
3552
+ }
3553
+ function Se(n, e, t) {
3554
+ const s = e.x - n.x, i = e.y - n.y, r = Math.hypot(s, i);
3555
+ if (r === 0)
3556
+ return { x: n.x, y: n.y };
3557
+ const o = t / r;
3558
+ return { x: n.x + s * o, y: n.y + i * o };
3559
+ }
3560
+ function D(n) {
3561
+ return n.toFixed(2);
3562
+ }
3563
+ class Ii extends te {
3564
+ constructor(e, t) {
3565
+ super(), this._model = e, this._options = t, this.element = document.createElement("div"), this.element.className = "md-editor", this._options?.classNames && this.element.classList.add(...this._options.classNames), this.element.tabIndex = 0, this.measuredLayout = new Pn(), this._selectionView = this._register(new ti(this.element, {
3566
+ selection: this._model.selection,
3567
+ visualLineMap: this.measuredLayout.visualLineMap,
3568
+ blocks: this._selectionBlocksObs
3569
+ })), this.element.appendChild(this._selectionView.element), this._cursorView = this._register(new ei(this.element, {
3570
+ offset: this._model.cursorOffset,
3571
+ visualLineMap: this.measuredLayout.visualLineMap
3572
+ })), this.element.appendChild(this._cursorView.element), this.editContext = new EditContext({
3573
+ text: this._model.sourceText.get().value,
3574
+ selectionStart: 0,
3575
+ selectionEnd: 0
3576
+ }), this.element.editContext = this.editContext, this._register(Z(this._renderAutorun)), this._register(Z((s) => {
3577
+ const i = s.readObservable(this._model.selection)?.range;
3578
+ this.editContext.updateSelection(i?.start ?? 0, i?.endExclusive ?? 0);
3579
+ })), this._register({ dispose: () => {
3580
+ this._document.get()?.dispose();
3581
+ } });
3582
+ }
3583
+ element;
3584
+ editContext;
3585
+ measuredLayout;
3586
+ _cursorView;
3587
+ _selectionView;
3588
+ /**
3589
+ * The mounted block sequence, in source order. Rebuilt (not mutated) each
3590
+ * frame by {@link DocumentViewNode.create}; the view just swaps one
3591
+ * immutable node for the next. Never used for source-of-truth lookups
3592
+ * (those go through the measured-layout model).
3593
+ */
3594
+ _document = G(this, void 0);
3595
+ /** The current view-node tree (AST overlaid with rendered DOM), for debugging. */
3596
+ get documentViewNode() {
3597
+ return this._document;
3598
+ }
3599
+ /**
3600
+ * Last frame's view-data overlay, threaded back into
3601
+ * {@link buildDocumentViewData} so any subtree whose ast and selection flags
3602
+ * are unchanged keeps its view-data object — which lets the renderer reuse
3603
+ * its DOM by identity.
3604
+ */
3605
+ _previousViewData;
3606
+ /** The current view-data tree (AST overlaid with selection flags), for debugging. */
3607
+ _viewData = G(this, void 0);
3608
+ get viewData() {
3609
+ return this._viewData;
3610
+ }
3611
+ /**
3612
+ * The block cache projected for views (selection painting) that need to
3613
+ * react to mount/unmount. Derived from {@link _document}, so it stays in
3614
+ * lock-step without any manual bookkeeping.
3615
+ */
3616
+ _selectionBlocksObs = A(this, (e) => {
3617
+ const t = this._document.read(e);
3618
+ return t ? t.blocks.map((s) => ({
3619
+ block: s.node.block,
3620
+ absoluteStart: s.absoluteStart,
3621
+ viewNode: s.node,
3622
+ element: s.node.element
3623
+ })) : [];
3624
+ });
3625
+ focus() {
3626
+ this.element.focus({ preventScroll: !0 });
3627
+ }
3628
+ /**
3629
+ * Client coordinates → absolute source offset (any block). Used during
3630
+ * drag to keep extending the selection even when the pointer leaves the
3631
+ * original block.
3632
+ */
3633
+ resolveOffsetFromPoint(e) {
3634
+ const t = bs(e);
3635
+ if (t)
3636
+ return this._document.get()?.resolveSource(t);
3637
+ }
3638
+ /**
3639
+ * Whether a client point falls on the rendered document content, as
3640
+ * opposed to the surrounding editor padding (the green area). Uses DOM
3641
+ * containment rather than the content node's bounding box so that markers
3642
+ * which overflow into the padding (e.g. a heading's `##`, which renders in
3643
+ * the left margin) still count as content. Overlays (cursor, selection)
3644
+ * have `pointer-events: none`, so the hit-test sees through them.
3645
+ */
3646
+ isPointInContent(e) {
3647
+ const t = this._document.get()?.contentDomNode;
3648
+ if (!t)
3649
+ return !1;
3650
+ const s = document.elementFromPoint(e.x, e.y);
3651
+ return s !== null && t.contains(s);
3652
+ }
3653
+ // ----- render autorun ------------------------------------------------
3654
+ _renderAutorun = (e) => {
3655
+ const t = e.readObservable(this._model.document), s = e.readObservable(this._model.sourceText).value, i = e.readObservable(this._model.activeBlocks), r = e.readObservable(this._model.selection);
3656
+ this.editContext.text !== s && this.editContext.updateText(0, this.editContext.text.length, s);
3657
+ const o = this._document.get(), c = Qt(t, i, r?.range, this._previousViewData);
3658
+ this._previousViewData = c, this._viewData.set(c, void 0);
3659
+ const l = je.create(c, this._options, o);
3660
+ if (o) {
3661
+ if (l.contentDomNode !== o.contentDomNode)
3662
+ throw new Error("DocumentViewNode.contentDomNode must be stable across rebuilds");
3663
+ } else
3664
+ this.element.insertBefore(l.contentDomNode, this.element.firstChild);
3665
+ this._document.set(l, void 0), this._publishMeasurements(l);
3666
+ };
3667
+ /** Current mounted blocks, or empty before the first render. */
3668
+ get _blocks() {
3669
+ return this._document.get()?.blocks ?? [];
3670
+ }
3671
+ /**
3672
+ * Measure each mounted block's rect and per-block visual line map, then
3673
+ * publish the result into the {@link MeasuredLayoutModel}. The model
3674
+ * is not read here, so there is no feedback loop into the render autorun.
3675
+ */
3676
+ _publishMeasurements(e) {
3677
+ const t = [];
3678
+ for (const s of e.blocks) {
3679
+ const i = s.node.element.getBoundingClientRect();
3680
+ s.node.recordMeasuredHeight(i.height);
3681
+ const r = _e.measure([{
3682
+ absoluteStart: s.absoluteStart,
3683
+ viewNode: s.node
3684
+ }]);
3685
+ t.push({
3686
+ block: s.node.block,
3687
+ absoluteStart: s.absoluteStart,
3688
+ height: i.height,
3689
+ isMeasured: !0,
3690
+ visualLineMap: r,
3691
+ viewNode: s.node
3692
+ });
3693
+ }
3694
+ this.measuredLayout.measurements.set(t, void 0);
3695
+ }
3696
+ }
3697
+ class Bi extends te {
3698
+ constructor(e, t) {
3699
+ super(), this._model = e, this._view = t;
3700
+ const s = this._view.element;
3701
+ s.addEventListener("mousedown", this._handleMouseDown), s.addEventListener("keydown", this._handleKeyDown), s.addEventListener("copy", this._handleCopy), s.addEventListener("cut", this._handleCut), s.addEventListener("paste", this._handlePaste), this._register({
3702
+ dispose: () => {
3703
+ s.removeEventListener("mousedown", this._handleMouseDown), s.removeEventListener("keydown", this._handleKeyDown), s.removeEventListener("copy", this._handleCopy), s.removeEventListener("cut", this._handleCut), s.removeEventListener("paste", this._handlePaste);
3704
+ }
3705
+ });
3706
+ const i = this._view.editContext;
3707
+ i.addEventListener("textupdate", this._handleTextUpdate), this._register({
3708
+ dispose: () => i.removeEventListener("textupdate", this._handleTextUpdate)
3709
+ });
3710
+ }
3711
+ _desiredColumn;
3712
+ _handleTextUpdate = (e) => {
3713
+ const t = y.replace(
3714
+ new _(e.updateRangeStart, e.updateRangeEnd),
3715
+ e.text
3716
+ );
3717
+ this._model.applyEdit(t);
3718
+ };
3719
+ _handleMouseDown = (e) => {
3720
+ e.preventDefault(), this._view.focus(), this._desiredColumn = void 0;
3721
+ const t = new ee(e.clientX, e.clientY);
3722
+ if (!this._view.isPointInContent(t)) {
3723
+ this._model.selection.set(void 0, void 0);
3724
+ return;
3725
+ }
3726
+ const s = this._view.resolveOffsetFromPoint(t) ?? this._model.sourceText.get().value.length;
3727
+ if (e.detail === 2) {
3728
+ const o = this._makeCursorContext();
3729
+ this._model.selection.set(ls(o, s), void 0);
3730
+ return;
3731
+ }
3732
+ if (e.detail === 3) {
3733
+ const o = ci(this._model.document.get(), s);
3734
+ o && this._model.selection.set(as(this._makeCursorContext(), o), void 0);
3735
+ return;
3736
+ }
3737
+ if (e.shiftKey) {
3738
+ const o = this._model.selection.get() ?? x.collapsed(s);
3739
+ this._model.selection.set(o.withActive(s), void 0);
3740
+ } else
3741
+ this._model.selection.set(x.collapsed(s), void 0);
3742
+ const i = (o) => {
3743
+ const c = this._model.selection.get() ?? x.collapsed(s), l = this._view.resolveOffsetFromPoint(new ee(o.clientX, o.clientY)) ?? c.active;
3744
+ this._model.selection.set(new x(c.anchor, l), void 0);
3745
+ }, r = () => {
3746
+ document.removeEventListener("mousemove", i), document.removeEventListener("mouseup", r);
3747
+ };
3748
+ document.addEventListener("mousemove", i), document.addEventListener("mouseup", r);
3749
+ };
3750
+ _makeCursorContext() {
3751
+ return {
3752
+ text: this._model.sourceText.get().value,
3753
+ selection: this._model.selection.get() ?? x.collapsed(0),
3754
+ document: this._model.document.get(),
3755
+ activeBlock: this._model.activeBlock.get()
3756
+ };
3757
+ }
3758
+ _makeVisualCursorContext() {
3759
+ return {
3760
+ ...this._makeCursorContext(),
3761
+ desiredColumn: this._desiredColumn,
3762
+ lineMap: this._view.measuredLayout.visualLineMap.get()
3763
+ };
3764
+ }
3765
+ _executeCursorCommand(e, t) {
3766
+ const s = this._makeCursorContext(), i = e(s), r = s.selection;
3767
+ this._model.selection.set(
3768
+ t ? r.withActive(i) : x.collapsed(i),
3769
+ void 0
3770
+ ), this._desiredColumn = void 0;
3771
+ }
3772
+ _executeEditCommand(e) {
3773
+ const t = this._makeCursorContext(), s = e(t);
3774
+ s && (this._model.applyEdit(s.edit), this._desiredColumn = void 0);
3775
+ }
3776
+ _executeVisualCursorCommand(e, t) {
3777
+ const s = this._makeVisualCursorContext(), i = e(s);
3778
+ this._model.selection.set(
3779
+ t ? s.selection.withActive(i.offset) : x.collapsed(i.offset),
3780
+ void 0
3781
+ ), this._desiredColumn = i.desiredColumn;
3782
+ }
3783
+ /** Move the cursor down one visual line (Arrow Down). */
3784
+ cursorDown(e = !1) {
3785
+ this._executeVisualCursorCommand(nt, e);
3786
+ }
3787
+ /** Move the cursor up one visual line (Arrow Up). */
3788
+ cursorUp(e = !1) {
3789
+ this._executeVisualCursorCommand(st, e);
3790
+ }
3791
+ _handleCopy = (e) => {
3792
+ const t = this._selectedText();
3793
+ t !== void 0 && (e.preventDefault(), e.clipboardData?.setData("text/plain", t));
3794
+ };
3795
+ _handleCut = (e) => {
3796
+ const t = this._selectedText();
3797
+ t !== void 0 && (e.preventDefault(), e.clipboardData?.setData("text/plain", t), this._executeEditCommand(it));
3798
+ };
3799
+ _handlePaste = (e) => {
3800
+ e.preventDefault();
3801
+ const t = e.clipboardData?.getData("text/plain");
3802
+ t && this._executeEditCommand(ss(t));
3803
+ };
3804
+ _selectedText() {
3805
+ const e = this._model.selection.get();
3806
+ if (!(!e || e.isCollapsed))
3807
+ return this._model.sourceText.get().value.slice(e.range.start, e.range.endExclusive);
3808
+ }
3809
+ _handleKeyDown = (e) => {
3810
+ const t = e.ctrlKey || e.metaKey;
3811
+ if (e.key === "ArrowLeft")
3812
+ e.preventDefault(), t ? this._executeCursorCommand((e.shiftKey, tt), e.shiftKey) : e.shiftKey ? this._executeCursorCommand(Xn, !0) : this._executeCursorCommand(Gn, !1);
3813
+ else if (e.key === "ArrowRight")
3814
+ e.preventDefault(), t ? this._executeCursorCommand(Qn, e.shiftKey) : e.shiftKey ? this._executeCursorCommand(zn, !0) : this._executeCursorCommand(jn, !1);
3815
+ else if (e.key === "ArrowUp")
3816
+ e.preventDefault(), this._executeVisualCursorCommand(st, e.shiftKey);
3817
+ else if (e.key === "ArrowDown")
3818
+ e.preventDefault(), this._executeVisualCursorCommand(nt, e.shiftKey);
3819
+ else if (e.key === "Home")
3820
+ e.preventDefault(), t ? this._executeCursorCommand(Jn, e.shiftKey) : this._executeCursorCommand(Un, e.shiftKey);
3821
+ else if (e.key === "End")
3822
+ e.preventDefault(), t ? this._executeCursorCommand(Zn, e.shiftKey) : this._executeCursorCommand(Yn, e.shiftKey);
3823
+ else if (e.key === "a" && t) {
3824
+ e.preventDefault();
3825
+ const s = this._makeCursorContext();
3826
+ this._model.selection.set(cs(s), void 0);
3827
+ } else e.key === "Backspace" ? (e.preventDefault(), this._executeEditCommand(t ? ts : it)) : e.key === "Delete" ? (e.preventDefault(), this._executeEditCommand(t ? ns : es)) : e.key === "Enter" && (e.preventDefault(), this._executeEditCommand(t ? is : e.shiftKey ? os : rs));
3828
+ };
3829
+ }
3830
+ function ci(n, e) {
3831
+ let t = 0;
3832
+ for (const s of n.children) {
3833
+ if (n.blocks.includes(s)) {
3834
+ const i = _.ofStartAndLength(t, s.length);
3835
+ if (i.contains(e) || i.endExclusive === e)
3836
+ return i;
3837
+ }
3838
+ t += s.length;
3839
+ }
3840
+ }
3841
+ const Ct = "md-debug-show-line-rects";
3842
+ function li(n, e) {
3843
+ try {
3844
+ const t = localStorage.getItem(n);
3845
+ return t === null ? e : t === "true";
3846
+ } catch {
3847
+ return e;
3848
+ }
3849
+ }
3850
+ function ai(n, e) {
3851
+ try {
3852
+ localStorage.setItem(n, String(e));
3853
+ } catch {
3854
+ }
3855
+ }
3856
+ class Ri extends te {
3857
+ constructor(e, t) {
3858
+ super(), this._overlayParent = e, this.overlayElement = document.createElement("div"), this.overlayElement.className = "md-debug-layout-overlay", Object.assign(this.overlayElement.style, {
3859
+ position: "absolute",
3860
+ top: "0",
3861
+ left: "0",
3862
+ right: "0",
3863
+ bottom: "0",
3864
+ pointerEvents: "none"
3865
+ }), this.infoElement = document.createElement("div"), this.infoElement.className = "md-debug-layout-info", Object.assign(this.infoElement.style, {
3866
+ fontFamily: "monospace",
3867
+ fontSize: "11px",
3868
+ padding: "8px 12px",
3869
+ background: "#111",
3870
+ color: "#fff",
3871
+ borderRadius: "4px",
3872
+ lineHeight: "1.4"
3873
+ });
3874
+ const s = document.createElement("label");
3875
+ Object.assign(s.style, {
3876
+ display: "flex",
3877
+ alignItems: "center",
3878
+ gap: "6px",
3879
+ marginBottom: "6px",
3880
+ cursor: "pointer",
3881
+ userSelect: "none"
3882
+ });
3883
+ const i = document.createElement("input");
3884
+ i.type = "checkbox", i.checked = this._showLineRects.get(), i.addEventListener("change", () => {
3885
+ this._showLineRects.set(i.checked, void 0), ai(Ct, i.checked);
3886
+ }), s.appendChild(i), s.appendChild(document.createTextNode("show line rects")), this.infoElement.appendChild(s);
3887
+ const r = document.createElement("pre");
3888
+ r.style.margin = "0", r.style.whiteSpace = "pre", this.infoElement.appendChild(r), this.rendering = A(this, (o) => {
3889
+ const c = o.readObservable(t.model.measurements), l = o.readObservable(this._showLineRects);
3890
+ return ui(this.overlayElement, r, this._overlayParent, c, l, t.colorForOffset, t.hoveredOffset);
3891
+ }), this.mappedOffsets = A(this, (o) => o.readObservable(this.rendering).mappedOffsets), this._register(Z((o) => {
3892
+ o.readObservable(this.rendering);
3893
+ const c = t.hoveredOffset ? o.readObservable(t.hoveredOffset) : void 0;
3894
+ t.hoveredOffset && fi(this.overlayElement, c);
3895
+ }));
3896
+ }
3897
+ overlayElement;
3898
+ infoElement;
3899
+ rendering;
3900
+ /** Absolute source offsets that map to a rendered DOM character. */
3901
+ mappedOffsets;
3902
+ /** Whether the dashed line-bands and run boxes are drawn (persisted). */
3903
+ _showLineRects = G(this, li(Ct, !0));
3904
+ }
3905
+ class hi {
3906
+ constructor(e, t, s, i) {
3907
+ this.blockCount = e, this.mountedCount = t, this.lineCount = s, this.mappedOffsets = i;
3908
+ }
3909
+ }
3910
+ function ui(n, e, t, s, i, r, o) {
3911
+ n.textContent = "";
3912
+ const c = t.getBoundingClientRect();
3913
+ let l = 0, a = 0, h = 0;
3914
+ const g = /* @__PURE__ */ new Set();
3915
+ for (let f = 0; f < s.length; f++) {
3916
+ const p = s[f], m = p.visualLineMap?.lines ?? [];
3917
+ if (p.isMeasured && l++, a += m.length, i)
3918
+ for (let w = 0; w < m.length; w++) {
3919
+ const v = m[w], I = v.rect, X = document.createElement("div");
3920
+ Object.assign(X.style, {
3921
+ position: "absolute",
3922
+ left: "0",
3923
+ right: "0",
3924
+ top: `${I.y - c.top}px`,
3925
+ height: `${I.height}px`,
3926
+ border: "1px dashed rgba(180, 0, 200, 0.45)",
3927
+ boxSizing: "border-box"
3928
+ }), n.appendChild(X);
3929
+ for (const q of v.runs) {
3930
+ const ce = document.createElement("div");
3931
+ Object.assign(ce.style, {
3932
+ position: "absolute",
3933
+ left: `${q.rect.x - c.left}px`,
3934
+ top: `${q.rect.y - c.top}px`,
3935
+ width: `${q.rect.width}px`,
3936
+ height: `${q.rect.height}px`,
3937
+ outline: "1px solid rgba(255, 100, 0, 0.45)",
3938
+ boxSizing: "border-box"
3939
+ }), n.appendChild(ce);
3940
+ }
3941
+ }
3942
+ p.viewNode && (h += di(n, c, p.viewNode, p.absoluteStart, g, r, o));
3943
+ }
3944
+ const u = `blocks: ${s.length} mounted: ${l} lines: ${a} chars: ${h}`, d = s.map((f, p) => {
3945
+ const m = f.isMeasured ? "M" : "e", w = f.visualLineMap?.lines.length ?? 0;
3946
+ return `${String(p).padStart(2)} ${m} start=${String(f.absoluteStart).padStart(4)} h=${f.height.toFixed(1).padStart(6)} lines=${w} kind=${f.block.kind}`;
3947
+ });
3948
+ return e.textContent = [u, ...d].join(`
3949
+ `), new hi(s.length, l, a, g);
3950
+ }
3951
+ function di(n, e, t, s, i, r, o) {
3952
+ let c = 0;
3953
+ const l = document.createRange(), a = s + t.sourceLength, h = [];
3954
+ t.forEachTextLeaf(s, (u, d) => {
3955
+ h.push({ start: d, end: d + u.sourceLength });
3956
+ });
3957
+ const g = (u) => h.some((d) => u >= d.start && u < d.end);
3958
+ for (let u = s; u < a; u++) {
3959
+ if (!g(u))
3960
+ continue;
3961
+ const d = t.sourceToDom(u + 1, s);
3962
+ if (!d || d.offset < 1)
3963
+ continue;
3964
+ l.setStart(d.node, d.offset - 1), l.setEnd(d.node, d.offset);
3965
+ const f = l.getBoundingClientRect();
3966
+ if (f.height === 0)
3967
+ continue;
3968
+ const p = f.width === 0 ? 2 : f.width, m = document.createElement("div");
3969
+ Object.assign(m.style, {
3970
+ position: "absolute",
3971
+ left: `${f.x - e.left}px`,
3972
+ top: `${f.y - e.top}px`,
3973
+ width: `${p}px`,
3974
+ height: `${f.height}px`,
3975
+ background: r?.(u) ?? "rgba(0, 120, 220, 0.30)",
3976
+ boxSizing: "border-box",
3977
+ pointerEvents: "auto"
3978
+ });
3979
+ const w = d.node.data?.[d.offset - 1] ?? "";
3980
+ m.title = `offset ${u}: ${JSON.stringify(w)}`, m.dataset.offset = String(u), o ? (m.addEventListener("mouseenter", () => o.set(u, void 0)), m.addEventListener("mouseleave", () => o.set(void 0, void 0))) : (m.addEventListener("mouseenter", () => Ot(n, m, !0)), m.addEventListener("mouseleave", () => Ot(n, m, !1))), n.appendChild(m), i.add(u), c++;
3981
+ }
3982
+ return c;
3983
+ }
3984
+ function fi(n, e) {
3985
+ for (const t of Array.from(n.children)) {
3986
+ if (!t.style)
3987
+ continue;
3988
+ const s = t.dataset.offset, i = s !== void 0 && Number(s) === e;
3989
+ t.style.visibility = e === void 0 || i ? "" : "hidden";
3990
+ }
3991
+ }
3992
+ function Ot(n, e, t) {
3993
+ for (const s of Array.from(n.children))
3994
+ s.style.visibility = t && s !== e ? "hidden" : "";
3995
+ }
3996
+ class de {
3997
+ constructor(e, t) {
3998
+ this.length = e, this.className = t;
3999
+ }
4000
+ }
4001
+ class gi {
4002
+ /**
4003
+ * @param _monaco The Monarch runtime ({@link IMonarchApi}), injected so this
4004
+ * package depends on `monaco-editor` for types only.
4005
+ * @param _grammars Maps a language id to its Monarch language definition.
4006
+ */
4007
+ constructor(e, t) {
4008
+ this._monaco = e, this._grammars = t;
4009
+ }
4010
+ _tokenizers = /* @__PURE__ */ new Map();
4011
+ create(e, t) {
4012
+ return new pi(this._tokenizerFor(e), t);
4013
+ }
4014
+ dispose() {
4015
+ for (const e of this._tokenizers.values())
4016
+ e.dispose();
4017
+ this._tokenizers.clear();
4018
+ }
4019
+ _tokenizerFor(e) {
4020
+ const t = this._grammars.get(e);
4021
+ if (t === void 0)
4022
+ return;
4023
+ let s = this._tokenizers.get(e);
4024
+ return s || (s = new this._monaco.MonarchTokenizer(ki, xi, e, this._monaco.compile(e, t), vi), this._tokenizers.set(e, s)), s;
4025
+ }
4026
+ }
4027
+ class pi {
4028
+ constructor(e, t) {
4029
+ this._tokenizer = e, this._initialState = e?.getInitialState(), this._text = t, this._lines = this._tokenizeFrom(0, [], this._initialState, t.split(`
4030
+ `)), this._lineStarts = this._computeLineStarts(), this._snapshotObs = G("syntaxSnapshot", new Mt(this, this._version));
4031
+ }
4032
+ _text;
4033
+ _lines;
4034
+ /** Source offset of each line's first char; `length === _lines.length`. */
4035
+ _lineStarts;
4036
+ _version = 1;
4037
+ _disposed = !1;
4038
+ _initialState;
4039
+ _snapshotObs;
4040
+ get snapshot() {
4041
+ return this._snapshotObs;
4042
+ }
4043
+ update(e, t) {
4044
+ if (this._disposed)
4045
+ throw new Error("document is disposed");
4046
+ if (e.isEmpty)
4047
+ return;
4048
+ const s = Tt(this._lines), i = e.apply(this._text), r = e.replacements[0].replaceRange.start, o = this._lineIndexAt(r), c = this._lines.slice(0, o), l = o === 0 ? this._initialState : this._lines[o - 1].endState;
4049
+ this._text = i, this._lines = this._tokenizeFrom(o, c, l, i.split(`
4050
+ `)), this._lineStarts = this._computeLineStarts(), this._version++;
4051
+ const a = wi(s, Tt(this._lines));
4052
+ this._snapshotObs.set(new Mt(this, this._version), t, a);
4053
+ }
4054
+ dispose() {
4055
+ this._disposed = !0;
4056
+ }
4057
+ /**
4058
+ * Tokenize `allLines[fromLine..]`, keeping `reusedPrefix` (already tokenized
4059
+ * lines `[0, fromLine)`) verbatim. `startState` is the state entering
4060
+ * `fromLine`.
4061
+ */
4062
+ _tokenizeFrom(e, t, s, i) {
4063
+ const r = t.slice();
4064
+ let o = s;
4065
+ for (let c = e; c < i.length; c++) {
4066
+ const l = i[c], a = c < i.length - 1;
4067
+ if (this._tokenizer && o) {
4068
+ const h = this._tokenizer.tokenize(l, a, o);
4069
+ r.push({ text: l, tokens: _i(h.tokens, l.length), endState: h.endState }), o = h.endState;
4070
+ } else
4071
+ r.push({ text: l, tokens: l.length === 0 ? [] : [new de(l.length, void 0)], endState: o });
4072
+ }
4073
+ return r;
4074
+ }
4075
+ _computeLineStarts() {
4076
+ const e = [];
4077
+ let t = 0;
4078
+ for (const s of this._lines)
4079
+ e.push(t), t += s.text.length + 1;
4080
+ return e;
4081
+ }
4082
+ _lineIndexAt(e) {
4083
+ for (let t = this._lineStarts.length - 1; t >= 0; t--)
4084
+ if (e >= this._lineStarts[t])
4085
+ return t;
4086
+ return 0;
4087
+ }
4088
+ /** @internal Called by {@link MonacoHighlightedSnapshot}. */
4089
+ _getTokens(e, t) {
4090
+ if (e !== this._version)
4091
+ throw new Error("stale snapshot");
4092
+ const s = this._text.length, i = Math.max(0, Math.min(t.start, s)), r = Math.max(i, Math.min(t.endExclusive, s)), o = [];
4093
+ let c = i, l = i, a = !1, h = 0;
4094
+ const g = (u, d) => {
4095
+ const f = h, p = h + u;
4096
+ h = p, u !== 0 && f < r && p > i && (a || (c = f, a = !0), o.push(new de(u, d)), l = p);
4097
+ };
4098
+ for (let u = 0; u < this._lines.length && h < r; u++) {
4099
+ for (const d of this._lines[u].tokens)
4100
+ g(d.length, d.className);
4101
+ u < this._lines.length - 1 && g(1, void 0);
4102
+ }
4103
+ return { range: new _(c, a ? l : i), tokens: o };
4104
+ }
4105
+ }
4106
+ class Mt {
4107
+ constructor(e, t) {
4108
+ this._doc = e, this._version = t;
4109
+ }
4110
+ getTokens(e) {
4111
+ return this._doc._getTokens(this._version, e);
4112
+ }
4113
+ }
4114
+ function mi(n) {
4115
+ return n === "" ? void 0 : n;
4116
+ }
4117
+ function _i(n, e) {
4118
+ if (n.length === 0)
4119
+ return e === 0 ? [] : [new de(e, void 0)];
4120
+ const t = [];
4121
+ for (let s = 0; s < n.length; s++) {
4122
+ const i = n[s].offset, r = s + 1 < n.length ? n[s + 1].offset : e;
4123
+ r > i && t.push(new de(r - i, mi(n[s].type)));
4124
+ }
4125
+ return t;
4126
+ }
4127
+ function Tt(n) {
4128
+ const e = [];
4129
+ for (let t = 0; t < n.length; t++) {
4130
+ for (const s of n[t].tokens)
4131
+ e.push(s);
4132
+ t < n.length - 1 && e.push(new de(1, void 0));
4133
+ }
4134
+ return e;
4135
+ }
4136
+ function St(n, e) {
4137
+ return n.length === e.length && n.className === e.className;
4138
+ }
4139
+ function Lt(n) {
4140
+ let e = 0;
4141
+ for (const t of n)
4142
+ e += t.length;
4143
+ return e;
4144
+ }
4145
+ function wi(n, e) {
4146
+ const t = n.length, s = e.length;
4147
+ let i = 0, r = 0;
4148
+ for (; i < t && i < s && St(n[i], e[i]); )
4149
+ r += n[i].length, i++;
4150
+ let o = 0, c = 0;
4151
+ for (; o < t - i && o < s - i && St(n[t - 1 - o], e[s - 1 - o]); )
4152
+ c += n[t - 1 - o].length, o++;
4153
+ const l = Lt(n), a = Lt(e), h = new _(r, l - c), g = a - c - r;
4154
+ return h.isEmpty && g === 0 ? J.empty : J.replace(h, g);
4155
+ }
4156
+ const ki = {
4157
+ languageIdCodec: { encodeLanguageId: () => 0, decodeLanguageId: () => "" },
4158
+ isRegisteredLanguageId: () => !1,
4159
+ getLanguageIdByLanguageName: () => null,
4160
+ getLanguageIdByMimeType: () => null,
4161
+ requestBasicLanguageFeatures: () => {
4162
+ }
4163
+ }, xi = {
4164
+ getColorTheme: () => ({ tokenTheme: {} })
4165
+ }, vi = {
4166
+ getValue: () => 2e4,
4167
+ onDidChangeConfiguration: () => ({ dispose() {
4168
+ } })
4169
+ };
4170
+ function Di(n, e) {
4171
+ const t = /* @__PURE__ */ new Map([
4172
+ ["typescript", e.typescript],
4173
+ ["ts", e.typescript],
4174
+ ["javascript", e.javascript],
4175
+ ["js", e.javascript],
4176
+ ["css", e.css],
4177
+ ["html", e.html],
4178
+ ["python", e.python],
4179
+ ["py", e.python],
4180
+ ["rust", e.rust],
4181
+ ["rs", e.rust],
4182
+ ["shell", e.shell],
4183
+ ["sh", e.shell],
4184
+ ["bash", e.shell]
4185
+ ]);
4186
+ return new gi(n, t);
4187
+ }
4188
+ export {
4189
+ E as AstNode,
4190
+ pe as BlockQuoteAstNode,
4191
+ M as BlockViewNode,
4192
+ K as CodeBlockAstNode,
4193
+ We as CodeBlockViewNode,
4194
+ ei as CursorView,
4195
+ yt as CursorViewRendering,
4196
+ Fe as DocumentAstNode,
4197
+ je as DocumentViewNode,
4198
+ Bi as EditorController,
4199
+ Ni as EditorModel,
4200
+ Ii as EditorView,
4201
+ Re as EmphasisAstNode,
4202
+ N as GlueAstNode,
4203
+ U as HeadingAstNode,
4204
+ ge as ImageAstNode,
4205
+ Ae as InlineCodeAstNode,
4206
+ $e as InlineMathAstNode,
4207
+ J as LengthEdit,
4208
+ Ie as LengthReplacement,
4209
+ fe as LinkAstNode,
4210
+ H as ListAstNode,
4211
+ $ as ListItemAstNode,
4212
+ On as MarkdownParser,
4213
+ k as MarkerAstNode,
4214
+ se as MathBlockAstNode,
4215
+ hi as MeasuredLayoutDebugRendering,
4216
+ Ri as MeasuredLayoutDebugView,
4217
+ Pn as MeasuredLayoutModel,
4218
+ gi as MonacoSyntaxHighlighter,
4219
+ _ as OffsetRange,
4220
+ z as ParagraphAstNode,
4221
+ ee as Point2D,
4222
+ R as Rect2D,
4223
+ x as Selection,
4224
+ ti as SelectionView,
4225
+ Et as SelectionViewRendering,
4226
+ De as StrikethroughAstNode,
4227
+ y as StringEdit,
4228
+ V as StringReplacement,
4229
+ Oe as StringValue,
4230
+ Be as StrongAstNode,
4231
+ me as TableAstNode,
4232
+ ue as TableCellAstNode,
4233
+ he as TableRowAstNode,
4234
+ ve as TextAstNode,
4235
+ ne as ThematicBreakAstNode,
4236
+ de as Token,
4237
+ ie as ViewNode,
4238
+ Bn as VisualLine,
4239
+ _e as VisualLineMap,
4240
+ Le as VisualRun,
4241
+ Di as createDefaultMonacoSyntaxHighlighter,
4242
+ Zn as cursorDocumentEnd,
4243
+ Jn as cursorDocumentStart,
4244
+ nt as cursorDown,
4245
+ Gn as cursorLeft,
4246
+ Yn as cursorLineEnd,
4247
+ Un as cursorLineStart,
4248
+ Xn as cursorMoveLeft,
4249
+ zn as cursorMoveRight,
4250
+ jn as cursorRight,
4251
+ st as cursorUp,
4252
+ tt as cursorWordLeft,
4253
+ Qn as cursorWordRight,
4254
+ it as deleteLeft,
4255
+ es as deleteRight,
4256
+ ts as deleteWordLeft,
4257
+ ns as deleteWordRight,
4258
+ fn as findNodeOffsetById,
4259
+ hn as findWordAt,
4260
+ It as findWordBoundaryLeft,
4261
+ Bt as findWordBoundaryRight,
4262
+ Mn as getAnnotatedSource,
4263
+ os as insertHardLineBreak,
4264
+ rs as insertLineBreak,
4265
+ is as insertParagraph,
4266
+ ss as insertText,
4267
+ Vt as nextCursorPosition,
4268
+ cs as selectAll,
4269
+ as as selectBlock,
4270
+ ls as selectWord,
4271
+ Si as taskCheckboxRange,
4272
+ Li as visualizeAst
4273
+ };
4274
+ //# sourceMappingURL=index.js.map