@quartz-community/remark-obsidian 2.1.1 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -33,8 +33,8 @@ var require_format = __commonJS({
33
33
  if (typeof module !== "undefined") {
34
34
  namespace = module.exports = format;
35
35
  } else {
36
- namespace = (function() {
37
- return this || (1, eval)("this");
36
+ namespace = /* @__PURE__ */ (function() {
37
+ return this;
38
38
  })();
39
39
  }
40
40
  namespace.format = format;
@@ -2480,11 +2480,11 @@ function toResult(value2) {
2480
2480
  return value2 === null || value2 === void 0 ? empty : [value2];
2481
2481
  }
2482
2482
 
2483
- // node_modules/unist-util-visit/lib/index.js
2484
- function visit(tree, testOrVisitor, visitorOrReverse, maybeReverse) {
2485
- let reverse;
2483
+ // src/lib/visit.ts
2484
+ var visit = function(tree, testOrVisitor, visitorOrReverse, maybeReverse) {
2486
2485
  let test;
2487
2486
  let visitor;
2487
+ let reverse;
2488
2488
  if (typeof testOrVisitor === "function" && typeof visitorOrReverse !== "function") {
2489
2489
  test = void 0;
2490
2490
  visitor = testOrVisitor;
@@ -2494,151 +2494,784 @@ function visit(tree, testOrVisitor, visitorOrReverse, maybeReverse) {
2494
2494
  visitor = visitorOrReverse;
2495
2495
  reverse = maybeReverse;
2496
2496
  }
2497
- visitParents(tree, test, overload, reverse);
2498
- function overload(node2, parents) {
2499
- const parent = parents[parents.length - 1];
2500
- const index = parent ? parent.children.indexOf(node2) : void 0;
2501
- return visitor(node2, index, parent);
2502
- }
2503
- }
2497
+ const is2 = convert(test);
2498
+ let seat;
2499
+ visitParents(
2500
+ tree,
2501
+ (node2, index, parent) => {
2502
+ seat = index;
2503
+ return is2(node2, index, parent);
2504
+ },
2505
+ (node2, parents) => visitor(node2, seat, parents[parents.length - 1]),
2506
+ reverse
2507
+ );
2508
+ };
2504
2509
 
2505
- // src/lib/math-display.ts
2506
- function mathDisplayTransform(tree, source) {
2507
- visit(tree, "inlineMath", (node2) => {
2508
- const math3 = node2;
2509
- const start = math3.position?.start?.offset;
2510
- const end = math3.position?.end?.offset;
2511
- if (start === void 0 || end === void 0) return;
2512
- const opening = /^\$+/.exec(source.slice(start, end));
2513
- if (!opening || opening[0].length < 2) return;
2514
- math3.data ??= {};
2515
- math3.data.display = true;
2516
- math3.data.hProperties ??= {};
2517
- math3.data.hProperties.className = ["language-math", "math-display"];
2510
+ // src/lib/byte-order-mark.ts
2511
+ function countByteOrderMark(tree) {
2512
+ const moved = /* @__PURE__ */ new Set();
2513
+ const move = (point) => {
2514
+ if (moved.has(point)) return;
2515
+ moved.add(point);
2516
+ if (point.offset !== void 0) point.offset += 1;
2517
+ if (point.line === 1) point.column += 1;
2518
+ };
2519
+ if (tree.position) {
2520
+ tree.position = {
2521
+ start: { ...tree.position.start },
2522
+ end: { ...tree.position.end }
2523
+ };
2524
+ move(tree.position.end);
2525
+ }
2526
+ visit(tree, (node2) => {
2527
+ const data = node2.data;
2528
+ if (node2.type === "callout" && data) {
2529
+ if (data.marker) {
2530
+ move(data.marker.start);
2531
+ move(data.marker.end);
2532
+ }
2533
+ if (data.lineEnd) move(data.lineEnd);
2534
+ }
2535
+ if (node2 === tree || !node2.position) return;
2536
+ move(node2.position.start);
2537
+ move(node2.position.end);
2518
2538
  });
2539
+ const offsets = tree.data?.lineOffsets;
2540
+ if (offsets?.[1] !== void 0) offsets[1] += 1;
2519
2541
  }
2520
2542
 
2521
- // src/lib/wikilink-target.ts
2522
- function resolveTarget(target) {
2523
- return target.replace(/\u00a0/gu, " ").normalize("NFC");
2543
+ // src/lib/line-map.ts
2544
+ var QUOTE_PREFIX = /^[ \t]*>[ \t]?/u;
2545
+ var LEADING_QUOTES = /^(?:[ \t]*>[ \t]?)*/u;
2546
+ function mapLines(source, start, end, startLine, quoteDepth2) {
2547
+ const raw = source.slice(start, end).split("\n");
2548
+ const text3 = [];
2549
+ const starts = [];
2550
+ const lines = [];
2551
+ let offset = start;
2552
+ const lineStart = source.lastIndexOf("\n", start - 1) + 1;
2553
+ const ownPrefix = LEADING_QUOTES.exec(source.slice(lineStart, start))?.[0] ?? "";
2554
+ const depth = quoteDepth2 ?? Math.max(1, (ownPrefix.match(/>/gu) ?? []).length);
2555
+ for (let index = 0; index < raw.length; index++) {
2556
+ const line = raw[index];
2557
+ let prefix2 = "";
2558
+ if (index > 0) {
2559
+ for (let level = 0; level < depth; level++) {
2560
+ const next = QUOTE_PREFIX.exec(line.slice(prefix2.length))?.[0];
2561
+ if (!next) break;
2562
+ prefix2 += next;
2563
+ }
2564
+ }
2565
+ starts.push(offset + prefix2.length);
2566
+ lines.push(startLine + index);
2567
+ text3.push(line.slice(prefix2.length));
2568
+ offset += line.length + 1;
2569
+ }
2570
+ return { text: text3.join("\n"), starts, lines };
2524
2571
  }
2525
- function spelledTarget(path, heading, resolved) {
2526
- const spelled = {};
2527
- if (path !== resolved.path) spelled.spelledPath = path;
2528
- if (heading !== resolved.heading) spelled.spelledHeading = heading;
2529
- return Object.keys(spelled).length > 0 ? spelled : void 0;
2572
+ function shiftByMap(node2, map, from) {
2573
+ const pending = [node2];
2574
+ while (pending.length > 0) {
2575
+ const candidate = pending.pop();
2576
+ if (candidate.position) {
2577
+ for (const point of [candidate.position.start, candidate.position.end]) {
2578
+ const index = from + point.line - 1;
2579
+ const lineStart = map.starts[index];
2580
+ const line = map.lines[index];
2581
+ if (lineStart !== void 0 && line !== void 0) {
2582
+ point.offset = lineStart + point.column - 1;
2583
+ point.line = line;
2584
+ }
2585
+ }
2586
+ }
2587
+ const children = candidate.children ?? [];
2588
+ for (let at3 = children.length - 1; at3 >= 0; at3--) {
2589
+ pending.push(children[at3]);
2590
+ }
2591
+ }
2530
2592
  }
2531
- function writtenPart(part, spelled, strippedBackslash) {
2532
- const resolved = strippedBackslash ? `${part}\\` : part;
2533
- return spelled !== void 0 && resolveTarget(spelled) === resolved ? spelled : resolved;
2593
+ function paragraphSlice(slice) {
2594
+ return slice.replace(/(\r?\n)[ \t]*(?:>[ \t]?)*[ \t]*/gu, "$1");
2534
2595
  }
2535
2596
 
2536
- // src/lib/syntax/legacy-scan.ts
2537
- function codeToCharacter(code) {
2538
- if (code === -5 || code === -4 || code === -3) return "\n";
2539
- if (code === -2) return " ";
2540
- if (code === -1) return " ";
2541
- return String.fromCodePoint(code);
2597
+ // src/lib/callout.ts
2598
+ var MARKER = /^\[!([^\]\r\n]+)\]([-+]?)(?=[ \t\r\n]|$)[ \t]?/;
2599
+ var QUOTE_MARKER = /^[ \t]{0,3}>/u;
2600
+ var LIST_MARKER = /^[ \t]{0,3}(?:[-*+]|\d{1,9}[.)])[ \t]+/u;
2601
+ var LABEL_MARKER = /^[ \t]{0,3}\[\^[^\]\s]+\]:[ \t]*/u;
2602
+ var AT_MARKER = /^ ?\[!/u;
2603
+ function atMarkerColumn(line) {
2604
+ let rest = line;
2605
+ for (; ; ) {
2606
+ const quote = QUOTE_MARKER.exec(rest);
2607
+ if (quote) {
2608
+ rest = rest.slice(quote[0].length);
2609
+ continue;
2610
+ }
2611
+ const list3 = LIST_MARKER.exec(rest) ?? LABEL_MARKER.exec(rest);
2612
+ if (!list3) break;
2613
+ rest = rest.slice(list3[0].length);
2614
+ }
2615
+ return AT_MARKER.test(rest);
2542
2616
  }
2543
- function createChunkScan(sink, stopAfter) {
2544
- return {
2545
- partial: true,
2546
- tokenize(effects, ok3) {
2547
- return function enter(code) {
2548
- effects.enter("legacyChunkScan");
2549
- return step2(code);
2550
- };
2551
- function step2(code) {
2552
- if (code === null) {
2553
- effects.exit("legacyChunkScan");
2554
- return ok3(code);
2555
- }
2556
- const character = codeToCharacter(code);
2557
- sink.push(character);
2558
- effects.consume(code);
2559
- if (stopAfter?.(character) === true) {
2560
- effects.exit("legacyChunkScan");
2561
- return ok3;
2562
- }
2563
- return step2;
2564
- }
2617
+ var LINE_PREFIX = /^[ \t]*(?:>[ \t]?)*/u;
2618
+ function shiftOnto(node2, offset, line) {
2619
+ const candidate = node2;
2620
+ if (candidate.position) {
2621
+ for (const point of [candidate.position.start, candidate.position.end]) {
2622
+ point.offset = offset + (point.offset ?? 0);
2623
+ point.line = line + point.line - 1;
2565
2624
  }
2566
- };
2625
+ }
2626
+ for (const child of candidate.children ?? []) shiftOnto(child, offset, line);
2567
2627
  }
2568
- function planBody(body, options = {}) {
2569
- const segments = [];
2570
- let index = 0;
2571
- while (index < body.length) {
2572
- if (body[index] === "\n") {
2573
- segments.push({ kind: "eol", length: 1 });
2574
- index++;
2575
- continue;
2628
+ function advanceStart(node2, by) {
2629
+ const start = node2.position?.start;
2630
+ if (!start || start.offset === void 0) return;
2631
+ start.offset += by;
2632
+ start.column += by;
2633
+ }
2634
+ function columnAt(source, offset) {
2635
+ return offset - (source.lastIndexOf("\n", offset - 1) + 1) + 1;
2636
+ }
2637
+ function moveStartTo(node2, source, offset, line) {
2638
+ const position = node2.position;
2639
+ if (!position) return;
2640
+ position.start = { line, column: columnAt(source, offset), offset };
2641
+ }
2642
+ function titleLineIsBlock(remainder, reparse) {
2643
+ if (remainder.trim() === "") return void 0;
2644
+ const parsed = reparse(remainder);
2645
+ const only = parsed.children[0];
2646
+ if (parsed.children.length === 1 && (only?.type === "paragraph" || only?.type === "math")) {
2647
+ return void 0;
2648
+ }
2649
+ return parsed.children;
2650
+ }
2651
+ function calloutTransform(tree, source = "", reparse = () => ({
2652
+ type: "root",
2653
+ children: []
2654
+ })) {
2655
+ visit(tree, "blockquote", (node2) => {
2656
+ const quote = node2;
2657
+ const first = quote.children[0];
2658
+ if (!first || first.type !== "paragraph" && first.type !== "heading") {
2659
+ return;
2576
2660
  }
2577
- let end = index;
2578
- while (end < body.length && body[end] !== "\n") end++;
2579
- if (options.stripContinuationIndent === true && index > 0) {
2580
- let after = index;
2581
- while (after < end && (body[after] === " " || body[after] === " "))
2582
- after++;
2583
- if (after > index) {
2584
- segments.push({ kind: "prefix", length: after - index });
2585
- index = after;
2586
- if (index === end) continue;
2587
- }
2661
+ const paragraph2 = first;
2662
+ if (paragraph2.position?.start?.line !== void 0 && quote.position?.start?.line !== void 0 && paragraph2.position.start.line !== quote.position.start.line) {
2663
+ return;
2588
2664
  }
2589
- const run = body.slice(index, end);
2590
- const kept = run.trimEnd();
2591
- const isLast = end >= body.length;
2592
- let trailingSlashes = 0;
2593
- while (trailingSlashes < run.length && run[run.length - 1 - trailingSlashes] === "\\") {
2594
- trailingSlashes++;
2665
+ const lead = paragraph2.children[0];
2666
+ if (!lead || lead.type !== "text") return;
2667
+ const match = MARKER.exec(lead.value);
2668
+ if (!match) return;
2669
+ if (match[1].trim() === "") return;
2670
+ const from = paragraph2.position?.start?.offset;
2671
+ if (source !== "" && from !== void 0) {
2672
+ const lineStart = source.lastIndexOf("\n", from - 1) + 1;
2673
+ const lineEnd2 = source.indexOf("\n", lineStart);
2674
+ const line = source.slice(
2675
+ lineStart,
2676
+ lineEnd2 === -1 ? void 0 : lineEnd2
2677
+ );
2678
+ if (!atMarkerColumn(line)) return;
2595
2679
  }
2596
- if (isLast && kept.length < run.length) {
2597
- if (kept.length > 0) segments.push({ kind: "text", length: kept.length });
2598
- segments.push({ kind: "data", length: run.length - kept.length });
2599
- } else if (!isLast && trailingSlashes % 2 === 1) {
2600
- if (run.length > 1) {
2601
- segments.push({ kind: "text", length: run.length - 1 });
2602
- }
2603
- segments.push({ kind: "hardBreak", length: 1 });
2604
- } else {
2605
- segments.push({ kind: "text", length: run.length });
2680
+ const [consumed, calloutType, fold2] = match;
2681
+ lead.value = lead.value.slice(consumed.length);
2682
+ advanceStart(lead, consumed.length);
2683
+ const collect = (nodes) => nodes.map(
2684
+ (c) => "value" in c ? String(c.value) : collect(c.children ?? [])
2685
+ ).join("");
2686
+ const leadText = lead;
2687
+ const newlineAt = leadText.value.indexOf("\n");
2688
+ const titleFrom = leadText.position?.start?.offset;
2689
+ let firstLine = newlineAt === -1 ? leadText.value : leadText.value.slice(0, newlineAt);
2690
+ if (source !== "" && titleFrom !== void 0) {
2691
+ const breakAt = source.indexOf("\n", titleFrom);
2692
+ firstLine = source.slice(titleFrom, breakAt === -1 ? void 0 : breakAt);
2606
2693
  }
2607
- index = end;
2608
- }
2609
- return segments;
2610
- }
2611
-
2612
- // node_modules/micromark-util-symbol/lib/codes.js
2613
- var codes = (
2614
- /** @type {const} */
2615
- {
2616
- carriageReturn: -5,
2617
- lineFeed: -4,
2618
- carriageReturnLineFeed: -3,
2619
- horizontalTab: -2,
2620
- virtualSpace: -1,
2621
- eof: null,
2622
- nul: 0,
2623
- soh: 1,
2624
- stx: 2,
2625
- etx: 3,
2626
- eot: 4,
2627
- enq: 5,
2628
- ack: 6,
2629
- bel: 7,
2630
- bs: 8,
2631
- ht: 9,
2632
- // `\t`
2633
- lf: 10,
2634
- // `\n`
2635
- vt: 11,
2636
- // `\v`
2637
- ff: 12,
2638
- // `\f`
2639
- cr: 13,
2640
- // `\r`
2641
- so: 14,
2694
+ if (firstLine.endsWith("\r")) firstLine = firstLine.slice(0, -1);
2695
+ function bodyUnderTitleLine() {
2696
+ const from2 = leadText.position?.start?.offset;
2697
+ const to = paragraph2.position?.end?.offset;
2698
+ if (from2 === void 0 || to === void 0 || source === "") return [];
2699
+ const breakAt = source.indexOf("\n", from2);
2700
+ if (breakAt === -1 || breakAt >= to) return [];
2701
+ const bodyFrom = breakAt + 1 + (LINE_PREFIX.exec(source.slice(breakAt + 1))?.[0].length ?? 0);
2702
+ if (bodyFrom >= to) return [];
2703
+ const map = mapLines(
2704
+ source,
2705
+ bodyFrom,
2706
+ to,
2707
+ source.slice(0, bodyFrom).split("\n").length
2708
+ );
2709
+ const parsed = reparse(map.text);
2710
+ for (const child of parsed.children) shiftByMap(child, map, 0);
2711
+ return parsed.children;
2712
+ }
2713
+ const asBlock = titleLineIsBlock(firstLine, reparse);
2714
+ if (asBlock) {
2715
+ const titleStart = leadText.position?.start;
2716
+ if (titleStart?.offset !== void 0) {
2717
+ for (const block of asBlock) {
2718
+ shiftOnto(block, titleStart.offset, titleStart.line);
2719
+ }
2720
+ }
2721
+ const body = bodyUnderTitleLine();
2722
+ const rest = quote.children.slice(1);
2723
+ quote.children = [
2724
+ {
2725
+ type: "calloutTitle",
2726
+ children: [],
2727
+ // Zero width at the title's own offset. The title is empty, not absent --
2728
+ // it has nowhere to span, but it does have a place, and an unplaced node
2729
+ // is one a formatter cannot edit around. Nine of these arrived with the
2730
+ // block-title cases alone.
2731
+ ...titleStart?.offset === void 0 ? {} : {
2732
+ position: {
2733
+ start: { ...titleStart },
2734
+ end: { ...titleStart }
2735
+ }
2736
+ }
2737
+ },
2738
+ ...asBlock,
2739
+ ...body,
2740
+ ...rest
2741
+ ];
2742
+ const callout2 = node2;
2743
+ callout2.type = "callout";
2744
+ callout2.calloutType = (calloutType ?? "").trim();
2745
+ callout2.title = "";
2746
+ callout2.collapsed = fold2 === "-" ? true : fold2 === "+" ? false : null;
2747
+ return;
2748
+ }
2749
+ function splitAcrossConstruct() {
2750
+ const from2 = leadText.position?.start?.offset;
2751
+ const to = paragraph2.position?.end?.offset;
2752
+ if (from2 === void 0 || to === void 0) return void 0;
2753
+ const breakAtSource = source.indexOf("\n", from2);
2754
+ if (breakAtSource === -1 || breakAtSource >= to) return void 0;
2755
+ const titleLine = source.slice(from2, breakAtSource);
2756
+ const bodyFrom = breakAtSource + 1 + (LINE_PREFIX.exec(source.slice(breakAtSource + 1))?.[0].length ?? 0);
2757
+ if (bodyFrom >= to) return void 0;
2758
+ const titleBlock = reparse(titleLine).children[0];
2759
+ const bodyMap = mapLines(
2760
+ source,
2761
+ bodyFrom,
2762
+ to,
2763
+ source.slice(0, bodyFrom).split("\n").length
2764
+ );
2765
+ const bodyBlock = reparse(bodyMap.text).children[0];
2766
+ if (titleBlock?.type !== "paragraph" || bodyBlock?.type !== "paragraph") {
2767
+ let reread;
2768
+ if (bodyBlock?.type === "paragraph") {
2769
+ for (const child of bodyBlock.children) shiftByMap(child, bodyMap, 0);
2770
+ reread = bodyBlock.children;
2771
+ }
2772
+ return splitAtLineEnd(breakAtSource, bodyFrom, reread);
2773
+ }
2774
+ for (const child of titleBlock.children) shiftOnto(child, from2, 1);
2775
+ for (const child of bodyBlock.children) shiftByMap(child, bodyMap, 0);
2776
+ return { title: titleBlock.children, body: bodyBlock.children };
2777
+ }
2778
+ const nonEmpty = (node3) => node3.type !== "text" || node3.value !== "";
2779
+ function splitAtLineEnd(lineEnd2, bodyFrom, reread) {
2780
+ const title2 = [];
2781
+ const body = [];
2782
+ for (const child of paragraph2.children) {
2783
+ const span2 = child.position;
2784
+ if (span2?.start.offset === void 0 || span2.end.offset === void 0) {
2785
+ return void 0;
2786
+ }
2787
+ if (span2.end.offset <= lineEnd2) title2.push(child);
2788
+ else if (span2.start.offset > lineEnd2) body.push(child);
2789
+ else if (child.type === "text") {
2790
+ const value2 = child.value;
2791
+ const cut = value2.indexOf("\n");
2792
+ if (cut === -1) return void 0;
2793
+ if (cut > 0) {
2794
+ title2.push({
2795
+ type: "text",
2796
+ value: value2.slice(0, cut),
2797
+ position: {
2798
+ start: { ...span2.start },
2799
+ end: {
2800
+ line: span2.start.line,
2801
+ column: span2.start.column + cut,
2802
+ offset: span2.start.offset + cut
2803
+ }
2804
+ }
2805
+ });
2806
+ }
2807
+ const rest = { ...child, value: value2.slice(cut + 1) };
2808
+ rest.position = { start: { ...span2.start }, end: { ...span2.end } };
2809
+ moveStartTo(rest, source, bodyFrom, span2.start.line + 1);
2810
+ if (rest.value !== "") body.push(rest);
2811
+ } else {
2812
+ const titleText = {
2813
+ type: "text",
2814
+ value: source.slice(span2.start.offset, lineEnd2),
2815
+ position: {
2816
+ start: { ...span2.start },
2817
+ end: {
2818
+ line: span2.start.line,
2819
+ column: span2.start.column + lineEnd2 - span2.start.offset,
2820
+ offset: lineEnd2
2821
+ }
2822
+ }
2823
+ };
2824
+ if (reread) {
2825
+ return {
2826
+ title: [...title2, titleText].filter(nonEmpty),
2827
+ body: reread
2828
+ };
2829
+ }
2830
+ const map = mapLines(
2831
+ source,
2832
+ bodyFrom,
2833
+ span2.end.offset,
2834
+ source.slice(0, bodyFrom).split("\n").length
2835
+ );
2836
+ const tail = reparse(map.text).children[0];
2837
+ if (tail?.type !== "paragraph") return void 0;
2838
+ for (const node3 of tail.children) shiftByMap(node3, map, 0);
2839
+ title2.push(titleText);
2840
+ body.push(...tail.children);
2841
+ }
2842
+ }
2843
+ return body.length > 0 ? { title: title2.filter(nonEmpty), body } : void 0;
2844
+ }
2845
+ let titleNodes;
2846
+ if (newlineAt !== -1) {
2847
+ const titleStart = leadText.position?.start;
2848
+ const titleEnd = titleStart?.offset === void 0 ? void 0 : titleStart.offset + newlineAt;
2849
+ const bodyStart = titleEnd === void 0 ? void 0 : titleEnd + 1 + (LINE_PREFIX.exec(source.slice(titleEnd + 1))?.[0].length ?? 0);
2850
+ titleNodes = leadText.value.slice(0, newlineAt).trim() === "" ? [] : [
2851
+ {
2852
+ type: "text",
2853
+ value: leadText.value.slice(0, newlineAt),
2854
+ ...titleStart && titleEnd !== void 0 ? {
2855
+ position: {
2856
+ start: { ...titleStart },
2857
+ end: {
2858
+ line: titleStart.line,
2859
+ column: titleStart.column + newlineAt,
2860
+ offset: titleEnd
2861
+ }
2862
+ }
2863
+ } : {}
2864
+ }
2865
+ ];
2866
+ leadText.value = leadText.value.slice(newlineAt + 1);
2867
+ if (leadText.value === "") paragraph2.children.shift();
2868
+ else if (bodyStart !== void 0 && titleStart) {
2869
+ moveStartTo(leadText, source, bodyStart, titleStart.line + 1);
2870
+ moveStartTo(paragraph2, source, bodyStart, titleStart.line + 1);
2871
+ }
2872
+ } else {
2873
+ const breakAt = paragraph2.children.findIndex((c) => c.type === "break");
2874
+ const split = breakAt === -1 ? splitAcrossConstruct() : void 0;
2875
+ titleNodes = split?.title ?? (breakAt === -1 ? paragraph2.children.slice() : paragraph2.children.slice(0, breakAt));
2876
+ if (split) paragraph2.children = split.body;
2877
+ if (!split) {
2878
+ paragraph2.children = breakAt === -1 ? [] : paragraph2.children.slice(breakAt + 1);
2879
+ }
2880
+ const first2 = paragraph2.children[0];
2881
+ const start = first2?.position?.start;
2882
+ if (start?.offset !== void 0 && paragraph2.position) {
2883
+ paragraph2.position.start = { ...start };
2884
+ } else if (first2 && paragraph2.position && titleFrom !== void 0) {
2885
+ const lineEnd2 = source.indexOf("\n", titleFrom);
2886
+ if (lineEnd2 !== -1) {
2887
+ const bodyStart = lineEnd2 + 1 + (LINE_PREFIX.exec(source.slice(lineEnd2 + 1))?.[0].length ?? 0);
2888
+ moveStartTo(
2889
+ paragraph2,
2890
+ source,
2891
+ bodyStart,
2892
+ source.slice(0, bodyStart).split("\n").length
2893
+ );
2894
+ }
2895
+ }
2896
+ }
2897
+ const title = collect(titleNodes).trim();
2898
+ if (paragraph2.children.length === 0 || paragraph2.children.length === 1 && paragraph2.children[0].type === "text" && paragraph2.children[0].value.trim() === "") {
2899
+ quote.children.shift();
2900
+ }
2901
+ const blankTitle = title === "" && firstLine !== "";
2902
+ const hasTitleNode = titleNodes.some(
2903
+ (node3) => node3.type !== "text" || (node3.value ?? "").trim() !== ""
2904
+ );
2905
+ if (title !== "" || blankTitle || hasTitleNode) {
2906
+ const head = titleNodes[0]?.position?.start;
2907
+ const tail = titleNodes[titleNodes.length - 1]?.position?.end;
2908
+ quote.children.unshift({
2909
+ type: "calloutTitle",
2910
+ children: titleNodes,
2911
+ // Copies, not the children's own points: a pass that moves every node's position (a
2912
+ // comment body's re-parse, placed back by `shiftByMap`) moved a shared point twice, so the
2913
+ // title ran past the source (`%%note` / blank / `[^1]: > [!note] $$y^2`).
2914
+ ...head && tail ? { position: { start: { ...head }, end: { ...tail } } } : {}
2915
+ });
2916
+ }
2917
+ for (const child of quote.children) {
2918
+ if (child.type !== "paragraph") continue;
2919
+ const first2 = child.children[0]?.position?.start;
2920
+ const start = child.position?.start;
2921
+ if (!first2 || !start) break;
2922
+ if (start.offset === void 0 || first2.offset === void 0) break;
2923
+ if (first2.offset > start.offset) child.position.start = { ...first2 };
2924
+ break;
2925
+ }
2926
+ const callout = node2;
2927
+ callout.type = "callout";
2928
+ callout.calloutType = (calloutType ?? "").trim();
2929
+ callout.title = title;
2930
+ callout.collapsed = fold2 === "-" ? true : fold2 === "+" ? false : null;
2931
+ });
2932
+ }
2933
+ function syncCalloutTitlesTransform(tree) {
2934
+ const collect = (nodes) => nodes.map(
2935
+ (child) => "value" in child ? String(child.value) : collect(child.children ?? [])
2936
+ ).join("");
2937
+ visit(tree, (node2) => {
2938
+ if (node2.type !== "callout") return;
2939
+ const titleNode = (node2.children ?? []).find(
2940
+ (child) => child.type === "calloutTitle"
2941
+ );
2942
+ if (titleNode) {
2943
+ node2.title = collect(
2944
+ titleNode.children ?? []
2945
+ ).trim();
2946
+ }
2947
+ });
2948
+ }
2949
+
2950
+ // src/lib/normalize-points.ts
2951
+ function pointAt(source, offset) {
2952
+ const starts = lineStartsOf(source);
2953
+ let low = 0;
2954
+ let high = starts.length - 1;
2955
+ while (low < high) {
2956
+ const middle = low + high + 1 >> 1;
2957
+ if (starts[middle] <= offset) low = middle;
2958
+ else high = middle - 1;
2959
+ }
2960
+ return { line: low + 1, column: offset - starts[low] + 1, offset };
2961
+ }
2962
+ var cachedSource;
2963
+ var cachedStarts = [0];
2964
+ function lineStartsOf(source) {
2965
+ if (source === cachedSource) return cachedStarts;
2966
+ const starts = [0];
2967
+ for (let index = 0; index < source.length; index++) {
2968
+ if (source[index] === "\r" && source[index + 1] === "\n") continue;
2969
+ if (source[index] === "\n" || source[index] === "\r")
2970
+ starts.push(index + 1);
2971
+ }
2972
+ cachedSource = source;
2973
+ cachedStarts = starts;
2974
+ return starts;
2975
+ }
2976
+ function span(candidate, source, from, to) {
2977
+ if (source !== "" && from !== void 0 && to !== void 0) {
2978
+ candidate.position = {
2979
+ start: pointAt(source, from),
2980
+ end: pointAt(source, to)
2981
+ };
2982
+ }
2983
+ return candidate;
2984
+ }
2985
+ function normalizePointsTransform(tree, source) {
2986
+ if (source === "") return;
2987
+ const starts = [0];
2988
+ for (let index = 0; index < source.length; index++) {
2989
+ const character = source[index];
2990
+ if (character === "\r" && source[index + 1] === "\n") continue;
2991
+ if (character === "\n" || character === "\r") starts.push(index + 1);
2992
+ }
2993
+ const lineAt = (offset) => {
2994
+ let low = 0;
2995
+ let high = starts.length - 1;
2996
+ while (low < high) {
2997
+ const middle = low + high + 1 >> 1;
2998
+ if (starts[middle] <= offset) low = middle;
2999
+ else high = middle - 1;
3000
+ }
3001
+ return low;
3002
+ };
3003
+ visit(tree, (node2) => {
3004
+ const position = node2.position;
3005
+ if (!position) return;
3006
+ for (const point of [position.start, position.end]) {
3007
+ const offset = point.offset;
3008
+ if (offset === void 0 || offset < 0 || offset > source.length) {
3009
+ continue;
3010
+ }
3011
+ const index = lineAt(offset);
3012
+ point.line = index + 1;
3013
+ point.column = offset - starts[index] + 1;
3014
+ }
3015
+ });
3016
+ }
3017
+
3018
+ // src/lib/callout-marker.ts
3019
+ var QUOTE_PREFIX2 = /^[ \t]*> ?/u;
3020
+ function placeMarker(callout, source) {
3021
+ const from = callout.position?.start.offset;
3022
+ if (from === void 0) return;
3023
+ let end = from;
3024
+ while (end < source.length && source[end] !== "\n" && source[end] !== "\r")
3025
+ end++;
3026
+ const line = source.slice(from, end);
3027
+ const prefix2 = QUOTE_PREFIX2.exec(line);
3028
+ if (!prefix2) return;
3029
+ const marker = MARKER.exec(line.slice(prefix2[0].length));
3030
+ if (!marker) return;
3031
+ const start = from + prefix2[0].length;
3032
+ const width = "[!]".length + marker[1].length + marker[2].length;
3033
+ callout.data = {
3034
+ ...callout.data,
3035
+ marker: {
3036
+ start: pointAt(source, start),
3037
+ end: pointAt(source, start + width)
3038
+ },
3039
+ lineEnd: pointAt(source, end)
3040
+ };
3041
+ }
3042
+ function calloutMarkerTransform(tree, source) {
3043
+ if (!source.includes("[!")) return;
3044
+ const walk = (node2) => {
3045
+ if (node2.type === "callout") placeMarker(node2, source);
3046
+ if (!Array.isArray(node2.children)) return;
3047
+ for (const child of node2.children) walk(child);
3048
+ };
3049
+ walk(tree);
3050
+ }
3051
+
3052
+ // src/lib/math-display.ts
3053
+ function mathDisplayTransform(tree, source) {
3054
+ visit(tree, "inlineMath", (node2) => {
3055
+ const math3 = node2;
3056
+ const start = math3.position?.start?.offset;
3057
+ const end = math3.position?.end?.offset;
3058
+ if (start === void 0 || end === void 0) return;
3059
+ const opening = /^\$+/.exec(source.slice(start, end));
3060
+ if (!opening || opening[0].length < 2) return;
3061
+ math3.data ??= {};
3062
+ math3.data.display = true;
3063
+ math3.data.hProperties ??= {};
3064
+ math3.data.hProperties.className = ["language-math", "math-display"];
3065
+ });
3066
+ }
3067
+
3068
+ // src/lib/wikilink-target.ts
3069
+ function resolveTarget(target) {
3070
+ return target.replace(/\u00a0/gu, " ").normalize("NFC");
3071
+ }
3072
+ function spelledTarget(path, heading, resolved) {
3073
+ const spelled = {};
3074
+ if (path !== resolved.path) spelled.spelledPath = path;
3075
+ if (heading !== resolved.heading) spelled.spelledHeading = heading;
3076
+ return Object.keys(spelled).length > 0 ? spelled : void 0;
3077
+ }
3078
+ function dropClosingBackslash(part) {
3079
+ return part.slice(0, -1).trimEnd();
3080
+ }
3081
+ function writtenPart(part, spelled, strippedBackslash) {
3082
+ if (spelled !== void 0) {
3083
+ const resolved = resolveTarget(spelled);
3084
+ if (strippedBackslash ? resolved.endsWith("\\") && dropClosingBackslash(resolved) === part : resolved === part) {
3085
+ return spelled;
3086
+ }
3087
+ }
3088
+ return strippedBackslash ? `${part}\\` : part;
3089
+ }
3090
+
3091
+ // src/lib/tag-char.ts
3092
+ var TAG_STOP = /[\u2000-\u206F\u2E00-\u2E7F'!"#$%&()*+,.:;<=>?@^`{|}~[\]\\\s]/u;
3093
+ function isTagCharacter(char) {
3094
+ return char !== "" && !TAG_STOP.test(char);
3095
+ }
3096
+
3097
+ // src/lib/colour-marker.ts
3098
+ var COLOUR_MARKERS = /* @__PURE__ */ new Set([
3099
+ 128308,
3100
+ 128997,
3101
+ 128992,
3102
+ 128999,
3103
+ 128993,
3104
+ 129e3,
3105
+ 128994,
3106
+ 129001,
3107
+ 128309,
3108
+ 128998,
3109
+ 128995,
3110
+ 129002
3111
+ ]);
3112
+ function colourMarkerLength(content) {
3113
+ const first = content.codePointAt(0);
3114
+ return first !== void 0 && COLOUR_MARKERS.has(first) ? 2 : 0;
3115
+ }
3116
+ function tagLength(value2) {
3117
+ if (value2.charAt(0) !== "#") return 0;
3118
+ let end = 1;
3119
+ let nonDigit = false;
3120
+ for (const character of value2.slice(1)) {
3121
+ if (!isTagCharacter(character)) break;
3122
+ if (!/[0-9]/u.test(character)) nonDigit = true;
3123
+ end += character.length;
3124
+ }
3125
+ return end > 1 && nonDigit ? end : 0;
3126
+ }
3127
+ function colourMarkerTagTransform(tree, source) {
3128
+ visit(tree, "highlight", (node2) => {
3129
+ const children = node2.children;
3130
+ const first = children[0];
3131
+ if (first?.type !== "text") return;
3132
+ const marker = colourMarkerLength(first.value);
3133
+ if (marker === 0) return;
3134
+ const length = tagLength(first.value.slice(marker));
3135
+ if (length === 0) return;
3136
+ const start = first.position?.start;
3137
+ if (!start || source.charAt((start.offset ?? 0) + marker) !== "#") return;
3138
+ const at3 = (columns2) => start && {
3139
+ line: start.line,
3140
+ column: start.column + columns2,
3141
+ offset: (start.offset ?? 0) + columns2
3142
+ };
3143
+ const place = (from, to) => start ? { position: { start: at3(from), end: at3(to) } } : {};
3144
+ const end = marker + length;
3145
+ const rest = first.value.slice(end);
3146
+ const split = [
3147
+ {
3148
+ type: "text",
3149
+ value: first.value.slice(0, marker),
3150
+ ...place(0, marker)
3151
+ },
3152
+ {
3153
+ type: "tag",
3154
+ value: first.value.slice(marker + 1, end),
3155
+ ...place(marker, end)
3156
+ }
3157
+ ];
3158
+ if (rest !== "") {
3159
+ split.push({
3160
+ type: "text",
3161
+ value: rest,
3162
+ ...first.position ? { position: { start: at3(end), end: first.position.end } } : {}
3163
+ });
3164
+ }
3165
+ children.splice(0, 1, ...split);
3166
+ });
3167
+ }
3168
+
3169
+ // src/lib/syntax/legacy-scan.ts
3170
+ function codeToCharacter(code) {
3171
+ if (code === -5 || code === -4 || code === -3) return "\n";
3172
+ if (code === -2) return " ";
3173
+ if (code === -1) return " ";
3174
+ return String.fromCodePoint(code);
3175
+ }
3176
+ function createChunkScan(sink, stopAfter) {
3177
+ return {
3178
+ partial: true,
3179
+ tokenize(effects, ok3) {
3180
+ return function enter(code) {
3181
+ effects.enter("legacyChunkScan");
3182
+ return step2(code);
3183
+ };
3184
+ function step2(code) {
3185
+ if (code === null) {
3186
+ effects.exit("legacyChunkScan");
3187
+ return ok3(code);
3188
+ }
3189
+ const character = codeToCharacter(code);
3190
+ sink.push(character);
3191
+ effects.consume(code);
3192
+ if (stopAfter?.(character) === true) {
3193
+ effects.exit("legacyChunkScan");
3194
+ return ok3;
3195
+ }
3196
+ return step2;
3197
+ }
3198
+ }
3199
+ };
3200
+ }
3201
+ function planBody(body, options = {}) {
3202
+ const segments = [];
3203
+ let index = 0;
3204
+ while (index < body.length) {
3205
+ if (body[index] === "\n") {
3206
+ segments.push({ kind: "eol", length: 1 });
3207
+ index++;
3208
+ continue;
3209
+ }
3210
+ let end = index;
3211
+ while (end < body.length && body[end] !== "\n") end++;
3212
+ if (options.stripContinuationIndent === true && index > 0) {
3213
+ let after = index;
3214
+ while (after < end && (body[after] === " " || body[after] === " "))
3215
+ after++;
3216
+ if (after > index) {
3217
+ segments.push({ kind: "prefix", length: after - index });
3218
+ index = after;
3219
+ if (index === end) continue;
3220
+ }
3221
+ }
3222
+ const run = body.slice(index, end);
3223
+ const kept = run.trimEnd();
3224
+ const isLast = end >= body.length;
3225
+ let trailingSlashes = 0;
3226
+ while (trailingSlashes < run.length && run[run.length - 1 - trailingSlashes] === "\\") {
3227
+ trailingSlashes++;
3228
+ }
3229
+ if (isLast && kept.length < run.length) {
3230
+ if (kept.length > 0) segments.push({ kind: "text", length: kept.length });
3231
+ segments.push({ kind: "data", length: run.length - kept.length });
3232
+ } else if (!isLast && trailingSlashes % 2 === 1) {
3233
+ if (run.length > 1) {
3234
+ segments.push({ kind: "text", length: run.length - 1 });
3235
+ }
3236
+ segments.push({ kind: "hardBreak", length: 1 });
3237
+ } else {
3238
+ segments.push({ kind: "text", length: run.length });
3239
+ }
3240
+ index = end;
3241
+ }
3242
+ return segments;
3243
+ }
3244
+
3245
+ // node_modules/micromark-util-symbol/lib/codes.js
3246
+ var codes = (
3247
+ /** @type {const} */
3248
+ {
3249
+ carriageReturn: -5,
3250
+ lineFeed: -4,
3251
+ carriageReturnLineFeed: -3,
3252
+ horizontalTab: -2,
3253
+ virtualSpace: -1,
3254
+ eof: null,
3255
+ nul: 0,
3256
+ soh: 1,
3257
+ stx: 2,
3258
+ etx: 3,
3259
+ eot: 4,
3260
+ enq: 5,
3261
+ ack: 6,
3262
+ bel: 7,
3263
+ bs: 8,
3264
+ ht: 9,
3265
+ // `\t`
3266
+ lf: 10,
3267
+ // `\n`
3268
+ vt: 11,
3269
+ // `\v`
3270
+ ff: 12,
3271
+ // `\f`
3272
+ cr: 13,
3273
+ // `\r`
3274
+ so: 14,
2642
3275
  si: 15,
2643
3276
  dle: 16,
2644
3277
  dc1: 17,
@@ -3275,34 +3908,44 @@ var types = (
3275
3908
 
3276
3909
  // src/lib/syntax/highlight.ts
3277
3910
  var EQUALS = 61;
3278
- var MARKER = "=";
3911
+ var MARKER2 = "=";
3279
3912
  var MARKER_LENGTH = 2;
3280
3913
  function legacyHighlightQueue(value2) {
3281
- if (value2.charAt(0) !== MARKER || value2.charAt(1) !== MARKER) return null;
3914
+ if (value2.charAt(0) !== MARKER2 || value2.charAt(1) !== MARKER2) return null;
3282
3915
  let index = MARKER_LENGTH;
3283
- let queue = "";
3284
3916
  while (index < value2.length) {
3285
- const character = value2.charAt(index);
3286
- if (character === MARKER && value2.charAt(index + 1) === MARKER) {
3287
- if (!queue.trim() || /^\s/u.test(queue)) return null;
3288
- if (!/\s$/u.test(queue)) return queue;
3917
+ if (value2.charAt(index) === MARKER2 && value2.charAt(index + 1) === MARKER2) {
3918
+ if (index === MARKER_LENGTH || isSpace(value2.charAt(MARKER_LENGTH)))
3919
+ return null;
3920
+ if (!isSpace(value2.charAt(index - 1)))
3921
+ return value2.slice(MARKER_LENGTH, index);
3289
3922
  }
3290
- queue += character;
3291
3923
  index++;
3292
3924
  }
3293
3925
  return null;
3294
3926
  }
3927
+ function isSpace(character) {
3928
+ return /\s/u.test(character);
3929
+ }
3930
+ var noCloserFrom = /* @__PURE__ */ new WeakMap();
3295
3931
  function createQueueBound() {
3296
3932
  let index = -1;
3297
3933
  let opened = false;
3934
+ let previous2 = "";
3935
+ let beforePrevious = "";
3298
3936
  return (character) => {
3299
3937
  index += 1;
3300
- if (index < MARKER_LENGTH) return character !== MARKER;
3938
+ const last = previous2;
3939
+ const beforeLast = beforePrevious;
3940
+ beforePrevious = previous2;
3941
+ previous2 = character;
3942
+ if (index < MARKER_LENGTH) return character !== MARKER2;
3301
3943
  if (index === MARKER_LENGTH) {
3302
- opened = character === MARKER;
3944
+ opened = character === MARKER2;
3303
3945
  return /^\s/u.test(character);
3304
3946
  }
3305
- return index === MARKER_LENGTH + 1 && opened && character === MARKER;
3947
+ if (index === MARKER_LENGTH + 1) return opened && character === MARKER2;
3948
+ return character === MARKER2 && last === MARKER2 && index >= MARKER_LENGTH + 2 && !/\s/u.test(beforeLast);
3306
3949
  };
3307
3950
  }
3308
3951
  var highlight = {
@@ -3345,12 +3988,18 @@ var highlight = {
3345
3988
  return start;
3346
3989
  function start(code) {
3347
3990
  if (code !== EQUALS) return nok(code);
3991
+ const known = noCloserFrom.get(self);
3992
+ if (known !== void 0 && self.now().offset >= known) return nok(code);
3348
3993
  characters.length = 0;
3349
3994
  return effects.check(scan, decide, decide)(code);
3350
3995
  }
3351
3996
  function decide(code) {
3352
3997
  const found = legacyHighlightQueue(characters.join(""));
3353
- if (found === null) return nok(code);
3998
+ if (found === null) {
3999
+ if (characters.length > MARKER_LENGTH + 2)
4000
+ noCloserFrom.set(self, self.now().offset);
4001
+ return nok(code);
4002
+ }
3354
4003
  if (emphasisClosesInside(self, found)) return nok(code);
3355
4004
  queue = found;
3356
4005
  effects.enter("highlight");
@@ -3383,6 +4032,7 @@ function group(character) {
3383
4032
  if (/\p{P}|\p{S}/u.test(character)) return 2;
3384
4033
  return void 0;
3385
4034
  }
4035
+ var pairedRuns = /* @__PURE__ */ new WeakMap();
3386
4036
  function flank(marker, size, before2, after) {
3387
4037
  const open = !after || after === 2 && Boolean(before2);
3388
4038
  const close2 = !before2 || before2 === 2 && Boolean(after);
@@ -3394,23 +4044,30 @@ function flank(marker, size, before2, after) {
3394
4044
  };
3395
4045
  }
3396
4046
  function emphasisClosesInside(self, body) {
3397
- const stack = [];
3398
- const take = (run, outer) => {
4047
+ const events = self.events;
4048
+ let paired = pairedRuns.get(self);
4049
+ if (paired?.events !== events || paired.read > events.length || paired.read > 0 && events[paired.read - 1] !== paired.last) {
4050
+ paired = { events, read: 0, last: void 0, stack: [] };
4051
+ pairedRuns.set(self, paired);
4052
+ }
4053
+ const stack = paired.stack;
4054
+ const take = (run, outer2) => {
3399
4055
  if (run.close) {
3400
4056
  for (let at3 = stack.length - 1; at3 >= 0 && run.size > 0; at3 -= 1) {
3401
4057
  const entry = stack[at3];
3402
4058
  if (entry.run.marker !== run.marker) continue;
3403
- if (entry.outer && !outer) return true;
4059
+ if (entry.outer && !outer2) return true;
3404
4060
  const used = Math.min(entry.run.size, run.size);
3405
4061
  entry.run.size -= used;
3406
4062
  run.size -= used;
3407
4063
  if (entry.run.size === 0) stack.splice(at3, 1);
3408
4064
  }
3409
4065
  }
3410
- if (run.open && run.size > 0) stack.push({ run, outer });
4066
+ if (run.open && run.size > 0) stack.push({ run, outer: outer2 });
3411
4067
  return false;
3412
4068
  };
3413
- for (const [kind, token] of self.events) {
4069
+ for (; paired.read < events.length; paired.read += 1) {
4070
+ const [kind, token] = events[paired.read];
3414
4071
  if (kind !== "enter" || token.type !== "attentionSequence") continue;
3415
4072
  const text3 = self.sliceSerialize(token);
3416
4073
  const flags = token;
@@ -3424,19 +4081,25 @@ function emphasisClosesInside(self, body) {
3424
4081
  true
3425
4082
  );
3426
4083
  }
3427
- if (!stack.some((entry) => entry.outer)) return false;
3428
- for (const match of body.matchAll(/\*+|_+/gu)) {
3429
- const start = match.index;
3430
- const end = start + match[0].length;
3431
- const run = flank(
3432
- match[0][0],
3433
- match[0].length,
3434
- group(start > 0 ? body[start - 1] : MARKER),
3435
- group(end < body.length ? body[end] : MARKER)
3436
- );
3437
- if (take(run, false)) return true;
4084
+ paired.last = events[paired.read - 1];
4085
+ const outer = stack.length;
4086
+ if (outer === 0) return false;
4087
+ try {
4088
+ for (const match of body.matchAll(/\*+|_+/gu)) {
4089
+ const start = match.index;
4090
+ const end = start + match[0].length;
4091
+ const run = flank(
4092
+ match[0][0],
4093
+ match[0].length,
4094
+ group(start > 0 ? body[start - 1] : MARKER2),
4095
+ group(end < body.length ? body[end] : MARKER2)
4096
+ );
4097
+ if (take(run, false)) return true;
4098
+ }
4099
+ return false;
4100
+ } finally {
4101
+ stack.length = outer;
3438
4102
  }
3439
- return false;
3440
4103
  }
3441
4104
  function highlightSyntax() {
3442
4105
  return { text: { [EQUALS]: highlight } };
@@ -4017,6 +4680,69 @@ function normalListItem(ctx, value2, position) {
4017
4680
  }
4018
4681
  }
4019
4682
 
4683
+ // src/rp8/reads-past-end.ts
4684
+ function definitionCutOff(doc, from) {
4685
+ const length = doc.length;
4686
+ const skip = (start, set) => {
4687
+ let at4 = start;
4688
+ while (at4 < length && set.includes(doc[at4])) at4++;
4689
+ return at4;
4690
+ };
4691
+ let at3 = skip(from, " ");
4692
+ if (doc[at3] !== "[" || doc[at3 + 1] === "^") return false;
4693
+ const labelStart = ++at3;
4694
+ while (at3 < length && doc[at3] !== "]") at3 += doc[at3] === "\\" ? 2 : 1;
4695
+ if (at3 >= length) return true;
4696
+ if (at3 === labelStart || doc[at3 + 1] !== ":") return false;
4697
+ at3 = skip(at3 + 2, " \n");
4698
+ if (at3 >= length) return true;
4699
+ let enclosed = 0;
4700
+ if (doc[at3] === "<") {
4701
+ const open = ++at3;
4702
+ while (at3 < length && !">[]".includes(doc[at3])) at3++;
4703
+ if (at3 >= length) return true;
4704
+ if (doc[at3] !== ">") return false;
4705
+ enclosed = at3 - open;
4706
+ at3++;
4707
+ }
4708
+ if (enclosed === 0) {
4709
+ const bare = at3;
4710
+ while (at3 < length && !/[[\]\s]/u.test(doc[at3])) at3++;
4711
+ if (at3 === bare) return at3 >= length;
4712
+ }
4713
+ const gap = skip(at3, " \n");
4714
+ if (gap === at3 || gap >= length) return false;
4715
+ const opener2 = doc[gap];
4716
+ const closer = opener2 === '"' || opener2 === "'" ? opener2 : opener2 === "(" ? ")" : "";
4717
+ if (closer === "") return false;
4718
+ at3 = gap + 1;
4719
+ while (at3 < length && doc[at3] !== closer) {
4720
+ if (doc[at3] === "\n") {
4721
+ at3++;
4722
+ if (doc[at3] === "\n" || doc[at3] === closer) return false;
4723
+ }
4724
+ at3++;
4725
+ }
4726
+ return at3 >= length;
4727
+ }
4728
+ function watchDefinitionReader(tables) {
4729
+ const stock = tables.blockTokenizers.definition;
4730
+ tables.blockTokenizers.definition = function(eat, value2, silent) {
4731
+ const result = stock.call(this, eat, value2, silent);
4732
+ if (!result && !this.definitionCutOff && !this.inList && !this.inBlock && definitionCutOff(value2, 0) && // A top-level block's remaining text is the file's tail only because `adapt.ts` normalizes
4733
+ // line endings and drops a byte order mark first. A direct caller passing raw `\r\n` text
4734
+ // would get a silent `false` here.
4735
+ this.file.toString().endsWith(value2)) {
4736
+ this.definitionCutOff = true;
4737
+ }
4738
+ return result;
4739
+ };
4740
+ }
4741
+ function readsPastEnd(parser, tree) {
4742
+ const last = tree.children?.[tree.children.length - 1];
4743
+ return parser.definitionCutOff === true || last?.type === "definition" && last.title == null;
4744
+ }
4745
+
4020
4746
  // src/rp8/vendor/remark-footnotes/index.ts
4021
4747
  var remark_footnotes_default = footnotes;
4022
4748
  var tab3 = 9;
@@ -8261,12 +8987,6 @@ function keys(value2) {
8261
8987
  return result;
8262
8988
  }
8263
8989
 
8264
- // src/lib/tag-char.ts
8265
- var TAG_STOP = /[\u2000-\u206F\u2E00-\u2E7F'!"#$%&()*+,.:;<=>?@^`{|}~[\]\\\s]/u;
8266
- function isTagCharacter(char) {
8267
- return char !== "" && !TAG_STOP.test(char);
8268
- }
8269
-
8270
8990
  // src/rp8/reader.ts
8271
8991
  function along(point, columns2) {
8272
8992
  return {
@@ -8403,9 +9123,27 @@ function tablePipeStyles(tables) {
8403
9123
  tables.blockTokenizers.table = function(eat, value2, silent) {
8404
9124
  const end = tableEnd(value2);
8405
9125
  if (end === -1) return void 0;
8406
- return stockTable.call(this, eat, value2.slice(0, end), silent);
9126
+ const table2 = stockTable.call(this, eat, value2.slice(0, end), silent);
9127
+ if (table2 && typeof table2 === "object" && !startsWithPipe(value2)) {
9128
+ leadingIndentCells(table2, value2.slice(0, end));
9129
+ }
9130
+ return table2;
8407
9131
  };
8408
9132
  }
9133
+ function leadingIndentCells(table2, source) {
9134
+ const lines = source.split("\n");
9135
+ lines.splice(1, 1);
9136
+ for (const [index, row] of (table2.children ?? []).entries()) {
9137
+ const pipe = /^[ \t]+\|/u.exec(lines[index] ?? "");
9138
+ if (!pipe) continue;
9139
+ const at3 = along(row.position.start, pipe[0].length - 1);
9140
+ row.children?.unshift({
9141
+ type: "tableCell",
9142
+ children: [],
9143
+ position: { start: at3, end: { ...at3 } }
9144
+ });
9145
+ }
9146
+ }
8409
9147
  var Wikilink = /^!?\[\[(?:(?!\[\[)[^\n\r\u2028\u2029])+?\]\]/;
8410
9148
  function wikilinks(tables) {
8411
9149
  const wikilink2 = function(eat, value2, silent) {
@@ -8468,10 +9206,12 @@ function highlights(tables) {
8468
9206
  if (body === null) return void 0;
8469
9207
  if (silent === true) return true;
8470
9208
  const now = eat.now();
8471
- return eat(`==${body}==`)({
8472
- type: "highlight",
8473
- children: this.tokenizeInline(body, along(now, 2))
8474
- });
9209
+ const marker = colourMarkerLength(body);
9210
+ const children = marker > 0 && body.charAt(marker) === "#" ? [
9211
+ ...this.tokenizeInline(body.slice(0, marker), along(now, 2)),
9212
+ ...this.tokenizeInline(body.slice(marker), along(now, 2 + marker))
9213
+ ] : this.tokenizeInline(body, along(now, 2));
9214
+ return eat(`==${body}==`)({ type: "highlight", children });
8475
9215
  };
8476
9216
  highlight2.locator = (value2, fromIndex) => value2.indexOf("==", fromIndex);
8477
9217
  tables.inlineTokenizers.highlight = highlight2;
@@ -8568,6 +9308,20 @@ function nextWhere(frame, key, test) {
8568
9308
  }
8569
9309
  return table2;
8570
9310
  }
9311
+ function backtickBefore(frame, at3) {
9312
+ let table2 = frame.next.get("backtick-before");
9313
+ if (!table2) {
9314
+ const { value: value2 } = frame;
9315
+ table2 = new Int32Array(value2.length + 1);
9316
+ let last = -1;
9317
+ for (let i = 0; i <= value2.length; i++) {
9318
+ table2[i] = last;
9319
+ if (value2.charCodeAt(i) === 96) last = i;
9320
+ }
9321
+ frame.next.set("backtick-before", table2);
9322
+ }
9323
+ return table2[at3];
9324
+ }
8571
9325
  function closingBracket(frame, open) {
8572
9326
  if (!frame.lower) {
8573
9327
  const { value: value2 } = frame;
@@ -8620,6 +9374,14 @@ function speedUps(tables, features) {
8620
9374
  frames.pop();
8621
9375
  }
8622
9376
  };
9377
+ const stockHtml = tables.blockTokenizers.html;
9378
+ const html = function(eat, value2, silent) {
9379
+ let at3 = 0;
9380
+ while (value2.charCodeAt(at3) === 32 || value2.charCodeAt(at3) === 9) at3++;
9381
+ if (value2.charCodeAt(at3) !== 60) return void 0;
9382
+ return stockHtml.call(this, eat, value2, silent);
9383
+ };
9384
+ tables.blockTokenizers.html = html;
8623
9385
  const stockText = tables.inlineTokenizers.text;
8624
9386
  const text3 = function(eat, value2, silent) {
8625
9387
  const frame = frameOf(this, value2);
@@ -8641,13 +9403,22 @@ function speedUps(tables, features) {
8641
9403
  if (asked === -1 || asked > from || at3 !== -1 && at3 < from) {
8642
9404
  const position = reader.locator.call(this, value2, 1);
8643
9405
  at3 = position === -1 ? -1 : base + position;
8644
- if (name2 === "code" && at3 !== -1) at3 = spanCapable(frame)[at3];
9406
+ if (name2 === "code" && at3 !== -1) {
9407
+ const skipTo = spanCapable(frame)[at3];
9408
+ const amp = nextWhere(frame, "&", (v, i) => v.charCodeAt(i) === 38)[at3 - 1];
9409
+ if (amp === -1 || skipTo !== -1 && skipTo < amp) at3 = skipTo;
9410
+ else at3 = Math.max(at3, backtickBefore(frame, amp));
9411
+ }
8645
9412
  frame.askedAt[index] = from;
8646
9413
  frame.answer[index] = at3;
8647
9414
  }
8648
9415
  if (at3 !== -1 && at3 - base < min) min = at3 - base;
8649
9416
  }
8650
9417
  const subvalue = value2.slice(0, min);
9418
+ if (subvalue !== "" && !subvalue.includes("&")) {
9419
+ eat(subvalue)({ type: "text", value: subvalue });
9420
+ return void 0;
9421
+ }
8651
9422
  this.decode(
8652
9423
  subvalue,
8653
9424
  eat.now(),
@@ -8692,7 +9463,7 @@ function speedUps(tables, features) {
8692
9463
  }
8693
9464
  tables.inlineTokenizers[name2] = wrapped;
8694
9465
  };
8695
- const isSpace = (value2, at3) => /\s/.test(value2.charAt(at3));
9466
+ const isSpace2 = (value2, at3) => /\s/.test(value2.charAt(at3));
8696
9467
  precheck("link", (frame, base, value2) => {
8697
9468
  const open = base + (value2.charCodeAt(0) === 33 ? 1 : 0);
8698
9469
  if (frame.value.charCodeAt(open) !== 91) return false;
@@ -8700,7 +9471,7 @@ function speedUps(tables, features) {
8700
9471
  if (close2 === -1) return true;
8701
9472
  if (frame.value.charCodeAt(close2 + 1) !== 40) return true;
8702
9473
  let at3 = close2 + 2;
8703
- while (at3 < frame.value.length && isSpace(frame.value, at3)) at3++;
9474
+ while (at3 < frame.value.length && isSpace2(frame.value, at3)) at3++;
8704
9475
  if (frame.value.charCodeAt(at3) !== 60) return false;
8705
9476
  const gt = nextWhere(frame, ">", (v, i) => v.charCodeAt(i) === 62)[at3 + 1];
8706
9477
  const lf = nextWhere(frame, "\n", (v, i) => v.charCodeAt(i) === 10)[at3 + 1];
@@ -8725,11 +9496,33 @@ function speedUps(tables, features) {
8725
9496
  return nextWhere(frame, "]", (v, i) => v.charCodeAt(i) === 93)[base + 2] === -1;
8726
9497
  });
8727
9498
  }
9499
+ if (features.highlights) {
9500
+ precheck("highlight", (frame, base, value2) => {
9501
+ if (value2.charCodeAt(0) !== 61 || value2.charCodeAt(1) !== 61)
9502
+ return false;
9503
+ return nextWhere(
9504
+ frame,
9505
+ "highlight-closer",
9506
+ (v, i) => v.charCodeAt(i) === 61 && v.charCodeAt(i + 1) === 61 && !isSpace2(v, i - 1)
9507
+ )[base + 3] === -1;
9508
+ });
9509
+ }
9510
+ precheck("deletion", (frame, base, value2) => {
9511
+ if (value2.charCodeAt(0) !== 126 || value2.charCodeAt(1) !== 126)
9512
+ return false;
9513
+ if (value2.charCodeAt(2) === 126 && value2.charCodeAt(3) === 126)
9514
+ return false;
9515
+ return nextWhere(
9516
+ frame,
9517
+ "strike-closer",
9518
+ (v, i) => i >= 2 && v.charCodeAt(i) === 126 && v.charCodeAt(i - 1) === 126 && !isSpace2(v, i - 2)
9519
+ )[base + 4] === -1;
9520
+ });
8728
9521
  precheck("autoLink", (frame, base, value2) => {
8729
9522
  if (value2.charCodeAt(0) !== 60) return false;
8730
9523
  const stop = nextWhere(frame, "autolink-stop", (v, i) => {
8731
9524
  const c2 = v.charCodeAt(i);
8732
- return c2 === 62 || c2 === 64 || c2 === 58 && v.charCodeAt(i + 1) === 47 || isSpace(v, i);
9525
+ return c2 === 62 || c2 === 64 || c2 === 58 && v.charCodeAt(i + 1) === 47 || isSpace2(v, i);
8733
9526
  })[base + 1];
8734
9527
  if (stop === -1 || stop === base + 1) return true;
8735
9528
  const c = frame.value.charCodeAt(stop);
@@ -8738,10 +9531,33 @@ function speedUps(tables, features) {
8738
9531
  const end = nextWhere(
8739
9532
  frame,
8740
9533
  "space-or-gt",
8741
- (v, i) => v.charCodeAt(i) === 62 || isSpace(v, i)
9534
+ (v, i) => v.charCodeAt(i) === 62 || isSpace2(v, i)
8742
9535
  )[rest];
8743
9536
  return end === -1 || end === rest || frame.value.charCodeAt(end) !== 62;
8744
9537
  });
9538
+ const searchOnce = (name2, opens2) => {
9539
+ const stock = tables.inlineTokenizers[name2];
9540
+ const wrapped = function(eat, value2, silent) {
9541
+ const frame = frameOf(this, value2);
9542
+ if (!frame || !opens2(value2)) return stock.call(this, eat, value2, silent);
9543
+ if (frame.noCloser?.has(name2)) return void 0;
9544
+ const result = stock.call(this, eat, value2, silent);
9545
+ if (result === void 0) (frame.noCloser ??= /* @__PURE__ */ new Set()).add(name2);
9546
+ return result;
9547
+ };
9548
+ wrapped.locator = stock.locator;
9549
+ tables.inlineTokenizers[name2] = wrapped;
9550
+ };
9551
+ if (features.math) {
9552
+ searchOnce("math", (value2) => {
9553
+ const after = value2.charCodeAt(1);
9554
+ return value2.charCodeAt(0) === 36 && after !== 36 && after !== 32 && after !== 9;
9555
+ });
9556
+ searchOnce(
9557
+ "doubleDollarMath",
9558
+ (value2) => value2.charCodeAt(0) === 36 && value2.charCodeAt(1) === 36
9559
+ );
9560
+ }
8745
9561
  }
8746
9562
  function backtickRuns(value2) {
8747
9563
  const runs = /* @__PURE__ */ new Map();
@@ -8786,6 +9602,7 @@ function buildParser(features, fast) {
8786
9602
  if (features.tags) tags(tables);
8787
9603
  if (features.positionedBreaks) softBreaks(tables);
8788
9604
  if (fast) speedUps(tables, features);
9605
+ if (features.readsPastEnd) watchDefinitionReader(tables);
8789
9606
  return ObsidianParser;
8790
9607
  }
8791
9608
  var parserClasses = /* @__PURE__ */ new Map();
@@ -8805,10 +9622,31 @@ function parse8WithOffsets(doc, features, fast = true) {
8805
9622
  };
8806
9623
  const parser = new parserClass(doc, file);
8807
9624
  const tree = parser.parse();
8808
- return { tree, offsets: { ...parser.offset } };
9625
+ const offsets = { ...parser.offset };
9626
+ return features.readsPastEnd ? { tree, offsets, readsPastEnd: readsPastEnd(parser, tree) } : { tree, offsets };
8809
9627
  }
8810
9628
 
8811
9629
  // src/rp8/adapt.ts
9630
+ var legacyDefaults = {
9631
+ wikilinks: true,
9632
+ highlights: true,
9633
+ comments: true,
9634
+ tags: true,
9635
+ customTaskChars: true,
9636
+ math: true,
9637
+ frontmatter: true,
9638
+ keepComments: true,
9639
+ parseComments: true,
9640
+ lineOffsets: true,
9641
+ bareFootnoteRefs: true,
9642
+ inlineFootnotes: true,
9643
+ callouts: true,
9644
+ positionedBreaks: true,
9645
+ bomOffsets: false,
9646
+ readsPastEnd: false,
9647
+ keepUndefinedReferences: false,
9648
+ inertFootnoteRefs: false
9649
+ };
8812
9650
  var decodeLabel = (label) => typeof label === "string" ? decodeString(label) : label;
8813
9651
  var fold = (label) => label.replace(/[\t\n\r ]+/gu, " ").toLowerCase().toUpperCase().toLowerCase();
8814
9652
  function collectDefinitions(node2, links, footnotes2) {
@@ -8832,7 +9670,7 @@ function mergeText2(node2) {
8832
9670
  }
8833
9671
  node2.children = out;
8834
9672
  }
8835
- function pointAt(doc, offset) {
9673
+ function pointAt2(doc, offset) {
8836
9674
  const bol = doc.lastIndexOf("\n", offset - 1) + 1;
8837
9675
  let line = 1;
8838
9676
  for (let at3 = doc.indexOf("\n"); at3 !== -1 && at3 < offset; at3 = doc.indexOf("\n", at3 + 1)) {
@@ -8843,7 +9681,7 @@ function pointAt(doc, offset) {
8843
9681
  function pullBack(node2, doc) {
8844
9682
  const { start, end } = node2.position;
8845
9683
  if (end.column === 1 && end.line > start.line && end.offset > start.offset) {
8846
- node2.position.end = pointAt(doc, end.offset - 1);
9684
+ node2.position.end = pointAt2(doc, end.offset - 1);
8847
9685
  }
8848
9686
  }
8849
9687
  function at(node2) {
@@ -8857,7 +9695,7 @@ function text2(doc, from, to) {
8857
9695
  return {
8858
9696
  type: "text",
8859
9697
  value: doc.slice(from, to),
8860
- position: { start: pointAt(doc, from), end: pointAt(doc, to) }
9698
+ position: { start: pointAt2(doc, from), end: pointAt2(doc, to) }
8861
9699
  };
8862
9700
  }
8863
9701
  var UNESCAPE = /\\(#)/g;
@@ -8899,12 +9737,20 @@ function wikilink(raw) {
8899
9737
  const writtenHeading = node2.heading;
8900
9738
  node2.path = resolveTarget(node2.path);
8901
9739
  node2.heading = resolveTarget(node2.heading);
8902
- const spelled = spelledTarget(written2, writtenHeading, node2);
9740
+ let spelled = spelledTarget(written2, writtenHeading, node2);
8903
9741
  const resolved = parts.heading ? "heading" : parts.path ? "path" : void 0;
8904
9742
  let stripped;
8905
9743
  if (resolved && node2[resolved].endsWith("\\")) {
8906
- node2[resolved] = node2[resolved].slice(0, -1);
9744
+ const kept = node2[resolved].slice(0, -1);
9745
+ node2[resolved] = dropClosingBackslash(node2[resolved]);
8907
9746
  stripped = resolved;
9747
+ if (kept !== node2[resolved] && !parts.alias) {
9748
+ const key = resolved === "path" ? "spelledPath" : "spelledHeading";
9749
+ spelled = {
9750
+ ...spelled,
9751
+ [key]: resolved === "path" ? written2 : writtenHeading
9752
+ };
9753
+ }
8908
9754
  }
8909
9755
  const value2 = embedded ? node2.path : parts.alias ? node2.alias : [written2, ...writtenHeading.split("#")].filter(Boolean).join(" > ");
8910
9756
  return {
@@ -8933,17 +9779,42 @@ var MATH_INLINE = (value2) => ({
8933
9779
  hProperties: { className: ["language-math", "math-inline"] },
8934
9780
  hChildren: [{ type: "text", value: value2 }]
8935
9781
  });
8936
- function convertAll(nodes, ctx, inNote) {
8937
- const out = [];
8938
- for (const node2 of nodes ?? []) out.push(...convert2(node2, ctx, inNote));
8939
- return out;
9782
+ function convertTree(root, ctx) {
9783
+ const { keepComments, parseComments } = ctx.options;
9784
+ const stack = [{ node: root, inNote: false, next: 0, kids: [] }];
9785
+ for (; ; ) {
9786
+ const top = stack[stack.length - 1];
9787
+ const { node: node2, inNote } = top;
9788
+ const child = node2.type !== "percentComment" || keepComments && parseComments ? node2.children?.[top.next++] : void 0;
9789
+ if (child) {
9790
+ const under = inNote || node2.type === "footnote";
9791
+ stack.push({ node: child, inNote: under, next: 0, kids: [] });
9792
+ continue;
9793
+ }
9794
+ stack.pop();
9795
+ const made = convert2(node2, ctx, inNote, top.kids);
9796
+ const parent = stack[stack.length - 1];
9797
+ if (!parent) return made[0];
9798
+ parent.kids.push(...made);
9799
+ }
8940
9800
  }
8941
- function convert2(node2, ctx, inNote) {
9801
+ function convert2(node2, ctx, inNote, converted) {
8942
9802
  const { doc } = ctx;
8943
9803
  const position = at(node2);
8944
- const kids = (n = inNote) => convertAll(node2.children, ctx, n);
9804
+ const kids = () => converted;
8945
9805
  switch (node2.type) {
8946
9806
  case "footnoteReference":
9807
+ if (inNote && ctx.options.inertFootnoteRefs) {
9808
+ return [
9809
+ {
9810
+ type: "footnoteReference",
9811
+ identifier: node2.identifier,
9812
+ label: node2.label,
9813
+ data: { inert: true },
9814
+ position
9815
+ }
9816
+ ];
9817
+ }
8947
9818
  if (inNote || !ctx.options.bareFootnoteRefs && !ctx.footnotes.has(String(node2.identifier)))
8948
9819
  return [text2(doc, position.start.offset, position.end.offset)];
8949
9820
  return [
@@ -8967,7 +9838,7 @@ function convert2(node2, ctx, inNote) {
8967
9838
  ];
8968
9839
  }
8969
9840
  case "footnote":
8970
- return [{ type: "footnote", children: kids(true), position }];
9841
+ return [{ type: "footnote", children: kids(), position }];
8971
9842
  case "highlight": {
8972
9843
  const strip = (n) => {
8973
9844
  if (n.type === "text")
@@ -9025,7 +9896,9 @@ function convert2(node2, ctx, inNote) {
9025
9896
  case "linkReference":
9026
9897
  case "imageReference": {
9027
9898
  const { type, label, referenceType, alt } = node2;
9028
- if (referenceType !== "full" && !ctx.definitions.has(fold(String(label)))) {
9899
+ const isDefined = ctx.definitions.has(fold(String(label)));
9900
+ const keep = ctx.options.keepUndefinedReferences;
9901
+ if (referenceType !== "full" && !isDefined && !keep) {
9029
9902
  const children = type === "linkReference" ? kids() : [];
9030
9903
  if (children.length === 0)
9031
9904
  return [text2(doc, position.start.offset, position.end.offset)];
@@ -9040,7 +9913,15 @@ function convert2(node2, ctx, inNote) {
9040
9913
  const identifier = normalizeIdentifier(
9041
9914
  String(label ?? node2.identifier)
9042
9915
  ).toLowerCase();
9043
- const spelled = referenceType === "full" && decodeLabel(label) !== label ? { data: { spelledLabel: label } } : {};
9916
+ const kept = keep && !isDefined;
9917
+ const written2 = (referenceType === "full" || kept) && decodeLabel(label) !== label ? { spelledLabel: label } : {};
9918
+ const spelled = kept || "spelledLabel" in written2 ? { data: { ...written2, ...kept ? { defined: false } : {} } } : {};
9919
+ const open = kept && referenceType === "shortcut" && doc[position.end.offset - 1] === "[";
9920
+ const own5 = open ? {
9921
+ start: position.start,
9922
+ end: pointAt2(doc, position.end.offset - 1)
9923
+ } : position;
9924
+ const tail = open ? [text2(doc, position.end.offset - 1, position.end.offset)] : [];
9044
9925
  return [
9045
9926
  type === "imageReference" ? {
9046
9927
  type,
@@ -9048,7 +9929,7 @@ function convert2(node2, ctx, inNote) {
9048
9929
  label: decodeLabel(label),
9049
9930
  referenceType,
9050
9931
  alt,
9051
- position,
9932
+ position: own5,
9052
9933
  ...spelled
9053
9934
  } : {
9054
9935
  type,
@@ -9056,9 +9937,10 @@ function convert2(node2, ctx, inNote) {
9056
9937
  label: decodeLabel(label),
9057
9938
  referenceType,
9058
9939
  children: kids(),
9059
- position,
9940
+ position: own5,
9060
9941
  ...spelled
9061
- }
9942
+ },
9943
+ ...tail
9062
9944
  ];
9063
9945
  }
9064
9946
  case "definition": {
@@ -9084,9 +9966,18 @@ function convert2(node2, ctx, inNote) {
9084
9966
  return [{ type: "blockquote", children, position }];
9085
9967
  const marker = /^\[!([^\]\n]+)\]([-+]?)/u.exec(String(head.marker));
9086
9968
  const titleBlocks = children[0].children ?? [];
9087
- const collect = (nodes) => nodes.map(
9088
- (n) => "value" in n ? String(n.value) : collect(n.children ?? [])
9089
- ).join("");
9969
+ const collect = (nodes) => nodes.map((n) => {
9970
+ if ("value" in n) return String(n.value);
9971
+ if (n.data?.defined === false && n.referenceType !== "full") {
9972
+ const { start, end } = n.position;
9973
+ const kids2 = n.children ?? [];
9974
+ if (kids2.length === 0) return doc.slice(start.offset, end.offset);
9975
+ const first = kids2[0].position.start.offset;
9976
+ const last = kids2[kids2.length - 1].position.end.offset;
9977
+ return doc.slice(start.offset, first) + collect(kids2) + doc.slice(last, end.offset);
9978
+ }
9979
+ return collect(n.children ?? []);
9980
+ }).join("");
9090
9981
  let title = collect(titleBlocks).trim();
9091
9982
  const body = children.slice(1);
9092
9983
  const titleNode = [];
@@ -9143,7 +10034,7 @@ function convert2(node2, ctx, inNote) {
9143
10034
  if (lead > 0 && lead < String(head.value).length) {
9144
10035
  head.value = String(head.value).slice(lead);
9145
10036
  head.position = {
9146
- start: pointAt(doc, head.position.start.offset + lead),
10037
+ start: pointAt2(doc, head.position.start.offset + lead),
9147
10038
  end: head.position.end
9148
10039
  };
9149
10040
  }
@@ -9167,7 +10058,7 @@ function clampToParent(node2) {
9167
10058
  clampToParent(child);
9168
10059
  }
9169
10060
  }
9170
- function restoreCrlf(node2, crlf) {
10061
+ function restoreCrlf(node2, crlf, seen = /* @__PURE__ */ new Set()) {
9171
10062
  const shift2 = (offset) => {
9172
10063
  let low = 0;
9173
10064
  let high = crlf.length;
@@ -9179,10 +10070,13 @@ function restoreCrlf(node2, crlf) {
9179
10070
  return offset + low;
9180
10071
  };
9181
10072
  if (node2.position) {
9182
- node2.position.start.offset = shift2(node2.position.start.offset);
9183
- node2.position.end.offset = shift2(node2.position.end.offset);
10073
+ for (const point of [node2.position.start, node2.position.end]) {
10074
+ if (seen.has(point)) continue;
10075
+ seen.add(point);
10076
+ point.offset = shift2(point.offset);
10077
+ }
9184
10078
  }
9185
- for (const child of node2.children ?? []) restoreCrlf(child, crlf);
10079
+ for (const child of node2.children ?? []) restoreCrlf(child, crlf, seen);
9186
10080
  }
9187
10081
  function parseLegacy8(input, options) {
9188
10082
  const source = input.startsWith("\uFEFF") ? input.slice(1) : input;
@@ -9191,26 +10085,28 @@ function parseLegacy8(input, options) {
9191
10085
  crlf.push(at3 - crlf.length);
9192
10086
  }
9193
10087
  const doc = source.replace(/\r\n?/g, "\n");
9194
- const { tree, offsets } = parse8WithOffsets(doc, options);
10088
+ const { tree, offsets, readsPastEnd: readsPastEnd2 } = parse8WithOffsets(doc, options);
9195
10089
  const definitions = /* @__PURE__ */ new Set();
9196
10090
  const footnotes2 = /* @__PURE__ */ new Set();
9197
10091
  collectDefinitions(tree, definitions, footnotes2);
9198
- const root = convert2(
9199
- tree,
9200
- { doc, definitions, footnotes: footnotes2, options },
9201
- false
9202
- )[0];
10092
+ const root = convertTree(tree, { doc, definitions, footnotes: footnotes2, options });
9203
10093
  mergeText2(root);
9204
10094
  clampToParent(root);
9205
10095
  if (options.lineOffsets) root.data = { lineOffsets: offsets };
10096
+ if (readsPastEnd2 !== void 0) {
10097
+ root.data = { ...root.data, readsPastEnd: readsPastEnd2 };
10098
+ }
9206
10099
  if (options.math) mathDisplayTransform(root, doc);
9207
10100
  if (crlf.length > 0) restoreCrlf(root, crlf);
10101
+ if (options.callouts) calloutMarkerTransform(root, source);
10102
+ if (source !== input && options.bomOffsets)
10103
+ countByteOrderMark(root);
9208
10104
  return root;
9209
10105
  }
9210
10106
 
9211
10107
  // src/lib/comment-hoist.ts
9212
10108
  var spansLines = (node2) => (node2.position?.end?.line ?? 0) > (node2.position?.start?.line ?? 0);
9213
- var MARKER2 = "%%";
10109
+ var MARKER3 = "%%";
9214
10110
  var CONTAINERS = /* @__PURE__ */ new Set(["list", "blockquote", "callout"]);
9215
10111
  function commentHoistTransform(tree, source) {
9216
10112
  visit(tree, "paragraph", (node2, index, parent) => {
@@ -9274,67 +10170,19 @@ function liftContainerComment(tree, source) {
9274
10170
  const comment2 = host.children[at3];
9275
10171
  const from = comment2.position?.start?.offset;
9276
10172
  if (from === void 0) continue;
9277
- if (source.slice(from + MARKER2.length).includes(MARKER2)) continue;
10173
+ if (source.slice(from + MARKER3.length).includes(MARKER3)) continue;
9278
10174
  host.children.splice(at3);
9279
10175
  for (const ancestor of [...path].reverse()) pullBack2(ancestor);
9280
10176
  tree.children.splice(index + 1, 0, comment2);
9281
- comment2.value = source.slice(from + MARKER2.length);
10177
+ comment2.value = source.slice(from + MARKER3.length);
9282
10178
  if (comment2.position) comment2.position.end = endPoint(source);
9283
10179
  delete comment2.children;
9284
10180
  tree.children.splice(index + 2);
9285
10181
  }
9286
10182
  }
9287
10183
 
9288
- // src/lib/line-map.ts
9289
- var QUOTE_PREFIX = /^[ \t]*>[ \t]?/u;
9290
- var LEADING_QUOTES = /^(?:[ \t]*>[ \t]?)*/u;
9291
- function mapLines(source, start, end, startLine, quoteDepth2) {
9292
- const raw = source.slice(start, end).split("\n");
9293
- const text3 = [];
9294
- const starts = [];
9295
- const lines = [];
9296
- let offset = start;
9297
- const lineStart = source.lastIndexOf("\n", start - 1) + 1;
9298
- const ownPrefix = LEADING_QUOTES.exec(source.slice(lineStart, start))?.[0] ?? "";
9299
- const depth = quoteDepth2 ?? Math.max(1, (ownPrefix.match(/>/gu) ?? []).length);
9300
- for (let index = 0; index < raw.length; index++) {
9301
- const line = raw[index];
9302
- let prefix2 = "";
9303
- if (index > 0) {
9304
- for (let level = 0; level < depth; level++) {
9305
- const next = QUOTE_PREFIX.exec(line.slice(prefix2.length))?.[0];
9306
- if (!next) break;
9307
- prefix2 += next;
9308
- }
9309
- }
9310
- starts.push(offset + prefix2.length);
9311
- lines.push(startLine + index);
9312
- text3.push(line.slice(prefix2.length));
9313
- offset += line.length + 1;
9314
- }
9315
- return { text: text3.join("\n"), starts, lines };
9316
- }
9317
- function shiftByMap(node2, map, from) {
9318
- const candidate = node2;
9319
- if (candidate.position) {
9320
- for (const point of [candidate.position.start, candidate.position.end]) {
9321
- const index = from + point.line - 1;
9322
- const lineStart = map.starts[index];
9323
- const line = map.lines[index];
9324
- if (lineStart !== void 0 && line !== void 0) {
9325
- point.offset = lineStart + point.column - 1;
9326
- point.line = line;
9327
- }
9328
- }
9329
- }
9330
- for (const child of candidate.children ?? []) shiftByMap(child, map, from);
9331
- }
9332
- function paragraphSlice(slice) {
9333
- return slice.replace(/(\r?\n)[ \t]*(?:>[ \t]?)*[ \t]*/gu, "$1");
9334
- }
9335
-
9336
10184
  // src/lib/comment-definition-extent.ts
9337
- var MARKER3 = "%%";
10185
+ var MARKER4 = "%%";
9338
10186
  var BLANK = /^[ \t]*(?:>[ \t]*)*$/u;
9339
10187
  var isComment = (node2) => node2?.type === "comment";
9340
10188
  function commentDefinitionExtentTransform(tree, source, reparse) {
@@ -9355,7 +10203,7 @@ function commentDefinitionExtentTransform(tree, source, reparse) {
9355
10203
  if (from === void 0 || ownEnd === void 0 || end === void 0 || startLine === void 0) {
9356
10204
  continue;
9357
10205
  }
9358
- if (source.slice(from, ownEnd).slice(MARKER3.length).includes(MARKER3)) {
10206
+ if (source.slice(from, ownEnd).slice(MARKER4.length).includes(MARKER4)) {
9359
10207
  continue;
9360
10208
  }
9361
10209
  const lines = source.slice(from, end).split("\n");
@@ -9365,7 +10213,7 @@ function commentDefinitionExtentTransform(tree, source, reparse) {
9365
10213
  if (blank === -1) continue;
9366
10214
  const kept = lines.slice(0, blank).join("\n");
9367
10215
  const cut = from + kept.length;
9368
- comment2.value = source.slice(from + MARKER3.length, cut);
10216
+ comment2.value = source.slice(from + MARKER4.length, cut);
9369
10217
  if (comment2.position) {
9370
10218
  comment2.position.end = { ...comment2.position.end, offset: cut };
9371
10219
  }
@@ -9403,74 +10251,6 @@ function resolveEscapes(line) {
9403
10251
  return out;
9404
10252
  }
9405
10253
 
9406
- // src/lib/normalize-points.ts
9407
- function pointAt2(source, offset) {
9408
- const starts = lineStartsOf(source);
9409
- let low = 0;
9410
- let high = starts.length - 1;
9411
- while (low < high) {
9412
- const middle = low + high + 1 >> 1;
9413
- if (starts[middle] <= offset) low = middle;
9414
- else high = middle - 1;
9415
- }
9416
- return { line: low + 1, column: offset - starts[low] + 1, offset };
9417
- }
9418
- var cachedSource;
9419
- var cachedStarts = [0];
9420
- function lineStartsOf(source) {
9421
- if (source === cachedSource) return cachedStarts;
9422
- const starts = [0];
9423
- for (let index = 0; index < source.length; index++) {
9424
- if (source[index] === "\r" && source[index + 1] === "\n") continue;
9425
- if (source[index] === "\n" || source[index] === "\r")
9426
- starts.push(index + 1);
9427
- }
9428
- cachedSource = source;
9429
- cachedStarts = starts;
9430
- return starts;
9431
- }
9432
- function span(candidate, source, from, to) {
9433
- if (source !== "" && from !== void 0 && to !== void 0) {
9434
- candidate.position = {
9435
- start: pointAt2(source, from),
9436
- end: pointAt2(source, to)
9437
- };
9438
- }
9439
- return candidate;
9440
- }
9441
- function normalizePointsTransform(tree, source) {
9442
- if (source === "") return;
9443
- const starts = [0];
9444
- for (let index = 0; index < source.length; index++) {
9445
- const character = source[index];
9446
- if (character === "\r" && source[index + 1] === "\n") continue;
9447
- if (character === "\n" || character === "\r") starts.push(index + 1);
9448
- }
9449
- const lineAt = (offset) => {
9450
- let low = 0;
9451
- let high = starts.length - 1;
9452
- while (low < high) {
9453
- const middle = low + high + 1 >> 1;
9454
- if (starts[middle] <= offset) low = middle;
9455
- else high = middle - 1;
9456
- }
9457
- return low;
9458
- };
9459
- visit(tree, (node2) => {
9460
- const position = node2.position;
9461
- if (!position) return;
9462
- for (const point of [position.start, position.end]) {
9463
- const offset = point.offset;
9464
- if (offset === void 0 || offset < 0 || offset > source.length) {
9465
- continue;
9466
- }
9467
- const index = lineAt(offset);
9468
- point.line = index + 1;
9469
- point.column = offset - starts[index] + 1;
9470
- }
9471
- });
9472
- }
9473
-
9474
10254
  // src/lib/comment-in-definition.ts
9475
10255
  var isComment2 = (node2) => node2?.type === "comment";
9476
10256
  var CONTINUATION_INDENT = /^(?: {1,4}|\t)/u;
@@ -9639,8 +10419,8 @@ function definitionLazySplitTransform(tree, source, reparse) {
9639
10419
  const tail = reparse(source.slice(firstBreak + 1, to)).children;
9640
10420
  if (tail.length === 0) continue;
9641
10421
  if (previous2.type === "list" && tail[0]?.type !== "comment") continue;
9642
- shiftOnto(head, from);
9643
- for (const node2 of tail) shiftOnto(node2, firstBreak + 1);
10422
+ shiftOnto2(head, from);
10423
+ for (const node2 of tail) shiftOnto2(node2, firstBreak + 1);
9644
10424
  children.splice(index, 1, head, ...tail);
9645
10425
  const last = tail[tail.length - 1];
9646
10426
  const opener2 = last?.position?.start?.offset;
@@ -9651,18 +10431,18 @@ function definitionLazySplitTransform(tree, source, reparse) {
9651
10431
  }
9652
10432
  }
9653
10433
  }
9654
- function shiftOnto(node2, offset) {
10434
+ function shiftOnto2(node2, offset) {
9655
10435
  const candidate = node2;
9656
10436
  if (candidate.position) {
9657
10437
  for (const point of [candidate.position.start, candidate.position.end]) {
9658
10438
  point.offset = offset + (point.offset ?? 0);
9659
10439
  }
9660
10440
  }
9661
- for (const child of candidate.children ?? []) shiftOnto(child, offset);
10441
+ for (const child of candidate.children ?? []) shiftOnto2(child, offset);
9662
10442
  }
9663
10443
 
9664
10444
  // src/lib/quote-comment-lazy.ts
9665
- var MARKER4 = "%%";
10445
+ var MARKER5 = "%%";
9666
10446
  var isComment3 = (node2) => node2?.type === "comment";
9667
10447
  var ENDS_CONTAINER = /^ {0,3}(?:#{1,6}(?:[ \t]|$)|```|~~~|[-*+][ \t]|\d{1,9}[.)][ \t])/u;
9668
10448
  var SETEXT_UNDERLINE = /^ {0,3}(?:=+|-+)[ \t]*$/u;
@@ -9684,7 +10464,7 @@ function quoteCommentLazyTransform(tree, source) {
9684
10464
  const from = comment2.position?.start?.offset;
9685
10465
  if (from === void 0) continue;
9686
10466
  const quoteEnd = container.position?.end?.offset ?? source.length;
9687
- if (source.slice(from + MARKER4.length, quoteEnd).includes(MARKER4)) continue;
10467
+ if (source.slice(from + MARKER5.length, quoteEnd).includes(MARKER5)) continue;
9688
10468
  const reach = containerBound(source, from);
9689
10469
  const swallowed = [];
9690
10470
  for (let at3 = chain.length - 1; at3 >= 1; at3--) {
@@ -9706,12 +10486,12 @@ function quoteCommentLazyTransform(tree, source) {
9706
10486
  if (swallowed.length === 0) continue;
9707
10487
  const body = mapLines(
9708
10488
  source,
9709
- from + MARKER4.length,
10489
+ from + MARKER5.length,
9710
10490
  reach,
9711
10491
  source.slice(0, from).split("\n").length,
9712
10492
  depth
9713
10493
  ).text;
9714
- const close2 = body.indexOf(MARKER4);
10494
+ const close2 = body.indexOf(MARKER5);
9715
10495
  comment2.value = close2 === -1 ? body : body.slice(0, close2);
9716
10496
  if (comment2.position) comment2.position.end = pointOf(source, reach);
9717
10497
  comment2.children = swallowed;
@@ -9723,13 +10503,27 @@ function quoteCommentLazyTransform(tree, source) {
9723
10503
  }
9724
10504
  }
9725
10505
  }
9726
- function commentPath(parent, chain) {
9727
- const tail = parent.children[parent.children.length - 1];
9728
- if (isComment3(tail)) return { comment: tail, chain };
9729
- for (const child of parent.children) {
10506
+ function commentPath(root, rootChain) {
10507
+ const pending = [];
10508
+ const enter = (parent, chain) => {
10509
+ const tail = parent.children[parent.children.length - 1];
10510
+ if (isComment3(tail)) return { comment: tail, chain };
10511
+ pending.push({ parent, chain, next: 0 });
10512
+ return void 0;
10513
+ };
10514
+ const first = enter(root, rootChain);
10515
+ if (first) return first;
10516
+ while (pending.length > 0) {
10517
+ const top = pending[pending.length - 1];
10518
+ const child = top.parent.children[top.next];
10519
+ if (child === void 0) {
10520
+ pending.pop();
10521
+ continue;
10522
+ }
10523
+ top.next += 1;
9730
10524
  if (child.type !== "blockquote") continue;
9731
- const found = commentPath(child, [
9732
- ...chain,
10525
+ const found = enter(child, [
10526
+ ...top.chain,
9733
10527
  child
9734
10528
  ]);
9735
10529
  if (found) return found;
@@ -9771,21 +10565,29 @@ function pointOf(source, offset) {
9771
10565
  }
9772
10566
 
9773
10567
  // src/lib/item-comment-lazy.ts
9774
- var MARKER5 = "%%";
10568
+ var MARKER6 = "%%";
9775
10569
  var isComment4 = (node2) => node2?.type === "comment";
9776
10570
  var ENDS_ITEM = /^ {0,3}(?:#{1,6}(?:[ \t]|$)|```|~~~|[-*+][ \t]|\d{1,9}[.)][ \t])/u;
9777
10571
  function itemCommentLazyTransform(tree, source) {
9778
10572
  if (source === "") return;
9779
10573
  absorb(tree, source, []);
9780
10574
  }
9781
- function absorb(node2, source, ancestors) {
9782
- const children = node2.children;
9783
- if (!Array.isArray(children)) return;
9784
- if (node2.type === "listItem") {
9785
- swallow(node2, source, ancestors);
9786
- }
9787
- for (const child of [...children]) {
9788
- absorb(child, source, [...ancestors, node2]);
10575
+ function absorb(root, source, rootAncestors) {
10576
+ const pending = [
10577
+ [root, rootAncestors]
10578
+ ];
10579
+ while (pending.length > 0) {
10580
+ const [node2, ancestors] = pending.pop();
10581
+ const children = node2.children;
10582
+ if (!Array.isArray(children)) continue;
10583
+ if (node2.type === "listItem") {
10584
+ swallow(node2, source, ancestors);
10585
+ }
10586
+ const below = [...ancestors, node2];
10587
+ const snapshot = [...children];
10588
+ for (let at3 = snapshot.length - 1; at3 >= 0; at3--) {
10589
+ pending.push([snapshot[at3], below]);
10590
+ }
9789
10591
  }
9790
10592
  }
9791
10593
  function swallow(item, source, ancestors) {
@@ -9794,7 +10596,7 @@ function swallow(item, source, ancestors) {
9794
10596
  const comment2 = item.children[at3];
9795
10597
  const from = comment2.position?.start?.offset;
9796
10598
  if (from === void 0) return;
9797
- if (source.slice(from + MARKER5.length).includes(MARKER5)) return;
10599
+ if (source.slice(from + MARKER6.length).includes(MARKER6)) return;
9798
10600
  const reach = itemBound(source, from);
9799
10601
  const chain = [...ancestors, item];
9800
10602
  const swallowed = [];
@@ -9811,7 +10613,7 @@ function swallow(item, source, ancestors) {
9811
10613
  }
9812
10614
  }
9813
10615
  if (swallowed.length === 0) return;
9814
- comment2.value = source.slice(from + MARKER5.length, reach);
10616
+ comment2.value = source.slice(from + MARKER6.length, reach);
9815
10617
  if (comment2.position) comment2.position.end = pointOf2(source, reach);
9816
10618
  comment2.children = swallowed;
9817
10619
  for (const ancestor of [item, ...ancestors].reverse()) {
@@ -10482,15 +11284,29 @@ function definitionNestedLabelTransform(tree, source) {
10482
11284
  unnest(target.parent, target.index, source);
10483
11285
  }
10484
11286
  }
10485
- function find(node2, inDefinition, lines) {
10486
- const children = node2.children;
10487
- if (!Array.isArray(children)) return void 0;
10488
- for (const [index, child] of children.entries()) {
11287
+ function find(root, inRootDefinition, lines) {
11288
+ if (!Array.isArray(root.children)) return void 0;
11289
+ const pending = [{ node: root, inDefinition: inRootDefinition, next: 0 }];
11290
+ while (pending.length > 0) {
11291
+ const top = pending[pending.length - 1];
11292
+ const { node: node2, inDefinition } = top;
11293
+ const index = top.next;
11294
+ const child = node2.children?.[index];
11295
+ if (child === void 0) {
11296
+ pending.pop();
11297
+ continue;
11298
+ }
11299
+ top.next = index + 1;
10489
11300
  const isDefinition = child.type === "footnoteDefinition";
10490
11301
  if (isDefinition && inDefinition && continues(child, lines))
10491
11302
  return { parent: node2, index };
10492
- const found = find(child, inDefinition || isDefinition, lines);
10493
- if (found) return found;
11303
+ if (Array.isArray(child.children)) {
11304
+ pending.push({
11305
+ node: child,
11306
+ inDefinition: inDefinition || isDefinition,
11307
+ next: 0
11308
+ });
11309
+ }
10494
11310
  }
10495
11311
  return void 0;
10496
11312
  }
@@ -10536,9 +11352,15 @@ function labelText(definition2, source, start) {
10536
11352
  };
10537
11353
  }
10538
11354
  function firstContentOffset(node2) {
10539
- const child = node2.children?.[0];
10540
- if (!child) return void 0;
10541
- return firstContentOffset(child) ?? child.position?.start.offset;
11355
+ const chain = [];
11356
+ for (let child = node2.children?.[0]; child; child = child.children?.[0]) {
11357
+ chain.push(child);
11358
+ }
11359
+ for (let at3 = chain.length - 1; at3 >= 0; at3--) {
11360
+ const offset = chain[at3].position?.start.offset;
11361
+ if (offset !== void 0) return offset;
11362
+ }
11363
+ return void 0;
10542
11364
  }
10543
11365
  function offsetPoint(definition2, offset) {
10544
11366
  const start = definition2.position.start;
@@ -10596,7 +11418,7 @@ function definitionMathAbsorbTransform(tree, source) {
10596
11418
 
10597
11419
  // src/lib/definition-end-before-label.ts
10598
11420
  var LABEL3 = /^\[\^?[^\]]*\]:/u;
10599
- var QUOTE_PREFIX2 = /^[ \t]*(?:> ?)+/u;
11421
+ var QUOTE_PREFIX3 = /^[ \t]*(?:> ?)+/u;
10600
11422
  function quoteDepth(line) {
10601
11423
  let rest = line.replace(/^[ \t]+/u, "");
10602
11424
  let depth = 0;
@@ -10619,7 +11441,7 @@ function definitionEndBeforeLabelTransform(tree, source) {
10619
11441
  const ownDepth = quoteDepth(lines[start.line - 1] ?? "");
10620
11442
  for (let line = start.line; line < end.line; line++) {
10621
11443
  const text3 = lines[line] ?? "";
10622
- const inner = text3.replace(QUOTE_PREFIX2, "");
11444
+ const inner = text3.replace(QUOTE_PREFIX3, "");
10623
11445
  if (inner !== text3) {
10624
11446
  const deeper = ownDepth > 0 && quoteDepth(text3) > ownDepth;
10625
11447
  const start2 = CANNOT_INTERRUPT.exec(inner);
@@ -10871,6 +11693,16 @@ function inlineFootnoteInertTransform(tree) {
10871
11693
  });
10872
11694
  });
10873
11695
  }
11696
+ function inlineFootnoteInertKeepTransform(tree, source) {
11697
+ visit(tree, "footnote", (note) => {
11698
+ bareFootnoteRefTransform(note, source);
11699
+ footnoteAfterLabelTransform(note, source);
11700
+ visit(note, "footnoteReference", (node2) => {
11701
+ const reference2 = node2;
11702
+ reference2.data = { ...reference2.data, inert: true };
11703
+ });
11704
+ });
11705
+ }
10874
11706
 
10875
11707
  // src/lib/syntax/wikilink.ts
10876
11708
  var EXCLAMATION = 33;
@@ -12055,9 +12887,14 @@ function wikilinkFromMarkdown() {
12055
12887
  const parts = partsOf(node2);
12056
12888
  const resolved = parts.heading ? "heading" : parts.path ? "path" : void 0;
12057
12889
  if (resolved !== void 0 && String(node2[resolved]).endsWith("\\")) {
12058
- node2[resolved] = String(node2[resolved]).slice(0, -1);
12890
+ const kept = String(node2[resolved]).slice(0, -1);
12891
+ node2[resolved] = dropClosingBackslash(String(node2[resolved]));
12059
12892
  if (!parts.alias) {
12060
12893
  node2.data = { ...node2.data, strippedBackslash: resolved };
12894
+ if (kept !== node2[resolved]) {
12895
+ const key = resolved === "path" ? "spelledPath" : "spelledHeading";
12896
+ node2.data[key] = resolved === "path" ? written2 : writtenHeading;
12897
+ }
12061
12898
  }
12062
12899
  }
12063
12900
  node2.value = node2.embedded ? node2.path : partsOf(node2).alias ? node2.alias : (
@@ -12222,6 +13059,25 @@ function node(value2) {
12222
13059
  return Boolean(value2 && typeof value2 === "object");
12223
13060
  }
12224
13061
 
13062
+ // src/lib/walk.ts
13063
+ var childrenOf = (node2) => node2.children ?? [];
13064
+ function eachBelow(root, visit2) {
13065
+ const stack = [[root, 0]];
13066
+ while (stack.length > 0) {
13067
+ const top = stack[stack.length - 1];
13068
+ const [parent, index] = top;
13069
+ const children = childrenOf(parent);
13070
+ if (index >= children.length) {
13071
+ stack.pop();
13072
+ continue;
13073
+ }
13074
+ top[1] = index + 1;
13075
+ const node2 = children[index];
13076
+ visit2(node2, parent, index);
13077
+ stack.push([node2, 0]);
13078
+ }
13079
+ }
13080
+
12225
13081
  // src/lib/mdast/empty-delete-to-markdown.ts
12226
13082
  var EMPTY_DELETE = "obsidianEmptyDelete";
12227
13083
  function isEmptyDelete(node2) {
@@ -12229,15 +13085,15 @@ function isEmptyDelete(node2) {
12229
13085
  }
12230
13086
  function markEmptyDeletes(tree) {
12231
13087
  const marked = [];
12232
- const walk = (node2) => {
13088
+ const mark = (node2) => {
12233
13089
  const children = node2.children;
12234
13090
  if (node2.type === "delete" && children?.length === 0) {
12235
13091
  node2.type = EMPTY_DELETE;
12236
13092
  marked.push(node2);
12237
13093
  }
12238
- children?.forEach(walk);
12239
13094
  };
12240
- walk(tree);
13095
+ mark(tree);
13096
+ eachBelow(tree, mark);
12241
13097
  return () => {
12242
13098
  for (const node2 of marked) node2.type = "delete";
12243
13099
  };
@@ -12292,668 +13148,326 @@ function breakToMarkdown(escaped2 = false) {
12292
13148
  ${(node2.depth === 1 ? "=" : "-").repeat(
12293
13149
  Math.max(value2.length - lastLine, 3)
12294
13150
  )}`;
12295
- },
12296
- break(node2, parent, _state) {
12297
- const opens2 = (parent?.type === "emphasis" || parent?.type === "strong") && parent.children[0] === node2;
12298
- const ending = escaped2 || opens2 ? "\\\n" : "\n";
12299
- const siblings = parent?.children ?? [];
12300
- const next = siblings[siblings.indexOf(node2) + 1];
12301
- if (isEmptyDelete(next)) {
12302
- return `${ending}&#x20;`;
12303
- }
12304
- return ending;
12305
- }
12306
- }
12307
- };
12308
- }
12309
-
12310
- // src/lib/mdast/highlight-to-markdown.ts
12311
- function highlightToMarkdown() {
12312
- return {
12313
- // A `==` that is text must stay text, as for `%%`: mid-line, `x \\==mark==` came back from a
12314
- // format as a highlight. At a line start the writer already escaped it, against setext.
12315
- unsafe: [{ character: "=", after: "=[^\\s]", inConstruct: "phrasing" }],
12316
- handlers: {
12317
- highlight(node2, _parent, state, info) {
12318
- const exit = state.enter("highlight");
12319
- const content = state.containerPhrasing(node2, {
12320
- ...info,
12321
- before: "==",
12322
- after: "=="
12323
- });
12324
- exit();
12325
- return `==${guardClosers(content, info.before === "\n" || info.before === "")}==`;
12326
- }
12327
- }
12328
- };
12329
- }
12330
- function guardClosers(content, lineStart) {
12331
- const units = content.match(/\\[\s\S]|&#?[\da-z]+;|[\s\S]/giu) ?? [];
12332
- const equals = (unit) => unit === "=" || unit === "\\=";
12333
- return units.map(
12334
- (unit, index) => equals(unit) && (index === 0 && lineStart || index === units.length - 1 || equals(units[index + 1]) || unit !== "=") ? "&#x3D;" : unit
12335
- ).join("");
12336
- }
12337
-
12338
- // src/lib/mdast/callout-to-markdown.ts
12339
- var prefix = (line, _index, blank) => ">" + (blank ? "" : " ") + line;
12340
- var BELOW_TITLE_LINE = /* @__PURE__ */ new Set(["code", "comment", "paragraph"]);
12341
- function calloutToMarkdown() {
12342
- return {
12343
- handlers: {
12344
- callout(node2, _parent, state, info) {
12345
- const exit = state.enter("blockquote");
12346
- const tracker = state.createTracker(info);
12347
- tracker.move("> ");
12348
- tracker.shift(2);
12349
- const titleNode = node2.children.find(
12350
- (child) => child.type === "calloutTitle"
12351
- );
12352
- const body = node2.children.filter(
12353
- (child) => child.type !== "calloutTitle"
12354
- );
12355
- const fold2 = node2.collapsed === true ? "-" : node2.collapsed === false ? "+" : "";
12356
- let title = "";
12357
- if (titleNode) {
12358
- const exitPhrasing = state.enter("phrasing");
12359
- title = state.containerPhrasing(titleNode, {
12360
- ...info,
12361
- ...tracker.current()
12362
- });
12363
- exitPhrasing();
12364
- }
12365
- const empty2 = titleNode !== void 0 && title === "";
12366
- const onTitleLine = empty2 && body.length > 0 && !BELOW_TITLE_LINE.has(body[0]?.type ?? "");
12367
- const head = `[!${node2.calloutType}]${fold2}${title ? ` ${title}` : onTitleLine ? " " : empty2 ? " &#x20;" : ""}`;
12368
- const rest = body.length > 0 ? state.containerFlow(
12369
- { type: "root", children: body },
12370
- tracker.current()
12371
- ) : "";
12372
- const value2 = state.indentLines(
12373
- rest === "" ? head : onTitleLine ? `${head}${rest}` : `${head}
12374
- ${rest}`,
12375
- prefix
12376
- );
12377
- exit();
12378
- return value2;
12379
- },
12380
- calloutTitle(node2, _parent, state, info) {
12381
- const exit = state.enter("phrasing");
12382
- const value2 = state.containerPhrasing(node2, info);
12383
- exit();
12384
- return value2;
12385
- }
12386
- }
12387
- };
12388
- }
12389
-
12390
- // src/lib/mdast/comment-to-markdown.ts
12391
- var FLOW = /* @__PURE__ */ new Set([
12392
- "root",
12393
- "listItem",
12394
- "blockquote",
12395
- "callout",
12396
- "footnoteDefinition",
12397
- "comment"
12398
- ]);
12399
- function commentToMarkdown() {
12400
- return {
12401
- // A `%%` that is text must stay text: written bare, `\\%%note%%` came back from a format as a
12402
- // comment, hiding the words (found by `fuzz/invariant-sweep.ts`).
12403
- unsafe: [{ character: "%", after: "%", inConstruct: "phrasing" }],
12404
- handlers: {
12405
- comment(node2, parent) {
12406
- const value2 = node2.value.split("\n").map(
12407
- (line, index, lines) => index > 0 && index < lines.length - 1 && /^[ \t]+$/u.test(line) ? "" : line
12408
- ).join("\n");
12409
- if (value2 !== node2.value) node2 = { ...node2, value: value2 };
12410
- if (parent && FLOW.has(parent.type) && !node2.value.includes("\n")) {
12411
- return `%%${node2.value}
12412
- %%`;
12413
- }
12414
- if (parent && FLOW.has(parent.type) && node2.value.endsWith("%")) {
12415
- return `%%${node2.value}
12416
- %%`;
12417
- }
12418
- return `%%${node2.value}%%`;
12419
- }
12420
- }
12421
- };
12422
- }
12423
-
12424
- // src/lib/footnote-identifier.ts
12425
- function footnoteIdentifierTransform(tree) {
12426
- visit(tree, (node2) => {
12427
- if (node2.type !== "footnoteReference" && node2.type !== "footnoteDefinition") {
12428
- return;
12429
- }
12430
- if (typeof node2.label === "string")
12431
- node2.identifier = node2.label.toLowerCase();
12432
- });
12433
- }
12434
-
12435
- // src/lib/mdast/tag-to-markdown.ts
12436
- var TAG_END = "\u2E7F";
12437
- var TAG_END_PATTERN = /(?:\u2E7F|&#x2E7F;)(?=[*_~])/gu;
12438
- function removeTagEnds(written2) {
12439
- return written2.replace(TAG_END_PATTERN, (match, at3) => {
12440
- let index = at3 - 1;
12441
- while (index >= 0 && isTagCharacter(written2[index])) index--;
12442
- return index >= 0 && index < at3 - 1 && written2[index] === "#" ? "" : match;
12443
- });
12444
- }
12445
- function tagToMarkdown(options = {}) {
12446
- return {
12447
- // A `#` that is text must stay text: written bare, `x \\#tag` came back from a format as a
12448
- // tag (found by `fuzz/invariant-sweep.ts`). Only where a tag could start, before a non-space:
12449
- // after whitespace, at a line start, or after ASCII punctuation -- which the writer may itself
12450
- // escape or encode, and an escape or a character reference counts as a construct a tag may
12451
- // follow, so `](#tag` written `]\\(#tag` and `&amp;#a` written `\\&#a` both grew a tag. A `#`
12452
- // after a letter or digit -- `page#section`, `C#` -- is left alone.
12453
- unsafe: [
12454
- {
12455
- character: "#",
12456
- before: "[\\s!-/:-@\\[-`{-~]",
12457
- after: "[^\\s]",
12458
- inConstruct: "phrasing"
12459
- },
12460
- {
12461
- character: "#",
12462
- atBreak: true,
12463
- after: "[^\\s]",
12464
- inConstruct: "phrasing"
12465
- }
12466
- ],
12467
- // `#` after an escape or a character reference is `textToMarkdown`'s, which the rule above
12468
- // cannot reach.
12469
- handlers: {
12470
- tag(node2, parent) {
12471
- const siblings = parent?.children ?? [];
12472
- const next = siblings[siblings.indexOf(node2) + 1];
12473
- const attention = next?.type === "emphasis" || next?.type === "strong" || next?.type === "delete";
12474
- return `#${node2.value}${options.legacy && attention ? TAG_END : ""}`;
12475
- }
12476
- }
12477
- };
12478
- }
12479
-
12480
- // src/lib/mdast/inline-footnote-to-markdown.ts
12481
- function inlineFootnoteToMarkdown() {
12482
- return {
12483
- // A `]` in an inline footnote's text closes it, so it is escaped there: written bare,
12484
- // `^[a\\]b]` came back as `^[a]` and `b]` (found by `fuzz/invariant-sweep.ts`).
12485
- unsafe: [{ character: "]", inConstruct: "inlineFootnote" }],
12486
- handlers: {
12487
- footnote(node2, _parent, state, info) {
12488
- const tracker = state.createTracker(info);
12489
- const exit = state.enter("inlineFootnote");
12490
- let value2 = tracker.move("^[");
12491
- value2 += balanceBrackets(
12492
- state.containerPhrasing(node2, {
12493
- ...tracker.current(),
12494
- before: value2,
12495
- after: "]"
12496
- })
12497
- );
12498
- value2 += tracker.move("]");
12499
- exit();
12500
- return value2;
12501
- }
12502
- }
12503
- };
12504
- }
12505
- function balanceBrackets(content) {
12506
- if (content.includes("`")) return content;
12507
- const units = content.match(/\\[\s\S]|[\s\S]/gu) ?? [];
12508
- let open = 0;
12509
- const escapedOpeners = [];
12510
- for (let index = 0; index < units.length; index++) {
12511
- if (units[index] === "[") open++;
12512
- else if (units[index] === "\\[") escapedOpeners.push(index);
12513
- else if (units[index] === "]") {
12514
- if (open > 0) open--;
12515
- else if (escapedOpeners.length > 0) units[escapedOpeners.shift()] = "[";
12516
- else units[index] = "\\]";
12517
- }
12518
- }
12519
- for (let index = units.length - 1; index >= 0 && open > 0; index--) {
12520
- if (units[index] === "\\]") {
12521
- units[index] = "]";
12522
- open--;
13151
+ },
13152
+ break(node2, parent, _state) {
13153
+ const opens2 = (parent?.type === "emphasis" || parent?.type === "strong") && parent.children[0] === node2;
13154
+ const ending = escaped2 || opens2 ? "\\\n" : "\n";
13155
+ const siblings = parent?.children ?? [];
13156
+ const next = siblings[siblings.indexOf(node2) + 1];
13157
+ if (isEmptyDelete(next)) {
13158
+ return `${ending}&#x20;`;
13159
+ }
13160
+ return ending;
13161
+ }
12523
13162
  }
12524
- }
12525
- return units.join("");
13163
+ };
12526
13164
  }
12527
13165
 
12528
- // src/lib/mdast/footnote-reference-to-markdown.ts
12529
- function footnoteReferenceToMarkdown() {
13166
+ // src/lib/mdast/highlight-to-markdown.ts
13167
+ function highlightToMarkdown() {
12530
13168
  return {
13169
+ // A `==` that is text must stay text, as for `%%`: mid-line, `x \\==mark==` came back from a
13170
+ // format as a highlight. At a line start the writer already escaped it, against setext.
13171
+ unsafe: [{ character: "=", after: "=[^\\s]", inConstruct: "phrasing" }],
12531
13172
  handlers: {
12532
- footnoteReference(node2, _parent, state, info) {
12533
- const tracker = state.createTracker(info);
12534
- let value2 = tracker.move("[^");
12535
- value2 += tracker.move(node2.label ?? node2.identifier);
12536
- value2 += tracker.move("]");
12537
- return value2;
13173
+ highlight(node2, _parent, state, info) {
13174
+ const exit = state.enter("highlight");
13175
+ const content = state.containerPhrasing(node2, {
13176
+ ...info,
13177
+ before: "==",
13178
+ after: "=="
13179
+ });
13180
+ exit();
13181
+ return `==${guardClosers(content, info.before === "\n" || info.before === "")}==`;
12538
13182
  }
12539
13183
  }
12540
13184
  };
12541
13185
  }
12542
- function footnoteDefinitionToMarkdown() {
13186
+ function guardClosers(content, lineStart) {
13187
+ const units = content.match(/\\[\s\S]|&#?[\da-z]+;|[\s\S]/giu) ?? [];
13188
+ const equals = (unit) => unit === "=" || unit === "\\=";
13189
+ return units.map(
13190
+ (unit, index) => equals(unit) && (index === 0 && lineStart || index === units.length - 1 || equals(units[index + 1]) || unit !== "=") ? "&#x3D;" : unit
13191
+ ).join("");
13192
+ }
13193
+
13194
+ // src/lib/mdast/callout-to-markdown.ts
13195
+ var prefix = (line, _index, blank) => ">" + (blank ? "" : " ") + line;
13196
+ var BELOW_TITLE_LINE = /* @__PURE__ */ new Set(["code", "comment", "paragraph"]);
13197
+ function calloutToMarkdown() {
12543
13198
  return {
12544
13199
  handlers: {
12545
- footnoteDefinition(node2, _parent, state, info) {
13200
+ callout(node2, _parent, state, info) {
13201
+ const exit = state.enter("blockquote");
12546
13202
  const tracker = state.createTracker(info);
12547
- let value2 = tracker.move("[^");
12548
- const exit = state.enter("footnoteDefinition");
12549
- value2 += tracker.move(node2.label ?? node2.identifier);
12550
- value2 += tracker.move("]:");
12551
- if (node2.children.length > 0) {
12552
- tracker.shift(4);
12553
- value2 += tracker.move(
12554
- " " + state.indentLines(
12555
- state.containerFlow(node2, tracker.current()),
12556
- (line, index, blank) => index === 0 ? line : (blank ? "" : " ") + line
12557
- )
12558
- );
13203
+ tracker.move("> ");
13204
+ tracker.shift(2);
13205
+ const titleNode = node2.children.find(
13206
+ (child) => child.type === "calloutTitle"
13207
+ );
13208
+ const body = node2.children.filter(
13209
+ (child) => child.type !== "calloutTitle"
13210
+ );
13211
+ const fold2 = node2.collapsed === true ? "-" : node2.collapsed === false ? "+" : "";
13212
+ let title = "";
13213
+ if (titleNode) {
13214
+ const exitPhrasing = state.enter("phrasing");
13215
+ title = state.containerPhrasing(titleNode, {
13216
+ ...info,
13217
+ ...tracker.current()
13218
+ });
13219
+ exitPhrasing();
12559
13220
  }
13221
+ const empty2 = titleNode !== void 0 && title === "";
13222
+ const onTitleLine = empty2 && body.length > 0 && !BELOW_TITLE_LINE.has(body[0]?.type ?? "");
13223
+ const head = `[!${node2.calloutType}]${fold2}${title ? ` ${title}` : onTitleLine ? " " : empty2 ? " &#x20;" : ""}`;
13224
+ const rest = body.length > 0 ? state.containerFlow(
13225
+ { type: "root", children: body },
13226
+ tracker.current()
13227
+ ) : "";
13228
+ const value2 = state.indentLines(
13229
+ rest === "" ? head : onTitleLine ? `${head}${rest}` : `${head}
13230
+ ${rest}`,
13231
+ prefix
13232
+ );
12560
13233
  exit();
12561
13234
  return value2;
12562
- }
12563
- }
12564
- };
12565
- }
12566
-
12567
- // src/lib/task-char.ts
12568
- function customTaskCharTransform(tree, source) {
12569
- visit(tree, "listItem", (node2) => {
12570
- if (typeof node2.checked === "boolean") {
12571
- let char = node2.checked ? "x" : " ";
12572
- if (source && node2.position?.start?.offset != null) {
12573
- const slice = source.slice(
12574
- node2.position.start.offset,
12575
- node2.position.start.offset + 20
12576
- );
12577
- const m = slice.match(/\[([^\]])\]/);
12578
- if (m) {
12579
- char = m[1];
12580
- }
12581
- }
12582
- node2.data ??= {};
12583
- node2.data.taskChar = char;
12584
- node2.data.hProperties ??= {};
12585
- node2.data.hProperties.dataTaskChar = char;
12586
- return;
12587
- }
12588
- const firstChild = node2.children?.[0];
12589
- if (!firstChild || firstChild.type !== "paragraph") return;
12590
- const firstText = firstChild.children?.[0];
12591
- if (!firstText || firstText.type !== "text") return;
12592
- const match = firstText.value.match(/^\[([^\]\u2028\u2029])\]\s/);
12593
- if (!match) return;
12594
- const taskChar = match[1];
12595
- node2.checked = taskChar !== " ";
12596
- node2.data ??= {};
12597
- node2.data.taskChar = taskChar;
12598
- node2.data.hProperties ??= {};
12599
- node2.data.hProperties.dataTaskChar = taskChar;
12600
- firstText.value = firstText.value.slice(match[0].length);
12601
- if (firstText.value.length === 0) {
12602
- firstChild.children.shift();
12603
- } else if (firstText.position && typeof firstText.position.start.offset === "number") {
12604
- firstText.position.start.column += match[0].length;
12605
- firstText.position.start.offset += match[0].length;
12606
- if (firstChild.position) {
12607
- firstChild.position.start = { ...firstText.position.start };
12608
- }
12609
- }
12610
- });
12611
- }
12612
-
12613
- // src/lib/callout.ts
12614
- var MARKER6 = /^\[!([^\]\n]+)\]([-+]?)(?=[ \t\n]|$)[ \t]?/;
12615
- var QUOTE_MARKER = /^[ \t]{0,3}>/u;
12616
- var LIST_MARKER = /^[ \t]{0,3}(?:[-*+]|\d{1,9}[.)])[ \t]+/u;
12617
- var AT_MARKER = /^ ?\[!/u;
12618
- function atMarkerColumn(line) {
12619
- let rest = line;
12620
- for (; ; ) {
12621
- const quote = QUOTE_MARKER.exec(rest);
12622
- if (quote) {
12623
- rest = rest.slice(quote[0].length);
12624
- continue;
12625
- }
12626
- const list3 = LIST_MARKER.exec(rest);
12627
- if (!list3) break;
12628
- rest = rest.slice(list3[0].length);
12629
- }
12630
- return AT_MARKER.test(rest);
12631
- }
12632
- var LINE_PREFIX = /^[ \t]*(?:>[ \t]?)*/u;
12633
- function shiftOnto2(node2, offset, line) {
12634
- const candidate = node2;
12635
- if (candidate.position) {
12636
- for (const point of [candidate.position.start, candidate.position.end]) {
12637
- point.offset = offset + (point.offset ?? 0);
12638
- point.line = line + point.line - 1;
12639
- }
12640
- }
12641
- for (const child of candidate.children ?? []) shiftOnto2(child, offset, line);
12642
- }
12643
- function advanceStart(node2, by) {
12644
- const start = node2.position?.start;
12645
- if (!start || start.offset === void 0) return;
12646
- start.offset += by;
12647
- start.column += by;
12648
- }
12649
- function columnAt(source, offset) {
12650
- return offset - (source.lastIndexOf("\n", offset - 1) + 1) + 1;
12651
- }
12652
- function moveStartTo(node2, source, offset, line) {
12653
- const position = node2.position;
12654
- if (!position) return;
12655
- position.start = { line, column: columnAt(source, offset), offset };
12656
- }
12657
- function titleLineIsBlock(remainder, reparse) {
12658
- if (remainder.trim() === "") return void 0;
12659
- const parsed = reparse(remainder);
12660
- const only = parsed.children[0];
12661
- if (parsed.children.length === 1 && (only?.type === "paragraph" || only?.type === "math")) {
12662
- return void 0;
12663
- }
12664
- return parsed.children;
12665
- }
12666
- function calloutTransform(tree, source = "", reparse = () => ({
12667
- type: "root",
12668
- children: []
12669
- })) {
12670
- visit(tree, "blockquote", (node2) => {
12671
- const quote = node2;
12672
- const first = quote.children[0];
12673
- if (!first || first.type !== "paragraph" && first.type !== "heading") {
12674
- return;
12675
- }
12676
- const paragraph2 = first;
12677
- if (paragraph2.position?.start?.line !== void 0 && quote.position?.start?.line !== void 0 && paragraph2.position.start.line !== quote.position.start.line) {
12678
- return;
12679
- }
12680
- const lead = paragraph2.children[0];
12681
- if (!lead || lead.type !== "text") return;
12682
- const match = MARKER6.exec(lead.value);
12683
- if (!match) return;
12684
- if (match[1].trim() === "") return;
12685
- const from = paragraph2.position?.start?.offset;
12686
- if (source !== "" && from !== void 0) {
12687
- const lineStart = source.lastIndexOf("\n", from - 1) + 1;
12688
- const lineEnd2 = source.indexOf("\n", lineStart);
12689
- const line = source.slice(
12690
- lineStart,
12691
- lineEnd2 === -1 ? void 0 : lineEnd2
12692
- );
12693
- if (!atMarkerColumn(line)) return;
12694
- }
12695
- const [consumed, calloutType, fold2] = match;
12696
- lead.value = lead.value.slice(consumed.length);
12697
- advanceStart(lead, consumed.length);
12698
- const collect = (nodes) => nodes.map(
12699
- (c) => "value" in c ? String(c.value) : collect(c.children ?? [])
12700
- ).join("");
12701
- const leadText = lead;
12702
- const newlineAt = leadText.value.indexOf("\n");
12703
- const titleFrom = leadText.position?.start?.offset;
12704
- let firstLine = newlineAt === -1 ? leadText.value : leadText.value.slice(0, newlineAt);
12705
- if (source !== "" && titleFrom !== void 0) {
12706
- const breakAt = source.indexOf("\n", titleFrom);
12707
- firstLine = source.slice(titleFrom, breakAt === -1 ? void 0 : breakAt);
12708
- }
12709
- function bodyUnderTitleLine() {
12710
- const from2 = leadText.position?.start?.offset;
12711
- const to = paragraph2.position?.end?.offset;
12712
- if (from2 === void 0 || to === void 0 || source === "") return [];
12713
- const breakAt = source.indexOf("\n", from2);
12714
- if (breakAt === -1 || breakAt >= to) return [];
12715
- const bodyFrom = breakAt + 1 + (LINE_PREFIX.exec(source.slice(breakAt + 1))?.[0].length ?? 0);
12716
- if (bodyFrom >= to) return [];
12717
- const map = mapLines(
12718
- source,
12719
- bodyFrom,
12720
- to,
12721
- source.slice(0, bodyFrom).split("\n").length
12722
- );
12723
- const parsed = reparse(map.text);
12724
- for (const child of parsed.children) shiftByMap(child, map, 0);
12725
- return parsed.children;
13235
+ },
13236
+ calloutTitle(node2, _parent, state, info) {
13237
+ const exit = state.enter("phrasing");
13238
+ const value2 = state.containerPhrasing(node2, info);
13239
+ exit();
13240
+ return value2;
13241
+ }
12726
13242
  }
12727
- const asBlock = titleLineIsBlock(firstLine, reparse);
12728
- if (asBlock) {
12729
- const titleStart = leadText.position?.start;
12730
- if (titleStart?.offset !== void 0) {
12731
- for (const block of asBlock) {
12732
- shiftOnto2(block, titleStart.offset, titleStart.line);
13243
+ };
13244
+ }
13245
+
13246
+ // src/lib/mdast/comment-to-markdown.ts
13247
+ var FLOW = /* @__PURE__ */ new Set([
13248
+ "root",
13249
+ "listItem",
13250
+ "blockquote",
13251
+ "callout",
13252
+ "footnoteDefinition",
13253
+ "comment"
13254
+ ]);
13255
+ function commentToMarkdown() {
13256
+ return {
13257
+ // A `%%` that is text must stay text: written bare, `\\%%note%%` came back from a format as a
13258
+ // comment, hiding the words (found by `fuzz/invariant-sweep.ts`).
13259
+ unsafe: [{ character: "%", after: "%", inConstruct: "phrasing" }],
13260
+ handlers: {
13261
+ comment(node2, parent) {
13262
+ const value2 = node2.value.split("\n").map(
13263
+ (line, index, lines) => index > 0 && index < lines.length - 1 && /^[ \t]+$/u.test(line) ? "" : line
13264
+ ).join("\n");
13265
+ if (value2 !== node2.value) node2 = { ...node2, value: value2 };
13266
+ if (parent && FLOW.has(parent.type) && !node2.value.includes("\n")) {
13267
+ return `%%${node2.value}
13268
+ %%`;
13269
+ }
13270
+ if (parent && FLOW.has(parent.type) && node2.value.endsWith("%")) {
13271
+ return `%%${node2.value}
13272
+ %%`;
12733
13273
  }
13274
+ return `%%${node2.value}%%`;
12734
13275
  }
12735
- const body = bodyUnderTitleLine();
12736
- const rest = quote.children.slice(1);
12737
- quote.children = [
12738
- {
12739
- type: "calloutTitle",
12740
- children: [],
12741
- // Zero width at the title's own offset. The title is empty, not absent --
12742
- // it has nowhere to span, but it does have a place, and an unplaced node
12743
- // is one a formatter cannot edit around. Nine of these arrived with the
12744
- // block-title cases alone.
12745
- ...titleStart?.offset === void 0 ? {} : {
12746
- position: {
12747
- start: { ...titleStart },
12748
- end: { ...titleStart }
12749
- }
12750
- }
12751
- },
12752
- ...asBlock,
12753
- ...body,
12754
- ...rest
12755
- ];
12756
- const callout2 = node2;
12757
- callout2.type = "callout";
12758
- callout2.calloutType = (calloutType ?? "").trim();
12759
- callout2.title = "";
12760
- callout2.collapsed = fold2 === "-" ? true : fold2 === "+" ? false : null;
13276
+ }
13277
+ };
13278
+ }
13279
+
13280
+ // src/lib/footnote-identifier.ts
13281
+ function footnoteIdentifierTransform(tree) {
13282
+ visit(tree, (node2) => {
13283
+ if (node2.type !== "footnoteReference" && node2.type !== "footnoteDefinition") {
12761
13284
  return;
12762
13285
  }
12763
- function splitAcrossConstruct() {
12764
- const from2 = leadText.position?.start?.offset;
12765
- const to = paragraph2.position?.end?.offset;
12766
- if (from2 === void 0 || to === void 0) return void 0;
12767
- const breakAtSource = source.indexOf("\n", from2);
12768
- if (breakAtSource === -1 || breakAtSource >= to) return void 0;
12769
- const titleLine = source.slice(from2, breakAtSource);
12770
- const bodyFrom = breakAtSource + 1 + (LINE_PREFIX.exec(source.slice(breakAtSource + 1))?.[0].length ?? 0);
12771
- if (bodyFrom >= to) return void 0;
12772
- const titleBlock = reparse(titleLine).children[0];
12773
- const bodyMap = mapLines(
12774
- source,
12775
- bodyFrom,
12776
- to,
12777
- source.slice(0, bodyFrom).split("\n").length
12778
- );
12779
- const bodyBlock = reparse(bodyMap.text).children[0];
12780
- if (titleBlock?.type !== "paragraph" || bodyBlock?.type !== "paragraph") {
12781
- let reread;
12782
- if (bodyBlock?.type === "paragraph") {
12783
- for (const child of bodyBlock.children) shiftByMap(child, bodyMap, 0);
12784
- reread = bodyBlock.children;
12785
- }
12786
- return splitAtLineEnd(breakAtSource, bodyFrom, reread);
13286
+ if (typeof node2.label === "string")
13287
+ node2.identifier = node2.label.toLowerCase();
13288
+ });
13289
+ }
13290
+
13291
+ // src/lib/mdast/tag-to-markdown.ts
13292
+ var TAG_END = "\u2E7F";
13293
+ var TAG_END_PATTERN = /(?:\u2E7F|&#x2E7F;)(?=[*_~])/gu;
13294
+ function removeTagEnds(written2) {
13295
+ return written2.replace(TAG_END_PATTERN, (match, at3) => {
13296
+ let index = at3 - 1;
13297
+ while (index >= 0 && isTagCharacter(written2[index])) index--;
13298
+ return index >= 0 && index < at3 - 1 && written2[index] === "#" ? "" : match;
13299
+ });
13300
+ }
13301
+ function opensWithStrike(node2) {
13302
+ let at3 = node2;
13303
+ while (at3?.type === "emphasis" || at3?.type === "strong")
13304
+ at3 = at3.children?.[0];
13305
+ return at3?.type === "delete";
13306
+ }
13307
+ function tagToMarkdown(options = {}) {
13308
+ return {
13309
+ // A `#` that is text must stay text: written bare, `x \\#tag` came back from a format as a
13310
+ // tag (found by `fuzz/invariant-sweep.ts`). Only where a tag could start, before a non-space:
13311
+ // after whitespace, at a line start, or after ASCII punctuation -- which the writer may itself
13312
+ // escape or encode, and an escape or a character reference counts as a construct a tag may
13313
+ // follow, so `](#tag` written `]\\(#tag` and `&amp;#a` written `\\&#a` both grew a tag. A `#`
13314
+ // after a letter or digit -- `page#section`, `C#` -- is left alone.
13315
+ unsafe: [
13316
+ {
13317
+ character: "#",
13318
+ before: "[\\s!-/:-@\\[-`{-~]",
13319
+ after: "[^\\s]",
13320
+ inConstruct: "phrasing"
13321
+ },
13322
+ {
13323
+ character: "#",
13324
+ atBreak: true,
13325
+ after: "[^\\s]",
13326
+ inConstruct: "phrasing"
12787
13327
  }
12788
- for (const child of titleBlock.children) shiftOnto2(child, from2, 1);
12789
- for (const child of bodyBlock.children) shiftByMap(child, bodyMap, 0);
12790
- return { title: titleBlock.children, body: bodyBlock.children };
12791
- }
12792
- const nonEmpty = (node3) => node3.type !== "text" || node3.value !== "";
12793
- function splitAtLineEnd(lineEnd2, bodyFrom, reread) {
12794
- const title2 = [];
12795
- const body = [];
12796
- for (const child of paragraph2.children) {
12797
- const span2 = child.position;
12798
- if (span2?.start.offset === void 0 || span2.end.offset === void 0) {
12799
- return void 0;
12800
- }
12801
- if (span2.end.offset <= lineEnd2) title2.push(child);
12802
- else if (span2.start.offset > lineEnd2) body.push(child);
12803
- else if (child.type === "text") {
12804
- const value2 = child.value;
12805
- const cut = value2.indexOf("\n");
12806
- if (cut === -1) return void 0;
12807
- if (cut > 0) {
12808
- title2.push({
12809
- type: "text",
12810
- value: value2.slice(0, cut),
12811
- position: {
12812
- start: { ...span2.start },
12813
- end: {
12814
- line: span2.start.line,
12815
- column: span2.start.column + cut,
12816
- offset: span2.start.offset + cut
12817
- }
12818
- }
12819
- });
12820
- }
12821
- const rest = { ...child, value: value2.slice(cut + 1) };
12822
- rest.position = { start: { ...span2.start }, end: { ...span2.end } };
12823
- moveStartTo(rest, source, bodyFrom, span2.start.line + 1);
12824
- if (rest.value !== "") body.push(rest);
12825
- } else {
12826
- const titleText = {
12827
- type: "text",
12828
- value: source.slice(span2.start.offset, lineEnd2),
12829
- position: {
12830
- start: { ...span2.start },
12831
- end: {
12832
- line: span2.start.line,
12833
- column: span2.start.column + lineEnd2 - span2.start.offset,
12834
- offset: lineEnd2
12835
- }
12836
- }
12837
- };
12838
- if (reread) {
12839
- return {
12840
- title: [...title2, titleText].filter(nonEmpty),
12841
- body: reread
12842
- };
12843
- }
12844
- const map = mapLines(
12845
- source,
12846
- bodyFrom,
12847
- span2.end.offset,
12848
- source.slice(0, bodyFrom).split("\n").length
12849
- );
12850
- const tail = reparse(map.text).children[0];
12851
- if (tail?.type !== "paragraph") return void 0;
12852
- for (const node3 of tail.children) shiftByMap(node3, map, 0);
12853
- title2.push(titleText);
12854
- body.push(...tail.children);
12855
- }
13328
+ ],
13329
+ // `#` after an escape or a character reference is `textToMarkdown`'s, which the rule above
13330
+ // cannot reach.
13331
+ handlers: {
13332
+ tag(node2, parent) {
13333
+ const siblings = parent?.children ?? [];
13334
+ const next = siblings[siblings.indexOf(node2) + 1];
13335
+ const attention = next?.type === "emphasis" || next?.type === "strong" || next?.type === "delete";
13336
+ return `#${node2.value}${attention && (options.legacy || opensWithStrike(next)) ? TAG_END : ""}`;
12856
13337
  }
12857
- return body.length > 0 ? { title: title2.filter(nonEmpty), body } : void 0;
12858
13338
  }
12859
- let titleNodes;
12860
- if (newlineAt !== -1) {
12861
- const titleStart = leadText.position?.start;
12862
- const titleEnd = titleStart?.offset === void 0 ? void 0 : titleStart.offset + newlineAt;
12863
- const bodyStart = titleEnd === void 0 ? void 0 : titleEnd + 1 + (LINE_PREFIX.exec(source.slice(titleEnd + 1))?.[0].length ?? 0);
12864
- titleNodes = leadText.value.slice(0, newlineAt).trim() === "" ? [] : [
12865
- {
12866
- type: "text",
12867
- value: leadText.value.slice(0, newlineAt),
12868
- ...titleStart && titleEnd !== void 0 ? {
12869
- position: {
12870
- start: { ...titleStart },
12871
- end: {
12872
- line: titleStart.line,
12873
- column: titleStart.column + newlineAt,
12874
- offset: titleEnd
12875
- }
12876
- }
12877
- } : {}
12878
- }
12879
- ];
12880
- leadText.value = leadText.value.slice(newlineAt + 1);
12881
- if (leadText.value === "") paragraph2.children.shift();
12882
- else if (bodyStart !== void 0 && titleStart) {
12883
- moveStartTo(leadText, source, bodyStart, titleStart.line + 1);
12884
- moveStartTo(paragraph2, source, bodyStart, titleStart.line + 1);
13339
+ };
13340
+ }
13341
+
13342
+ // src/lib/mdast/inline-footnote-to-markdown.ts
13343
+ function inlineFootnoteToMarkdown() {
13344
+ return {
13345
+ // A `]` in an inline footnote's text closes it, so it is escaped there: written bare,
13346
+ // `^[a\\]b]` came back as `^[a]` and `b]` (found by `fuzz/invariant-sweep.ts`).
13347
+ unsafe: [{ character: "]", inConstruct: "inlineFootnote" }],
13348
+ handlers: {
13349
+ footnote(node2, _parent, state, info) {
13350
+ const tracker = state.createTracker(info);
13351
+ const exit = state.enter("inlineFootnote");
13352
+ let value2 = tracker.move("^[");
13353
+ value2 += balanceBrackets(
13354
+ state.containerPhrasing(node2, {
13355
+ ...tracker.current(),
13356
+ before: value2,
13357
+ after: "]"
13358
+ })
13359
+ );
13360
+ value2 += tracker.move("]");
13361
+ exit();
13362
+ return value2;
12885
13363
  }
12886
- } else {
12887
- const breakAt = paragraph2.children.findIndex((c) => c.type === "break");
12888
- const split = breakAt === -1 ? splitAcrossConstruct() : void 0;
12889
- titleNodes = split?.title ?? (breakAt === -1 ? paragraph2.children.slice() : paragraph2.children.slice(0, breakAt));
12890
- if (split) paragraph2.children = split.body;
12891
- if (!split) {
12892
- paragraph2.children = breakAt === -1 ? [] : paragraph2.children.slice(breakAt + 1);
13364
+ }
13365
+ };
13366
+ }
13367
+ function balanceBrackets(content) {
13368
+ if (content.includes("`")) return content;
13369
+ const units = content.match(/\\[\s\S]|[\s\S]/gu) ?? [];
13370
+ let open = 0;
13371
+ const escapedOpeners = [];
13372
+ for (let index = 0; index < units.length; index++) {
13373
+ if (units[index] === "[") open++;
13374
+ else if (units[index] === "\\[") escapedOpeners.push(index);
13375
+ else if (units[index] === "]") {
13376
+ if (open > 0) open--;
13377
+ else if (escapedOpeners.length > 0) units[escapedOpeners.shift()] = "[";
13378
+ else units[index] = "\\]";
13379
+ }
13380
+ }
13381
+ for (let index = units.length - 1; index >= 0 && open > 0; index--) {
13382
+ if (units[index] === "\\]") {
13383
+ units[index] = "]";
13384
+ open--;
13385
+ }
13386
+ }
13387
+ return units.join("");
13388
+ }
13389
+
13390
+ // src/lib/mdast/footnote-reference-to-markdown.ts
13391
+ function footnoteReferenceToMarkdown() {
13392
+ return {
13393
+ handlers: {
13394
+ footnoteReference(node2, _parent, state, info) {
13395
+ const tracker = state.createTracker(info);
13396
+ let value2 = tracker.move("[^");
13397
+ value2 += tracker.move(node2.label ?? node2.identifier);
13398
+ value2 += tracker.move("]");
13399
+ return value2;
12893
13400
  }
12894
- const first2 = paragraph2.children[0];
12895
- const start = first2?.position?.start;
12896
- if (start?.offset !== void 0 && paragraph2.position) {
12897
- paragraph2.position.start = { ...start };
12898
- } else if (first2 && paragraph2.position && titleFrom !== void 0) {
12899
- const lineEnd2 = source.indexOf("\n", titleFrom);
12900
- if (lineEnd2 !== -1) {
12901
- const bodyStart = lineEnd2 + 1 + (LINE_PREFIX.exec(source.slice(lineEnd2 + 1))?.[0].length ?? 0);
12902
- moveStartTo(
12903
- paragraph2,
12904
- source,
12905
- bodyStart,
12906
- source.slice(0, bodyStart).split("\n").length
13401
+ }
13402
+ };
13403
+ }
13404
+ function footnoteDefinitionToMarkdown() {
13405
+ return {
13406
+ handlers: {
13407
+ footnoteDefinition(node2, _parent, state, info) {
13408
+ const tracker = state.createTracker(info);
13409
+ let value2 = tracker.move("[^");
13410
+ const exit = state.enter("footnoteDefinition");
13411
+ value2 += tracker.move(node2.label ?? node2.identifier);
13412
+ value2 += tracker.move("]:");
13413
+ if (node2.children.length > 0) {
13414
+ tracker.shift(4);
13415
+ value2 += tracker.move(
13416
+ " " + state.indentLines(
13417
+ state.containerFlow(node2, tracker.current()),
13418
+ (line, index, blank) => index === 0 ? line : (blank ? "" : " ") + line
13419
+ )
12907
13420
  );
12908
13421
  }
13422
+ exit();
13423
+ return value2;
12909
13424
  }
12910
13425
  }
12911
- const title = collect(titleNodes).trim();
12912
- if (paragraph2.children.length === 0 || paragraph2.children.length === 1 && paragraph2.children[0].type === "text" && paragraph2.children[0].value.trim() === "") {
12913
- quote.children.shift();
12914
- }
12915
- const blankTitle = title === "" && firstLine !== "";
12916
- const hasTitleNode = titleNodes.some(
12917
- (node3) => node3.type !== "text" || (node3.value ?? "").trim() !== ""
12918
- );
12919
- if (title !== "" || blankTitle || hasTitleNode) {
12920
- const head = titleNodes[0]?.position?.start;
12921
- const tail = titleNodes[titleNodes.length - 1]?.position?.end;
12922
- quote.children.unshift({
12923
- type: "calloutTitle",
12924
- children: titleNodes,
12925
- ...head && tail ? { position: { start: head, end: tail } } : {}
12926
- });
12927
- }
12928
- for (const child of quote.children) {
12929
- if (child.type !== "paragraph") continue;
12930
- const first2 = child.children[0]?.position?.start;
12931
- const start = child.position?.start;
12932
- if (!first2 || !start) break;
12933
- if (start.offset === void 0 || first2.offset === void 0) break;
12934
- if (first2.offset > start.offset) child.position.start = { ...first2 };
12935
- break;
12936
- }
12937
- const callout = node2;
12938
- callout.type = "callout";
12939
- callout.calloutType = (calloutType ?? "").trim();
12940
- callout.title = title;
12941
- callout.collapsed = fold2 === "-" ? true : fold2 === "+" ? false : null;
12942
- });
13426
+ };
12943
13427
  }
12944
- function syncCalloutTitlesTransform(tree) {
12945
- const collect = (nodes) => nodes.map(
12946
- (child) => "value" in child ? String(child.value) : collect(child.children ?? [])
12947
- ).join("");
12948
- visit(tree, (node2) => {
12949
- if (node2.type !== "callout") return;
12950
- const titleNode = (node2.children ?? []).find(
12951
- (child) => child.type === "calloutTitle"
12952
- );
12953
- if (titleNode) {
12954
- node2.title = collect(
12955
- titleNode.children ?? []
12956
- ).trim();
13428
+
13429
+ // src/lib/task-char.ts
13430
+ function customTaskCharTransform(tree, source) {
13431
+ visit(tree, "listItem", (node2) => {
13432
+ if (typeof node2.checked === "boolean") {
13433
+ let char = node2.checked ? "x" : " ";
13434
+ if (source && node2.position?.start?.offset != null) {
13435
+ const slice = source.slice(
13436
+ node2.position.start.offset,
13437
+ node2.position.start.offset + 20
13438
+ );
13439
+ const m = slice.match(/\[([^\]])\]/);
13440
+ if (m) {
13441
+ char = m[1];
13442
+ }
13443
+ }
13444
+ node2.data ??= {};
13445
+ node2.data.taskChar = char;
13446
+ node2.data.hProperties ??= {};
13447
+ node2.data.hProperties.dataTaskChar = char;
13448
+ return;
13449
+ }
13450
+ const firstChild = node2.children?.[0];
13451
+ if (!firstChild || firstChild.type !== "paragraph") return;
13452
+ const firstText = firstChild.children?.[0];
13453
+ if (!firstText || firstText.type !== "text") return;
13454
+ const match = firstText.value.match(/^\[([^\]\u2028\u2029])\]\s/);
13455
+ if (!match) return;
13456
+ const taskChar = match[1];
13457
+ node2.checked = taskChar !== " ";
13458
+ node2.data ??= {};
13459
+ node2.data.taskChar = taskChar;
13460
+ node2.data.hProperties ??= {};
13461
+ node2.data.hProperties.dataTaskChar = taskChar;
13462
+ firstText.value = firstText.value.slice(match[0].length);
13463
+ if (firstText.value.length === 0) {
13464
+ firstChild.children.shift();
13465
+ } else if (firstText.position && typeof firstText.position.start.offset === "number") {
13466
+ firstText.position.start.column += match[0].length;
13467
+ firstText.position.start.offset += match[0].length;
13468
+ if (firstChild.position) {
13469
+ firstChild.position.start = { ...firstText.position.start };
13470
+ }
12957
13471
  }
12958
13472
  });
12959
13473
  }
@@ -13055,11 +13569,20 @@ function detect(tree, source, lines, parse3, options) {
13055
13569
  const calloutTitles2 = /* @__PURE__ */ new Set();
13056
13570
  const listStarts = /* @__PURE__ */ new Map();
13057
13571
  const definitions = [];
13058
- const walk = (node2, inDefinition, item, hostLine, parent, container) => {
13572
+ const keepsLazyLines = /* @__PURE__ */ new WeakSet();
13573
+ const nextItemLine = /* @__PURE__ */ new WeakMap();
13574
+ const walk = (node2, inDefinition, item, hostLine, parent, container, index = -1) => {
13059
13575
  if (options.comments && !inDefinition && (container === "blockquote" || container === "listItem")) {
13060
13576
  const split = node2.type === "paragraph" ? lazyCommentOpener(node2, lines) : node2.type === "comment" && parent && parent.children[0] !== node2 ? lazyCommentNode(node2, parent, lines) : void 0;
13061
13577
  if (split) alone.push(split);
13062
13578
  }
13579
+ if (node2.type === "paragraph" && inDefinition && node2.position && options.callouts && isCalloutTitle(node2, parent) && // Only where `calloutTransform` will build one: a quote on a definition's indented line
13580
+ // (`[^1]:` / `····>[!~]`) is no callout, and splitting its title changed its paragraph.
13581
+ atMarkerColumn(lineOf(source, node2.position.start.offset ?? 0))) {
13582
+ calloutTitles2.add(node2);
13583
+ const split = calloutBodySplit(node2, lines);
13584
+ if (split) alone.push(split);
13585
+ }
13063
13586
  if (node2.type === "paragraph" && !inDefinition && node2.position) {
13064
13587
  if (options.callouts && isCalloutTitle(node2, parent)) {
13065
13588
  calloutTitles2.add(node2);
@@ -13077,18 +13600,38 @@ function detect(tree, source, lines, parse3, options) {
13077
13600
  if (unbox) alone.push(unbox);
13078
13601
  }
13079
13602
  if (!inDefinition && (container === "blockquote" || container === "listItem") && (node2.type === "math" || options.comments && node2.type === "comment")) {
13080
- if (!closesMidline(node2, parent)) {
13081
- late.push(...keptLazyLines(node2, source, lines, parse3));
13603
+ if (!closesMidline(node2, parent, index)) {
13604
+ const lazy = keptLazyLines(node2, source, lines, parse3);
13605
+ late.push(...lazy);
13606
+ if (lazy.length > 0) keepsLazyLines.add(node2);
13082
13607
  }
13083
13608
  if (node2.type === "math") edits.push(...containedMathOpener(node2, lines));
13084
13609
  }
13610
+ if (!inDefinition && node2.type === "math" && parent && (parent.type === "root" || parent.type === "blockquote" || parent.type === "listItem") && !keepsLazyLines.has(node2) && !closesSpanAbove(node2, parent, index)) {
13611
+ edits.push(
13612
+ ...lastLineMathOpener(node2, parent, lines, nextItemLine.get(parent))
13613
+ );
13614
+ }
13615
+ if (node2.type === "list") {
13616
+ const items = node2.children;
13617
+ items.forEach((listItem3, at3) => {
13618
+ const next = items[at3 + 1]?.position?.start.line;
13619
+ if (next !== void 0) nextItemLine.set(listItem3, next);
13620
+ });
13621
+ }
13085
13622
  if (node2.type === "footnoteReference" && parent) {
13086
- const caret2 = linkedReference(node2, parent, source);
13623
+ const caret2 = linkedReference(node2, parent, index, source);
13087
13624
  if (caret2 !== void 0)
13088
13625
  edits.push({ kind: "neutralize", offset: caret2 });
13089
13626
  }
13090
13627
  if (node2.type === "footnoteDefinition" && parent) {
13091
- const colon5 = labelRowUnderTable(node2, parent, source, lines);
13628
+ const colon5 = labelRowUnderTable(
13629
+ node2,
13630
+ parent,
13631
+ index,
13632
+ source,
13633
+ lines
13634
+ );
13092
13635
  if (colon5 !== void 0)
13093
13636
  edits.push({ kind: "neutralize", offset: colon5 });
13094
13637
  }
@@ -13112,18 +13655,20 @@ function detect(tree, source, lines, parse3, options) {
13112
13655
  if (options.comments && node2.type === "footnoteDefinition") {
13113
13656
  settled.push(...lazyCommentUnderDefinition(node2, lines));
13114
13657
  }
13115
- for (const child of children) {
13116
- walk(
13117
- child,
13658
+ for (let at3 = children.length - 1; at3 >= 0; at3--) {
13659
+ pending.push([
13660
+ children[at3],
13118
13661
  inDefinition || node2.type === "footnoteDefinition",
13119
13662
  node2.type === "listItem" ? node2 : item,
13120
13663
  node2.type === "root" ? 0 : node2.position?.start.line ?? 0,
13121
13664
  node2,
13122
- CONTAINERS2.has(node2.type) ? node2.type : container
13123
- );
13665
+ CONTAINERS2.has(node2.type) ? node2.type : container,
13666
+ at3
13667
+ ]);
13124
13668
  }
13125
13669
  };
13126
- walk(tree, false, void 0, 0);
13670
+ const pending = [[tree, false, void 0, 0]];
13671
+ while (pending.length > 0) walk(...pending.pop());
13127
13672
  for (const { node: definition2, item, hostLine } of definitions) {
13128
13673
  const position = definition2.position;
13129
13674
  if (!position || position.start.offset === void 0) continue;
@@ -13261,9 +13806,8 @@ function keptLazyLines(node2, source, lines, parse3) {
13261
13806
  }
13262
13807
  return edits;
13263
13808
  }
13264
- function closesMidline(node2, parent) {
13809
+ function closesMidline(node2, parent, index) {
13265
13810
  if (node2.type !== "math" || !parent) return false;
13266
- const index = parent.children.indexOf(node2);
13267
13811
  const before2 = parent.children[index - 1];
13268
13812
  if (before2?.type !== "paragraph") return false;
13269
13813
  if (before2.position?.end.line !== (node2.position?.start.line ?? 0) - 1) {
@@ -13298,6 +13842,36 @@ function containedMathOpener(node2, lines) {
13298
13842
  { kind: "neutralize", offset: position.start.offset + 1 }
13299
13843
  ];
13300
13844
  }
13845
+ function lastLineMathOpener(node2, parent, lines, nextItem) {
13846
+ const position = node2.position;
13847
+ if (!position || position.start.offset === void 0) return [];
13848
+ const line = position.start.line;
13849
+ if (endLine(position.end) !== line) return [];
13850
+ const start = position.start.offset - (lines.starts[line - 1] ?? 0);
13851
+ if (!/^\$\$[ \t]*$/u.test((lines.text[line - 1] ?? "").slice(start))) {
13852
+ return [];
13853
+ }
13854
+ const last = parent.type === "root" ? line === lines.text.length : parent.position !== void 0 && Math.min(endLine(parent.position.end), (nextItem ?? Infinity) - 1) === line;
13855
+ if (!last) return [];
13856
+ return [
13857
+ { kind: "neutralize", offset: position.start.offset },
13858
+ { kind: "neutralize", offset: position.start.offset + 1 }
13859
+ ];
13860
+ }
13861
+ function closesSpanAbove(node2, parent, index) {
13862
+ const before2 = parent.children[index - 1];
13863
+ if (!before2 || before2.position?.end.line !== (node2.position?.start.line ?? 0) - 1) {
13864
+ return false;
13865
+ }
13866
+ let text3 = "";
13867
+ visitText(before2, (value2) => {
13868
+ text3 += value2;
13869
+ });
13870
+ return (text3.match(/\$\$/gu)?.length ?? 0) % 2 === 1;
13871
+ }
13872
+ function endLine(end) {
13873
+ return end.column === 1 && end.line > 1 ? end.line - 1 : end.line;
13874
+ }
13301
13875
  function lazyCommentUnderDefinition(definition2, lines) {
13302
13876
  const comment2 = definition2.children.at(-1);
13303
13877
  const paragraph2 = definition2.children.at(-2);
@@ -13318,8 +13892,7 @@ function lazyCommentUnderDefinition(definition2, lines) {
13318
13892
  const lazy = own5.split(">").length - 1 < lead.split(">").length - 1 || columns(own5) < columns(lead);
13319
13893
  return lazy ? [] : [{ kind: "neutralize", offset }];
13320
13894
  }
13321
- function linkedReference(reference2, parent, source) {
13322
- const index = parent.children.indexOf(reference2);
13895
+ function linkedReference(reference2, parent, index, source) {
13323
13896
  const next = parent.children[index + 1];
13324
13897
  const start = reference2.position?.start.offset;
13325
13898
  const end = reference2.position?.end.offset;
@@ -13329,8 +13902,7 @@ function linkedReference(reference2, parent, source) {
13329
13902
  }
13330
13903
  return source.slice(start, start + 2) === "[^" ? start + 1 : void 0;
13331
13904
  }
13332
- function labelRowUnderTable(definition2, parent, source, lines) {
13333
- const index = parent.children.indexOf(definition2);
13905
+ function labelRowUnderTable(definition2, parent, index, source, lines) {
13334
13906
  const table2 = parent.children[index - 1];
13335
13907
  const at3 = table2?.position?.start.offset;
13336
13908
  const line = definition2.position?.start.line;
@@ -13344,12 +13916,17 @@ function labelRowUnderTable(definition2, parent, source, lines) {
13344
13916
  const lineEnd2 = (lines.starts[line - 1] ?? 0) + (lines.text[line - 1] ?? "").length;
13345
13917
  return source.slice(colon5, lineEnd2).includes("|") ? colon5 : void 0;
13346
13918
  }
13919
+ function lineOf(source, offset) {
13920
+ const start = source.lastIndexOf("\n", offset - 1) + 1;
13921
+ const end = source.indexOf("\n", offset);
13922
+ return source.slice(start, end === -1 ? void 0 : end);
13923
+ }
13347
13924
  function isCalloutTitle(paragraph2, parent) {
13348
13925
  if (parent?.type !== "blockquote" || parent.children[0] !== paragraph2) {
13349
13926
  return false;
13350
13927
  }
13351
13928
  const lead = paragraph2.children[0];
13352
- return lead?.type === "text" && MARKER6.test(lead.value);
13929
+ return lead?.type === "text" && MARKER.test(lead.value);
13353
13930
  }
13354
13931
  function titleLeavesLabel(paragraph2, labelLine, lines, parse3) {
13355
13932
  const position = paragraph2.position;
@@ -13786,14 +14363,15 @@ var BLOCK_PARENTS = /* @__PURE__ */ new Set([
13786
14363
  function commentParseTransform(tree, source, reparse) {
13787
14364
  if (source === "") return;
13788
14365
  const depthOf = /* @__PURE__ */ new Map();
13789
- const walk = (node2, depth) => {
14366
+ const pending = [[tree, 0]];
14367
+ while (pending.length > 0) {
14368
+ const [node2, depth] = pending.pop();
13790
14369
  const inner = node2.type === "blockquote" || node2.type === "callout" ? depth + 1 : depth;
13791
14370
  for (const child of node2.children ?? []) {
13792
14371
  if (child.type === "comment") depthOf.set(child, inner);
13793
- walk(child, inner);
14372
+ pending.push([child, inner]);
13794
14373
  }
13795
- };
13796
- walk(tree, 0);
14374
+ }
13797
14375
  visit(tree, "comment", (node2, _index, parent) => {
13798
14376
  const comment2 = node2;
13799
14377
  const start = node2.position?.start?.offset;
@@ -13979,7 +14557,7 @@ function phrasingBefore(paragraph2, at3, source) {
13979
14557
  value: value2,
13980
14558
  position: {
13981
14559
  start: { ...child.position.start },
13982
- end: pointAt2(source, at3)
14560
+ end: pointAt(source, at3)
13983
14561
  }
13984
14562
  });
13985
14563
  }
@@ -14011,10 +14589,10 @@ function calloutTitleInterruptTransform(tree, source, reparse) {
14011
14589
  const titleLine = titled ? callout.children[0]?.position?.start?.line : callout.position?.start?.line;
14012
14590
  const from = next.position?.start?.offset;
14013
14591
  const startLine = next.position?.start?.line;
14014
- const endLine = next.position?.end?.line;
14592
+ const endLine2 = next.position?.end?.line;
14015
14593
  if (titleLine === void 0 || from === void 0) return;
14016
- if (startLine === void 0 || endLine === void 0) return;
14017
- if (startLine !== titleLine + 1 || endLine !== startLine) return;
14594
+ if (startLine === void 0 || endLine2 === void 0) return;
14595
+ if (startLine !== titleLine + 1 || endLine2 !== startLine) return;
14018
14596
  const nl = source.indexOf("\n", from);
14019
14597
  const text3 = source.slice(from, nl === -1 ? void 0 : nl);
14020
14598
  const ordered = ORDERED.exec(text3);
@@ -14085,15 +14663,15 @@ function restoreInlinePositionsTransform(tree, source) {
14085
14663
  const span2 = spans[index];
14086
14664
  if (span2 === void 0) return;
14087
14665
  child.position = {
14088
- start: pointAt2(source, span2[0]),
14089
- end: pointAt2(source, span2[1])
14666
+ start: pointAt(source, span2[0]),
14667
+ end: pointAt(source, span2[1])
14090
14668
  };
14091
14669
  if (child.type === "link" && child.children.length === 1) {
14092
14670
  const only = child.children[0];
14093
14671
  if (only !== void 0 && only.position === void 0) {
14094
14672
  only.position = {
14095
- start: pointAt2(source, span2[0]),
14096
- end: pointAt2(source, span2[1])
14673
+ start: pointAt(source, span2[0]),
14674
+ end: pointAt(source, span2[1])
14097
14675
  };
14098
14676
  }
14099
14677
  }
@@ -14104,11 +14682,10 @@ function restoreInlinePositionsTransform(tree, source) {
14104
14682
  // src/lib/mdast/attention-to-markdown.ts
14105
14683
  import { defaultHandlers as defaultHandlers2 } from "mdast-util-to-markdown";
14106
14684
  var parentOf = /* @__PURE__ */ new WeakMap();
14107
- function recordParents(node2) {
14108
- for (const child of node2.children ?? []) {
14109
- parentOf.set(child, node2);
14110
- recordParents(child);
14111
- }
14685
+ function recordParents(root) {
14686
+ eachBelow(root, (node2, parent) => {
14687
+ parentOf.set(node2, parent);
14688
+ });
14112
14689
  }
14113
14690
  function nextOf(node2) {
14114
14691
  const siblings = parentOf.get(node2)?.children;
@@ -14199,14 +14776,12 @@ function holdsStar(node2) {
14199
14776
  function attentionToMarkdown(options) {
14200
14777
  const pinned = /* @__PURE__ */ new WeakSet();
14201
14778
  const beforeAttention = /* @__PURE__ */ new WeakSet();
14202
- const isAttention = (node2) => node2?.type === "emphasis" || node2?.type === "strong";
14203
- const markAdjacent = (node2) => {
14204
- const children = node2.children ?? [];
14205
- children.forEach((child, index) => {
14206
- if (isAttention(child) && isAttention(children[index + 1])) {
14207
- beforeAttention.add(child);
14208
- }
14209
- markAdjacent(child);
14779
+ const isAttention2 = (node2) => node2?.type === "emphasis" || node2?.type === "strong";
14780
+ const markAdjacent = (root2) => {
14781
+ eachBelow(root2, (node2, parent, index) => {
14782
+ if (isAttention2(node2) && isAttention2(childrenOf(parent)[index + 1])) {
14783
+ beforeAttention.add(node2);
14784
+ }
14210
14785
  });
14211
14786
  };
14212
14787
  const wrap = (base) => {
@@ -14226,7 +14801,7 @@ function attentionToMarkdown(options) {
14226
14801
  const only2 = node2.children.length === 1 ? node2.children[0] : void 0;
14227
14802
  return {
14228
14803
  ...result,
14229
- markers: node2.type === "emphasis" && isAttention(only2) ? ["_", "*"] : ["*"]
14804
+ markers: node2.type === "emphasis" && isAttention2(only2) ? ["_", "*"] : ["*"]
14230
14805
  };
14231
14806
  }
14232
14807
  if (!options.legacy) {
@@ -14240,11 +14815,11 @@ function attentionToMarkdown(options) {
14240
14815
  if (node2.type === "emphasis" && only?.type === "emphasis" && only.children.length === 1 && only.children[0]?.type === "strong") {
14241
14816
  return { ...result, markers: ["*"] };
14242
14817
  }
14243
- if (node2.type === "strong" && only?.type === "emphasis" && !(only.children.length === 1 && isAttention(only.children[0]))) {
14818
+ if (node2.type === "strong" && only?.type === "emphasis" && !(only.children.length === 1 && isAttention2(only.children[0]))) {
14244
14819
  pinned.add(only);
14245
14820
  return { ...result, markers: ["*"] };
14246
14821
  }
14247
- if (node2.type === "emphasis" && (endsInStar(node2.children.at(-1)) || isAttention(node2.children[0])) || // Not when something verbatim inside holds a `_` too: `_` would close on it as
14822
+ if (node2.type === "emphasis" && (endsInStar(node2.children.at(-1)) || isAttention2(node2.children[0])) || // Not when something verbatim inside holds a `_` too: `_` would close on it as
14248
14823
  // `*` would on a `*` (`*em` / `![[_under_]]` / `_under_. *`).
14249
14824
  holdsStar(node2) && !underscoreUnsafe(node2, false) || beforeAttention.has(node2) && !opensWithAttention(nextOf(node2))) {
14250
14825
  return { ...result, markers: ["_", "*"] };
@@ -14314,6 +14889,44 @@ function isGfmLiteralEmail(value2) {
14314
14889
  }
14315
14890
  return true;
14316
14891
  }
14892
+ var BARE_URL = /^https?:\/\/[a-z\d-]+(?:\.[a-z\d-]+)+(?:\/[\w\-./?#+:@=]*)?$/iu;
14893
+ var isAttention = (node2) => node2?.type === "emphasis" || node2?.type === "strong" || node2?.type === "delete";
14894
+ var siblingsOf = (node2) => parentOf.get(node2)?.children ?? [];
14895
+ function opensAttentionBare(link3, text3) {
14896
+ if (link3.url !== text3 || !BARE_URL.test(text3) || text3.includes("==")) {
14897
+ return false;
14898
+ }
14899
+ const end = text3.at(-1);
14900
+ if (!/[a-z\d/]/iu.test(end)) return false;
14901
+ let node2 = link3;
14902
+ let opens2 = false;
14903
+ for (; ; ) {
14904
+ const parent = parentOf.get(node2);
14905
+ if (!isAttention(parent) || siblingsOf(node2)[0] !== node2) break;
14906
+ opens2 = true;
14907
+ node2 = parent;
14908
+ }
14909
+ if (!opens2) return false;
14910
+ node2 = link3;
14911
+ for (; ; ) {
14912
+ const siblings = siblingsOf(node2);
14913
+ const next = siblings[siblings.indexOf(node2) + 1];
14914
+ if (next) {
14915
+ if (next.type === "break") return true;
14916
+ if (next.type !== "text") return false;
14917
+ const rest = next.value.replace(/^[.,:;!?]+/u, "");
14918
+ if (rest !== "") return /^[ \t\n]/u.test(rest);
14919
+ node2 = next;
14920
+ continue;
14921
+ }
14922
+ const parent = parentOf.get(node2);
14923
+ if (isAttention(parent)) {
14924
+ node2 = parent;
14925
+ continue;
14926
+ }
14927
+ return parent?.type === "paragraph" || parent?.type === "heading" || parent?.type === "tableCell";
14928
+ }
14929
+ }
14317
14930
  function soleText(node2) {
14318
14931
  if (node2.children.length !== 1) return void 0;
14319
14932
  const only = node2.children[0];
@@ -14327,6 +14940,9 @@ function autolinkLiteralToMarkdown(options = {}) {
14327
14940
  if (text3 !== void 0 && !node2.title && node2.url === MAILTO + text3 && !isCommonMarkEmail(text3) && isGfmLiteralEmail(text3)) {
14328
14941
  return text3;
14329
14942
  }
14943
+ if (options.strict && text3 !== void 0 && !node2.title && opensAttentionBare(node2, text3)) {
14944
+ return text3;
14945
+ }
14330
14946
  const written2 = defaultHandlers3.link(
14331
14947
  node2,
14332
14948
  parent,
@@ -14385,10 +15001,22 @@ function withSpelledLabel(node2, write) {
14385
15001
  const written2 = write({ ...node2, label: LABEL4 });
14386
15002
  return written2.includes(LABEL4) ? written2.replace(LABEL4, () => spelled) : write(node2);
14387
15003
  }
15004
+ function keptReference(node2, parent, state) {
15005
+ const data = node2.data;
15006
+ if (data?.defined !== false || node2.referenceType === "full")
15007
+ return void 0;
15008
+ const label = data.spelledLabel ?? node2.label;
15009
+ if (label === void 0 || label === null) return void 0;
15010
+ const next = parent?.type === "paragraph" ? parent.children[1] : void 0;
15011
+ const callout = parent?.type === "paragraph" && parent.children[0] === node2 && state.stack.includes("blockquote") && state.indexStack[state.indexStack.length - 2] === 0 && /^![^\]\n]+$/u.test(label) && (next === void 0 || next.type === "break" || next.type === "text" && /^[+-]?(?:[ \t\n]|$)/u.test(next.value));
15012
+ return (node2.type === "imageReference" ? "![" : "[") + (callout ? `\\${label}` : label) + "]" + (node2.referenceType === "collapsed" ? "[]" : "");
15013
+ }
14388
15014
  function linkReferenceToMarkdown(options = {}) {
14389
15015
  return {
14390
15016
  handlers: {
14391
15017
  linkReference(node2, parent, state, info) {
15018
+ const kept = keptReference(node2, parent, state);
15019
+ if (kept !== void 0) return kept;
14392
15020
  if (node2.referenceType === "shortcut" && BLANK3.test(node2.label ?? node2.identifier)) {
14393
15021
  const exit = state.enter("linkReference");
14394
15022
  const label = node2.label ?? node2.identifier;
@@ -14402,7 +15030,7 @@ function linkReferenceToMarkdown(options = {}) {
14402
15030
  return balanceLabel(node2, state, written2, options.strict);
14403
15031
  },
14404
15032
  imageReference(node2, parent, state, info) {
14405
- return withSpelledLabel(
15033
+ return keptReference(node2, parent, state) ?? withSpelledLabel(
14406
15034
  node2,
14407
15035
  (own5) => defaultHandlers4.imageReference(own5, parent, state, info)
14408
15036
  );
@@ -14424,7 +15052,6 @@ function frontmatterTailTransform(tree, source, reparse) {
14424
15052
  }
14425
15053
  if (closeIndex === -1) return;
14426
15054
  const tail = lines[closeIndex].slice(3);
14427
- if (tail.trim() === "") return;
14428
15055
  let offset = 0;
14429
15056
  const lineStarts = lines.map((line) => {
14430
15057
  const start = offset;
@@ -14432,6 +15059,13 @@ function frontmatterTailTransform(tree, source, reparse) {
14432
15059
  return start;
14433
15060
  });
14434
15061
  const closeEnd = lineStarts[closeIndex] + 3;
15062
+ if (tail.trim() === "") {
15063
+ const yaml2 = tree.children[0];
15064
+ if (tail !== "" && yaml2?.type === "yaml" && yaml2.position && (yaml2.position.end.offset ?? 0) > closeEnd) {
15065
+ yaml2.position.end = { line: closeIndex + 1, column: 4, offset: closeEnd };
15066
+ }
15067
+ return;
15068
+ }
14435
15069
  const rest = [tail, ...lines.slice(closeIndex + 1)].join("\n");
14436
15070
  const map = {
14437
15071
  text: rest,
@@ -14748,11 +15382,6 @@ function taskCharToMarkdown(options = {}) {
14748
15382
  const indent2 = state.options.listItemIndent;
14749
15383
  if (wide) state.options.listItemIndent = "tab";
14750
15384
  try {
14751
- return writeItem();
14752
- } finally {
14753
- state.options.listItemIndent = indent2;
14754
- }
14755
- function writeItem() {
14756
15385
  const head = node2.children[0];
14757
15386
  const isTask = taskChar !== null;
14758
15387
  if (!isTask) {
@@ -14774,6 +15403,8 @@ function taskCharToMarkdown(options = {}) {
14774
15403
  (match) => match + checkbox
14775
15404
  );
14776
15405
  return value2;
15406
+ } finally {
15407
+ state.options.listItemIndent = indent2;
14777
15408
  }
14778
15409
  }
14779
15410
  }
@@ -14796,8 +15427,14 @@ function textToMarkdown(tags2, highlights2 = false, legacy = false) {
14796
15427
  defaultHandlers6.text(node2, parent, state, info),
14797
15428
  tags2
14798
15429
  ).replace(/(?<!\\)((?:\\\\)*)\\(\s+)$/u, "$1\\\\$2");
14799
- const siblingsOf = parent?.children ?? [];
14800
- const afterBreak = siblingsOf[siblingsOf.indexOf(node2) - 1]?.type === "break";
15430
+ if (tags2 && parent?.type === "highlight" && parent.children[0] === node2) {
15431
+ const marker = colourMarkerLength(written2);
15432
+ if (marker > 0 && /^#[^\s]/u.test(written2.slice(marker))) {
15433
+ written2 = `${written2.slice(0, marker)}\\${written2.slice(marker)}`;
15434
+ }
15435
+ }
15436
+ const siblingsOf2 = parent?.children ?? [];
15437
+ const afterBreak = siblingsOf2[siblingsOf2.indexOf(node2) - 1]?.type === "break";
14801
15438
  written2 = written2.replace(/\u2E7F/gu, "&#11903;");
14802
15439
  written2 = guardAmpersands(
14803
15440
  legacy ? guardBlankLines(written2, afterBreak, info.after) : written2
@@ -14815,7 +15452,7 @@ function textToMarkdown(tags2, highlights2 = false, legacy = false) {
14815
15452
  written2 = `\\${written2}`;
14816
15453
  }
14817
15454
  const beforeReference = siblings[siblings.indexOf(node2) - 2];
14818
- if (previous2?.type === "footnoteReference" && written2.startsWith(":") && (beforeReference === void 0 || beforeReference.type === "break" || beforeReference.type === "text" && /\n[ \t]*$/u.test(beforeReference.value ?? ""))) {
15455
+ if ((previous2?.type === "footnoteReference" || previous2?.data?.defined === false) && written2.startsWith(":") && (beforeReference === void 0 || beforeReference.type === "break" || beforeReference.type === "text" && /\n[ \t]*$/u.test(beforeReference.value ?? ""))) {
14819
15456
  written2 = `&#x3A;${written2.slice(1)}`;
14820
15457
  }
14821
15458
  if (previous2?.type === "tag" && /^[_/-]/u.test(written2)) {
@@ -15248,7 +15885,10 @@ function hasUpstreamMathText(context) {
15248
15885
  const list3 = Array.isArray(registered) ? registered : [registered];
15249
15886
  return list3.some((construct) => construct?.name === "mathText");
15250
15887
  }
15888
+ var noCloserFrom2 = /* @__PURE__ */ new WeakMap();
15251
15889
  function tokenizeMathText(effects, ok3, nok) {
15890
+ const self = this;
15891
+ let opener2 = 0;
15252
15892
  let sizeOpen = 0;
15253
15893
  let sizeClose = 0;
15254
15894
  let token;
@@ -15256,6 +15896,7 @@ function tokenizeMathText(effects, ok3, nok) {
15256
15896
  return start;
15257
15897
  function start(code) {
15258
15898
  if (code !== DOLLAR2) return nok(code);
15899
+ opener2 = self.now().offset;
15259
15900
  effects.enter("mathText");
15260
15901
  effects.enter("mathTextSequence");
15261
15902
  return sequenceOpen(code);
@@ -15269,11 +15910,22 @@ function tokenizeMathText(effects, ok3, nok) {
15269
15910
  if (sizeOpen === 1 && (isDigit(code) || code === null || code === codes.space || code === codes.horizontalTab)) {
15270
15911
  return nok(code);
15271
15912
  }
15913
+ if (sizeOpen === 1) {
15914
+ const known = noCloserFrom2.get(self);
15915
+ if (known !== void 0 && opener2 > known) return nok(code);
15916
+ }
15272
15917
  effects.exit("mathTextSequence");
15273
15918
  return between(code);
15274
15919
  }
15275
15920
  function between(code) {
15276
- if (code === null) return nok(code);
15921
+ if (code === null) {
15922
+ if (sizeOpen === 1) {
15923
+ const known = noCloserFrom2.get(self);
15924
+ if (known === void 0 || opener2 < known)
15925
+ noCloserFrom2.set(self, opener2);
15926
+ }
15927
+ return nok(code);
15928
+ }
15277
15929
  if (code === DOLLAR2) {
15278
15930
  if (sizeOpen === 1 && afterSpace) {
15279
15931
  afterSpace = false;
@@ -15952,21 +16604,10 @@ var PRESETS = {
15952
16604
  strict: STRICT_OVERRIDES
15953
16605
  };
15954
16606
  var defaultOptions = {
15955
- wikilinks: true,
15956
- highlights: true,
15957
- comments: true,
15958
- tags: true,
15959
- customTaskChars: true,
15960
- math: true,
15961
- frontmatter: true,
15962
- keepComments: true,
15963
- parseComments: true,
15964
- lineOffsets: true,
15965
- bareFootnoteRefs: true,
15966
- inlineFootnotes: true,
15967
- callouts: true,
15968
- positionedBreaks: true,
15969
- escapedLineBreaks: false
16607
+ ...legacyDefaults,
16608
+ readsPastEnd: false,
16609
+ escapedLineBreaks: false,
16610
+ bomOffsets: false
15970
16611
  };
15971
16612
  function displayAwareMathToMarkdown() {
15972
16613
  const single = mathToMarkdown();
@@ -16070,6 +16711,7 @@ function undefinedReferencesTransform(tree, source) {
16070
16711
  visit(tree, "footnoteReference", (node2, index, parent) => {
16071
16712
  if (parent === void 0 || index === void 0) return void 0;
16072
16713
  if (defined2.has(node2.identifier)) return void 0;
16714
+ if (node2.data?.inert) return void 0;
16073
16715
  const from = node2.position?.start?.offset;
16074
16716
  const to = node2.position?.end?.offset;
16075
16717
  const value2 = from !== void 0 && to !== void 0 ? decodeString(source.slice(from, to)) : `[^${node2.label ?? node2.identifier}]`;
@@ -16276,7 +16918,11 @@ function remarkObsidian(userOpts) {
16276
16918
  definitionMathAbsorbTransform(tree, String(file));
16277
16919
  definitionEndBeforeLabelTransform(tree, String(file));
16278
16920
  tagAgainstTextTransform(tree, String(file));
16279
- if (opts.inlineFootnotes) inlineFootnoteInertTransform(tree);
16921
+ if (opts.tags && opts.highlights)
16922
+ colourMarkerTagTransform(tree, String(file));
16923
+ if (opts.inlineFootnotes && !opts.inertFootnoteRefs) {
16924
+ inlineFootnoteInertTransform(tree);
16925
+ }
16280
16926
  if (opts.lineOffsets) ensureLineOffsets(tree);
16281
16927
  if (opts.keepComments && opts.parseComments) {
16282
16928
  commentParseTransform(tree, String(file), reparse);
@@ -16297,7 +16943,12 @@ function remarkObsidian(userOpts) {
16297
16943
  );
16298
16944
  return SKIP;
16299
16945
  });
16300
- if (opts.inlineFootnotes) inlineFootnoteInertTransform(tree);
16946
+ if (opts.inlineFootnotes && !opts.inertFootnoteRefs) {
16947
+ inlineFootnoteInertTransform(tree);
16948
+ }
16949
+ }
16950
+ if (opts.inlineFootnotes && opts.inertFootnoteRefs) {
16951
+ inlineFootnoteInertKeepTransform(tree, String(file));
16301
16952
  }
16302
16953
  const literals = gfmLiteralTransforms(data.fromMarkdownExtensions);
16303
16954
  if (literals.length > 0) {
@@ -16314,6 +16965,7 @@ function remarkObsidian(userOpts) {
16314
16965
  footnoteCodeBlankLinesTransform(tree);
16315
16966
  adjacentIndentedCodeTransform(tree, String(file));
16316
16967
  footnoteIdentifierTransform(tree);
16968
+ if (opts.math) mathDisplayTransform(tree, String(file));
16317
16969
  if (!opts.bareFootnoteRefs)
16318
16970
  undefinedReferencesTransform(tree, String(file));
16319
16971
  if (opts.callouts) syncCalloutTitlesTransform(tree);
@@ -16331,7 +16983,14 @@ function remarkObsidian(userOpts) {
16331
16983
  }
16332
16984
  return fragment;
16333
16985
  };
16334
- return transform;
16986
+ return (tree, file) => {
16987
+ const source = String(file);
16988
+ const marked = source.charCodeAt(0) === 65279;
16989
+ const text3 = marked ? source.slice(1) : source;
16990
+ transform(tree, marked ? text3 : file);
16991
+ if (opts.callouts) calloutMarkerTransform(tree, text3);
16992
+ if (marked && opts.bomOffsets) countByteOrderMark(tree);
16993
+ };
16335
16994
  }
16336
16995
  export {
16337
16996
  COMMENT_ELEMENT,