@liminis/editor 0.5.0 → 0.7.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.
@@ -5,7 +5,7 @@ import { $isCodeNode } from '@lexical/code';
5
5
  import { $isLinkNode } from '@lexical/link';
6
6
  import { $isMarkNode } from '@lexical/mark';
7
7
  import { $isTableNode, $isTableRowNode, $isTableCellNode } from '@lexical/table';
8
- import { $isHorizontalRuleNode, $isImageNode, $isCalloutNode, $isToggleContainerNode, $isToggleTitleNode, $isToggleContentNode, $isEquationNode, $isMermaidNode, $isC4Node, $isFrontmatterNode, $isFootnoteNode, $isCustomListNode, $isDefinitionListNode, $isDefinitionTermNode, $isDefinitionDescriptionNode, $isCustomListItemNode, $isHtmlNode, $isListItemParagraphBreakNode, $isTransclusionNode, } from '../editor/nodes/index.js';
8
+ import { $isHorizontalRuleNode, $isImageNode, $isCalloutNode, $isToggleContainerNode, $isToggleTitleNode, $isToggleContentNode, $isEquationNode, $isMermaidNode, $isC4Node, $isFrontmatterNode, $isFootnoteNode, $isBlockAnchorNode, $isCustomListNode, $isDefinitionListNode, $isDefinitionTermNode, $isDefinitionDescriptionNode, $isCustomListItemNode, $isHtmlNode, $isListItemParagraphBreakNode, $isTransclusionNode, } from '../editor/nodes/index.js';
9
9
  import { SENTINEL_CLOSE_END, SENTINEL_CLOSE_START, SENTINEL_OPEN_END, SENTINEL_OPEN_START, stripAnnotateSentinels, } from '../../markdown/annotate-sentinels.js';
10
10
  // Convert Lexical editor state to mdast tree
11
11
  export function exportLexicalToMdast(editor, options = {}) {
@@ -245,6 +245,7 @@ function isHoistableConstruct(node) {
245
245
  $isImageNode(node) ||
246
246
  $isEquationNode(node) ||
247
247
  $isFootnoteNode(node) ||
248
+ $isBlockAnchorNode(node) ||
248
249
  $isHtmlNode(node) ||
249
250
  $isTransclusionNode(node));
250
251
  }
@@ -377,26 +378,32 @@ function canCarryHoistedTokens(parent) {
377
378
  * The same question asked of the *construct* rather than its container, because
378
379
  * one container routes its children unevenly.
379
380
  *
380
- * `convertListItemNode` sends only text runs, line breaks and links through the
381
- * inline phrasing path; every other child goes to the block dispatcher, which
382
- * has nowhere to put a phrasing token. Hoisting a boundary onto one of those
383
- * would drop the token silently, and `locateLiveMarkdownRange` would then fail
384
- * to find the mark at all.
381
+ * `convertListItemNode` sends only text runs, line breaks, links and block
382
+ * anchors through the inline phrasing path; every other child goes to the
383
+ * block dispatcher, which has nowhere to put a phrasing token. Hoisting a
384
+ * boundary onto one of those would drop the token silently, and
385
+ * `locateLiveMarkdownRange` would then fail to find the mark at all.
385
386
  *
386
- * No document reaches that state today, which is why this is a guard rather
387
- * than a fix: an image, inline equation, footnote reference or inline HTML
388
- * inside a list item is *itself* block-promoted by that same dispatcher, so it
389
- * does not survive a round trip inline (`- The value $x^2$ matters` exports as
390
- * three blocks) and any range over it is already rejected by
391
- * `locateLiveMarkdownRange`'s slice check. Repairing that unrelated,
392
- * pre-existing round-trip defect must not silently regress annotation ranges as
393
- * its side effect.
387
+ * No document reaches that state today for the remaining types, which is why
388
+ * this is a guard rather than a fix: an image, inline equation, footnote
389
+ * reference or inline HTML inside a list item is *itself* block-promoted by
390
+ * that same dispatcher, so it does not survive a round trip inline (`- The
391
+ * value $x^2$ matters` exports as three blocks) and any range over it is
392
+ * already rejected by `locateLiveMarkdownRange`'s slice check. Repairing that
393
+ * unrelated, pre-existing round-trip defect must not silently regress
394
+ * annotation ranges as its side effect.
395
+ *
396
+ * Block anchors are the one exception (#122): `- [ ] ... ^ULID` is the
397
+ * primary real-world shape this issue targets, so `convertListItemNode` keeps
398
+ * a `BlockAnchorNode` inline rather than letting it fall to the block
399
+ * dispatcher — see the `$isBlockAnchorNode` branch there — and a hoisted
400
+ * boundary onto one genuinely does reach the output.
394
401
  */
395
402
  function hoistedTokenReachesOutput(node) {
396
403
  let parent = node.getParent();
397
404
  while (parent && $isMarkNode(parent))
398
405
  parent = parent.getParent();
399
- return $isListItemNode(parent) ? $isLinkNode(node) : true;
406
+ return $isListItemNode(parent) ? ($isLinkNode(node) || $isBlockAnchorNode(node)) : true;
400
407
  }
401
408
  /**
402
409
  * The node a mark's boundary token should be emitted outside of, or null to
@@ -716,6 +723,9 @@ function convertFootnoteInlineChildren(labelNode) {
716
723
  label: child.getFootnoteId(),
717
724
  });
718
725
  }
726
+ else if ($isBlockAnchorNode(child)) {
727
+ contentChildren.push({ type: 'blockAnchor', id: child.getId() });
728
+ }
719
729
  contentChildren.push(...hoistedTokenNodesFor(child, 'after'));
720
730
  restIndex++;
721
731
  child = rest[restIndex] ?? null;
@@ -967,6 +977,30 @@ function convertListItemNode(node, _ordered, spread) {
967
977
  // be emitted here too.
968
978
  inlineChildren.push(...hoistedTokenNodesFor(child, 'before'), convertLinkNode(child), ...hoistedTokenNodesFor(child, 'after'));
969
979
  }
980
+ else if ($isBlockAnchorNode(child)) {
981
+ // A block anchor sitting directly under a ListItemNode — the primary
982
+ // real-world shape this issue targets (`- [ ] ... ^ULID`, #122) — must
983
+ // stay inline rather than fall to the block dispatcher below: unlike
984
+ // image/equation/footnote/html (a documented, pre-existing gap this
985
+ // issue does not touch — see `hoistedTokenReachesOutput`'s docstring),
986
+ // a dropped anchor here would break the checkbox-action-item pattern
987
+ // the spec calls out as the primary usage.
988
+ //
989
+ // Unlike the $isLinkNode branch above, a link's format lives on its
990
+ // text children (applied on import via applyFormatToLinkChildren), but
991
+ // a BlockAnchorNode carries its own format bits directly (like
992
+ // FootnoteNode/EquationNode) — so they must be wrapped here explicitly,
993
+ // the same way the general inline path does via getMergeableFormat +
994
+ // buildFormattedContent, or a bold/italic badge would silently lose its
995
+ // markers on export.
996
+ flushTextRun();
997
+ const anchorMdast = { type: 'blockAnchor', id: child.getId() };
998
+ const format = getMergeableFormat(child) ?? 0;
999
+ const anchorContent = format
1000
+ ? wrapWithFormat([anchorMdast], format, child.getStrongMarker(), child.getEmphasisMarker())
1001
+ : anchorMdast;
1002
+ inlineChildren.push(...hoistedTokenNodesFor(child, 'before'), anchorContent, ...hoistedTokenNodesFor(child, 'after'));
1003
+ }
970
1004
  else {
971
1005
  // Any other block-type child (ParagraphNode, CodeNode, TableNode,
972
1006
  // QuoteNode, etc.) — flush accumulated inline text first, then delegate
@@ -1304,7 +1338,7 @@ function getMergeableFormat(child) {
1304
1338
  if ($isTextNode(child)) {
1305
1339
  return child.getFormat() & MERGEABLE_FORMAT_MASK;
1306
1340
  }
1307
- if ($isEquationNode(child) || $isFootnoteNode(child)) {
1341
+ if ($isEquationNode(child) || $isFootnoteNode(child) || $isBlockAnchorNode(child)) {
1308
1342
  return child.getFormat() & MERGEABLE_FORMAT_MASK;
1309
1343
  }
1310
1344
  return null;
@@ -1348,6 +1382,10 @@ function convertSingleInlineChild(child) {
1348
1382
  label: child.getFootnoteId(),
1349
1383
  }];
1350
1384
  }
1385
+ else if ($isBlockAnchorNode(child)) {
1386
+ // Block anchor badge (#122): convert back to a blockAnchor mdast node
1387
+ return [{ type: 'blockAnchor', id: child.getId() }];
1388
+ }
1351
1389
  else if ($isHtmlNode(child)) {
1352
1390
  // Inline HTML preserved opaquely: convert back to a phrasing html mdast node
1353
1391
  return [{ type: 'html', value: child.getHtml() }];
@@ -1431,6 +1469,10 @@ function convertLeavesRaw(nodes) {
1431
1469
  label: node.getFootnoteId(),
1432
1470
  });
1433
1471
  }
1472
+ else if ($isBlockAnchorNode(node)) {
1473
+ flushCode();
1474
+ content.push({ type: 'blockAnchor', id: node.getId() });
1475
+ }
1434
1476
  }
1435
1477
  flushCode();
1436
1478
  return content;
@@ -1457,7 +1499,7 @@ function resolveMarkers(nodes) {
1457
1499
  strongMarker = strongMarker ?? getMarkdownMarker(style, '--md-strong-marker');
1458
1500
  emphasisMarker = emphasisMarker ?? getMarkdownMarker(style, '--md-emphasis-marker');
1459
1501
  }
1460
- else if ($isEquationNode(node) || $isFootnoteNode(node)) {
1502
+ else if ($isEquationNode(node) || $isFootnoteNode(node) || $isBlockAnchorNode(node)) {
1461
1503
  strongMarker = strongMarker ?? node.getStrongMarker();
1462
1504
  emphasisMarker = emphasisMarker ?? node.getEmphasisMarker();
1463
1505
  }
@@ -1837,6 +1879,9 @@ function convertLinkNode(node) {
1837
1879
  label: child.getFootnoteId(),
1838
1880
  });
1839
1881
  }
1882
+ else if ($isBlockAnchorNode(child)) {
1883
+ children.push({ type: 'blockAnchor', id: child.getId() });
1884
+ }
1840
1885
  else if ($isHtmlNode(child)) {
1841
1886
  children.push({ type: 'html', value: child.getHtml() });
1842
1887
  }
@@ -2,7 +2,7 @@ import { $createParagraphNode, $createTextNode, $createLineBreakNode, $isLineBre
2
2
  import { $createHeadingNode, $createQuoteNode } from '@lexical/rich-text';
3
3
  import { $createCodeNode } from '@lexical/code';
4
4
  import { $isLinkNode } from '@lexical/link';
5
- import { $createHorizontalRuleNode, $createImageNode, $isImageNode, $createCalloutNode, $createToggleContainerNode, $createToggleTitleNode, $createToggleContentNode, $createEquationNode, $isEquationNode, $createFootnoteNode, $isFootnoteNode, $createHtmlNode, $isHtmlNode, $createMermaidNode, $createC4Node, $createFrontmatterNode, $createCustomLinkNode, $createCustomListNode, $createDefinitionListNode, $createDefinitionTermNode, $createDefinitionDescriptionNode, $createCustomListItemNode, $createListItemParagraphBreakNode, $createTransclusionNode, } from '../editor/nodes/index.js';
5
+ import { $createHorizontalRuleNode, $createImageNode, $isImageNode, $createCalloutNode, $createToggleContainerNode, $createToggleTitleNode, $createToggleContentNode, $createEquationNode, $isEquationNode, $createFootnoteNode, $isFootnoteNode, $createBlockAnchorNode, $isBlockAnchorNode, $createHtmlNode, $isHtmlNode, $createMermaidNode, $createC4Node, $createFrontmatterNode, $createCustomLinkNode, $createCustomListNode, $createDefinitionListNode, $createDefinitionTermNode, $createDefinitionDescriptionNode, $createCustomListItemNode, $createListItemParagraphBreakNode, $createTransclusionNode, } from '../editor/nodes/index.js';
6
6
  import { parseFormattedAlias } from '../editor/MarkdownShortcutsPlugin.js';
7
7
  import { $createTableNode, $createTableRowNode, $createTableCellNode, TableCellHeaderStates } from '@lexical/table';
8
8
  import { getFileType } from '../../utils/file-types.js';
@@ -845,6 +845,12 @@ function convertInlineNode(node) {
845
845
  const fnRef = node;
846
846
  return [$createFootnoteNode(fnRef.identifier)];
847
847
  }
848
+ case 'blockAnchor': {
849
+ // Block anchor badge (#122): render `^ULID` as a compact badge instead
850
+ // of raw text, using BlockAnchorNode to preserve the id for round-trip.
851
+ const anchor = node;
852
+ return [$createBlockAnchorNode(anchor.id)];
853
+ }
848
854
  case 'wikiLink': {
849
855
  // Wiki-links from mdast-util-wiki-link: [[path|alias]]
850
856
  const wikiLink = node;
@@ -991,7 +997,7 @@ function convertStrong(node) {
991
997
  applyFormatToLinkChildren(n, 'bold', marker);
992
998
  nodes.push(n);
993
999
  }
994
- else if ($isEquationNode(n) || $isFootnoteNode(n)) {
1000
+ else if ($isEquationNode(n) || $isFootnoteNode(n) || $isBlockAnchorNode(n)) {
995
1001
  if (!n.hasFormat('bold')) {
996
1002
  n.toggleFormat('bold');
997
1003
  }
@@ -1026,7 +1032,7 @@ function convertEmphasis(node) {
1026
1032
  applyFormatToLinkChildren(n, 'italic', marker);
1027
1033
  nodes.push(n);
1028
1034
  }
1029
- else if ($isEquationNode(n) || $isFootnoteNode(n)) {
1035
+ else if ($isEquationNode(n) || $isFootnoteNode(n) || $isBlockAnchorNode(n)) {
1030
1036
  if (!n.hasFormat('italic')) {
1031
1037
  n.toggleFormat('italic');
1032
1038
  }
@@ -1125,7 +1131,7 @@ function convertDelete(node) {
1125
1131
  applyFormatToLinkChildren(n, 'strikethrough');
1126
1132
  nodes.push(n);
1127
1133
  }
1128
- else if ($isEquationNode(n) || $isFootnoteNode(n)) {
1134
+ else if ($isEquationNode(n) || $isFootnoteNode(n) || $isBlockAnchorNode(n)) {
1129
1135
  if (!n.hasFormat('strikethrough')) {
1130
1136
  n.toggleFormat('strikethrough');
1131
1137
  }
@@ -16,7 +16,8 @@ const wikiLinkOptions = { aliasDivider: '|' };
16
16
  const EMPTY_ALIAS_SENTINEL = '__EMPTY_ALIAS__';
17
17
  // Private-Use-Area sentinel marking a `!` that immediately precedes `[[`
18
18
  // (candidate transclusion/embed marker, #119). The next free codepoint after
19
- // `annotate-sentinels.ts`'s E000-E003 range and `stringify.ts`'s E004.
19
+ // `annotate-sentinels.ts`'s E000-E003 range and E004, which `stringify.ts`
20
+ // used as its force-escape placeholder until that moved to U+2BFF.
20
21
  //
21
22
  // Why substitute at all, rather than just checking "does the wikiLink node
22
23
  // have a `!` text sibling" after parsing: the micromark wiki-link tokenizer
@@ -566,6 +567,313 @@ function splitTextNodeEscapes(node, normalizedText) {
566
567
  flushRun(runStart, parts.length);
567
568
  return result;
568
569
  }
570
+ // Any id shape the resolver and the reference-side wiki-link parser already
571
+ // accept — ULID included — gated by the resolver's own position rule: the
572
+ // caret must start a token (line-start or preceded by whitespace), and the
573
+ // captured id must run to end of line (trailing spaces/tabs allowed). This is
574
+ // what makes badge and resolver agree by construction for every id form with
575
+ // no carve-out (#124/FR-002, #126/FR-001) — ULID no longer gets a permissive,
576
+ // position-free rule of its own; Crockford Base32 (ULID's charset) is already
577
+ // a strict subset of this charset, so every ULID that satisfies this position
578
+ // rule keeps badging unchanged (see ADR-122's amendments).
579
+ //
580
+ // The charset excludes `*` (in addition to `#`/`]`/whitespace) to match
581
+ // `liminis-app/src/main/fs.ts`'s `ANCHOR_LINE_PATTERN`
582
+ // (`[^\s\]#*]`) as actually implemented for `liminis#1114` — that resolver
583
+ // pattern narrowed *both* its wrapped and unwrapped branches to exclude `*`,
584
+ // unlike the stale, pre-implementation regex the #127 issue body quoted.
585
+ // `_` stays allowed: excluding it would break the NanoID-with-underscore
586
+ // case (#124/FR-004), and the resolver's own pattern allows it too.
587
+ const WIDE_ID_CHAR = /[^\s\]#*]/;
588
+ // A symmetric emphasis wrapper (`**`, `__`, `*`, `_`) around `^<id>` at line
589
+ // end badges too (#127), so this rule agrees with the widened resolver
590
+ // (`liminis#1114`) for wrapped ids the same way it already agrees for plain
591
+ // ones. The wrapped id charset matches the resolver's actual wrapped-branch
592
+ // charset (`[^\s\]#*]+?` in
593
+ // `ANCHOR_LINE_PATTERN`) — same as `WIDE_ID_CHAR` above; kept as a distinct,
594
+ // separately-named constant since the wrapped and unwrapped charsets are
595
+ // independent knobs in the resolver's pattern and could diverge again.
596
+ // Longest markers first so a `**`/`__` candidate is tried before its `*`/`_`
597
+ // prefix.
598
+ const WRAPPED_ID_CHAR = /[^\s\]#*]/;
599
+ const WRAPPER_MARKERS = ['**', '__', '*', '_'];
600
+ const isSpaceOrTab = (ch) => ch === ' ' || ch === '\t';
601
+ /**
602
+ * Look for one of `WRAPPER_MARKERS` immediately before `offset` in the raw,
603
+ * pre-parse `normalizedText`, itself preceded by whitespace or document
604
+ * start — the same left-boundary rule already applied to a bare caret, just
605
+ * one token further out. Returns the matched marker string, or
606
+ * `null` if none of them fit.
607
+ *
608
+ * This only needs to look *outside* the current text node (at `offset`, the
609
+ * node's own start) because a caret can only be adjacent to a *structural*
610
+ * wrapper marker when it is the first character of its own text node: if a
611
+ * literal `**`/`_` sat before the caret inside the same text node, that run
612
+ * of emphasis markers never found a matching closer and CommonMark left it
613
+ * as ordinary text, which the plain (non-wrapped) left-boundary check
614
+ * already handles correctly with no wrapper logic involved.
615
+ */
616
+ function matchWrapperMarkerBefore(normalizedText, offset) {
617
+ for (const marker of WRAPPER_MARKERS) {
618
+ const markerStart = offset - marker.length;
619
+ if (markerStart < 0)
620
+ continue;
621
+ if (normalizedText.slice(markerStart, offset) !== marker)
622
+ continue;
623
+ if (markerStart === 0 || /\s/.test(normalizedText[markerStart - 1]))
624
+ return marker;
625
+ }
626
+ return null;
627
+ }
628
+ /**
629
+ * Find every block-anchor match in `decoded` — any id shape, position-gated
630
+ * by the resolver's own rule, with no separate ULID carve-out (#126) — at
631
+ * each unescaped `^`, left to right.
632
+ *
633
+ * The boundary checks peek one character outside this text node's own
634
+ * `decoded`/`source` span — into `normalizedText` at `start - 1` (left) or
635
+ * from `end` onward (right) — rather than inspecting sibling AST nodes: the
636
+ * raw source character is equivalent and needs no tree traversal (see Plan
637
+ * stage's "Key Decisions"). This is what lets the rule correctly refuse a
638
+ * match immediately followed by more prose on the same line, even when that
639
+ * prose lives in a following sibling node (e.g. a wiki-link right after the
640
+ * id), and correctly accept one immediately after a preceding sibling ends
641
+ * with whitespace.
642
+ *
643
+ * A symmetric emphasis wrapper (`**`, `__`, `*`, `_`) around the id at line
644
+ * end also badges (#127), using the same outside-the-node peek:
645
+ * when the caret is the first character of this text node (`i === 0`) and
646
+ * the plain whitespace/start rule doesn't hold, it peeks backward for a
647
+ * wrapper marker; when one is found, the id must then run to this text
648
+ * node's own end (`idEnd === decoded.length`) and be followed immediately
649
+ * by the *same* marker string before the usual trailing-whitespace/EOL
650
+ * check. Both `i === 0` and `idEnd === decoded.length` are load-bearing
651
+ * invariants, not incidental: they are exactly the positions at which a
652
+ * *structural* wrapper marker (one CommonMark parsed as real emphasis,
653
+ * rather than literal text left over from an unmatched delimiter run) can
654
+ * be adjacent to the caret/id at all — see `matchWrapperMarkerBefore`.
655
+ */
656
+ function findBlockAnchorMatches(decoded, parts, normalizedText, start, end) {
657
+ const matches = [];
658
+ let cursor = 0;
659
+ for (let i = 0; i < decoded.length; i++) {
660
+ if (i < cursor)
661
+ continue;
662
+ if (decoded[i] !== '^' || parts[i].escaped)
663
+ continue;
664
+ // Left boundary: preceded by whitespace, or true document start — or,
665
+ // when the caret is the first character of this text node, a symmetric
666
+ // emphasis wrapper immediately before it (#127). That `i === 0` gate is
667
+ // load-bearing: it's the only position at which a *structural* wrapper
668
+ // marker (one CommonMark actually parsed as emphasis, not literal text)
669
+ // can sit immediately before the caret — see `matchWrapperMarkerBefore`.
670
+ const leftOk = i > 0 ? /\s/.test(decoded[i - 1]) : start === 0 || /\s/.test(normalizedText[start - 1]);
671
+ let wrapMarker = null;
672
+ if (!leftOk) {
673
+ if (i === 0)
674
+ wrapMarker = matchWrapperMarkerBefore(normalizedText, start);
675
+ if (!wrapMarker)
676
+ continue;
677
+ }
678
+ // Id capture: greedy run of non-whitespace, non-`]`, non-`#`, non-`*`,
679
+ // tested against `decoded` rather than raw source. A backslash-escaped `#` or
680
+ // `]` inside an id (`^abc\#def`) therefore truncates the capture one
681
+ // character earlier than a regex run over raw, undecoded file text
682
+ // would (the resolver's own matching model) — but this never produces a
683
+ // badge/resolver disagreement: whatever follows the truncation point is
684
+ // identical, non-whitespace content in both the decoded and raw views
685
+ // (backslash-escaping only ever turns `\X` into `X`, never anything
686
+ // into whitespace), so the right-boundary check below rejects the match
687
+ // in both models alike whenever this truncation is reachable. See the
688
+ // "does not badge an id containing a backslash-escaped delimiter"
689
+ // regression test.
690
+ //
691
+ // When wrapped, `WRAPPED_ID_CHAR` applies instead of `WIDE_ID_CHAR` (see
692
+ // their definitions) — currently identical charsets, kept as separate
693
+ // named constants since the resolver's wrapped/unwrapped branches are
694
+ // independent knobs that could diverge again.
695
+ const idCharTest = wrapMarker ? WRAPPED_ID_CHAR : WIDE_ID_CHAR;
696
+ let idEnd = i + 1;
697
+ while (idEnd < decoded.length && idCharTest.test(decoded[idEnd]))
698
+ idEnd++;
699
+ // Empty capture: the character right after `^` is already whitespace
700
+ // (or end of text), e.g. `a ^ b`, `a ^`, `x ^\t`. `a ^ b` would also be
701
+ // rejected by the right-boundary check below regardless (the trailing
702
+ // `b` isn't end-of-line), but a bare trailing caret like `a ^` or
703
+ // `x ^\t` reaches true end-of-line/end-of-document and would otherwise
704
+ // pass that check with an empty id — this guard is what actually stops
705
+ // that case. See "does not badge a bare trailing caret" regression test.
706
+ if (idEnd === i + 1)
707
+ continue;
708
+ // Right boundary: only trailing spaces/tabs before end of line or end of
709
+ // document — peeking past this node's own end into `normalizedText` if
710
+ // the capture runs all the way to it.
711
+ //
712
+ // When wrapped, the closer is required instead: the id capture must run
713
+ // all the way to this text node's own end (the mirror image of the
714
+ // `i === 0` left-boundary gate — a structural closer can only be
715
+ // adjacent to the id there), the exact same marker string that opened
716
+ // it must appear immediately after, and only trailing spaces/tabs and
717
+ // end-of-line/end-of-document may follow that. No fallback to the
718
+ // unwrapped rule on mismatch — an asymmetric wrapper (e.g. `**^id_`)
719
+ // must never badge with a corrupted id (#127/SC-003).
720
+ let rightOk;
721
+ if (wrapMarker) {
722
+ if (idEnd !== decoded.length)
723
+ continue;
724
+ const closerEnd = end + wrapMarker.length;
725
+ if (normalizedText.slice(end, closerEnd) !== wrapMarker)
726
+ continue;
727
+ let pos = closerEnd;
728
+ while (pos < normalizedText.length && isSpaceOrTab(normalizedText[pos]))
729
+ pos++;
730
+ rightOk = pos === normalizedText.length || normalizedText[pos] === '\n';
731
+ }
732
+ else {
733
+ let j = idEnd;
734
+ while (j < decoded.length && isSpaceOrTab(decoded[j]))
735
+ j++;
736
+ if (j < decoded.length) {
737
+ rightOk = decoded[j] === '\n';
738
+ }
739
+ else {
740
+ let pos = end;
741
+ while (pos < normalizedText.length && isSpaceOrTab(normalizedText[pos]))
742
+ pos++;
743
+ rightOk = pos === normalizedText.length || normalizedText[pos] === '\n';
744
+ }
745
+ }
746
+ if (!rightOk)
747
+ continue;
748
+ matches.push({ index: i, length: idEnd - i });
749
+ cursor = idEnd;
750
+ }
751
+ return matches;
752
+ }
753
+ /**
754
+ * Split a single `text` node into `[before, blockAnchor, after, ...]`
755
+ * siblings wherever it contains a {@link findBlockAnchorMatches} match,
756
+ * mirroring `splitTextNodeEscapes`'s decode-replay + position-mapping
757
+ * machinery exactly (including its conservative bail-out when replayed
758
+ * decoding doesn't exactly reproduce `node.value`, e.g. a character
759
+ * reference in the span) rather than duplicating it (#122).
760
+ *
761
+ * Because this only ever inspects a `text` node's own `value` (plus, for
762
+ * the boundary checks, one character immediately outside it), it can
763
+ * never see into `inlineCode`, `code`, `inlineMath`, `wikiLink` or
764
+ * `wikiEmbed` node content — none of those are `text` nodes once mdast has
765
+ * typed them — which satisfies the code-span/fenced-code/math edge case and
766
+ * FR-006 by construction, with no "protected ranges" pre-parse machinery
767
+ * needed.
768
+ */
769
+ function splitTextNodeBlockAnchors(node, normalizedText) {
770
+ const start = node.position?.start?.offset;
771
+ const end = node.position?.end?.offset;
772
+ if (start == null || end == null) {
773
+ return [node];
774
+ }
775
+ const source = normalizedText.slice(start, end);
776
+ const { decoded, parts } = replayDecodeEscapes(source);
777
+ if (decoded !== node.value) {
778
+ return [node];
779
+ }
780
+ // A caret whose leading `^` came from a backslash escape (`\^`) in the
781
+ // source is never a match candidate — `findBlockAnchorMatches` checks
782
+ // `parts[i].escaped` itself before trying either branch. `decoded` has
783
+ // already resolved `\^` to a plain `^`, so nothing downstream can tell the
784
+ // two apart on its own; an author who deliberately escaped a caret meant
785
+ // literal text, not an anchor, and `stringify.ts`'s `blockAnchor` handler
786
+ // always emits a bare `^id` with no escaping, so badging it would silently
787
+ // drop the escape on the next save.
788
+ const matches = findBlockAnchorMatches(decoded, parts, normalizedText, start, end);
789
+ if (matches.length === 0) {
790
+ return [node];
791
+ }
792
+ const startPos = node.position.start;
793
+ // Per-offset line/column within `source`, mirroring splitTextNodeEscapes.
794
+ const positionAt = new Array(source.length + 1);
795
+ {
796
+ let line = startPos.line;
797
+ let column = startPos.column;
798
+ positionAt[0] = { line, column };
799
+ for (let i = 0; i < source.length; i++) {
800
+ if (source[i] === '\n') {
801
+ line += 1;
802
+ column = 1;
803
+ }
804
+ else {
805
+ column += 1;
806
+ }
807
+ positionAt[i + 1] = { line, column };
808
+ }
809
+ }
810
+ const makePosition = (srcStart, srcEnd) => ({
811
+ start: { ...positionAt[srcStart - start], offset: srcStart },
812
+ end: { ...positionAt[srcEnd - start], offset: srcEnd },
813
+ });
814
+ const makeTextNode = (value, srcStart, srcEnd) => ({
815
+ type: 'text',
816
+ value,
817
+ position: makePosition(srcStart, srcEnd),
818
+ });
819
+ const result = [];
820
+ let cursor = 0; // index into `parts`/`decoded`
821
+ for (const match of matches) {
822
+ const matchStart = match.index;
823
+ const matchEnd = matchStart + match.length;
824
+ if (matchStart > cursor) {
825
+ const value = parts.slice(cursor, matchStart).map((p) => p.char).join('');
826
+ result.push(makeTextNode(value, start + parts[cursor].srcStart, start + parts[matchStart - 1].srcEnd));
827
+ }
828
+ // Captured from raw `source`, not `decoded`: stringify.ts's `blockAnchor`
829
+ // handler re-emits `^${node.id}` verbatim with no escaping, so the id
830
+ // must already carry any backslash the author wrote (e.g. `^ab\_cd`) or
831
+ // that escape is silently dropped on the next save — a round-trip
832
+ // corruption distinct from, and not covered by, the decoded-vs-raw
833
+ // truncation reasoning above (that reasoning only shows the *match
834
+ // boundary* never diverges; it says nothing about what ends up inside
835
+ // an id that does match).
836
+ result.push({
837
+ type: 'blockAnchor',
838
+ id: source.slice(parts[matchStart + 1].srcStart, parts[matchEnd - 1].srcEnd),
839
+ position: makePosition(start + parts[matchStart].srcStart, start + parts[matchEnd - 1].srcEnd),
840
+ });
841
+ cursor = matchEnd;
842
+ }
843
+ if (cursor < parts.length) {
844
+ const value = parts.slice(cursor, parts.length).map((p) => p.char).join('');
845
+ result.push(makeTextNode(value, start + parts[cursor].srcStart, start + parts[parts.length - 1].srcEnd));
846
+ }
847
+ return result;
848
+ }
849
+ /**
850
+ * Walk the tree and split every `text` node containing a block anchor (see
851
+ * `splitTextNodeBlockAnchors`) into siblings. Run after `resolveWikiEmbeds`/
852
+ * `annotateEmphasisMarkers` (so this never sees wiki-link/embed target text —
853
+ * FR-006) and before `splitEscapedPunctuation` (so that pass still sees, and
854
+ * can process, any escaped punctuation left in this split's "before"/"after"
855
+ * text siblings) (#122).
856
+ */
857
+ function splitBlockAnchors(root, normalizedText) {
858
+ function walk(node) {
859
+ if (!node || typeof node !== 'object')
860
+ return node;
861
+ if (node.children && Array.isArray(node.children)) {
862
+ const children = [];
863
+ for (const child of node.children) {
864
+ if (child?.type === 'text') {
865
+ children.push(...splitTextNodeBlockAnchors(child, normalizedText));
866
+ }
867
+ else {
868
+ children.push(walk(child));
869
+ }
870
+ }
871
+ return { ...node, children };
872
+ }
873
+ return node;
874
+ }
875
+ return walk(root);
876
+ }
569
877
  /**
570
878
  * Walk the tree and split every `text` node containing a force-escaped
571
879
  * character (see `splitTextNodeEscapes`) into siblings. Run last, after all
@@ -684,6 +992,9 @@ export function parseMarkdown(text, _options = {}) {
684
992
  root = resolveWikiEmbeds(root);
685
993
  // Post-process: annotate emphasis/strong marker characters from original source
686
994
  root = annotateEmphasisMarkers(root, text, replacements);
995
+ // Post-process: split a bare `^ULID` block anchor out of its surrounding
996
+ // text so it can render as a badge instead of raw text (#122)
997
+ root = splitBlockAnchors(root, normalizedText);
687
998
  // Post-process: split out backslash-escaped punctuation so its escape can
688
999
  // be carried through Lexical and restored at stringify time (#17)
689
1000
  root = splitEscapedPunctuation(root, normalizedText);
@@ -145,7 +145,18 @@ function addCheckboxTextToOrderedLists(root) {
145
145
  // backslash-free, it is provably invisible to the existing intraword-
146
146
  // underscore and bracket-preservation post-processes below, so neither one
147
147
  // needs to change to account for it.
148
- const FORCE_ESCAPE_PLACEHOLDER = '\u{E004}';
148
+ //
149
+ // It must classify as punctuation, because it stands in for a backslash.
150
+ // mdast-util-to-markdown >= 2.1.3 leaves an underscore unescaped when both
151
+ // neighbours are neither whitespace nor punctuation (`a_b`), and it sees the
152
+ // placeholder, not the backslash. The previous placeholder, U+E004, is
153
+ // private-use and so classified as a word character: an underscore typed
154
+ // beside a preserved escape went out bare and re-parsed as emphasis. No
155
+ // private-use codepoint can be punctuation, so this is an assigned symbol:
156
+ // U+2BFF HELLSCHREIBER PAUSE SYMBOL (`So`), chosen because nothing writes
157
+ // it. It must stay in the BMP: upstream reads neighbours with `charCodeAt`,
158
+ // which would see half of a surrogate pair and call that a word character.
159
+ const FORCE_ESCAPE_PLACEHOLDER = '\u{2BFF}';
149
160
  // The character class the placeholder-restoration post-process (below) is
150
161
  // allowed to wrap in a backslash. Kept narrow — rather than matching any
151
162
  // character between two placeholder codepoints — so that if the placeholder
@@ -348,6 +359,11 @@ export function stringifyMarkdown(root, options = {}) {
348
359
  return marker + content + marker;
349
360
  },
350
361
  escapedChar: (node) => `${FORCE_ESCAPE_PLACEHOLDER}${node.value}${FORCE_ESCAPE_PLACEHOLDER}`,
362
+ // Block anchor badge (#122): a `blockAnchor` node produced by
363
+ // `splitBlockAnchors` in parse.ts always carries exactly the id
364
+ // matched from the source, so re-emitting `^id` is lossless by
365
+ // construction — no escaping needed, mirroring `wikiLink`/`wikiEmbed`.
366
+ blockAnchor: (node) => `^${node.id}`,
351
367
  wikiLink: (node) => `[[${formatWikiLinkBody(node)}]]`,
352
368
  // Transclusion/embed (#119): the `!`-prefixed form of a block-scoped
353
369
  // wiki-link. `formatWikiLinkBody` requires `data.blockId` be present