@quartz-community/remark-obsidian 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,149 +2494,696 @@ 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,
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 writtenPart(part, spelled, strippedBackslash) {
3079
+ const resolved = strippedBackslash ? `${part}\\` : part;
3080
+ return spelled !== void 0 && resolveTarget(spelled) === resolved ? spelled : resolved;
3081
+ }
3082
+
3083
+ // src/lib/syntax/legacy-scan.ts
3084
+ function codeToCharacter(code) {
3085
+ if (code === -5 || code === -4 || code === -3) return "\n";
3086
+ if (code === -2) return " ";
3087
+ if (code === -1) return " ";
3088
+ return String.fromCodePoint(code);
3089
+ }
3090
+ function createChunkScan(sink, stopAfter) {
3091
+ return {
3092
+ partial: true,
3093
+ tokenize(effects, ok3) {
3094
+ return function enter(code) {
3095
+ effects.enter("legacyChunkScan");
3096
+ return step2(code);
3097
+ };
3098
+ function step2(code) {
3099
+ if (code === null) {
3100
+ effects.exit("legacyChunkScan");
3101
+ return ok3(code);
3102
+ }
3103
+ const character = codeToCharacter(code);
3104
+ sink.push(character);
3105
+ effects.consume(code);
3106
+ if (stopAfter?.(character) === true) {
3107
+ effects.exit("legacyChunkScan");
3108
+ return ok3;
3109
+ }
3110
+ return step2;
3111
+ }
3112
+ }
3113
+ };
3114
+ }
3115
+ function planBody(body, options = {}) {
3116
+ const segments = [];
3117
+ let index = 0;
3118
+ while (index < body.length) {
3119
+ if (body[index] === "\n") {
3120
+ segments.push({ kind: "eol", length: 1 });
3121
+ index++;
3122
+ continue;
3123
+ }
3124
+ let end = index;
3125
+ while (end < body.length && body[end] !== "\n") end++;
3126
+ if (options.stripContinuationIndent === true && index > 0) {
3127
+ let after = index;
3128
+ while (after < end && (body[after] === " " || body[after] === " "))
3129
+ after++;
3130
+ if (after > index) {
3131
+ segments.push({ kind: "prefix", length: after - index });
3132
+ index = after;
3133
+ if (index === end) continue;
3134
+ }
3135
+ }
3136
+ const run = body.slice(index, end);
3137
+ const kept = run.trimEnd();
3138
+ const isLast = end >= body.length;
3139
+ let trailingSlashes = 0;
3140
+ while (trailingSlashes < run.length && run[run.length - 1 - trailingSlashes] === "\\") {
3141
+ trailingSlashes++;
3142
+ }
3143
+ if (isLast && kept.length < run.length) {
3144
+ if (kept.length > 0) segments.push({ kind: "text", length: kept.length });
3145
+ segments.push({ kind: "data", length: run.length - kept.length });
3146
+ } else if (!isLast && trailingSlashes % 2 === 1) {
3147
+ if (run.length > 1) {
3148
+ segments.push({ kind: "text", length: run.length - 1 });
3149
+ }
3150
+ segments.push({ kind: "hardBreak", length: 1 });
3151
+ } else {
3152
+ segments.push({ kind: "text", length: run.length });
3153
+ }
3154
+ index = end;
3155
+ }
3156
+ return segments;
3157
+ }
3158
+
3159
+ // node_modules/micromark-util-symbol/lib/codes.js
3160
+ var codes = (
3161
+ /** @type {const} */
3162
+ {
3163
+ carriageReturn: -5,
3164
+ lineFeed: -4,
3165
+ carriageReturnLineFeed: -3,
3166
+ horizontalTab: -2,
3167
+ virtualSpace: -1,
3168
+ eof: null,
3169
+ nul: 0,
3170
+ soh: 1,
3171
+ stx: 2,
3172
+ etx: 3,
3173
+ eot: 4,
3174
+ enq: 5,
3175
+ ack: 6,
3176
+ bel: 7,
3177
+ bs: 8,
3178
+ ht: 9,
3179
+ // `\t`
3180
+ lf: 10,
3181
+ // `\n`
3182
+ vt: 11,
3183
+ // `\v`
3184
+ ff: 12,
3185
+ // `\f`
3186
+ cr: 13,
2640
3187
  // `\r`
2641
3188
  so: 14,
2642
3189
  si: 15,
@@ -3275,34 +3822,44 @@ var types = (
3275
3822
 
3276
3823
  // src/lib/syntax/highlight.ts
3277
3824
  var EQUALS = 61;
3278
- var MARKER = "=";
3825
+ var MARKER2 = "=";
3279
3826
  var MARKER_LENGTH = 2;
3280
3827
  function legacyHighlightQueue(value2) {
3281
- if (value2.charAt(0) !== MARKER || value2.charAt(1) !== MARKER) return null;
3828
+ if (value2.charAt(0) !== MARKER2 || value2.charAt(1) !== MARKER2) return null;
3282
3829
  let index = MARKER_LENGTH;
3283
- let queue = "";
3284
3830
  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;
3831
+ if (value2.charAt(index) === MARKER2 && value2.charAt(index + 1) === MARKER2) {
3832
+ if (index === MARKER_LENGTH || isSpace(value2.charAt(MARKER_LENGTH)))
3833
+ return null;
3834
+ if (!isSpace(value2.charAt(index - 1)))
3835
+ return value2.slice(MARKER_LENGTH, index);
3289
3836
  }
3290
- queue += character;
3291
3837
  index++;
3292
3838
  }
3293
3839
  return null;
3294
3840
  }
3841
+ function isSpace(character) {
3842
+ return /\s/u.test(character);
3843
+ }
3844
+ var noCloserFrom = /* @__PURE__ */ new WeakMap();
3295
3845
  function createQueueBound() {
3296
3846
  let index = -1;
3297
3847
  let opened = false;
3848
+ let previous2 = "";
3849
+ let beforePrevious = "";
3298
3850
  return (character) => {
3299
3851
  index += 1;
3300
- if (index < MARKER_LENGTH) return character !== MARKER;
3852
+ const last = previous2;
3853
+ const beforeLast = beforePrevious;
3854
+ beforePrevious = previous2;
3855
+ previous2 = character;
3856
+ if (index < MARKER_LENGTH) return character !== MARKER2;
3301
3857
  if (index === MARKER_LENGTH) {
3302
- opened = character === MARKER;
3858
+ opened = character === MARKER2;
3303
3859
  return /^\s/u.test(character);
3304
3860
  }
3305
- return index === MARKER_LENGTH + 1 && opened && character === MARKER;
3861
+ if (index === MARKER_LENGTH + 1) return opened && character === MARKER2;
3862
+ return character === MARKER2 && last === MARKER2 && index >= MARKER_LENGTH + 2 && !/\s/u.test(beforeLast);
3306
3863
  };
3307
3864
  }
3308
3865
  var highlight = {
@@ -3345,12 +3902,18 @@ var highlight = {
3345
3902
  return start;
3346
3903
  function start(code) {
3347
3904
  if (code !== EQUALS) return nok(code);
3905
+ const known = noCloserFrom.get(self);
3906
+ if (known !== void 0 && self.now().offset >= known) return nok(code);
3348
3907
  characters.length = 0;
3349
3908
  return effects.check(scan, decide, decide)(code);
3350
3909
  }
3351
3910
  function decide(code) {
3352
3911
  const found = legacyHighlightQueue(characters.join(""));
3353
- if (found === null) return nok(code);
3912
+ if (found === null) {
3913
+ if (characters.length > MARKER_LENGTH + 2)
3914
+ noCloserFrom.set(self, self.now().offset);
3915
+ return nok(code);
3916
+ }
3354
3917
  if (emphasisClosesInside(self, found)) return nok(code);
3355
3918
  queue = found;
3356
3919
  effects.enter("highlight");
@@ -3383,6 +3946,7 @@ function group(character) {
3383
3946
  if (/\p{P}|\p{S}/u.test(character)) return 2;
3384
3947
  return void 0;
3385
3948
  }
3949
+ var pairedRuns = /* @__PURE__ */ new WeakMap();
3386
3950
  function flank(marker, size, before2, after) {
3387
3951
  const open = !after || after === 2 && Boolean(before2);
3388
3952
  const close2 = !before2 || before2 === 2 && Boolean(after);
@@ -3394,23 +3958,30 @@ function flank(marker, size, before2, after) {
3394
3958
  };
3395
3959
  }
3396
3960
  function emphasisClosesInside(self, body) {
3397
- const stack = [];
3398
- const take = (run, outer) => {
3961
+ const events = self.events;
3962
+ let paired = pairedRuns.get(self);
3963
+ if (paired?.events !== events || paired.read > events.length || paired.read > 0 && events[paired.read - 1] !== paired.last) {
3964
+ paired = { events, read: 0, last: void 0, stack: [] };
3965
+ pairedRuns.set(self, paired);
3966
+ }
3967
+ const stack = paired.stack;
3968
+ const take = (run, outer2) => {
3399
3969
  if (run.close) {
3400
3970
  for (let at3 = stack.length - 1; at3 >= 0 && run.size > 0; at3 -= 1) {
3401
3971
  const entry = stack[at3];
3402
3972
  if (entry.run.marker !== run.marker) continue;
3403
- if (entry.outer && !outer) return true;
3973
+ if (entry.outer && !outer2) return true;
3404
3974
  const used = Math.min(entry.run.size, run.size);
3405
3975
  entry.run.size -= used;
3406
3976
  run.size -= used;
3407
3977
  if (entry.run.size === 0) stack.splice(at3, 1);
3408
3978
  }
3409
3979
  }
3410
- if (run.open && run.size > 0) stack.push({ run, outer });
3980
+ if (run.open && run.size > 0) stack.push({ run, outer: outer2 });
3411
3981
  return false;
3412
3982
  };
3413
- for (const [kind, token] of self.events) {
3983
+ for (; paired.read < events.length; paired.read += 1) {
3984
+ const [kind, token] = events[paired.read];
3414
3985
  if (kind !== "enter" || token.type !== "attentionSequence") continue;
3415
3986
  const text3 = self.sliceSerialize(token);
3416
3987
  const flags = token;
@@ -3424,19 +3995,25 @@ function emphasisClosesInside(self, body) {
3424
3995
  true
3425
3996
  );
3426
3997
  }
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;
3998
+ paired.last = events[paired.read - 1];
3999
+ const outer = stack.length;
4000
+ if (outer === 0) return false;
4001
+ try {
4002
+ for (const match of body.matchAll(/\*+|_+/gu)) {
4003
+ const start = match.index;
4004
+ const end = start + match[0].length;
4005
+ const run = flank(
4006
+ match[0][0],
4007
+ match[0].length,
4008
+ group(start > 0 ? body[start - 1] : MARKER2),
4009
+ group(end < body.length ? body[end] : MARKER2)
4010
+ );
4011
+ if (take(run, false)) return true;
4012
+ }
4013
+ return false;
4014
+ } finally {
4015
+ stack.length = outer;
3438
4016
  }
3439
- return false;
3440
4017
  }
3441
4018
  function highlightSyntax() {
3442
4019
  return { text: { [EQUALS]: highlight } };
@@ -4017,6 +4594,69 @@ function normalListItem(ctx, value2, position) {
4017
4594
  }
4018
4595
  }
4019
4596
 
4597
+ // src/rp8/reads-past-end.ts
4598
+ function definitionCutOff(doc, from) {
4599
+ const length = doc.length;
4600
+ const skip = (start, set) => {
4601
+ let at4 = start;
4602
+ while (at4 < length && set.includes(doc[at4])) at4++;
4603
+ return at4;
4604
+ };
4605
+ let at3 = skip(from, " ");
4606
+ if (doc[at3] !== "[" || doc[at3 + 1] === "^") return false;
4607
+ const labelStart = ++at3;
4608
+ while (at3 < length && doc[at3] !== "]") at3 += doc[at3] === "\\" ? 2 : 1;
4609
+ if (at3 >= length) return true;
4610
+ if (at3 === labelStart || doc[at3 + 1] !== ":") return false;
4611
+ at3 = skip(at3 + 2, " \n");
4612
+ if (at3 >= length) return true;
4613
+ let enclosed = 0;
4614
+ if (doc[at3] === "<") {
4615
+ const open = ++at3;
4616
+ while (at3 < length && !">[]".includes(doc[at3])) at3++;
4617
+ if (at3 >= length) return true;
4618
+ if (doc[at3] !== ">") return false;
4619
+ enclosed = at3 - open;
4620
+ at3++;
4621
+ }
4622
+ if (enclosed === 0) {
4623
+ const bare = at3;
4624
+ while (at3 < length && !/[[\]\s]/u.test(doc[at3])) at3++;
4625
+ if (at3 === bare) return at3 >= length;
4626
+ }
4627
+ const gap = skip(at3, " \n");
4628
+ if (gap === at3 || gap >= length) return false;
4629
+ const opener2 = doc[gap];
4630
+ const closer = opener2 === '"' || opener2 === "'" ? opener2 : opener2 === "(" ? ")" : "";
4631
+ if (closer === "") return false;
4632
+ at3 = gap + 1;
4633
+ while (at3 < length && doc[at3] !== closer) {
4634
+ if (doc[at3] === "\n") {
4635
+ at3++;
4636
+ if (doc[at3] === "\n" || doc[at3] === closer) return false;
4637
+ }
4638
+ at3++;
4639
+ }
4640
+ return at3 >= length;
4641
+ }
4642
+ function watchDefinitionReader(tables) {
4643
+ const stock = tables.blockTokenizers.definition;
4644
+ tables.blockTokenizers.definition = function(eat, value2, silent) {
4645
+ const result = stock.call(this, eat, value2, silent);
4646
+ 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
4647
+ // line endings and drops a byte order mark first. A direct caller passing raw `\r\n` text
4648
+ // would get a silent `false` here.
4649
+ this.file.toString().endsWith(value2)) {
4650
+ this.definitionCutOff = true;
4651
+ }
4652
+ return result;
4653
+ };
4654
+ }
4655
+ function readsPastEnd(parser, tree) {
4656
+ const last = tree.children?.[tree.children.length - 1];
4657
+ return parser.definitionCutOff === true || last?.type === "definition" && last.title == null;
4658
+ }
4659
+
4020
4660
  // src/rp8/vendor/remark-footnotes/index.ts
4021
4661
  var remark_footnotes_default = footnotes;
4022
4662
  var tab3 = 9;
@@ -8568,6 +9208,20 @@ function nextWhere(frame, key, test) {
8568
9208
  }
8569
9209
  return table2;
8570
9210
  }
9211
+ function backtickBefore(frame, at3) {
9212
+ let table2 = frame.next.get("backtick-before");
9213
+ if (!table2) {
9214
+ const { value: value2 } = frame;
9215
+ table2 = new Int32Array(value2.length + 1);
9216
+ let last = -1;
9217
+ for (let i = 0; i <= value2.length; i++) {
9218
+ table2[i] = last;
9219
+ if (value2.charCodeAt(i) === 96) last = i;
9220
+ }
9221
+ frame.next.set("backtick-before", table2);
9222
+ }
9223
+ return table2[at3];
9224
+ }
8571
9225
  function closingBracket(frame, open) {
8572
9226
  if (!frame.lower) {
8573
9227
  const { value: value2 } = frame;
@@ -8620,6 +9274,14 @@ function speedUps(tables, features) {
8620
9274
  frames.pop();
8621
9275
  }
8622
9276
  };
9277
+ const stockHtml = tables.blockTokenizers.html;
9278
+ const html = function(eat, value2, silent) {
9279
+ let at3 = 0;
9280
+ while (value2.charCodeAt(at3) === 32 || value2.charCodeAt(at3) === 9) at3++;
9281
+ if (value2.charCodeAt(at3) !== 60) return void 0;
9282
+ return stockHtml.call(this, eat, value2, silent);
9283
+ };
9284
+ tables.blockTokenizers.html = html;
8623
9285
  const stockText = tables.inlineTokenizers.text;
8624
9286
  const text3 = function(eat, value2, silent) {
8625
9287
  const frame = frameOf(this, value2);
@@ -8641,13 +9303,22 @@ function speedUps(tables, features) {
8641
9303
  if (asked === -1 || asked > from || at3 !== -1 && at3 < from) {
8642
9304
  const position = reader.locator.call(this, value2, 1);
8643
9305
  at3 = position === -1 ? -1 : base + position;
8644
- if (name2 === "code" && at3 !== -1) at3 = spanCapable(frame)[at3];
9306
+ if (name2 === "code" && at3 !== -1) {
9307
+ const skipTo = spanCapable(frame)[at3];
9308
+ const amp = nextWhere(frame, "&", (v, i) => v.charCodeAt(i) === 38)[at3 - 1];
9309
+ if (amp === -1 || skipTo !== -1 && skipTo < amp) at3 = skipTo;
9310
+ else at3 = Math.max(at3, backtickBefore(frame, amp));
9311
+ }
8645
9312
  frame.askedAt[index] = from;
8646
9313
  frame.answer[index] = at3;
8647
9314
  }
8648
9315
  if (at3 !== -1 && at3 - base < min) min = at3 - base;
8649
9316
  }
8650
9317
  const subvalue = value2.slice(0, min);
9318
+ if (subvalue !== "" && !subvalue.includes("&")) {
9319
+ eat(subvalue)({ type: "text", value: subvalue });
9320
+ return void 0;
9321
+ }
8651
9322
  this.decode(
8652
9323
  subvalue,
8653
9324
  eat.now(),
@@ -8692,7 +9363,7 @@ function speedUps(tables, features) {
8692
9363
  }
8693
9364
  tables.inlineTokenizers[name2] = wrapped;
8694
9365
  };
8695
- const isSpace = (value2, at3) => /\s/.test(value2.charAt(at3));
9366
+ const isSpace2 = (value2, at3) => /\s/.test(value2.charAt(at3));
8696
9367
  precheck("link", (frame, base, value2) => {
8697
9368
  const open = base + (value2.charCodeAt(0) === 33 ? 1 : 0);
8698
9369
  if (frame.value.charCodeAt(open) !== 91) return false;
@@ -8700,7 +9371,7 @@ function speedUps(tables, features) {
8700
9371
  if (close2 === -1) return true;
8701
9372
  if (frame.value.charCodeAt(close2 + 1) !== 40) return true;
8702
9373
  let at3 = close2 + 2;
8703
- while (at3 < frame.value.length && isSpace(frame.value, at3)) at3++;
9374
+ while (at3 < frame.value.length && isSpace2(frame.value, at3)) at3++;
8704
9375
  if (frame.value.charCodeAt(at3) !== 60) return false;
8705
9376
  const gt = nextWhere(frame, ">", (v, i) => v.charCodeAt(i) === 62)[at3 + 1];
8706
9377
  const lf = nextWhere(frame, "\n", (v, i) => v.charCodeAt(i) === 10)[at3 + 1];
@@ -8725,11 +9396,33 @@ function speedUps(tables, features) {
8725
9396
  return nextWhere(frame, "]", (v, i) => v.charCodeAt(i) === 93)[base + 2] === -1;
8726
9397
  });
8727
9398
  }
9399
+ if (features.highlights) {
9400
+ precheck("highlight", (frame, base, value2) => {
9401
+ if (value2.charCodeAt(0) !== 61 || value2.charCodeAt(1) !== 61)
9402
+ return false;
9403
+ return nextWhere(
9404
+ frame,
9405
+ "highlight-closer",
9406
+ (v, i) => v.charCodeAt(i) === 61 && v.charCodeAt(i + 1) === 61 && !isSpace2(v, i - 1)
9407
+ )[base + 3] === -1;
9408
+ });
9409
+ }
9410
+ precheck("deletion", (frame, base, value2) => {
9411
+ if (value2.charCodeAt(0) !== 126 || value2.charCodeAt(1) !== 126)
9412
+ return false;
9413
+ if (value2.charCodeAt(2) === 126 && value2.charCodeAt(3) === 126)
9414
+ return false;
9415
+ return nextWhere(
9416
+ frame,
9417
+ "strike-closer",
9418
+ (v, i) => i >= 2 && v.charCodeAt(i) === 126 && v.charCodeAt(i - 1) === 126 && !isSpace2(v, i - 2)
9419
+ )[base + 4] === -1;
9420
+ });
8728
9421
  precheck("autoLink", (frame, base, value2) => {
8729
9422
  if (value2.charCodeAt(0) !== 60) return false;
8730
9423
  const stop = nextWhere(frame, "autolink-stop", (v, i) => {
8731
9424
  const c2 = v.charCodeAt(i);
8732
- return c2 === 62 || c2 === 64 || c2 === 58 && v.charCodeAt(i + 1) === 47 || isSpace(v, i);
9425
+ return c2 === 62 || c2 === 64 || c2 === 58 && v.charCodeAt(i + 1) === 47 || isSpace2(v, i);
8733
9426
  })[base + 1];
8734
9427
  if (stop === -1 || stop === base + 1) return true;
8735
9428
  const c = frame.value.charCodeAt(stop);
@@ -8738,10 +9431,33 @@ function speedUps(tables, features) {
8738
9431
  const end = nextWhere(
8739
9432
  frame,
8740
9433
  "space-or-gt",
8741
- (v, i) => v.charCodeAt(i) === 62 || isSpace(v, i)
9434
+ (v, i) => v.charCodeAt(i) === 62 || isSpace2(v, i)
8742
9435
  )[rest];
8743
9436
  return end === -1 || end === rest || frame.value.charCodeAt(end) !== 62;
8744
9437
  });
9438
+ const searchOnce = (name2, opens2) => {
9439
+ const stock = tables.inlineTokenizers[name2];
9440
+ const wrapped = function(eat, value2, silent) {
9441
+ const frame = frameOf(this, value2);
9442
+ if (!frame || !opens2(value2)) return stock.call(this, eat, value2, silent);
9443
+ if (frame.noCloser?.has(name2)) return void 0;
9444
+ const result = stock.call(this, eat, value2, silent);
9445
+ if (result === void 0) (frame.noCloser ??= /* @__PURE__ */ new Set()).add(name2);
9446
+ return result;
9447
+ };
9448
+ wrapped.locator = stock.locator;
9449
+ tables.inlineTokenizers[name2] = wrapped;
9450
+ };
9451
+ if (features.math) {
9452
+ searchOnce("math", (value2) => {
9453
+ const after = value2.charCodeAt(1);
9454
+ return value2.charCodeAt(0) === 36 && after !== 36 && after !== 32 && after !== 9;
9455
+ });
9456
+ searchOnce(
9457
+ "doubleDollarMath",
9458
+ (value2) => value2.charCodeAt(0) === 36 && value2.charCodeAt(1) === 36
9459
+ );
9460
+ }
8745
9461
  }
8746
9462
  function backtickRuns(value2) {
8747
9463
  const runs = /* @__PURE__ */ new Map();
@@ -8786,6 +9502,7 @@ function buildParser(features, fast) {
8786
9502
  if (features.tags) tags(tables);
8787
9503
  if (features.positionedBreaks) softBreaks(tables);
8788
9504
  if (fast) speedUps(tables, features);
9505
+ if (features.readsPastEnd) watchDefinitionReader(tables);
8789
9506
  return ObsidianParser;
8790
9507
  }
8791
9508
  var parserClasses = /* @__PURE__ */ new Map();
@@ -8805,10 +9522,31 @@ function parse8WithOffsets(doc, features, fast = true) {
8805
9522
  };
8806
9523
  const parser = new parserClass(doc, file);
8807
9524
  const tree = parser.parse();
8808
- return { tree, offsets: { ...parser.offset } };
9525
+ const offsets = { ...parser.offset };
9526
+ return features.readsPastEnd ? { tree, offsets, readsPastEnd: readsPastEnd(parser, tree) } : { tree, offsets };
8809
9527
  }
8810
9528
 
8811
9529
  // src/rp8/adapt.ts
9530
+ var legacyDefaults = {
9531
+ wikilinks: true,
9532
+ highlights: true,
9533
+ comments: true,
9534
+ tags: true,
9535
+ customTaskChars: true,
9536
+ math: true,
9537
+ frontmatter: true,
9538
+ keepComments: true,
9539
+ parseComments: true,
9540
+ lineOffsets: true,
9541
+ bareFootnoteRefs: true,
9542
+ inlineFootnotes: true,
9543
+ callouts: true,
9544
+ positionedBreaks: true,
9545
+ bomOffsets: false,
9546
+ readsPastEnd: false,
9547
+ keepUndefinedReferences: false,
9548
+ inertFootnoteRefs: false
9549
+ };
8812
9550
  var decodeLabel = (label) => typeof label === "string" ? decodeString(label) : label;
8813
9551
  var fold = (label) => label.replace(/[\t\n\r ]+/gu, " ").toLowerCase().toUpperCase().toLowerCase();
8814
9552
  function collectDefinitions(node2, links, footnotes2) {
@@ -8832,7 +9570,7 @@ function mergeText2(node2) {
8832
9570
  }
8833
9571
  node2.children = out;
8834
9572
  }
8835
- function pointAt(doc, offset) {
9573
+ function pointAt2(doc, offset) {
8836
9574
  const bol = doc.lastIndexOf("\n", offset - 1) + 1;
8837
9575
  let line = 1;
8838
9576
  for (let at3 = doc.indexOf("\n"); at3 !== -1 && at3 < offset; at3 = doc.indexOf("\n", at3 + 1)) {
@@ -8843,7 +9581,7 @@ function pointAt(doc, offset) {
8843
9581
  function pullBack(node2, doc) {
8844
9582
  const { start, end } = node2.position;
8845
9583
  if (end.column === 1 && end.line > start.line && end.offset > start.offset) {
8846
- node2.position.end = pointAt(doc, end.offset - 1);
9584
+ node2.position.end = pointAt2(doc, end.offset - 1);
8847
9585
  }
8848
9586
  }
8849
9587
  function at(node2) {
@@ -8857,7 +9595,7 @@ function text2(doc, from, to) {
8857
9595
  return {
8858
9596
  type: "text",
8859
9597
  value: doc.slice(from, to),
8860
- position: { start: pointAt(doc, from), end: pointAt(doc, to) }
9598
+ position: { start: pointAt2(doc, from), end: pointAt2(doc, to) }
8861
9599
  };
8862
9600
  }
8863
9601
  var UNESCAPE = /\\(#)/g;
@@ -8933,17 +9671,42 @@ var MATH_INLINE = (value2) => ({
8933
9671
  hProperties: { className: ["language-math", "math-inline"] },
8934
9672
  hChildren: [{ type: "text", value: value2 }]
8935
9673
  });
8936
- function convertAll(nodes, ctx, inNote) {
8937
- const out = [];
8938
- for (const node2 of nodes ?? []) out.push(...convert2(node2, ctx, inNote));
8939
- return out;
9674
+ function convertTree(root, ctx) {
9675
+ const { keepComments, parseComments } = ctx.options;
9676
+ const stack = [{ node: root, inNote: false, next: 0, kids: [] }];
9677
+ for (; ; ) {
9678
+ const top = stack[stack.length - 1];
9679
+ const { node: node2, inNote } = top;
9680
+ const child = node2.type !== "percentComment" || keepComments && parseComments ? node2.children?.[top.next++] : void 0;
9681
+ if (child) {
9682
+ const under = inNote || node2.type === "footnote";
9683
+ stack.push({ node: child, inNote: under, next: 0, kids: [] });
9684
+ continue;
9685
+ }
9686
+ stack.pop();
9687
+ const made = convert2(node2, ctx, inNote, top.kids);
9688
+ const parent = stack[stack.length - 1];
9689
+ if (!parent) return made[0];
9690
+ parent.kids.push(...made);
9691
+ }
8940
9692
  }
8941
- function convert2(node2, ctx, inNote) {
9693
+ function convert2(node2, ctx, inNote, converted) {
8942
9694
  const { doc } = ctx;
8943
9695
  const position = at(node2);
8944
- const kids = (n = inNote) => convertAll(node2.children, ctx, n);
9696
+ const kids = () => converted;
8945
9697
  switch (node2.type) {
8946
9698
  case "footnoteReference":
9699
+ if (inNote && ctx.options.inertFootnoteRefs) {
9700
+ return [
9701
+ {
9702
+ type: "footnoteReference",
9703
+ identifier: node2.identifier,
9704
+ label: node2.label,
9705
+ data: { inert: true },
9706
+ position
9707
+ }
9708
+ ];
9709
+ }
8947
9710
  if (inNote || !ctx.options.bareFootnoteRefs && !ctx.footnotes.has(String(node2.identifier)))
8948
9711
  return [text2(doc, position.start.offset, position.end.offset)];
8949
9712
  return [
@@ -8967,7 +9730,7 @@ function convert2(node2, ctx, inNote) {
8967
9730
  ];
8968
9731
  }
8969
9732
  case "footnote":
8970
- return [{ type: "footnote", children: kids(true), position }];
9733
+ return [{ type: "footnote", children: kids(), position }];
8971
9734
  case "highlight": {
8972
9735
  const strip = (n) => {
8973
9736
  if (n.type === "text")
@@ -9025,7 +9788,9 @@ function convert2(node2, ctx, inNote) {
9025
9788
  case "linkReference":
9026
9789
  case "imageReference": {
9027
9790
  const { type, label, referenceType, alt } = node2;
9028
- if (referenceType !== "full" && !ctx.definitions.has(fold(String(label)))) {
9791
+ const isDefined = ctx.definitions.has(fold(String(label)));
9792
+ const keep = ctx.options.keepUndefinedReferences;
9793
+ if (referenceType !== "full" && !isDefined && !keep) {
9029
9794
  const children = type === "linkReference" ? kids() : [];
9030
9795
  if (children.length === 0)
9031
9796
  return [text2(doc, position.start.offset, position.end.offset)];
@@ -9040,7 +9805,15 @@ function convert2(node2, ctx, inNote) {
9040
9805
  const identifier = normalizeIdentifier(
9041
9806
  String(label ?? node2.identifier)
9042
9807
  ).toLowerCase();
9043
- const spelled = referenceType === "full" && decodeLabel(label) !== label ? { data: { spelledLabel: label } } : {};
9808
+ const kept = keep && !isDefined;
9809
+ const written2 = (referenceType === "full" || kept) && decodeLabel(label) !== label ? { spelledLabel: label } : {};
9810
+ const spelled = kept || "spelledLabel" in written2 ? { data: { ...written2, ...kept ? { defined: false } : {} } } : {};
9811
+ const open = kept && referenceType === "shortcut" && doc[position.end.offset - 1] === "[";
9812
+ const own5 = open ? {
9813
+ start: position.start,
9814
+ end: pointAt2(doc, position.end.offset - 1)
9815
+ } : position;
9816
+ const tail = open ? [text2(doc, position.end.offset - 1, position.end.offset)] : [];
9044
9817
  return [
9045
9818
  type === "imageReference" ? {
9046
9819
  type,
@@ -9048,7 +9821,7 @@ function convert2(node2, ctx, inNote) {
9048
9821
  label: decodeLabel(label),
9049
9822
  referenceType,
9050
9823
  alt,
9051
- position,
9824
+ position: own5,
9052
9825
  ...spelled
9053
9826
  } : {
9054
9827
  type,
@@ -9056,9 +9829,10 @@ function convert2(node2, ctx, inNote) {
9056
9829
  label: decodeLabel(label),
9057
9830
  referenceType,
9058
9831
  children: kids(),
9059
- position,
9832
+ position: own5,
9060
9833
  ...spelled
9061
- }
9834
+ },
9835
+ ...tail
9062
9836
  ];
9063
9837
  }
9064
9838
  case "definition": {
@@ -9084,9 +9858,18 @@ function convert2(node2, ctx, inNote) {
9084
9858
  return [{ type: "blockquote", children, position }];
9085
9859
  const marker = /^\[!([^\]\n]+)\]([-+]?)/u.exec(String(head.marker));
9086
9860
  const titleBlocks = children[0].children ?? [];
9087
- const collect = (nodes) => nodes.map(
9088
- (n) => "value" in n ? String(n.value) : collect(n.children ?? [])
9089
- ).join("");
9861
+ const collect = (nodes) => nodes.map((n) => {
9862
+ if ("value" in n) return String(n.value);
9863
+ if (n.data?.defined === false && n.referenceType !== "full") {
9864
+ const { start, end } = n.position;
9865
+ const kids2 = n.children ?? [];
9866
+ if (kids2.length === 0) return doc.slice(start.offset, end.offset);
9867
+ const first = kids2[0].position.start.offset;
9868
+ const last = kids2[kids2.length - 1].position.end.offset;
9869
+ return doc.slice(start.offset, first) + collect(kids2) + doc.slice(last, end.offset);
9870
+ }
9871
+ return collect(n.children ?? []);
9872
+ }).join("");
9090
9873
  let title = collect(titleBlocks).trim();
9091
9874
  const body = children.slice(1);
9092
9875
  const titleNode = [];
@@ -9143,7 +9926,7 @@ function convert2(node2, ctx, inNote) {
9143
9926
  if (lead > 0 && lead < String(head.value).length) {
9144
9927
  head.value = String(head.value).slice(lead);
9145
9928
  head.position = {
9146
- start: pointAt(doc, head.position.start.offset + lead),
9929
+ start: pointAt2(doc, head.position.start.offset + lead),
9147
9930
  end: head.position.end
9148
9931
  };
9149
9932
  }
@@ -9167,7 +9950,7 @@ function clampToParent(node2) {
9167
9950
  clampToParent(child);
9168
9951
  }
9169
9952
  }
9170
- function restoreCrlf(node2, crlf) {
9953
+ function restoreCrlf(node2, crlf, seen = /* @__PURE__ */ new Set()) {
9171
9954
  const shift2 = (offset) => {
9172
9955
  let low = 0;
9173
9956
  let high = crlf.length;
@@ -9179,10 +9962,13 @@ function restoreCrlf(node2, crlf) {
9179
9962
  return offset + low;
9180
9963
  };
9181
9964
  if (node2.position) {
9182
- node2.position.start.offset = shift2(node2.position.start.offset);
9183
- node2.position.end.offset = shift2(node2.position.end.offset);
9965
+ for (const point of [node2.position.start, node2.position.end]) {
9966
+ if (seen.has(point)) continue;
9967
+ seen.add(point);
9968
+ point.offset = shift2(point.offset);
9969
+ }
9184
9970
  }
9185
- for (const child of node2.children ?? []) restoreCrlf(child, crlf);
9971
+ for (const child of node2.children ?? []) restoreCrlf(child, crlf, seen);
9186
9972
  }
9187
9973
  function parseLegacy8(input, options) {
9188
9974
  const source = input.startsWith("\uFEFF") ? input.slice(1) : input;
@@ -9191,26 +9977,28 @@ function parseLegacy8(input, options) {
9191
9977
  crlf.push(at3 - crlf.length);
9192
9978
  }
9193
9979
  const doc = source.replace(/\r\n?/g, "\n");
9194
- const { tree, offsets } = parse8WithOffsets(doc, options);
9980
+ const { tree, offsets, readsPastEnd: readsPastEnd2 } = parse8WithOffsets(doc, options);
9195
9981
  const definitions = /* @__PURE__ */ new Set();
9196
9982
  const footnotes2 = /* @__PURE__ */ new Set();
9197
9983
  collectDefinitions(tree, definitions, footnotes2);
9198
- const root = convert2(
9199
- tree,
9200
- { doc, definitions, footnotes: footnotes2, options },
9201
- false
9202
- )[0];
9984
+ const root = convertTree(tree, { doc, definitions, footnotes: footnotes2, options });
9203
9985
  mergeText2(root);
9204
9986
  clampToParent(root);
9205
9987
  if (options.lineOffsets) root.data = { lineOffsets: offsets };
9988
+ if (readsPastEnd2 !== void 0) {
9989
+ root.data = { ...root.data, readsPastEnd: readsPastEnd2 };
9990
+ }
9206
9991
  if (options.math) mathDisplayTransform(root, doc);
9207
9992
  if (crlf.length > 0) restoreCrlf(root, crlf);
9993
+ if (options.callouts) calloutMarkerTransform(root, source);
9994
+ if (source !== input && options.bomOffsets)
9995
+ countByteOrderMark(root);
9208
9996
  return root;
9209
9997
  }
9210
9998
 
9211
9999
  // src/lib/comment-hoist.ts
9212
10000
  var spansLines = (node2) => (node2.position?.end?.line ?? 0) > (node2.position?.start?.line ?? 0);
9213
- var MARKER2 = "%%";
10001
+ var MARKER3 = "%%";
9214
10002
  var CONTAINERS = /* @__PURE__ */ new Set(["list", "blockquote", "callout"]);
9215
10003
  function commentHoistTransform(tree, source) {
9216
10004
  visit(tree, "paragraph", (node2, index, parent) => {
@@ -9274,67 +10062,19 @@ function liftContainerComment(tree, source) {
9274
10062
  const comment2 = host.children[at3];
9275
10063
  const from = comment2.position?.start?.offset;
9276
10064
  if (from === void 0) continue;
9277
- if (source.slice(from + MARKER2.length).includes(MARKER2)) continue;
10065
+ if (source.slice(from + MARKER3.length).includes(MARKER3)) continue;
9278
10066
  host.children.splice(at3);
9279
10067
  for (const ancestor of [...path].reverse()) pullBack2(ancestor);
9280
10068
  tree.children.splice(index + 1, 0, comment2);
9281
- comment2.value = source.slice(from + MARKER2.length);
10069
+ comment2.value = source.slice(from + MARKER3.length);
9282
10070
  if (comment2.position) comment2.position.end = endPoint(source);
9283
10071
  delete comment2.children;
9284
10072
  tree.children.splice(index + 2);
9285
10073
  }
9286
10074
  }
9287
10075
 
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
10076
  // src/lib/comment-definition-extent.ts
9337
- var MARKER3 = "%%";
10077
+ var MARKER4 = "%%";
9338
10078
  var BLANK = /^[ \t]*(?:>[ \t]*)*$/u;
9339
10079
  var isComment = (node2) => node2?.type === "comment";
9340
10080
  function commentDefinitionExtentTransform(tree, source, reparse) {
@@ -9355,7 +10095,7 @@ function commentDefinitionExtentTransform(tree, source, reparse) {
9355
10095
  if (from === void 0 || ownEnd === void 0 || end === void 0 || startLine === void 0) {
9356
10096
  continue;
9357
10097
  }
9358
- if (source.slice(from, ownEnd).slice(MARKER3.length).includes(MARKER3)) {
10098
+ if (source.slice(from, ownEnd).slice(MARKER4.length).includes(MARKER4)) {
9359
10099
  continue;
9360
10100
  }
9361
10101
  const lines = source.slice(from, end).split("\n");
@@ -9365,7 +10105,7 @@ function commentDefinitionExtentTransform(tree, source, reparse) {
9365
10105
  if (blank === -1) continue;
9366
10106
  const kept = lines.slice(0, blank).join("\n");
9367
10107
  const cut = from + kept.length;
9368
- comment2.value = source.slice(from + MARKER3.length, cut);
10108
+ comment2.value = source.slice(from + MARKER4.length, cut);
9369
10109
  if (comment2.position) {
9370
10110
  comment2.position.end = { ...comment2.position.end, offset: cut };
9371
10111
  }
@@ -9392,83 +10132,15 @@ function resolveEscapes(line) {
9392
10132
  let at3 = 0;
9393
10133
  while (at3 < line.length) {
9394
10134
  const next = line[at3 + 1];
9395
- if (line[at3] === "\\" && next !== void 0 && PUNCTUATION.has(next)) {
9396
- out += next;
9397
- at3 += 2;
9398
- continue;
9399
- }
9400
- out += line[at3];
9401
- at3 += 1;
9402
- }
9403
- return out;
9404
- }
9405
-
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;
10135
+ if (line[at3] === "\\" && next !== void 0 && PUNCTUATION.has(next)) {
10136
+ out += next;
10137
+ at3 += 2;
10138
+ continue;
9470
10139
  }
9471
- });
10140
+ out += line[at3];
10141
+ at3 += 1;
10142
+ }
10143
+ return out;
9472
10144
  }
9473
10145
 
9474
10146
  // src/lib/comment-in-definition.ts
@@ -9639,8 +10311,8 @@ function definitionLazySplitTransform(tree, source, reparse) {
9639
10311
  const tail = reparse(source.slice(firstBreak + 1, to)).children;
9640
10312
  if (tail.length === 0) continue;
9641
10313
  if (previous2.type === "list" && tail[0]?.type !== "comment") continue;
9642
- shiftOnto(head, from);
9643
- for (const node2 of tail) shiftOnto(node2, firstBreak + 1);
10314
+ shiftOnto2(head, from);
10315
+ for (const node2 of tail) shiftOnto2(node2, firstBreak + 1);
9644
10316
  children.splice(index, 1, head, ...tail);
9645
10317
  const last = tail[tail.length - 1];
9646
10318
  const opener2 = last?.position?.start?.offset;
@@ -9651,18 +10323,18 @@ function definitionLazySplitTransform(tree, source, reparse) {
9651
10323
  }
9652
10324
  }
9653
10325
  }
9654
- function shiftOnto(node2, offset) {
10326
+ function shiftOnto2(node2, offset) {
9655
10327
  const candidate = node2;
9656
10328
  if (candidate.position) {
9657
10329
  for (const point of [candidate.position.start, candidate.position.end]) {
9658
10330
  point.offset = offset + (point.offset ?? 0);
9659
10331
  }
9660
10332
  }
9661
- for (const child of candidate.children ?? []) shiftOnto(child, offset);
10333
+ for (const child of candidate.children ?? []) shiftOnto2(child, offset);
9662
10334
  }
9663
10335
 
9664
10336
  // src/lib/quote-comment-lazy.ts
9665
- var MARKER4 = "%%";
10337
+ var MARKER5 = "%%";
9666
10338
  var isComment3 = (node2) => node2?.type === "comment";
9667
10339
  var ENDS_CONTAINER = /^ {0,3}(?:#{1,6}(?:[ \t]|$)|```|~~~|[-*+][ \t]|\d{1,9}[.)][ \t])/u;
9668
10340
  var SETEXT_UNDERLINE = /^ {0,3}(?:=+|-+)[ \t]*$/u;
@@ -9684,7 +10356,7 @@ function quoteCommentLazyTransform(tree, source) {
9684
10356
  const from = comment2.position?.start?.offset;
9685
10357
  if (from === void 0) continue;
9686
10358
  const quoteEnd = container.position?.end?.offset ?? source.length;
9687
- if (source.slice(from + MARKER4.length, quoteEnd).includes(MARKER4)) continue;
10359
+ if (source.slice(from + MARKER5.length, quoteEnd).includes(MARKER5)) continue;
9688
10360
  const reach = containerBound(source, from);
9689
10361
  const swallowed = [];
9690
10362
  for (let at3 = chain.length - 1; at3 >= 1; at3--) {
@@ -9706,12 +10378,12 @@ function quoteCommentLazyTransform(tree, source) {
9706
10378
  if (swallowed.length === 0) continue;
9707
10379
  const body = mapLines(
9708
10380
  source,
9709
- from + MARKER4.length,
10381
+ from + MARKER5.length,
9710
10382
  reach,
9711
10383
  source.slice(0, from).split("\n").length,
9712
10384
  depth
9713
10385
  ).text;
9714
- const close2 = body.indexOf(MARKER4);
10386
+ const close2 = body.indexOf(MARKER5);
9715
10387
  comment2.value = close2 === -1 ? body : body.slice(0, close2);
9716
10388
  if (comment2.position) comment2.position.end = pointOf(source, reach);
9717
10389
  comment2.children = swallowed;
@@ -9723,13 +10395,27 @@ function quoteCommentLazyTransform(tree, source) {
9723
10395
  }
9724
10396
  }
9725
10397
  }
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) {
10398
+ function commentPath(root, rootChain) {
10399
+ const pending = [];
10400
+ const enter = (parent, chain) => {
10401
+ const tail = parent.children[parent.children.length - 1];
10402
+ if (isComment3(tail)) return { comment: tail, chain };
10403
+ pending.push({ parent, chain, next: 0 });
10404
+ return void 0;
10405
+ };
10406
+ const first = enter(root, rootChain);
10407
+ if (first) return first;
10408
+ while (pending.length > 0) {
10409
+ const top = pending[pending.length - 1];
10410
+ const child = top.parent.children[top.next];
10411
+ if (child === void 0) {
10412
+ pending.pop();
10413
+ continue;
10414
+ }
10415
+ top.next += 1;
9730
10416
  if (child.type !== "blockquote") continue;
9731
- const found = commentPath(child, [
9732
- ...chain,
10417
+ const found = enter(child, [
10418
+ ...top.chain,
9733
10419
  child
9734
10420
  ]);
9735
10421
  if (found) return found;
@@ -9771,21 +10457,29 @@ function pointOf(source, offset) {
9771
10457
  }
9772
10458
 
9773
10459
  // src/lib/item-comment-lazy.ts
9774
- var MARKER5 = "%%";
10460
+ var MARKER6 = "%%";
9775
10461
  var isComment4 = (node2) => node2?.type === "comment";
9776
10462
  var ENDS_ITEM = /^ {0,3}(?:#{1,6}(?:[ \t]|$)|```|~~~|[-*+][ \t]|\d{1,9}[.)][ \t])/u;
9777
10463
  function itemCommentLazyTransform(tree, source) {
9778
10464
  if (source === "") return;
9779
10465
  absorb(tree, source, []);
9780
10466
  }
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]);
10467
+ function absorb(root, source, rootAncestors) {
10468
+ const pending = [
10469
+ [root, rootAncestors]
10470
+ ];
10471
+ while (pending.length > 0) {
10472
+ const [node2, ancestors] = pending.pop();
10473
+ const children = node2.children;
10474
+ if (!Array.isArray(children)) continue;
10475
+ if (node2.type === "listItem") {
10476
+ swallow(node2, source, ancestors);
10477
+ }
10478
+ const below = [...ancestors, node2];
10479
+ const snapshot = [...children];
10480
+ for (let at3 = snapshot.length - 1; at3 >= 0; at3--) {
10481
+ pending.push([snapshot[at3], below]);
10482
+ }
9789
10483
  }
9790
10484
  }
9791
10485
  function swallow(item, source, ancestors) {
@@ -9794,7 +10488,7 @@ function swallow(item, source, ancestors) {
9794
10488
  const comment2 = item.children[at3];
9795
10489
  const from = comment2.position?.start?.offset;
9796
10490
  if (from === void 0) return;
9797
- if (source.slice(from + MARKER5.length).includes(MARKER5)) return;
10491
+ if (source.slice(from + MARKER6.length).includes(MARKER6)) return;
9798
10492
  const reach = itemBound(source, from);
9799
10493
  const chain = [...ancestors, item];
9800
10494
  const swallowed = [];
@@ -9811,7 +10505,7 @@ function swallow(item, source, ancestors) {
9811
10505
  }
9812
10506
  }
9813
10507
  if (swallowed.length === 0) return;
9814
- comment2.value = source.slice(from + MARKER5.length, reach);
10508
+ comment2.value = source.slice(from + MARKER6.length, reach);
9815
10509
  if (comment2.position) comment2.position.end = pointOf2(source, reach);
9816
10510
  comment2.children = swallowed;
9817
10511
  for (const ancestor of [item, ...ancestors].reverse()) {
@@ -10482,15 +11176,29 @@ function definitionNestedLabelTransform(tree, source) {
10482
11176
  unnest(target.parent, target.index, source);
10483
11177
  }
10484
11178
  }
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()) {
11179
+ function find(root, inRootDefinition, lines) {
11180
+ if (!Array.isArray(root.children)) return void 0;
11181
+ const pending = [{ node: root, inDefinition: inRootDefinition, next: 0 }];
11182
+ while (pending.length > 0) {
11183
+ const top = pending[pending.length - 1];
11184
+ const { node: node2, inDefinition } = top;
11185
+ const index = top.next;
11186
+ const child = node2.children?.[index];
11187
+ if (child === void 0) {
11188
+ pending.pop();
11189
+ continue;
11190
+ }
11191
+ top.next = index + 1;
10489
11192
  const isDefinition = child.type === "footnoteDefinition";
10490
11193
  if (isDefinition && inDefinition && continues(child, lines))
10491
11194
  return { parent: node2, index };
10492
- const found = find(child, inDefinition || isDefinition, lines);
10493
- if (found) return found;
11195
+ if (Array.isArray(child.children)) {
11196
+ pending.push({
11197
+ node: child,
11198
+ inDefinition: inDefinition || isDefinition,
11199
+ next: 0
11200
+ });
11201
+ }
10494
11202
  }
10495
11203
  return void 0;
10496
11204
  }
@@ -10536,9 +11244,15 @@ function labelText(definition2, source, start) {
10536
11244
  };
10537
11245
  }
10538
11246
  function firstContentOffset(node2) {
10539
- const child = node2.children?.[0];
10540
- if (!child) return void 0;
10541
- return firstContentOffset(child) ?? child.position?.start.offset;
11247
+ const chain = [];
11248
+ for (let child = node2.children?.[0]; child; child = child.children?.[0]) {
11249
+ chain.push(child);
11250
+ }
11251
+ for (let at3 = chain.length - 1; at3 >= 0; at3--) {
11252
+ const offset = chain[at3].position?.start.offset;
11253
+ if (offset !== void 0) return offset;
11254
+ }
11255
+ return void 0;
10542
11256
  }
10543
11257
  function offsetPoint(definition2, offset) {
10544
11258
  const start = definition2.position.start;
@@ -10596,7 +11310,7 @@ function definitionMathAbsorbTransform(tree, source) {
10596
11310
 
10597
11311
  // src/lib/definition-end-before-label.ts
10598
11312
  var LABEL3 = /^\[\^?[^\]]*\]:/u;
10599
- var QUOTE_PREFIX2 = /^[ \t]*(?:> ?)+/u;
11313
+ var QUOTE_PREFIX3 = /^[ \t]*(?:> ?)+/u;
10600
11314
  function quoteDepth(line) {
10601
11315
  let rest = line.replace(/^[ \t]+/u, "");
10602
11316
  let depth = 0;
@@ -10619,7 +11333,7 @@ function definitionEndBeforeLabelTransform(tree, source) {
10619
11333
  const ownDepth = quoteDepth(lines[start.line - 1] ?? "");
10620
11334
  for (let line = start.line; line < end.line; line++) {
10621
11335
  const text3 = lines[line] ?? "";
10622
- const inner = text3.replace(QUOTE_PREFIX2, "");
11336
+ const inner = text3.replace(QUOTE_PREFIX3, "");
10623
11337
  if (inner !== text3) {
10624
11338
  const deeper = ownDepth > 0 && quoteDepth(text3) > ownDepth;
10625
11339
  const start2 = CANNOT_INTERRUPT.exec(inner);
@@ -10871,6 +11585,16 @@ function inlineFootnoteInertTransform(tree) {
10871
11585
  });
10872
11586
  });
10873
11587
  }
11588
+ function inlineFootnoteInertKeepTransform(tree, source) {
11589
+ visit(tree, "footnote", (note) => {
11590
+ bareFootnoteRefTransform(note, source);
11591
+ footnoteAfterLabelTransform(note, source);
11592
+ visit(note, "footnoteReference", (node2) => {
11593
+ const reference2 = node2;
11594
+ reference2.data = { ...reference2.data, inert: true };
11595
+ });
11596
+ });
11597
+ }
10874
11598
 
10875
11599
  // src/lib/syntax/wikilink.ts
10876
11600
  var EXCLAMATION = 33;
@@ -12222,6 +12946,25 @@ function node(value2) {
12222
12946
  return Boolean(value2 && typeof value2 === "object");
12223
12947
  }
12224
12948
 
12949
+ // src/lib/walk.ts
12950
+ var childrenOf = (node2) => node2.children ?? [];
12951
+ function eachBelow(root, visit2) {
12952
+ const stack = [[root, 0]];
12953
+ while (stack.length > 0) {
12954
+ const top = stack[stack.length - 1];
12955
+ const [parent, index] = top;
12956
+ const children = childrenOf(parent);
12957
+ if (index >= children.length) {
12958
+ stack.pop();
12959
+ continue;
12960
+ }
12961
+ top[1] = index + 1;
12962
+ const node2 = children[index];
12963
+ visit2(node2, parent, index);
12964
+ stack.push([node2, 0]);
12965
+ }
12966
+ }
12967
+
12225
12968
  // src/lib/mdast/empty-delete-to-markdown.ts
12226
12969
  var EMPTY_DELETE = "obsidianEmptyDelete";
12227
12970
  function isEmptyDelete(node2) {
@@ -12229,15 +12972,15 @@ function isEmptyDelete(node2) {
12229
12972
  }
12230
12973
  function markEmptyDeletes(tree) {
12231
12974
  const marked = [];
12232
- const walk = (node2) => {
12975
+ const mark = (node2) => {
12233
12976
  const children = node2.children;
12234
12977
  if (node2.type === "delete" && children?.length === 0) {
12235
12978
  node2.type = EMPTY_DELETE;
12236
12979
  marked.push(node2);
12237
12980
  }
12238
- children?.forEach(walk);
12239
12981
  };
12240
- walk(tree);
12982
+ mark(tree);
12983
+ eachBelow(tree, mark);
12241
12984
  return () => {
12242
12985
  for (const node2 of marked) node2.type = "delete";
12243
12986
  };
@@ -12287,673 +13030,331 @@ function breakToMarkdown(escaped2 = false) {
12287
13030
  });
12288
13031
  subexit();
12289
13032
  exit();
12290
- const lastLine = Math.max(value2.lastIndexOf("\r"), value2.lastIndexOf("\n")) + 1;
12291
- return `${value2}
12292
- ${(node2.depth === 1 ? "=" : "-").repeat(
12293
- Math.max(value2.length - lastLine, 3)
12294
- )}`;
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;
13033
+ const lastLine = Math.max(value2.lastIndexOf("\r"), value2.lastIndexOf("\n")) + 1;
13034
+ return `${value2}
13035
+ ${(node2.depth === 1 ? "=" : "-").repeat(
13036
+ Math.max(value2.length - lastLine, 3)
13037
+ )}`;
13038
+ },
13039
+ break(node2, parent, _state) {
13040
+ const opens2 = (parent?.type === "emphasis" || parent?.type === "strong") && parent.children[0] === node2;
13041
+ const ending = escaped2 || opens2 ? "\\\n" : "\n";
13042
+ const siblings = parent?.children ?? [];
13043
+ const next = siblings[siblings.indexOf(node2) + 1];
13044
+ if (isEmptyDelete(next)) {
13045
+ return `${ending}&#x20;`;
13046
+ }
13047
+ return ending;
12501
13048
  }
12502
13049
  }
12503
13050
  };
12504
13051
  }
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--;
12523
- }
12524
- }
12525
- return units.join("");
12526
- }
12527
13052
 
12528
- // src/lib/mdast/footnote-reference-to-markdown.ts
12529
- function footnoteReferenceToMarkdown() {
13053
+ // src/lib/mdast/highlight-to-markdown.ts
13054
+ function highlightToMarkdown() {
12530
13055
  return {
13056
+ // A `==` that is text must stay text, as for `%%`: mid-line, `x \\==mark==` came back from a
13057
+ // format as a highlight. At a line start the writer already escaped it, against setext.
13058
+ unsafe: [{ character: "=", after: "=[^\\s]", inConstruct: "phrasing" }],
12531
13059
  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;
13060
+ highlight(node2, _parent, state, info) {
13061
+ const exit = state.enter("highlight");
13062
+ const content = state.containerPhrasing(node2, {
13063
+ ...info,
13064
+ before: "==",
13065
+ after: "=="
13066
+ });
13067
+ exit();
13068
+ return `==${guardClosers(content, info.before === "\n" || info.before === "")}==`;
12538
13069
  }
12539
13070
  }
12540
13071
  };
12541
13072
  }
12542
- function footnoteDefinitionToMarkdown() {
13073
+ function guardClosers(content, lineStart) {
13074
+ const units = content.match(/\\[\s\S]|&#?[\da-z]+;|[\s\S]/giu) ?? [];
13075
+ const equals = (unit) => unit === "=" || unit === "\\=";
13076
+ return units.map(
13077
+ (unit, index) => equals(unit) && (index === 0 && lineStart || index === units.length - 1 || equals(units[index + 1]) || unit !== "=") ? "&#x3D;" : unit
13078
+ ).join("");
13079
+ }
13080
+
13081
+ // src/lib/mdast/callout-to-markdown.ts
13082
+ var prefix = (line, _index, blank) => ">" + (blank ? "" : " ") + line;
13083
+ var BELOW_TITLE_LINE = /* @__PURE__ */ new Set(["code", "comment", "paragraph"]);
13084
+ function calloutToMarkdown() {
12543
13085
  return {
12544
13086
  handlers: {
12545
- footnoteDefinition(node2, _parent, state, info) {
13087
+ callout(node2, _parent, state, info) {
13088
+ const exit = state.enter("blockquote");
12546
13089
  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
- );
12559
- }
12560
- exit();
12561
- 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
13090
+ tracker.move("> ");
13091
+ tracker.shift(2);
13092
+ const titleNode = node2.children.find(
13093
+ (child) => child.type === "calloutTitle"
12576
13094
  );
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;
13095
+ const body = node2.children.filter(
13096
+ (child) => child.type !== "calloutTitle"
13097
+ );
13098
+ const fold2 = node2.collapsed === true ? "-" : node2.collapsed === false ? "+" : "";
13099
+ let title = "";
13100
+ if (titleNode) {
13101
+ const exitPhrasing = state.enter("phrasing");
13102
+ title = state.containerPhrasing(titleNode, {
13103
+ ...info,
13104
+ ...tracker.current()
13105
+ });
13106
+ exitPhrasing();
13107
+ }
13108
+ const empty2 = titleNode !== void 0 && title === "";
13109
+ const onTitleLine = empty2 && body.length > 0 && !BELOW_TITLE_LINE.has(body[0]?.type ?? "");
13110
+ const head = `[!${node2.calloutType}]${fold2}${title ? ` ${title}` : onTitleLine ? " " : empty2 ? " &#x20;" : ""}`;
13111
+ const rest = body.length > 0 ? state.containerFlow(
13112
+ { type: "root", children: body },
13113
+ tracker.current()
13114
+ ) : "";
13115
+ const value2 = state.indentLines(
13116
+ rest === "" ? head : onTitleLine ? `${head}${rest}` : `${head}
13117
+ ${rest}`,
13118
+ prefix
13119
+ );
13120
+ exit();
13121
+ return value2;
13122
+ },
13123
+ calloutTitle(node2, _parent, state, info) {
13124
+ const exit = state.enter("phrasing");
13125
+ const value2 = state.containerPhrasing(node2, info);
13126
+ exit();
13127
+ return value2;
13128
+ }
12726
13129
  }
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);
13130
+ };
13131
+ }
13132
+
13133
+ // src/lib/mdast/comment-to-markdown.ts
13134
+ var FLOW = /* @__PURE__ */ new Set([
13135
+ "root",
13136
+ "listItem",
13137
+ "blockquote",
13138
+ "callout",
13139
+ "footnoteDefinition",
13140
+ "comment"
13141
+ ]);
13142
+ function commentToMarkdown() {
13143
+ return {
13144
+ // A `%%` that is text must stay text: written bare, `\\%%note%%` came back from a format as a
13145
+ // comment, hiding the words (found by `fuzz/invariant-sweep.ts`).
13146
+ unsafe: [{ character: "%", after: "%", inConstruct: "phrasing" }],
13147
+ handlers: {
13148
+ comment(node2, parent) {
13149
+ const value2 = node2.value.split("\n").map(
13150
+ (line, index, lines) => index > 0 && index < lines.length - 1 && /^[ \t]+$/u.test(line) ? "" : line
13151
+ ).join("\n");
13152
+ if (value2 !== node2.value) node2 = { ...node2, value: value2 };
13153
+ if (parent && FLOW.has(parent.type) && !node2.value.includes("\n")) {
13154
+ return `%%${node2.value}
13155
+ %%`;
13156
+ }
13157
+ if (parent && FLOW.has(parent.type) && node2.value.endsWith("%")) {
13158
+ return `%%${node2.value}
13159
+ %%`;
12733
13160
  }
13161
+ return `%%${node2.value}%%`;
12734
13162
  }
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;
13163
+ }
13164
+ };
13165
+ }
13166
+
13167
+ // src/lib/footnote-identifier.ts
13168
+ function footnoteIdentifierTransform(tree) {
13169
+ visit(tree, (node2) => {
13170
+ if (node2.type !== "footnoteReference" && node2.type !== "footnoteDefinition") {
12761
13171
  return;
12762
13172
  }
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);
13173
+ if (typeof node2.label === "string")
13174
+ node2.identifier = node2.label.toLowerCase();
13175
+ });
13176
+ }
13177
+
13178
+ // src/lib/mdast/tag-to-markdown.ts
13179
+ var TAG_END = "\u2E7F";
13180
+ var TAG_END_PATTERN = /(?:\u2E7F|&#x2E7F;)(?=[*_~])/gu;
13181
+ function removeTagEnds(written2) {
13182
+ return written2.replace(TAG_END_PATTERN, (match, at3) => {
13183
+ let index = at3 - 1;
13184
+ while (index >= 0 && isTagCharacter(written2[index])) index--;
13185
+ return index >= 0 && index < at3 - 1 && written2[index] === "#" ? "" : match;
13186
+ });
13187
+ }
13188
+ function opensWithStrike(node2) {
13189
+ let at3 = node2;
13190
+ while (at3?.type === "emphasis" || at3?.type === "strong")
13191
+ at3 = at3.children?.[0];
13192
+ return at3?.type === "delete";
13193
+ }
13194
+ function tagToMarkdown(options = {}) {
13195
+ return {
13196
+ // A `#` that is text must stay text: written bare, `x \\#tag` came back from a format as a
13197
+ // tag (found by `fuzz/invariant-sweep.ts`). Only where a tag could start, before a non-space:
13198
+ // after whitespace, at a line start, or after ASCII punctuation -- which the writer may itself
13199
+ // escape or encode, and an escape or a character reference counts as a construct a tag may
13200
+ // follow, so `](#tag` written `]\\(#tag` and `&amp;#a` written `\\&#a` both grew a tag. A `#`
13201
+ // after a letter or digit -- `page#section`, `C#` -- is left alone.
13202
+ unsafe: [
13203
+ {
13204
+ character: "#",
13205
+ before: "[\\s!-/:-@\\[-`{-~]",
13206
+ after: "[^\\s]",
13207
+ inConstruct: "phrasing"
13208
+ },
13209
+ {
13210
+ character: "#",
13211
+ atBreak: true,
13212
+ after: "[^\\s]",
13213
+ inConstruct: "phrasing"
13214
+ }
13215
+ ],
13216
+ // `#` after an escape or a character reference is `textToMarkdown`'s, which the rule above
13217
+ // cannot reach.
13218
+ handlers: {
13219
+ tag(node2, parent) {
13220
+ const siblings = parent?.children ?? [];
13221
+ const next = siblings[siblings.indexOf(node2) + 1];
13222
+ const attention = next?.type === "emphasis" || next?.type === "strong" || next?.type === "delete";
13223
+ return `#${node2.value}${attention && (options.legacy || opensWithStrike(next)) ? TAG_END : ""}`;
12787
13224
  }
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
13225
  }
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
- }
13226
+ };
13227
+ }
13228
+
13229
+ // src/lib/mdast/inline-footnote-to-markdown.ts
13230
+ function inlineFootnoteToMarkdown() {
13231
+ return {
13232
+ // A `]` in an inline footnote's text closes it, so it is escaped there: written bare,
13233
+ // `^[a\\]b]` came back as `^[a]` and `b]` (found by `fuzz/invariant-sweep.ts`).
13234
+ unsafe: [{ character: "]", inConstruct: "inlineFootnote" }],
13235
+ handlers: {
13236
+ footnote(node2, _parent, state, info) {
13237
+ const tracker = state.createTracker(info);
13238
+ const exit = state.enter("inlineFootnote");
13239
+ let value2 = tracker.move("^[");
13240
+ value2 += balanceBrackets(
13241
+ state.containerPhrasing(node2, {
13242
+ ...tracker.current(),
13243
+ before: value2,
13244
+ after: "]"
13245
+ })
13246
+ );
13247
+ value2 += tracker.move("]");
13248
+ exit();
13249
+ return value2;
12856
13250
  }
12857
- return body.length > 0 ? { title: title2.filter(nonEmpty), body } : void 0;
12858
13251
  }
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);
12885
- }
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);
13252
+ };
13253
+ }
13254
+ function balanceBrackets(content) {
13255
+ if (content.includes("`")) return content;
13256
+ const units = content.match(/\\[\s\S]|[\s\S]/gu) ?? [];
13257
+ let open = 0;
13258
+ const escapedOpeners = [];
13259
+ for (let index = 0; index < units.length; index++) {
13260
+ if (units[index] === "[") open++;
13261
+ else if (units[index] === "\\[") escapedOpeners.push(index);
13262
+ else if (units[index] === "]") {
13263
+ if (open > 0) open--;
13264
+ else if (escapedOpeners.length > 0) units[escapedOpeners.shift()] = "[";
13265
+ else units[index] = "\\]";
13266
+ }
13267
+ }
13268
+ for (let index = units.length - 1; index >= 0 && open > 0; index--) {
13269
+ if (units[index] === "\\]") {
13270
+ units[index] = "]";
13271
+ open--;
13272
+ }
13273
+ }
13274
+ return units.join("");
13275
+ }
13276
+
13277
+ // src/lib/mdast/footnote-reference-to-markdown.ts
13278
+ function footnoteReferenceToMarkdown() {
13279
+ return {
13280
+ handlers: {
13281
+ footnoteReference(node2, _parent, state, info) {
13282
+ const tracker = state.createTracker(info);
13283
+ let value2 = tracker.move("[^");
13284
+ value2 += tracker.move(node2.label ?? node2.identifier);
13285
+ value2 += tracker.move("]");
13286
+ return value2;
12893
13287
  }
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
13288
+ }
13289
+ };
13290
+ }
13291
+ function footnoteDefinitionToMarkdown() {
13292
+ return {
13293
+ handlers: {
13294
+ footnoteDefinition(node2, _parent, state, info) {
13295
+ const tracker = state.createTracker(info);
13296
+ let value2 = tracker.move("[^");
13297
+ const exit = state.enter("footnoteDefinition");
13298
+ value2 += tracker.move(node2.label ?? node2.identifier);
13299
+ value2 += tracker.move("]:");
13300
+ if (node2.children.length > 0) {
13301
+ tracker.shift(4);
13302
+ value2 += tracker.move(
13303
+ " " + state.indentLines(
13304
+ state.containerFlow(node2, tracker.current()),
13305
+ (line, index, blank) => index === 0 ? line : (blank ? "" : " ") + line
13306
+ )
12907
13307
  );
12908
13308
  }
13309
+ exit();
13310
+ return value2;
12909
13311
  }
12910
13312
  }
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
- });
13313
+ };
12943
13314
  }
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();
13315
+
13316
+ // src/lib/task-char.ts
13317
+ function customTaskCharTransform(tree, source) {
13318
+ visit(tree, "listItem", (node2) => {
13319
+ if (typeof node2.checked === "boolean") {
13320
+ let char = node2.checked ? "x" : " ";
13321
+ if (source && node2.position?.start?.offset != null) {
13322
+ const slice = source.slice(
13323
+ node2.position.start.offset,
13324
+ node2.position.start.offset + 20
13325
+ );
13326
+ const m = slice.match(/\[([^\]])\]/);
13327
+ if (m) {
13328
+ char = m[1];
13329
+ }
13330
+ }
13331
+ node2.data ??= {};
13332
+ node2.data.taskChar = char;
13333
+ node2.data.hProperties ??= {};
13334
+ node2.data.hProperties.dataTaskChar = char;
13335
+ return;
13336
+ }
13337
+ const firstChild = node2.children?.[0];
13338
+ if (!firstChild || firstChild.type !== "paragraph") return;
13339
+ const firstText = firstChild.children?.[0];
13340
+ if (!firstText || firstText.type !== "text") return;
13341
+ const match = firstText.value.match(/^\[([^\]\u2028\u2029])\]\s/);
13342
+ if (!match) return;
13343
+ const taskChar = match[1];
13344
+ node2.checked = taskChar !== " ";
13345
+ node2.data ??= {};
13346
+ node2.data.taskChar = taskChar;
13347
+ node2.data.hProperties ??= {};
13348
+ node2.data.hProperties.dataTaskChar = taskChar;
13349
+ firstText.value = firstText.value.slice(match[0].length);
13350
+ if (firstText.value.length === 0) {
13351
+ firstChild.children.shift();
13352
+ } else if (firstText.position && typeof firstText.position.start.offset === "number") {
13353
+ firstText.position.start.column += match[0].length;
13354
+ firstText.position.start.offset += match[0].length;
13355
+ if (firstChild.position) {
13356
+ firstChild.position.start = { ...firstText.position.start };
13357
+ }
12957
13358
  }
12958
13359
  });
12959
13360
  }
@@ -13055,11 +13456,18 @@ function detect(tree, source, lines, parse3, options) {
13055
13456
  const calloutTitles2 = /* @__PURE__ */ new Set();
13056
13457
  const listStarts = /* @__PURE__ */ new Map();
13057
13458
  const definitions = [];
13058
- const walk = (node2, inDefinition, item, hostLine, parent, container) => {
13459
+ const walk = (node2, inDefinition, item, hostLine, parent, container, index = -1) => {
13059
13460
  if (options.comments && !inDefinition && (container === "blockquote" || container === "listItem")) {
13060
13461
  const split = node2.type === "paragraph" ? lazyCommentOpener(node2, lines) : node2.type === "comment" && parent && parent.children[0] !== node2 ? lazyCommentNode(node2, parent, lines) : void 0;
13061
13462
  if (split) alone.push(split);
13062
13463
  }
13464
+ 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
13465
+ // (`[^1]:` / `····>[!~]`) is no callout, and splitting its title changed its paragraph.
13466
+ atMarkerColumn(lineOf(source, node2.position.start.offset ?? 0))) {
13467
+ calloutTitles2.add(node2);
13468
+ const split = calloutBodySplit(node2, lines);
13469
+ if (split) alone.push(split);
13470
+ }
13063
13471
  if (node2.type === "paragraph" && !inDefinition && node2.position) {
13064
13472
  if (options.callouts && isCalloutTitle(node2, parent)) {
13065
13473
  calloutTitles2.add(node2);
@@ -13077,18 +13485,24 @@ function detect(tree, source, lines, parse3, options) {
13077
13485
  if (unbox) alone.push(unbox);
13078
13486
  }
13079
13487
  if (!inDefinition && (container === "blockquote" || container === "listItem") && (node2.type === "math" || options.comments && node2.type === "comment")) {
13080
- if (!closesMidline(node2, parent)) {
13488
+ if (!closesMidline(node2, parent, index)) {
13081
13489
  late.push(...keptLazyLines(node2, source, lines, parse3));
13082
13490
  }
13083
13491
  if (node2.type === "math") edits.push(...containedMathOpener(node2, lines));
13084
13492
  }
13085
13493
  if (node2.type === "footnoteReference" && parent) {
13086
- const caret2 = linkedReference(node2, parent, source);
13494
+ const caret2 = linkedReference(node2, parent, index, source);
13087
13495
  if (caret2 !== void 0)
13088
13496
  edits.push({ kind: "neutralize", offset: caret2 });
13089
13497
  }
13090
13498
  if (node2.type === "footnoteDefinition" && parent) {
13091
- const colon5 = labelRowUnderTable(node2, parent, source, lines);
13499
+ const colon5 = labelRowUnderTable(
13500
+ node2,
13501
+ parent,
13502
+ index,
13503
+ source,
13504
+ lines
13505
+ );
13092
13506
  if (colon5 !== void 0)
13093
13507
  edits.push({ kind: "neutralize", offset: colon5 });
13094
13508
  }
@@ -13112,18 +13526,20 @@ function detect(tree, source, lines, parse3, options) {
13112
13526
  if (options.comments && node2.type === "footnoteDefinition") {
13113
13527
  settled.push(...lazyCommentUnderDefinition(node2, lines));
13114
13528
  }
13115
- for (const child of children) {
13116
- walk(
13117
- child,
13529
+ for (let at3 = children.length - 1; at3 >= 0; at3--) {
13530
+ pending.push([
13531
+ children[at3],
13118
13532
  inDefinition || node2.type === "footnoteDefinition",
13119
13533
  node2.type === "listItem" ? node2 : item,
13120
13534
  node2.type === "root" ? 0 : node2.position?.start.line ?? 0,
13121
13535
  node2,
13122
- CONTAINERS2.has(node2.type) ? node2.type : container
13123
- );
13536
+ CONTAINERS2.has(node2.type) ? node2.type : container,
13537
+ at3
13538
+ ]);
13124
13539
  }
13125
13540
  };
13126
- walk(tree, false, void 0, 0);
13541
+ const pending = [[tree, false, void 0, 0]];
13542
+ while (pending.length > 0) walk(...pending.pop());
13127
13543
  for (const { node: definition2, item, hostLine } of definitions) {
13128
13544
  const position = definition2.position;
13129
13545
  if (!position || position.start.offset === void 0) continue;
@@ -13261,9 +13677,8 @@ function keptLazyLines(node2, source, lines, parse3) {
13261
13677
  }
13262
13678
  return edits;
13263
13679
  }
13264
- function closesMidline(node2, parent) {
13680
+ function closesMidline(node2, parent, index) {
13265
13681
  if (node2.type !== "math" || !parent) return false;
13266
- const index = parent.children.indexOf(node2);
13267
13682
  const before2 = parent.children[index - 1];
13268
13683
  if (before2?.type !== "paragraph") return false;
13269
13684
  if (before2.position?.end.line !== (node2.position?.start.line ?? 0) - 1) {
@@ -13318,8 +13733,7 @@ function lazyCommentUnderDefinition(definition2, lines) {
13318
13733
  const lazy = own5.split(">").length - 1 < lead.split(">").length - 1 || columns(own5) < columns(lead);
13319
13734
  return lazy ? [] : [{ kind: "neutralize", offset }];
13320
13735
  }
13321
- function linkedReference(reference2, parent, source) {
13322
- const index = parent.children.indexOf(reference2);
13736
+ function linkedReference(reference2, parent, index, source) {
13323
13737
  const next = parent.children[index + 1];
13324
13738
  const start = reference2.position?.start.offset;
13325
13739
  const end = reference2.position?.end.offset;
@@ -13329,8 +13743,7 @@ function linkedReference(reference2, parent, source) {
13329
13743
  }
13330
13744
  return source.slice(start, start + 2) === "[^" ? start + 1 : void 0;
13331
13745
  }
13332
- function labelRowUnderTable(definition2, parent, source, lines) {
13333
- const index = parent.children.indexOf(definition2);
13746
+ function labelRowUnderTable(definition2, parent, index, source, lines) {
13334
13747
  const table2 = parent.children[index - 1];
13335
13748
  const at3 = table2?.position?.start.offset;
13336
13749
  const line = definition2.position?.start.line;
@@ -13344,12 +13757,17 @@ function labelRowUnderTable(definition2, parent, source, lines) {
13344
13757
  const lineEnd2 = (lines.starts[line - 1] ?? 0) + (lines.text[line - 1] ?? "").length;
13345
13758
  return source.slice(colon5, lineEnd2).includes("|") ? colon5 : void 0;
13346
13759
  }
13760
+ function lineOf(source, offset) {
13761
+ const start = source.lastIndexOf("\n", offset - 1) + 1;
13762
+ const end = source.indexOf("\n", offset);
13763
+ return source.slice(start, end === -1 ? void 0 : end);
13764
+ }
13347
13765
  function isCalloutTitle(paragraph2, parent) {
13348
13766
  if (parent?.type !== "blockquote" || parent.children[0] !== paragraph2) {
13349
13767
  return false;
13350
13768
  }
13351
13769
  const lead = paragraph2.children[0];
13352
- return lead?.type === "text" && MARKER6.test(lead.value);
13770
+ return lead?.type === "text" && MARKER.test(lead.value);
13353
13771
  }
13354
13772
  function titleLeavesLabel(paragraph2, labelLine, lines, parse3) {
13355
13773
  const position = paragraph2.position;
@@ -13786,14 +14204,15 @@ var BLOCK_PARENTS = /* @__PURE__ */ new Set([
13786
14204
  function commentParseTransform(tree, source, reparse) {
13787
14205
  if (source === "") return;
13788
14206
  const depthOf = /* @__PURE__ */ new Map();
13789
- const walk = (node2, depth) => {
14207
+ const pending = [[tree, 0]];
14208
+ while (pending.length > 0) {
14209
+ const [node2, depth] = pending.pop();
13790
14210
  const inner = node2.type === "blockquote" || node2.type === "callout" ? depth + 1 : depth;
13791
14211
  for (const child of node2.children ?? []) {
13792
14212
  if (child.type === "comment") depthOf.set(child, inner);
13793
- walk(child, inner);
14213
+ pending.push([child, inner]);
13794
14214
  }
13795
- };
13796
- walk(tree, 0);
14215
+ }
13797
14216
  visit(tree, "comment", (node2, _index, parent) => {
13798
14217
  const comment2 = node2;
13799
14218
  const start = node2.position?.start?.offset;
@@ -13979,7 +14398,7 @@ function phrasingBefore(paragraph2, at3, source) {
13979
14398
  value: value2,
13980
14399
  position: {
13981
14400
  start: { ...child.position.start },
13982
- end: pointAt2(source, at3)
14401
+ end: pointAt(source, at3)
13983
14402
  }
13984
14403
  });
13985
14404
  }
@@ -14085,15 +14504,15 @@ function restoreInlinePositionsTransform(tree, source) {
14085
14504
  const span2 = spans[index];
14086
14505
  if (span2 === void 0) return;
14087
14506
  child.position = {
14088
- start: pointAt2(source, span2[0]),
14089
- end: pointAt2(source, span2[1])
14507
+ start: pointAt(source, span2[0]),
14508
+ end: pointAt(source, span2[1])
14090
14509
  };
14091
14510
  if (child.type === "link" && child.children.length === 1) {
14092
14511
  const only = child.children[0];
14093
14512
  if (only !== void 0 && only.position === void 0) {
14094
14513
  only.position = {
14095
- start: pointAt2(source, span2[0]),
14096
- end: pointAt2(source, span2[1])
14514
+ start: pointAt(source, span2[0]),
14515
+ end: pointAt(source, span2[1])
14097
14516
  };
14098
14517
  }
14099
14518
  }
@@ -14104,11 +14523,10 @@ function restoreInlinePositionsTransform(tree, source) {
14104
14523
  // src/lib/mdast/attention-to-markdown.ts
14105
14524
  import { defaultHandlers as defaultHandlers2 } from "mdast-util-to-markdown";
14106
14525
  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
- }
14526
+ function recordParents(root) {
14527
+ eachBelow(root, (node2, parent) => {
14528
+ parentOf.set(node2, parent);
14529
+ });
14112
14530
  }
14113
14531
  function nextOf(node2) {
14114
14532
  const siblings = parentOf.get(node2)?.children;
@@ -14199,14 +14617,12 @@ function holdsStar(node2) {
14199
14617
  function attentionToMarkdown(options) {
14200
14618
  const pinned = /* @__PURE__ */ new WeakSet();
14201
14619
  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);
14620
+ const isAttention2 = (node2) => node2?.type === "emphasis" || node2?.type === "strong";
14621
+ const markAdjacent = (root2) => {
14622
+ eachBelow(root2, (node2, parent, index) => {
14623
+ if (isAttention2(node2) && isAttention2(childrenOf(parent)[index + 1])) {
14624
+ beforeAttention.add(node2);
14625
+ }
14210
14626
  });
14211
14627
  };
14212
14628
  const wrap = (base) => {
@@ -14226,7 +14642,7 @@ function attentionToMarkdown(options) {
14226
14642
  const only2 = node2.children.length === 1 ? node2.children[0] : void 0;
14227
14643
  return {
14228
14644
  ...result,
14229
- markers: node2.type === "emphasis" && isAttention(only2) ? ["_", "*"] : ["*"]
14645
+ markers: node2.type === "emphasis" && isAttention2(only2) ? ["_", "*"] : ["*"]
14230
14646
  };
14231
14647
  }
14232
14648
  if (!options.legacy) {
@@ -14240,11 +14656,11 @@ function attentionToMarkdown(options) {
14240
14656
  if (node2.type === "emphasis" && only?.type === "emphasis" && only.children.length === 1 && only.children[0]?.type === "strong") {
14241
14657
  return { ...result, markers: ["*"] };
14242
14658
  }
14243
- if (node2.type === "strong" && only?.type === "emphasis" && !(only.children.length === 1 && isAttention(only.children[0]))) {
14659
+ if (node2.type === "strong" && only?.type === "emphasis" && !(only.children.length === 1 && isAttention2(only.children[0]))) {
14244
14660
  pinned.add(only);
14245
14661
  return { ...result, markers: ["*"] };
14246
14662
  }
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
14663
+ 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
14664
  // `*` would on a `*` (`*em` / `![[_under_]]` / `_under_. *`).
14249
14665
  holdsStar(node2) && !underscoreUnsafe(node2, false) || beforeAttention.has(node2) && !opensWithAttention(nextOf(node2))) {
14250
14666
  return { ...result, markers: ["_", "*"] };
@@ -14314,6 +14730,44 @@ function isGfmLiteralEmail(value2) {
14314
14730
  }
14315
14731
  return true;
14316
14732
  }
14733
+ var BARE_URL = /^https?:\/\/[a-z\d-]+(?:\.[a-z\d-]+)+(?:\/[\w\-./?#+:@=]*)?$/iu;
14734
+ var isAttention = (node2) => node2?.type === "emphasis" || node2?.type === "strong" || node2?.type === "delete";
14735
+ var siblingsOf = (node2) => parentOf.get(node2)?.children ?? [];
14736
+ function opensAttentionBare(link3, text3) {
14737
+ if (link3.url !== text3 || !BARE_URL.test(text3) || text3.includes("==")) {
14738
+ return false;
14739
+ }
14740
+ const end = text3.at(-1);
14741
+ if (!/[a-z\d/]/iu.test(end)) return false;
14742
+ let node2 = link3;
14743
+ let opens2 = false;
14744
+ for (; ; ) {
14745
+ const parent = parentOf.get(node2);
14746
+ if (!isAttention(parent) || siblingsOf(node2)[0] !== node2) break;
14747
+ opens2 = true;
14748
+ node2 = parent;
14749
+ }
14750
+ if (!opens2) return false;
14751
+ node2 = link3;
14752
+ for (; ; ) {
14753
+ const siblings = siblingsOf(node2);
14754
+ const next = siblings[siblings.indexOf(node2) + 1];
14755
+ if (next) {
14756
+ if (next.type === "break") return true;
14757
+ if (next.type !== "text") return false;
14758
+ const rest = next.value.replace(/^[.,:;!?]+/u, "");
14759
+ if (rest !== "") return /^[ \t\n]/u.test(rest);
14760
+ node2 = next;
14761
+ continue;
14762
+ }
14763
+ const parent = parentOf.get(node2);
14764
+ if (isAttention(parent)) {
14765
+ node2 = parent;
14766
+ continue;
14767
+ }
14768
+ return parent?.type === "paragraph" || parent?.type === "heading" || parent?.type === "tableCell";
14769
+ }
14770
+ }
14317
14771
  function soleText(node2) {
14318
14772
  if (node2.children.length !== 1) return void 0;
14319
14773
  const only = node2.children[0];
@@ -14327,6 +14781,9 @@ function autolinkLiteralToMarkdown(options = {}) {
14327
14781
  if (text3 !== void 0 && !node2.title && node2.url === MAILTO + text3 && !isCommonMarkEmail(text3) && isGfmLiteralEmail(text3)) {
14328
14782
  return text3;
14329
14783
  }
14784
+ if (options.strict && text3 !== void 0 && !node2.title && opensAttentionBare(node2, text3)) {
14785
+ return text3;
14786
+ }
14330
14787
  const written2 = defaultHandlers3.link(
14331
14788
  node2,
14332
14789
  parent,
@@ -14385,10 +14842,22 @@ function withSpelledLabel(node2, write) {
14385
14842
  const written2 = write({ ...node2, label: LABEL4 });
14386
14843
  return written2.includes(LABEL4) ? written2.replace(LABEL4, () => spelled) : write(node2);
14387
14844
  }
14845
+ function keptReference(node2, parent, state) {
14846
+ const data = node2.data;
14847
+ if (data?.defined !== false || node2.referenceType === "full")
14848
+ return void 0;
14849
+ const label = data.spelledLabel ?? node2.label;
14850
+ if (label === void 0 || label === null) return void 0;
14851
+ const next = parent?.type === "paragraph" ? parent.children[1] : void 0;
14852
+ 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));
14853
+ return (node2.type === "imageReference" ? "![" : "[") + (callout ? `\\${label}` : label) + "]" + (node2.referenceType === "collapsed" ? "[]" : "");
14854
+ }
14388
14855
  function linkReferenceToMarkdown(options = {}) {
14389
14856
  return {
14390
14857
  handlers: {
14391
14858
  linkReference(node2, parent, state, info) {
14859
+ const kept = keptReference(node2, parent, state);
14860
+ if (kept !== void 0) return kept;
14392
14861
  if (node2.referenceType === "shortcut" && BLANK3.test(node2.label ?? node2.identifier)) {
14393
14862
  const exit = state.enter("linkReference");
14394
14863
  const label = node2.label ?? node2.identifier;
@@ -14402,7 +14871,7 @@ function linkReferenceToMarkdown(options = {}) {
14402
14871
  return balanceLabel(node2, state, written2, options.strict);
14403
14872
  },
14404
14873
  imageReference(node2, parent, state, info) {
14405
- return withSpelledLabel(
14874
+ return keptReference(node2, parent, state) ?? withSpelledLabel(
14406
14875
  node2,
14407
14876
  (own5) => defaultHandlers4.imageReference(own5, parent, state, info)
14408
14877
  );
@@ -14748,11 +15217,6 @@ function taskCharToMarkdown(options = {}) {
14748
15217
  const indent2 = state.options.listItemIndent;
14749
15218
  if (wide) state.options.listItemIndent = "tab";
14750
15219
  try {
14751
- return writeItem();
14752
- } finally {
14753
- state.options.listItemIndent = indent2;
14754
- }
14755
- function writeItem() {
14756
15220
  const head = node2.children[0];
14757
15221
  const isTask = taskChar !== null;
14758
15222
  if (!isTask) {
@@ -14774,6 +15238,8 @@ function taskCharToMarkdown(options = {}) {
14774
15238
  (match) => match + checkbox
14775
15239
  );
14776
15240
  return value2;
15241
+ } finally {
15242
+ state.options.listItemIndent = indent2;
14777
15243
  }
14778
15244
  }
14779
15245
  }
@@ -14796,8 +15262,8 @@ function textToMarkdown(tags2, highlights2 = false, legacy = false) {
14796
15262
  defaultHandlers6.text(node2, parent, state, info),
14797
15263
  tags2
14798
15264
  ).replace(/(?<!\\)((?:\\\\)*)\\(\s+)$/u, "$1\\\\$2");
14799
- const siblingsOf = parent?.children ?? [];
14800
- const afterBreak = siblingsOf[siblingsOf.indexOf(node2) - 1]?.type === "break";
15265
+ const siblingsOf2 = parent?.children ?? [];
15266
+ const afterBreak = siblingsOf2[siblingsOf2.indexOf(node2) - 1]?.type === "break";
14801
15267
  written2 = written2.replace(/\u2E7F/gu, "&#11903;");
14802
15268
  written2 = guardAmpersands(
14803
15269
  legacy ? guardBlankLines(written2, afterBreak, info.after) : written2
@@ -14815,7 +15281,7 @@ function textToMarkdown(tags2, highlights2 = false, legacy = false) {
14815
15281
  written2 = `\\${written2}`;
14816
15282
  }
14817
15283
  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 ?? ""))) {
15284
+ 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
15285
  written2 = `&#x3A;${written2.slice(1)}`;
14820
15286
  }
14821
15287
  if (previous2?.type === "tag" && /^[_/-]/u.test(written2)) {
@@ -15248,7 +15714,10 @@ function hasUpstreamMathText(context) {
15248
15714
  const list3 = Array.isArray(registered) ? registered : [registered];
15249
15715
  return list3.some((construct) => construct?.name === "mathText");
15250
15716
  }
15717
+ var noCloserFrom2 = /* @__PURE__ */ new WeakMap();
15251
15718
  function tokenizeMathText(effects, ok3, nok) {
15719
+ const self = this;
15720
+ let opener2 = 0;
15252
15721
  let sizeOpen = 0;
15253
15722
  let sizeClose = 0;
15254
15723
  let token;
@@ -15256,6 +15725,7 @@ function tokenizeMathText(effects, ok3, nok) {
15256
15725
  return start;
15257
15726
  function start(code) {
15258
15727
  if (code !== DOLLAR2) return nok(code);
15728
+ opener2 = self.now().offset;
15259
15729
  effects.enter("mathText");
15260
15730
  effects.enter("mathTextSequence");
15261
15731
  return sequenceOpen(code);
@@ -15269,11 +15739,22 @@ function tokenizeMathText(effects, ok3, nok) {
15269
15739
  if (sizeOpen === 1 && (isDigit(code) || code === null || code === codes.space || code === codes.horizontalTab)) {
15270
15740
  return nok(code);
15271
15741
  }
15742
+ if (sizeOpen === 1) {
15743
+ const known = noCloserFrom2.get(self);
15744
+ if (known !== void 0 && opener2 > known) return nok(code);
15745
+ }
15272
15746
  effects.exit("mathTextSequence");
15273
15747
  return between(code);
15274
15748
  }
15275
15749
  function between(code) {
15276
- if (code === null) return nok(code);
15750
+ if (code === null) {
15751
+ if (sizeOpen === 1) {
15752
+ const known = noCloserFrom2.get(self);
15753
+ if (known === void 0 || opener2 < known)
15754
+ noCloserFrom2.set(self, opener2);
15755
+ }
15756
+ return nok(code);
15757
+ }
15277
15758
  if (code === DOLLAR2) {
15278
15759
  if (sizeOpen === 1 && afterSpace) {
15279
15760
  afterSpace = false;
@@ -15952,21 +16433,10 @@ var PRESETS = {
15952
16433
  strict: STRICT_OVERRIDES
15953
16434
  };
15954
16435
  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
16436
+ ...legacyDefaults,
16437
+ readsPastEnd: false,
16438
+ escapedLineBreaks: false,
16439
+ bomOffsets: false
15970
16440
  };
15971
16441
  function displayAwareMathToMarkdown() {
15972
16442
  const single = mathToMarkdown();
@@ -16070,6 +16540,7 @@ function undefinedReferencesTransform(tree, source) {
16070
16540
  visit(tree, "footnoteReference", (node2, index, parent) => {
16071
16541
  if (parent === void 0 || index === void 0) return void 0;
16072
16542
  if (defined2.has(node2.identifier)) return void 0;
16543
+ if (node2.data?.inert) return void 0;
16073
16544
  const from = node2.position?.start?.offset;
16074
16545
  const to = node2.position?.end?.offset;
16075
16546
  const value2 = from !== void 0 && to !== void 0 ? decodeString(source.slice(from, to)) : `[^${node2.label ?? node2.identifier}]`;
@@ -16276,7 +16747,9 @@ function remarkObsidian(userOpts) {
16276
16747
  definitionMathAbsorbTransform(tree, String(file));
16277
16748
  definitionEndBeforeLabelTransform(tree, String(file));
16278
16749
  tagAgainstTextTransform(tree, String(file));
16279
- if (opts.inlineFootnotes) inlineFootnoteInertTransform(tree);
16750
+ if (opts.inlineFootnotes && !opts.inertFootnoteRefs) {
16751
+ inlineFootnoteInertTransform(tree);
16752
+ }
16280
16753
  if (opts.lineOffsets) ensureLineOffsets(tree);
16281
16754
  if (opts.keepComments && opts.parseComments) {
16282
16755
  commentParseTransform(tree, String(file), reparse);
@@ -16297,7 +16770,12 @@ function remarkObsidian(userOpts) {
16297
16770
  );
16298
16771
  return SKIP;
16299
16772
  });
16300
- if (opts.inlineFootnotes) inlineFootnoteInertTransform(tree);
16773
+ if (opts.inlineFootnotes && !opts.inertFootnoteRefs) {
16774
+ inlineFootnoteInertTransform(tree);
16775
+ }
16776
+ }
16777
+ if (opts.inlineFootnotes && opts.inertFootnoteRefs) {
16778
+ inlineFootnoteInertKeepTransform(tree, String(file));
16301
16779
  }
16302
16780
  const literals = gfmLiteralTransforms(data.fromMarkdownExtensions);
16303
16781
  if (literals.length > 0) {
@@ -16314,6 +16792,7 @@ function remarkObsidian(userOpts) {
16314
16792
  footnoteCodeBlankLinesTransform(tree);
16315
16793
  adjacentIndentedCodeTransform(tree, String(file));
16316
16794
  footnoteIdentifierTransform(tree);
16795
+ if (opts.math) mathDisplayTransform(tree, String(file));
16317
16796
  if (!opts.bareFootnoteRefs)
16318
16797
  undefinedReferencesTransform(tree, String(file));
16319
16798
  if (opts.callouts) syncCalloutTitlesTransform(tree);
@@ -16331,7 +16810,14 @@ function remarkObsidian(userOpts) {
16331
16810
  }
16332
16811
  return fragment;
16333
16812
  };
16334
- return transform;
16813
+ return (tree, file) => {
16814
+ const source = String(file);
16815
+ const marked = source.charCodeAt(0) === 65279;
16816
+ const text3 = marked ? source.slice(1) : source;
16817
+ transform(tree, marked ? text3 : file);
16818
+ if (opts.callouts) calloutMarkerTransform(tree, text3);
16819
+ if (marked && opts.bomOffsets) countByteOrderMark(tree);
16820
+ };
16335
16821
  }
16336
16822
  export {
16337
16823
  COMMENT_ELEMENT,