Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
comment on remaining non text content (#3219)
shagun-singh-inkeepd82e7ea287ce
diff --git a/.changeset/comment-on-attribute-held-content.md b/.changeset/comment-on-attribute-held-content.mdnew file mode 100644index 000000000..b948c43d1--- /dev/null+++ b/.changeset/comment-on-attribute-held-content.md@@ -0,0 +1,7 @@+---+'@inkeep/open-knowledge': minor+---++Comment on mermaid diagrams, math, images, wiki links, and tags.++Commenting on ordinary prose failed with "The quoted passage is not in the document" whenever the paragraph merely contained a highlight, an image, a wiki link, a tag, inline math, a footnote marker, an underline, or an autolink. In a wiki-style document that is most paragraphs, not an edge case, so a comment on a normal sentence often just produced an error and was lost. Each of those constructs spends characters on markup that renders as less than it spells, and the anchor matcher only knew how to look past some of them. It now looks past all of them, including an aliased wiki link's hidden target and a link's heading fragment.diff --git a/knip.config.ts b/knip.config.tsindex 13582ee18..f004e6bc3 100644--- a/knip.config.ts+++ b/knip.config.ts@@ -123,7 +123,9 @@ export default {'packages/server': {entry: ['src/**/*.test.ts', 'src/parse-worker.ts'],project: 'src/**',- ignoreDependencies: ['@types/shell-quote'],+ ignoreDependencies: [+ '@types/shell-quote',+ ],},'packages/cli': {entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],diff --git a/packages/app/src/comments/CommentMarginRail.tsx b/packages/app/src/comments/CommentMarginRail.tsxindex 14f002d74..d44c6d36f 100644--- a/packages/app/src/comments/CommentMarginRail.tsx+++ b/packages/app/src/comments/CommentMarginRail.tsx@@ -33,7 +33,7 @@ import { createPortal } from 'react-dom';import { Button } from '@/components/ui/button';import { getEditorView } from '@/editor/utils/get-editor-view';import { cn } from '@/lib/utils';-import { buildTextIndex, findQuoteRange, findRangeInIndex } from './anchor-search';+import { createAnchorResolver, findQuoteRange } from './anchor-search';import {propertyRowRect,revealPropertyValueRange,@@ -136,12 +136,9 @@ export function CommentMarginRail({ editor, docName }: { editor: Editor; docNameconst rect = container.getBoundingClientRect();const targets: { id: string; y: number }[] = [];if (open.length > 0) {- // ONE index for the whole pass. `findQuoteRange` builds one internally,- // so calling it per thread walked the entire document once per comment- // — and this runs on every scroll frame and every edit. Measured at 40- // comments in a 100k-character document that was ~35 ms a frame, about- // twenty times what resolving them against a shared index costs.- const index = buildTextIndex(editor.state.doc);+ // ONE resolver for the whole pass — see `createAnchorResolver`, which+ // owns the shared-index reasoning and the prose-before-components order.+ const resolve = createAnchorResolver(editor.state.doc);for (const thread of open) {let y: number;if (thread.target.kind === 'property') {@@ -152,7 +149,7 @@ export function CommentMarginRail({ editor, docName }: { editor: Editor; docNamey = rect.top + rect.height / 2;} else {if (thread.anchor === null) continue;- const range = findRangeInIndex(index, thread.anchor.quote, thread.anchor);+ const range = resolve(thread.anchor.quote, thread.anchor);if (range === null) continue;try {// Viewport coords of the anchored text itself. Throws for a positiondiff --git a/packages/app/src/comments/CommentSelectionAffordance.tsx b/packages/app/src/comments/CommentSelectionAffordance.tsxindex e951f31bf..7d7035eec 100644--- a/packages/app/src/comments/CommentSelectionAffordance.tsx+++ b/packages/app/src/comments/CommentSelectionAffordance.tsx@@ -19,6 +19,7 @@*/import { autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/dom';+import { commentQuoteText } from '@inkeep/open-knowledge-core';import { Trans, useLingui } from '@lingui/react/macro';import { posToDOMRect } from '@tiptap/core';import type { Editor } from '@tiptap/react';@@ -97,7 +98,13 @@ export function CommentSelectionAffordance({// "- **Peanut sauce:** 3 tbsp peanut butter" — a quote that is in no// document. The server matches rendered text against the markdown body// with syntax treated as elastic, so the plain text is what it wants.- const quote = editor.state.doc.textBetween(from, to, '\n').trim();+ // `commentQuoteText`, not `textBetween`: an inline atom (wiki link, tag,+ // image, inline math, footnote marker) and a promoted fence (mermaid,+ // math) both keep their reader-visible text in attributes, so plain+ // `textBetween` reads them as empty. Selecting one then produced no quote+ // at all, and selecting prose AROUND one produced a quote with a hole the+ // anchor resolver could not match.+ const quote = commentQuoteText(editor.state.doc, from, to).trim();if (quote.length === 0) return;// Captured HERE, at the moment of the pick — this component is the only// place that knows which occurrence of a repeated passage was selected.diff --git a/packages/app/src/comments/anchor-decorations.tsx b/packages/app/src/comments/anchor-decorations.tsxindex 6d74a0ac5..b3f3418e1 100644--- a/packages/app/src/comments/anchor-decorations.tsx+++ b/packages/app/src/comments/anchor-decorations.tsx@@ -17,7 +17,7 @@ import { Decoration, DecorationSet } from '@tiptap/pm/view';import type { Editor } from '@tiptap/react';import { useEffect } from 'react';import { buildAnchorSegments, type PlacedAnchor } from './anchor-layers';-import { buildTextIndex, findRangeInIndex } from './anchor-search';+import { createAnchorResolver } from './anchor-search';import {emitOpenThreadPopover,getActiveThread,@@ -75,10 +75,10 @@ function placeAnchors(docName: string, doc: PMNode, draft: DraftRange | null): P(t) => t.status === 'open' && t.target.kind === 'body' && t.anchor !== null,);if (threads.length > 0) {- const index = buildTextIndex(doc);+ const resolve = createAnchorResolver(doc);for (const thread of threads) {if (thread.anchor === null) continue;- const range = findRangeInIndex(index, thread.anchor.quote, thread.anchor);+ const range = resolve(thread.anchor.quote, thread.anchor);if (range === null) continue;placed.push({ id: thread.id, from: range.from, to: range.to });}diff --git a/packages/app/src/comments/anchor-search.test.ts b/packages/app/src/comments/anchor-search.test.tsindex 190580329..31aee2092 100644--- a/packages/app/src/comments/anchor-search.test.ts+++ b/packages/app/src/comments/anchor-search.test.ts@@ -10,8 +10,19 @@* shape (`text` + per-character positions) is trivially constructible.*/+import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';+import { getSchema } from '@tiptap/core';+import type { Node as PMNode } from '@tiptap/pm/model';import { describe, expect, test } from 'vitest';-import { findRangeInIndex } from './anchor-search';+import {+ buildTextIndex,+ captureSelectionContext,+ createAnchorResolver,+ findRangeInIndex,+} from './anchor-search';++const mdManager = new MarkdownManager({ extensions: sharedExtensions });+const schema = getSchema(sharedExtensions);/** Build the index `buildTextIndex` would produce, with PM positions offset by 1. */function indexOf(text: string): { text: string; positions: number[] } {@@ -50,15 +61,18 @@ describe('findRangeInIndex — repeated quote', () => {expect(range?.from).toBe(FIRST + 1);});- test('identical context on both hits falls back to the nearest position hint', () => {+ /**+ * A stored `Anchor` carries a `start` measured against the MARKDOWN BODY.+ * There is no rendered-offset tie-break for it to be mistaken for any more —+ * a genuine tie takes the earliest hit, which is what the server does with+ * the same tied set, so the two agree by construction rather than by luck.+ */+ test('a genuine tie takes the earliest hit, ignoring a stored body offset', () => {const doc = 'x TARGET y x TARGET y';- const second = doc.indexOf('TARGET', doc.indexOf('TARGET') + 1);- const range = findRangeInIndex(indexOf(doc), 'TARGET', {- prefix: 'x ',- suffix: ' y',- start: second,- });- expect(range?.from).toBe(second + 1);+ const first = doc.indexOf('TARGET');+ const second = doc.indexOf('TARGET', first + 1);+ const anchor = { quote: 'TARGET', prefix: 'x ', suffix: ' y', start: second, end: 0 };+ expect(findRangeInIndex(indexOf(doc), 'TARGET', anchor)?.from).toBe(first + 1);});test('a quote that is gone resolves to null', () => {@@ -156,3 +170,236 @@ describe('findRangeInIndex — the passage was edited, not removed', () => {).toBeNull();});});++/**+ * Nodes whose reader-visible text lives in attributes.+ *+ * The index is what the highlight and the scroll-to are computed from, so a+ * node missing from it is a comment that anchors on the server and then cannot+ * be shown in the document.+ */+describe('buildTextIndex — text held in attributes', () => {+ function docOf(md: string): PMNode {+ return schema.nodeFromJSON(mdManager.parse(md));+ }++ /** The node of `typeName` in `doc`, as `[from, to)`. */+ function spanOf(doc: PMNode, typeName: string): { from: number; to: number } {+ let span: { from: number; to: number } | null = null;+ doc.descendants((node, pos) => {+ if (span === null && node.type.name === typeName) {+ span = { from: pos, to: pos + node.nodeSize };+ }+ return true;+ });+ if (span === null) throw new Error(`no ${typeName} in fixture`);+ return span;+ }++ test('a wiki link contributes its target', () => {+ expect(buildTextIndex(docOf('A [[page]] word.')).text).toBe('A page word.');+ });++ test('a tag contributes its `#` and name', () => {+ expect(buildTextIndex(docOf('A #tagname word.')).text).toBe('A #tagname word.');+ });++ test('a mermaid fence stays OUT of the prose index', () => {+ expect(buildTextIndex(docOf('```mermaid\ngraph TD;\n```')).text).toBe('');+ });++ test('a mermaid fence is in the component index', () => {+ expect(+ buildTextIndex(docOf('```mermaid\ngraph TD;\n```'), { includeBlockComponents: true }).text,+ ).toBe('graph TD;');+ });++ test('a hit inside an inline atom resolves to the whole atom', () => {+ const doc = docOf('A [[page]] word.');+ const range = findRangeInIndex(buildTextIndex(doc), 'page');+ expect(range).toEqual(spanOf(doc, 'wikiLink'));+ });++ /**+ * The case that made the per-character position mapping load-bearing: a+ * mermaid fence is two positions wide plus its content, so mapping every+ * character to the node's start highlighted only its opening token.+ */+ test('a hit inside a promoted fence resolves to the whole node', () => {+ const doc = docOf('```mermaid\ngraph TD;\n```');+ const range = createAnchorResolver(doc)('graph TD;');+ expect(range).toEqual(spanOf(doc, 'jsxComponent'));+ });+});++/**+ * Prose before diagram interiors.+ *+ * A diagram's interior is identifiers and edge labels, not prose, but it is+ * full of common letter pairs. Searching it alongside the prose let a short+ * quote match inside a drawing: a comment on the word "hi" resolved into a+ * diagram containing "thinks", so its margin chip docked beside the diagram+ * instead of beside the sentence it was about.+ */+describe('createAnchorResolver — a diagram never outbids the prose', () => {+ const MD = [+ '```mermaid',+ 'sequenceDiagram',+ ' Note right of John: Bob thinks a long long time.',+ '```',+ '',+ 'hi',+ ].join('\n');++ function docOf(md: string): PMNode {+ return schema.nodeFromJSON(mdManager.parse(md));+ }++ test('a short quote resolves to the paragraph, not into the diagram', () => {+ const doc = docOf(MD);+ let hiPos = -1;+ doc.descendants((node, pos) => {+ if (node.isText && node.text === 'hi') hiPos = pos;+ return true;+ });+ expect(hiPos).toBeGreaterThanOrEqual(0);+ expect(createAnchorResolver(doc)('hi')?.from).toBe(hiPos);+ });++ test('the diagram is still reachable by a quote only it can satisfy', () => {+ const doc = docOf(MD);+ let span: { from: number; to: number } | null = null;+ doc.descendants((node, pos) => {+ if (span === null && node.type.name === 'jsxComponent') {+ span = { from: pos, to: pos + node.nodeSize };+ }+ return true;+ });+ const range = createAnchorResolver(doc)(+ 'sequenceDiagram\n Note right of John: Bob thinks a long long time.',+ );+ expect(range).toEqual(span);+ });+});++/**+ * The reported failure, end to end: a comment on the FIRST of eight identical+ * `hi`s highlighted a later one.+ *+ * Two things had to line up. The math block above the target erased the+ * captured prefix — `textBetween` reads a block that keeps its text in+ * attributes as empty — which dropped the decision to the tie-break; and the+ * tie-break was reading the stored anchor's body offset as a rendered one,+ * which points past the target.+ *+ * Both are closed now, and the first is why the prefix asserted below is no+ * longer empty: capture reads the same substrate the quote does, so the block+ * contributes its formula and the context decides on its own.+ */+describe('a repeated quote under a block that renders no text', () => {+ const MD = ['$$', '1 + 1', '$$', '', 'hi', '', '> hello', '', ...Array(7).fill('- hi')].join(+ '\n',+ );++ test('resolves to the occurrence that was commented on', () => {+ const doc = schema.nodeFromJSON(mdManager.parse(MD));+ const occurrences: number[] = [];+ doc.descendants((node, pos) => {+ if (node.isText && node.text === 'hi') occurrences.push(pos);+ return true;+ });+ expect(occurrences.length).toBe(8);+ const target = occurrences[0] as number;++ const context = captureSelectionContext(doc, target, target + 2);+ // The math block contributes its formula rather than nothing, so the+ // decision no longer rests on a fallback at all.+ expect(context.prefix).toContain('1 + 1');++ // Exactly what the rail and the decorations pass: the stored anchor,+ // body-measured `start` and all.+ const anchor = { quote: 'hi', ...context, start: MD.indexOf('\nhi\n') + 1, end: 0 };+ expect(createAnchorResolver(doc)('hi', anchor)?.from).toBe(target);+ });+});++/**+ * Context has to survive a block boundary.+ *+ * Captured context joins blocks with a newline; the index has nothing between+ * them. Scoring them byte for byte meant any context reaching past the end of+ * its own paragraph scored zero, so repeats were separated by the position+ * tie-break rather than by the words around them — the thing actually captured+ * to tell them apart.+ */+describe('context scoring across a block boundary', () => {+ test('picks the occurrence whose neighbouring block matches', () => {+ const MD = ['- hi', '- hi', '- hi', '', 'the marker paragraph', '', '- hi', '- hi'].join('\n');+ const doc = schema.nodeFromJSON(mdManager.parse(MD));+ const occurrences: number[] = [];+ doc.descendants((node, pos) => {+ if (node.isText && node.text === 'hi') occurrences.push(pos);+ return true;+ });+ expect(occurrences.length).toBe(5);++ // The third "hi" — the one directly above the marker paragraph. Its suffix+ // is the only thing that distinguishes it, and only across a block break.+ const target = occurrences[2] as number;+ const context = captureSelectionContext(doc, target, target + 2);+ expect(context.suffix).toContain('the marker paragraph');++ expect(createAnchorResolver(doc)('hi', context)?.from).toBe(target);+ });++ test('and the one after the marker, which only its PREFIX separates', () => {+ const MD = ['- hi', '- hi', '- hi', '', 'the marker paragraph', '', '- hi', '- hi'].join('\n');+ const doc = schema.nodeFromJSON(mdManager.parse(MD));+ const occurrences: number[] = [];+ doc.descendants((node, pos) => {+ if (node.isText && node.text === 'hi') occurrences.push(pos);+ return true;+ });+ const target = occurrences[3] as number;+ const context = captureSelectionContext(doc, target, target + 2);+ expect(context.prefix).toContain('the marker paragraph');++ expect(createAnchorResolver(doc)('hi', context)?.from).toBe(target);+ });+});++/**+ * Context capture reads the same substrate the quote does.+ *+ * `textBetween` reads an inline atom as empty, so a context window containing a+ * wiki link or a tag came back with a hole exactly where the distinguishing+ * word was — the same failure the quote fix closed, one field over, and it+ * returned the wrong occurrence rather than merely a weaker score.+ */+describe('captureSelectionContext across an inline atom', () => {+ const MD = ['- [[alpha]] done', '- [[beta]] done'].join('\n');++ function occurrencesOfDone(doc: PMNode): number[] {+ const out: number[] = [];+ doc.descendants((node, pos) => {+ if (node.isText && node.text?.includes('done')) out.push(pos + node.text.indexOf('done'));+ return true;+ });+ return out;+ }++ test('the prefix carries the wiki link that tells the two apart', () => {+ const doc = schema.nodeFromJSON(mdManager.parse(MD));+ const target = occurrencesOfDone(doc)[1] as number;+ expect(captureSelectionContext(doc, target, target + 4).prefix).toContain('beta');+ });++ test('so the second occurrence resolves to itself, not the first', () => {+ const doc = schema.nodeFromJSON(mdManager.parse(MD));+ const hits = occurrencesOfDone(doc);+ expect(hits.length).toBe(2);+ const target = hits[1] as number;+ const context = captureSelectionContext(doc, target, target + 4);+ expect(createAnchorResolver(doc)('done', context)?.from).toBe(target);+ });+});diff --git a/packages/app/src/comments/anchor-search.ts b/packages/app/src/comments/anchor-search.tsindex bf81f366f..071d79893 100644--- a/packages/app/src/comments/anchor-search.ts+++ b/packages/app/src/comments/anchor-search.ts@@ -12,9 +12,10 @@* A REPEATED quote is the case that matters: `indexOf` alone always returns the* first match, so a phrase appearing twice highlights and scrolls to the wrong* one. The stored `prefix`/`suffix` (widened at create time until the passage- * was unique) disambiguate, mirroring how the server re-finds it — score each- * candidate by how much surrounding context still matches, then fall back to- * whichever sits nearest the last known position.+ * was unique) disambiguate, scoring each candidate by how much surrounding+ * context still matches — literally the same `contextMatchScore` the server+ * ranks with, so the two cannot pick different occurrences of one thread. Ties+ * fall to the earliest hit on both sides.** The server's `start`/`end` can't be used directly here: those are offsets into* the markdown BODY, while this index is over RENDERED text, so markdown syntax@@ -22,7 +23,14 @@* degrades gracefully when the surroundings have also changed.*/-import { findAllPassages, type PassageMatch } from '@inkeep/open-knowledge-core';+import {+ commentLeafText,+ commentQuoteText,+ contextMatchScore,+ findAllPassages,+ type PassageMatch,+ rewriteCeiling,+} from '@inkeep/open-knowledge-core';import type { Node as PMNode } from '@tiptap/pm/model';interface TextIndex {@@ -34,11 +42,27 @@ interface TextIndex {export interface AnchorContext {prefix?: string;suffix?: string;- /** Last known offset — a weak final tie-break when context can't separate hits. */- start?: number;}-export function buildTextIndex(doc: PMNode): TextIndex {+/**+ * `includeBlockComponents` admits the text a BLOCK holds in its attributes — a+ * mermaid diagram's chart, a math block's formula.+ *+ * Off by default, and that default is load-bearing. A diagram's interior is not+ * prose: it is identifiers and edge labels that no reader is reading as part of+ * the sentence next to it, but it is full of common letter pairs. Admitting it+ * to the prose index let a short quote match INSIDE a diagram — a comment on+ * the word "hi" landed on a diagram containing "thinks", and its margin chip+ * docked beside the drawing instead of beside the sentence it was about.+ *+ * A comment ON a diagram still resolves: `findQuoteRange` retries with this on+ * once the prose pass has come up empty, so the interior is reachable only by a+ * quote that nothing in the prose could satisfy.+ */+export function buildTextIndex(+ doc: PMNode,+ { includeBlockComponents = false }: { includeBlockComponents?: boolean } = {},+): TextIndex {let text = '';const positions: number[] = [];doc.descendants((node, pos) => {@@ -47,6 +71,27 @@ export function buildTextIndex(doc: PMNode): TextIndex {text += node.text[i];positions.push(pos + i);}+ return true;+ }+ // Inline atoms carry text a reader sees but ProseMirror stores as+ // attributes — a wiki link's label, a tag, an image's alt. Those ARE part+ // of the sentence, so they belong in the prose index unconditionally;+ // quotes are captured with them (see `commentQuoteText`), and without them+ // a passage spanning one could never be highlighted here.+ if (node.isBlock && !includeBlockComponents) return true;+ const leaf = commentLeafText(node);+ if (leaf.length > 0) {+ // Every character maps to the node's own position except the last, which+ // maps to its final one — `toRange` adds 1 to the last character's+ // position, so this is what makes a hit anywhere inside resolve to the+ // WHOLE node. For a one-wide inline atom the two are the same position;+ // for a mermaid fence they are not, and mapping every character to `pos`+ // would have highlighted only the node's opening token.+ const last = pos + node.nodeSize - 1;+ for (let i = 0; i < leaf.length; i++) {+ text += leaf[i];+ positions.push(i === leaf.length - 1 ? last : pos);+ }}return true;});@@ -64,34 +109,6 @@ function allOccurrences(haystack: string, needle: string): number[] {return out;}-/** Length of the longest common suffix of `a` and `b`. */-function commonSuffixLen(a: string, b: string): number {- const n = Math.min(a.length, b.length);- let i = 0;- while (i < n && a[a.length - 1 - i] === b[b.length - 1 - i]) i += 1;- return i;-}--/** Length of the longest common prefix of `a` and `b`. */-function commonPrefixLen(a: string, b: string): number {- const n = Math.min(a.length, b.length);- let i = 0;- while (i < n && a[i] === b[i]) i += 1;- return i;-}--/**- * How much the passage may grow when recovered from its brackets.- *- * DRIFT WARNING: mirrors `MAX_REWRITE_GROWTH` / `REWRITE_GROWTH_FLOOR` in the- * server's `anchor.ts`. The two run on different substrates — rendered text here,- * the markdown body there — so they cannot share an implementation, but they- * must agree on the policy or a passage recovers in the document and orphans on- * dispatch, or the reverse.- */-const MAX_REWRITE_GROWTH = 4;-const REWRITE_GROWTH_FLOOR = 64;-/*** Locate a passage whose text changed, by the context still surrounding it.*@@ -127,7 +144,7 @@ function findBetweenBrackets(while (start < end && /\s/.test(text[start] ?? '')) start += 1;while (end > start && /\s/.test(text[end - 1] ?? '')) end -= 1;if (end <= start) return null;- const ceiling = Math.max(quote.length * MAX_REWRITE_GROWTH, quote.length + REWRITE_GROWTH_FLOOR);+ const ceiling = rewriteCeiling(quote.length);if (end - start > ceiling) return null;return { start, end };}@@ -172,9 +189,9 @@ export function findRangeInIndex(let best: PassageMatch[] = [];let bestScore = -1;for (const hit of hits) {- const score =- commonSuffixLen(prefix, index.text.slice(0, hit.start)) +- commonPrefixLen(suffix, index.text.slice(hit.end));+ // `index.text` is rendered text, so there is no markdown syntax to be+ // elastic about — only whitespace. Same function the server ranks with.+ const score = contextMatchScore(index.text, hit, { prefix, suffix }, { syntaxIn: 'none' });if (score > bestScore) {bestScore = score;best = [hit];@@ -182,23 +199,13 @@ export function findRangeInIndex(best.push(hit);}}- if (best.length === 1) return toRange(index, best[0]);-- // Context couldn't separate them — take the one nearest the last known- // position. Body and rendered offsets aren't identical, but both advance- // monotonically through the document, so "nearest" still orders correctly.- const hint = context?.start;- if (hint === undefined) return toRange(index, best[0]);- let nearest = best[0];- let nearestDist = Number.POSITIVE_INFINITY;- for (const hit of best) {- const dist = Math.abs(hit.start - hint);- if (dist < nearestDist) {- nearestDist = dist;- nearest = hit;- }- }- return toRange(index, nearest);+ // Context could not separate them — the earliest hit, which is exactly what+ // the server's `bestByContext` caller does with the same tied set. There was+ // once a nearest-known-position tie-break here; it took the offset off a+ // stored anchor, which is measured against the markdown body, and compared it+ // to rendered positions. No caller ever had an offset in the right units, so+ // it only ever added a confident wrong answer to a genuine tie.+ return toRange(index, best[0]);}/**@@ -226,9 +233,15 @@ export function captureSelectionContext(from: number,to: number,): { prefix: string; suffix: string } {+ // `commentQuoteText`, for the same reason the QUOTE uses it: `textBetween`+ // reads an inline atom as empty, so a window containing a wiki link or a tag+ // came back with a hole where the distinguishing word was. Two list items+ // reading `[[alpha]] done` and `[[beta]] done` produced the identical prefix+ // ` done\n `, and the comment on the second anchored to the first — the same+ // failure the quote fix closed, one field over.return {- prefix: doc.textBetween(Math.max(0, from - SELECTION_CONTEXT_LEN), from, '\n'),- suffix: doc.textBetween(to, Math.min(doc.content.size, to + SELECTION_CONTEXT_LEN), '\n'),+ prefix: commentQuoteText(doc, Math.max(0, from - SELECTION_CONTEXT_LEN), from),+ suffix: commentQuoteText(doc, to, Math.min(doc.content.size, to + SELECTION_CONTEXT_LEN)),};}@@ -237,5 +250,30 @@ export function findQuoteRange(quote: string,context?: AnchorContext,): { from: number; to: number } | null {- return findRangeInIndex(buildTextIndex(doc), quote, context);+ return createAnchorResolver(doc)(quote, context);+}++/**+ * Resolve many quotes against one document.+ *+ * Callers place every open thread in a single pass, on every scroll frame and+ * every edit, so the indexes are built once for the pass rather than once per+ * thread — walking a 100k-character document per comment was measured at about+ * twenty times the cost.+ *+ * Two passes per quote, prose first (see `buildTextIndex` for why the order is+ * the protection). The component index is built lazily: a document whose+ * comments all land in prose — nearly all of them — never pays for it.+ */+export function createAnchorResolver(+ doc: PMNode,+): (quote: string, context?: AnchorContext) => { from: number; to: number } | null {+ const prose = buildTextIndex(doc);+ let components: TextIndex | null = null;+ return (quote, context) => {+ const inProse = findRangeInIndex(prose, quote, context);+ if (inProse !== null) return inProse;+ components ??= buildTextIndex(doc, { includeBlockComponents: true });+ return findRangeInIndex(components, quote, context);+ };}diff --git a/packages/app/src/comments/comment-composer-submit.dom.test.tsx b/packages/app/src/comments/comment-composer-submit.dom.test.tsxindex 7e5a806b8..55102afbf 100644--- a/packages/app/src/comments/comment-composer-submit.dom.test.tsx+++ b/packages/app/src/comments/comment-composer-submit.dom.test.tsx@@ -37,6 +37,14 @@ interface CreateArgs {onCreated?: (threadId: string) => void;}+/**+ * The document the editor double models. Real text with real offsets, so the+ * captured prefix and suffix are the ones this document would actually yield.+ */+const DOC_TEXT = 'Toss the tofu with cornstarch.';+const QUOTE_FROM = DOC_TEXT.indexOf('the tofu');+const QUOTE_TO = QUOTE_FROM + 'the tofu'.length;+const captured = {created: [] as CreateArgs[],startComment: null as (() => void) | null,@@ -115,18 +123,33 @@ function fakeEditor() {isDestroyed: false,state: {selection: {- from: 4,- to: 12,+ from: QUOTE_FROM,+ to: QUOTE_TO,empty: false,// The composer takes its span from `ranges`, not `from`/`to` — those// report the first range only, which on a table CellSelection is one// cell. A real Selection always carries `ranges`, so the double does// too; a single range is the TextSelection case this file covers.- ranges: [{ $from: { pos: 4 }, $to: { pos: 12 } }],+ ranges: [{ $from: { pos: QUOTE_FROM }, $to: { pos: QUOTE_TO } }],},// `content.size` bounds the selection-context capture — a real PM node// always carries it, so the double has to as well.- doc: { textBetween: () => 'the tofu', content: { size: 40 } },+ doc: {+ // Both the quote and its surrounding context come from+ // `commentQuoteText`, which walks the doc rather than calling+ // `textBetween` — that is what lets an inline atom (a wiki link, a tag)+ // contribute the text it keeps in attributes. So the double yields+ // nodes, and yields the slice actually asked for: a window-insensitive+ // double would let the context assertions below pass on a prefix and+ // suffix no document could have produced.+ nodesBetween: (from: number, to: number, fn: (node: unknown, pos: number) => void) => {+ fn(+ { isText: true, text: DOC_TEXT.slice(from, to), isBlock: false, isTextblock: false },+ from,+ );+ },+ content: { size: DOC_TEXT.length },+ },},// Records the collapse so the test can assert the passage stops being// "selected for whatever you do next" once it has been filed.@@ -157,14 +180,14 @@ describe('the comment composer', () => {fireEvent.click(screen.getByRole('button', { name: /add comment/i }));// The surrounding text rides along: it is what tells the server which- // occurrence was picked when the quoted words appear more than once. The- // double returns the same string for every range, so both sides read alike.+ // occurrence was picked when the quoted words appear more than once — so+ // these are the exact words either side of the pick in the document above.const [args] = captured.created;expect(args.docName).toBe('recipes/stir-fry');expect(args.quote).toBe('the tofu');expect(args.body).toBe('press it?');- expect(args.prefix).toBe('the tofu');- expect(args.suffix).toBe('the tofu');+ expect(args.prefix).toBe('Toss ');+ expect(args.suffix).toBe(' with cornstarch.');// No `onCreated` — filing it is the whole action, so nothing is handed on.expect(args.onCreated).toBeUndefined();});@@ -203,7 +226,7 @@ describe('the comment composer', () => {fireEvent.change(field, { target: { value: 'press it?' } });fireEvent.keyDown(field, { key: 'Enter' });- expect(captured.collapsedTo).toEqual([12]); // the end of the picked range+ expect(captured.collapsedTo).toEqual([QUOTE_TO]); // the end of the picked range});test('cancelling leaves the selection alone', async () => {diff --git a/packages/app/src/editor/bubble-menu/LinkEditPopover.dom.test.tsx b/packages/app/src/editor/bubble-menu/LinkEditPopover.dom.test.tsxindex 079a38995..ab0fcc4ab 100644--- a/packages/app/src/editor/bubble-menu/LinkEditPopover.dom.test.tsx+++ b/packages/app/src/editor/bubble-menu/LinkEditPopover.dom.test.tsx@@ -64,7 +64,14 @@ function makeEditor({return {state: {selection: { empty: selectionEmpty, from: selectionFrom, to: selectionTo },- doc: { textBetween: () => (selectionEmpty ? '' : 'docs') },+ // `shouldShowBubbleMenu` walks the doc via `commentQuoteText` so an+ // inline atom counts as text-bearing; the double yields one text node.+ doc: {+ nodesBetween: (from: number, _to: number, fn: (node: unknown, pos: number) => void) => {+ if (selectionEmpty) return;+ fn({ isText: true, text: 'docs', isBlock: false, isTextblock: false }, from);+ },+ },},view: { hasFocus: () => viewFocused },// The claim reads the non-throwing `editorView` field (never the `view`diff --git a/packages/app/src/editor/bubble-menu/bubble-menu-state.test.ts b/packages/app/src/editor/bubble-menu/bubble-menu-state.test.tsindex 2f3e2c23a..ecd41a4d8 100644--- a/packages/app/src/editor/bubble-menu/bubble-menu-state.test.ts+++ b/packages/app/src/editor/bubble-menu/bubble-menu-state.test.ts@@ -7,14 +7,16 @@* to the command palette.*/-import { LinkFidelity } from '@inkeep/open-knowledge-core';+import { LinkFidelity, MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';import { Editor, Extension } from '@tiptap/core';-import { TextSelection } from '@tiptap/pm/state';+import { NodeSelection, TextSelection } from '@tiptap/pm/state';import StarterKit from '@tiptap/starter-kit';import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';import { markIdentityKey, markIdentityPlugin } from '../extensions/mark-identity';import { installDomGlobals } from '../walk-currency-test-harness';-import { resolveAddLinkShortcutAction } from './bubble-menu-state';+import { resolveAddLinkShortcutAction, shouldShowBubbleMenu } from './bubble-menu-state';++const mdManager = new MarkdownManager({ extensions: sharedExtensions });let restoreDomGlobals: (() => void) | null = null;@@ -118,3 +120,66 @@ describe('resolveAddLinkShortcutAction', () => {expect(resolveAddLinkShortcutAction(editor)).toBeNull();});});++/**+ * Which selections the bar opens over.+ *+ * The distinction that matters is inline versus block. An inline atom carries+ * every mark this bar applies, so counting its text is what lets a wiki link or+ * a tag be selected and acted on. A block that keeps its content in attributes+ * — a mermaid diagram, a math block — carries none of them, and a bar offering+ * bold and superscript over a diagram is noise; those blocks have an Ask AI+ * button on their own chrome instead.+ */+describe('shouldShowBubbleMenu — text held in attributes', () => {+ function makeRichEditor(md: string): Editor {+ const host = document.createElement('div');+ document.body.appendChild(host);+ const editor = new Editor({+ element: host,+ content: mdManager.parse(md),+ extensions: sharedExtensions,+ });+ editors.push(editor);+ return editor;+ }++ /** Select the whole first node of `typeName`. */+ function selectNode(editor: Editor, typeName: string): void {+ let target: number | null = null;+ editor.state.doc.descendants((node, pos) => {+ if (target === null && node.type.name === typeName) target = pos;+ return true;+ });+ if (target === null) throw new Error(`no ${typeName} in fixture`);+ editor.view.dispatch(+ editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, target)),+ );+ }++ test('opens over an inline atom, which carries the marks the bar applies', () => {+ const editor = makeRichEditor('A [[page]] word.');+ selectNode(editor, 'wikiLink');+ expect(shouldShowBubbleMenu({ editor })).toBe(true);+ });++ test('does NOT open over a mermaid diagram', () => {+ const editor = makeRichEditor('```mermaid\ngraph TD;\n A-->B;\n```');+ selectNode(editor, 'jsxComponent');+ expect(shouldShowBubbleMenu({ editor })).toBe(false);+ });++ test('does NOT open over a math block', () => {+ const editor = makeRichEditor('$$\nx = 1\n$$');+ selectNode(editor, 'jsxComponent');+ expect(shouldShowBubbleMenu({ editor })).toBe(false);+ });++ test('still opens over an ordinary text selection', () => {+ const editor = makeRichEditor('Just ordinary words.');+ editor.view.dispatch(+ editor.state.tr.setSelection(TextSelection.create(editor.state.doc, 1, 5)),+ );+ expect(shouldShowBubbleMenu({ editor })).toBe(true);+ });+});diff --git a/packages/app/src/editor/bubble-menu/bubble-menu-state.ts b/packages/app/src/editor/bubble-menu/bubble-menu-state.tsindex 0a2bba377..5524ec868 100644--- a/packages/app/src/editor/bubble-menu/bubble-menu-state.ts+++ b/packages/app/src/editor/bubble-menu/bubble-menu-state.ts@@ -7,6 +7,7 @@* (BubbleMenuBar → LinkEditPopover → this module).*/+import { commentQuoteText } from '@inkeep/open-knowledge-core';import type { Editor } from '@tiptap/react';import { findMarkIdAt } from '../extensions/mark-identity';import { getFindReplaceState } from '../find-replace/tiptap-find-replace-extension';@@ -24,7 +25,17 @@ export function shouldShowBubbleMenu({ editor }: { editor: Editor }): boolean {if (isFileNodeSelected(editor)) return true;if (editor.state.selection.empty) return false;const { from, to } = editor.state.selection;- const text = editor.state.doc.textBetween(from, to, ' ');+ // `commentQuoteText` so a selection whose only content is an INLINE atom — a+ // wiki link, a tag, inline math, a footnote marker — counts as text-bearing.+ // Those nodes report no text of their own, so the bar (and with it the "Ask+ // AI" entry) never appeared for them, and every mark this bar applies is one+ // they legally carry.+ //+ // `inlineOnly` because a BLOCK that holds its text in attributes — a mermaid+ // diagram, a math block — must NOT open this bar. Bold, links, and+ // superscript have nothing to say about a diagram; those blocks carry their+ // own chrome bar with an Ask AI button, the way a code block does.+ const text = commentQuoteText(editor.state.doc, from, to, ' ', { inlineOnly: true });if (!text.trim()) return false;return true;}diff --git a/packages/app/src/editor/extensions/JsxComponentView.ask-ai.dom.test.tsx b/packages/app/src/editor/extensions/JsxComponentView.ask-ai.dom.test.tsxnew file mode 100644index 000000000..6d9ce634e--- /dev/null+++ b/packages/app/src/editor/extensions/JsxComponentView.ask-ai.dom.test.tsx@@ -0,0 +1,166 @@+/**+ * The Ask AI button on a childless component's chrome bar.+ *+ * Sister to `CodeBlockView.dom.test.tsx`'s pinned click-to-compose test, and+ * load-bearing for the same reason: this button is the ONLY route to commenting+ * on a mermaid diagram, a math block, an image or an attached file. None of+ * them has text to select, so the selection toolbar is not a way in — and for+ * an image the toolbar does open, on its alignment branch, which carries no+ * comment entry at all.+ */++import { cleanup, fireEvent, render, waitFor } from '@testing-library/react';+import type { NodeViewProps } from '@tiptap/react';+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';++const nodeSelections: number[] = [];+let starts = 0;+let embedded = false;++vi.mock('@/hooks/use-is-embedded', () => ({+ useIsEmbedded: () => embedded,+}));++vi.mock('@/comments/store', () => ({+ emitStartComment: () => {+ starts += 1;+ },+ subscribeStartComment: () => () => {},+}));++beforeEach(() => {+ nodeSelections.length = 0;+ starts = 0;+ embedded = false;+ vi.restoreAllMocks();+});++// Each case renders its own view and then asks the DOM whether the button is+// there; without this an earlier render's button answers for a later case.+afterEach(() => {+ cleanup();+});++interface Overrides {+ /** How a stale position fails: `setNodeSelection` clamps, so this is real. */+ setNodeSelectionThrows?: 'type-error';+ /** Non-zero makes the component one WITH children. */+ contentSize?: number;+ /** A slash-inserted component: no source captured, only default props. */+ freshlyInserted?: boolean;+ /** A NodeView whose position cannot be resolved hands `getPos` as undefined. */+ noPos?: boolean;+}++function makeProps({+ setNodeSelectionThrows,+ contentSize = 0,+ noPos = false,+ freshlyInserted = false,+}: Overrides = {}) {+ return {+ editor: {+ isEditable: true,+ isDestroyed: false,+ commands: {+ setNodeSelection: (p: number) => {+ if (setNodeSelectionThrows === 'type-error') {+ // What ProseMirror actually raises for a position that clamped into+ // bounds but has no node after it — see the handler's comment.+ throw new TypeError("Cannot read properties of null (reading 'nodeSize')");+ }+ nodeSelections.push(p);+ },+ },+ state: { doc: { resolve: () => ({ depth: 0 }) }, selection: { from: 0, to: 0 } },+ on: () => {},+ off: () => {},+ },+ node: {+ type: { name: 'jsxComponent' },+ attrs: {+ componentName: 'MermaidFence',+ kind: 'element',+ attributes: [{ type: 'mdxJsxAttribute', name: 'chart', value: 'graph TD;' }],+ props: freshlyInserted ? { chart: '' } : { chart: 'graph TD;' },+ // `createChildNode` captures no source for a slash insertion.+ sourceRaw: freshlyInserted ? '' : '```mermaid\ngraph TD;\n```',+ sourceDirty: freshlyInserted,+ },+ content: { size: contentSize },+ nodeSize: 2,+ },+ getPos: noPos ? undefined : () => 5,+ selected: true,+ updateAttributes: () => {},+ deleteNode: () => {},+ decorations: [],+ extension: { options: {} },+ view: {},+ } as unknown as NodeViewProps;+}++async function mount(props: NodeViewProps): Promise<HTMLButtonElement | null> {+ const { JsxComponentView } = await import('./JsxComponentView.tsx');+ render(<JsxComponentView {...props} />);+ return document.querySelector<HTMLButtonElement>('[data-testid="jsx-component-ask-ai-btn"]');+}++describe('the chrome bar Ask AI button', () => {+ test('selects the block and opens the composer', async () => {+ const btn = await mount(makeProps());+ expect(btn).toBeTruthy();+ fireEvent.click(btn as HTMLButtonElement);++ await waitFor(() => expect(starts).toBe(1));+ // The block itself is the subject — there is nothing inside to prefer.+ expect(nodeSelections).toEqual([5]);+ });++ test('is absent on a component that HAS children', async () => {+ // Its text is real content, so the selection toolbar already reaches it and+ // a second entry point here would be a second door to one room.+ expect(await mount(makeProps({ contentSize: 4 }))).toBeNull();+ });++ test('is absent without a resolvable position', async () => {+ expect(await mount(makeProps({ noPos: true }))).toBeNull();+ });++ /**+ * A slash-inserted component has no source captured and nothing but default+ * props, so there is no text to quote until the author types into it. The+ * button is gated on the same `commentLeafText` the composer reads, so it+ * cannot offer an action that would silently decline.+ */+ test('is absent while there is nothing yet to quote', async () => {+ expect(await mount(makeProps({ freshlyInserted: true }))).toBeNull();+ });++ test('is absent inside a host agent, where the comment queue is unreachable', async () => {+ // Same gate the code block's Ask AI button applies.+ embedded = true;+ expect(await mount(makeProps())).toBeNull();+ });++ /**+ * The failure this catch exists for. `setNodeSelection` clamps its argument,+ * so a stale position does not raise `RangeError` — it lands in bounds and+ * fails inside `NodeSelection.create` as a `TypeError`. Narrowing the catch to+ * `RangeError`, as the delete and move handlers correctly do for their own+ * `doc.resolve` calls, would re-throw this into `ComponentErrorBoundary` and+ * convert the block to a stuck placeholder — losing the rendered component+ * because a button missed.+ */+ test('a stale position is logged, not thrown, and opens no composer', async () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});+ const btn = await mount(makeProps({ setNodeSelectionThrows: 'type-error' }));+ expect(btn).toBeTruthy();++ expect(() => fireEvent.click(btn as HTMLButtonElement)).not.toThrow();+ expect(warn).toHaveBeenCalledTimes(1);+ expect(String(warn.mock.calls[0]?.[0])).toContain('jsx-component-chrome-ask-ai-failed');+ expect(nodeSelections).toEqual([]);+ expect(starts).toBe(0);+ });+});diff --git a/packages/app/src/editor/extensions/JsxComponentView.tsx b/packages/app/src/editor/extensions/JsxComponentView.tsxindex cde4675d2..3cf4b3568 100644--- a/packages/app/src/editor/extensions/JsxComponentView.tsx+++ b/packages/app/src/editor/extensions/JsxComponentView.tsx@@ -35,6 +35,7 @@*/import {+ commentLeafText,incrementJsxAutoConvertFailed,incrementJsxAutoConvertSucceeded,incrementJsxKeyboardDeleteFailed,@@ -48,11 +49,21 @@ import { Trans, useLingui } from '@lingui/react/macro';import type { NodeViewProps } from '@tiptap/core';import { TextSelection } from '@tiptap/pm/state';import { NodeViewContent, NodeViewWrapper } from '@tiptap/react';-import { ArrowDown, ArrowUp, ExternalLink, Pencil, Settings2, Trash2 } from 'lucide-react';+import {+ ArrowDown,+ ArrowUp,+ ExternalLink,+ Pencil,+ Settings2,+ Sparkles,+ Trash2,+} from 'lucide-react';import type { ReactNode } from 'react';import { useEffect, useRef, useState } from 'react';import { ErrorBoundary, type FallbackProps } from 'react-error-boundary';+import { emitStartComment } from '@/comments/store';import { Button } from '@/components/ui/button';+import { useIsEmbedded } from '@/hooks/use-is-embedded';import { hashFromDocName } from '@/lib/doc-hash';import {Popover,@@ -253,6 +264,7 @@ export function JsxComponentView({ node, editor, extension, getPos, selected }:const wasSelected = useRef(false);const pos = typeof getPos === 'function' ? getPos() : undefined;+ const isEmbedded = useIsEmbedded();let isChildOfComponent = false;let siblingIndex = 0;@@ -1211,6 +1223,90 @@ export function JsxComponentView({ node, editor, extension, getPos, selected }:</button>) : null}+ {/* Ask AI — the same composer the text bubble menu's Ask AI opens,+ reached the way a code block reaches it: from the block's own+ chrome. A component that keeps its content in attributes rather+ than as children (a mermaid diagram, a math block) has no text to+ select, so the selection toolbar is not a route to it — and it+ would be the wrong route anyway, since bold and links have nothing+ to say about a diagram.++ Images and embeds included. They DO raise the bubble menu on+ selection, but it opens on its image branch, which carries the+ alignment buttons and nothing else — so an image was the one block+ with a selection toolbar and still no way to comment on it.++ Gated on there actually BEING a quote — `commentLeafText` is the+ same function the composer reads, so the button cannot appear in a+ state where pressing it does nothing. That is not hypothetical: a+ slash-inserted component arrives with `sourceRaw: ''` and nothing+ but default props, so a freshly added Math block has no formula to+ quote until it is typed into. It also subsumes the childless+ check — a component with children returns `''` here and keeps the+ selection toolbar as its route.++ Hidden inside a host agent for the same reason the code block's is:+ the comment queue is not reachable there. */}+ {!isEmbedded && commentLeafText(node).length > 0 && typeof pos === 'number' ? (+ <button+ type="button"+ className="jsx-chrome-btn"+ aria-label={t`Comment or ask AI about this ${descriptor.displayName ?? descriptor.name}`}+ data-testid="jsx-component-ask-ai-btn"+ onClick={() => {+ // The block itself is the subject — there is no selection+ // inside a childless component to prefer over it.+ try {+ editor.commands.setNodeSelection(pos);+ } catch (err) {+ // Deliberately NOT the delete/move handlers' `RangeError`+ // narrowing. Those call `doc.resolve` / `doc.slice`, which do+ // throw `RangeError` on an out-of-bounds position. This one+ // does not reach either: `setNodeSelection` clamps through+ // `minMax` first, so a stale `pos` lands in bounds and fails+ // later inside `NodeSelection.create`, as a `TypeError` off+ // `$pos.nodeAfter`. Narrowing here would re-throw the very+ // race the catch is for, into `ComponentErrorBoundary`, which+ // converts the block to `rawMdxFallback` — the reader loses+ // the rendered component because a button missed.+ console.warn(+ JSON.stringify({+ event: 'jsx-component-chrome-ask-ai-failed',+ component: descriptor.name,+ rawComponentName: String(node.attrs.componentName ?? '').slice(0, 200),+ reason: err instanceof Error ? err.message.slice(0, 500) : String(err),+ }),+ );+ return;+ }+ // After the selection transaction has landed, so the composer+ // captures the range this click just established.+ requestAnimationFrame(() => {+ // Its own catch: this runs a frame later, outside the one+ // above, and `emitStartComment` dispatches synchronously — so+ // a listener reading `editor.state` on an editor destroyed+ // between the two frames throws here. Uncaught inside a rAF+ // that reaches `window.onerror` with no component and no+ // event key, which is not enough to triage from.+ try {+ emitStartComment();+ } catch (err) {+ console.warn(+ JSON.stringify({+ event: 'jsx-component-chrome-ask-ai-emit-failed',+ component: descriptor.name,+ rawComponentName: String(node.attrs.componentName ?? '').slice(0, 200),+ reason: err instanceof Error ? err.message.slice(0, 500) : String(err),+ }),+ );+ }+ });+ }}+ >+ <Sparkles size={12} aria-hidden="true" />+ </button>+ ) : null}+{/* Delete — positioned between move arrows and settings so thesettings gear stays anchored at the right edge of the chrome bar(consistent "destructive action mid, config action far-right"diff --git a/packages/app/src/locales/en/messages.json b/packages/app/src/locales/en/messages.jsonindex 1e5e5d535..1ac6fd9be 100644--- a/packages/app/src/locales/en/messages.json+++ b/packages/app/src/locales/en/messages.json@@ -1176,6 +1176,7 @@"IFezUv": ["The audit could not be completed. Try again."],"IInnZo": ["Conflict"],"IKoTAC": ["Keep disabled"],+ "INp_z6": ["Comment or ask AI about this ", ["0"]],"IO8REi": ["Couldn't open \"", ["name"], "\": ", ["0"]],"IQPyrD": ["Missing page — click to create"],"IQgeSn": ["Failed to save the schema field"],diff --git a/packages/app/src/locales/en/messages.po b/packages/app/src/locales/en/messages.poindex 4c5d05d25..802832df7 100644--- a/packages/app/src/locales/en/messages.po+++ b/packages/app/src/locales/en/messages.po@@ -2381,6 +2381,11 @@ msgstr "Comment on {address}"msgid "Comment on selection"msgstr "Comment on selection"+#. placeholder {0}: descriptor.displayName ?? descriptor.name+#: src/editor/extensions/JsxComponentView.tsx+msgid "Comment or ask AI about this {0}"+msgstr "Comment or ask AI about this {0}"+#: src/editor/extensions/CodeBlockView.tsxmsgid "Comment or ask AI about this code block"msgstr "Comment or ask AI about this code block"diff --git a/packages/app/src/locales/pseudo/messages.json b/packages/app/src/locales/pseudo/messages.jsonindex 95541e1f5..d710eb134 100644--- a/packages/app/src/locales/pseudo/messages.json+++ b/packages/app/src/locales/pseudo/messages.json@@ -1176,6 +1176,7 @@"IFezUv": ["Ţĥē àũďĩţ ćōũĺď ńōţ ƀē ćōḿƥĺēţēď. Ţŕŷ àĝàĩń."],"IInnZo": ["Ćōńƒĺĩćţ"],"IKoTAC": ["Ķēēƥ ďĩśàƀĺēď"],+ "INp_z6": ["Ćōḿḿēńţ ōŕ àśķ ÀĨ àƀōũţ ţĥĩś ", ["0"]],"IO8REi": ["Ćōũĺďń'ţ ōƥēń \"", ["name"], "\": ", ["0"]],"IQPyrD": ["Ḿĩśśĩńĝ ƥàĝē — ćĺĩćķ ţō ćŕēàţē"],"IQgeSn": ["Ƒàĩĺēď ţō śàvē ţĥē śćĥēḿà ƒĩēĺď"],diff --git a/packages/app/src/locales/pseudo/messages.po b/packages/app/src/locales/pseudo/messages.poindex 6a948b889..00eb49d23 100644--- a/packages/app/src/locales/pseudo/messages.po+++ b/packages/app/src/locales/pseudo/messages.po@@ -2377,6 +2377,11 @@ msgstr ""msgid "Comment on selection"msgstr ""+#. placeholder {0}: descriptor.displayName ?? descriptor.name+#: src/editor/extensions/JsxComponentView.tsx+msgid "Comment or ask AI about this {0}"+msgstr ""+#: src/editor/extensions/CodeBlockView.tsxmsgid "Comment or ask AI about this code block"msgstr ""diff --git a/packages/core/src/bridge/bind-frontmatter-doc.ts b/packages/core/src/bridge/bind-frontmatter-doc.tsindex d7a845280..dfd95ef49 100644--- a/packages/core/src/bridge/bind-frontmatter-doc.ts+++ b/packages/core/src/bridge/bind-frontmatter-doc.ts@@ -1,3 +1,4 @@+import type * as Y from 'yjs';import type { Err, Ok, Result } from '../config/result.ts';import { type FrontmatterValidationError, toFrontmatterIssue } from '../frontmatter/errors.ts';diff --git a/packages/core/src/bridge/bridge-invariant.ts b/packages/core/src/bridge/bridge-invariant.tsindex 005627195..8bfdb40cd 100644--- a/packages/core/src/bridge/bridge-invariant.ts+++ b/packages/core/src/bridge/bridge-invariant.ts@@ -1,3 +1,4 @@+import { fnv1aDigest } from './hash-util.ts';export type BridgeInvariantSite = 'observer-b' | 'persistence' | 'test-harness';diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.tsindex f4cc8a286..2a597ec3d 100644--- a/packages/core/src/bridge/compose.ts+++ b/packages/core/src/bridge/compose.ts@@ -1,3 +1,4 @@+import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.tsindex 8fb428b90..01ef76a9f 100644--- a/packages/core/src/bridge/doc-boundary-space.ts+++ b/packages/core/src/bridge/doc-boundary-space.ts@@ -1,3 +1,4 @@+import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';diff --git a/packages/core/src/bridge/growth-detect.ts b/packages/core/src/bridge/growth-detect.tsindex 9d9c244b0..463e89595 100644--- a/packages/core/src/bridge/growth-detect.ts+++ b/packages/core/src/bridge/growth-detect.ts@@ -1,3 +1,4 @@+const DEFAULT_MIN_SUBSTANTIVE_LINE_LENGTH = 16;export const DUPLICATION_GATE_MIN_LINE_LENGTH = 8;diff --git a/packages/core/src/bridge/intraline-predicate.ts b/packages/core/src/bridge/intraline-predicate.tsindex c43fdd7e7..0939885bb 100644--- a/packages/core/src/bridge/intraline-predicate.ts+++ b/packages/core/src/bridge/intraline-predicate.ts@@ -1,3 +1,4 @@+export function rawLineCounts(doc: string): Map<string, number> {const counts = new Map<string, number>();for (const line of doc.split('\n')) {diff --git a/packages/core/src/bridge/merge-three-way.ts b/packages/core/src/bridge/merge-three-way.tsindex 251560a35..c0dc65705 100644--- a/packages/core/src/bridge/merge-three-way.ts+++ b/packages/core/src/bridge/merge-three-way.ts@@ -65,6 +65,7 @@ export function tryLineLevelCombine(return { clean: true, merged };}+export type BridgeMergeContentLossSide = 'user' | 'agent';export type BridgeMergeContentLossWhich = 'substring' | 'order' | 'growth';diff --git a/packages/core/src/bridge/normalize.ts b/packages/core/src/bridge/normalize.tsindex 01ac80099..dcc5b0690 100644--- a/packages/core/src/bridge/normalize.ts+++ b/packages/core/src/bridge/normalize.ts@@ -1,3 +1,4 @@+import { stripFrontmatter } from '../extensions/frontmatter.ts';import { bodyEdgeEmpties } from './doc-boundary-space.ts';diff --git a/packages/core/src/bridge/pm-structural-equivalence.ts b/packages/core/src/bridge/pm-structural-equivalence.tsindex 754dc5cc2..0be5203e4 100644--- a/packages/core/src/bridge/pm-structural-equivalence.ts+++ b/packages/core/src/bridge/pm-structural-equivalence.ts@@ -32,6 +32,7 @@ const BR_SENTINEL: PmStructuralNode = { type: '__br__' };const BR_LITERAL_RE = /^<br\s*\/?>$/i;+interface DegradeEntry {readonly label: string;readonly severity: ToleranceClassSeverity;@@ -127,6 +128,7 @@ export interface ComparePmStructuralOptions {ignoreAttrs?: (attrKey: string) => boolean;}+/** Rebuild a node with `fn` applied to each child; identity when childless.* Shared by the degrade canonicalizers so none re-implements the walk. */function mapChildren(@@ -248,6 +250,7 @@ function applyDegrades(tree: PmStructuralNode): {return { normalized: current, fired };}+export function comparePmStructural(expected: PmStructuralNode,actual: PmStructuralNode,diff --git a/packages/core/src/bridge/subsequence.ts b/packages/core/src/bridge/subsequence.tsindex af20de1e2..dca644141 100644--- a/packages/core/src/bridge/subsequence.ts+++ b/packages/core/src/bridge/subsequence.ts@@ -1,3 +1,4 @@+/** True when `needle` is a subsequence of `haystack` (two-pointer, O(n+m)):* insertions in `haystack` are free; any dropped or substituted `needle` byte* fails. */diff --git a/packages/core/src/comments/leaf-text.test.ts b/packages/core/src/comments/leaf-text.test.tsnew file mode 100644index 000000000..51afe4b6f--- /dev/null+++ b/packages/core/src/comments/leaf-text.test.ts@@ -0,0 +1,294 @@+/**+ * Quote text for nodes that keep their words in attributes.+ *+ * The cases below all produced either an empty quote (so the composer declined+ * to open and the construct could not be commented on at all) or a quote with a+ * hole in it (so a comment on the surrounding prose failed to anchor).+ */++import { getSchema } from '@tiptap/core';+import { describe, expect, test } from 'vitest';+import { sharedExtensions } from '../extensions/shared.ts';+import { MarkdownManager } from '../markdown/index.ts';+import { commentQuoteText } from './leaf-text.ts';+import { findPassage } from './passage-match.ts';++const mdManager = new MarkdownManager({ extensions: sharedExtensions });+const schema = getSchema(sharedExtensions);++/** The quote a select-all in this document would capture. */+function quote(md: string): string {+ const doc = schema.nodeFromJSON(mdManager.parse(md));+ return commentQuoteText(doc, 0, doc.content.size).trim();+}++/**+ * The slice of `md` the captured quote resolves to — the span, not merely the+ * fact that one exists. A quote that anchors somewhere is not the claim; a+ * quote that anchors over the construct it came from is.+ */+function locate(md: string): string | null {+ const captured = quote(md);+ if (captured.length === 0) return null;+ const hit = findPassage(md, captured, { syntaxIn: 'haystack' });+ return hit ? md.slice(hit.start, hit.end) : null;+}++describe('text an inline atom contributes', () => {+ test('a wiki link reads as its target', () => {+ expect(quote('A [[page]] word.')).toBe('A page word.');+ });++ test('an aliased wiki link reads as the alias, which is what is on screen', () => {+ expect(quote('A [[page|Nice Name]] word.')).toBe('A Nice Name word.');+ });++ test('a tag reads with its `#`, which is on screen and part of the word', () => {+ expect(quote('A #tagname word.')).toBe('A #tagname word.');+ });++ test('inline math reads as its formula', () => {+ expect(quote('A $$x^2$$ word.')).toBe('A x^2 word.');+ });++ test('an image reads as its alt text', () => {+ expect(quote('A  word.')).toBe('A alt word.');+ });++ test('an atom contributes no line break to the paragraph it sits in', () => {+ // It is inline, so a separator here would split a sentence mid-way and the+ // quote would read as two lines in the comments panel.+ expect(quote('Start [[page]] end.')).toBe('Start page end.');+ });+});++describe('text a promoted fence contributes', () => {+ /**+ * A ```` ```mermaid ```` fence is rewritten to a childless `jsxComponent`+ * carrying the diagram in `props`, so it is not a leaf and ProseMirror's+ * `leafText` hook never fires for it — the reason `textBetween` alone could+ * not see it.+ */+ test('a mermaid fence reads as its chart', () => {+ expect(quote('```mermaid\ngraph TD;\n A-->B;\n```')).toBe('graph TD;\n A-->B;');+ });++ test('a math block reads as its formula', () => {+ expect(quote('$$\nx = 1\n$$')).toBe('x = 1');+ });++ test('a promoted construct with no named prop falls back to its source', () => {+ expect(quote('{/* mdx expr */}')).toBe('{/* mdx expr */}');+ });++ test('a component WITH children still reads as its children', () => {+ expect(quote('<Callout type="info">\n Body text\n</Callout>')).toBe('Body text');+ });++ test('a fence separates from the prose around it', () => {+ expect(quote('Before.\n\n```mermaid\ngraph TD;\n```\n\nAfter.')).toBe(+ 'Before.\ngraph TD;\nAfter.',+ );+ });+});++describe('parity with ProseMirror textBetween', () => {+ test('paragraphs are still separated', () => {+ expect(quote('First para.\n\nSecond para.')).toBe('First para.\nSecond para.');+ });++ test('list items are still separated', () => {+ expect(quote('- First item\n- Second item')).toBe('First item\nSecond item');+ });++ test('table cells are still separated', () => {+ expect(quote('| A | B |\n| --- | --- |\n| one | two |')).toBe('A\nB\none\ntwo');+ });++ test('a code fence still reads as its code', () => {+ expect(quote('```ts\nconst x = 1;\n```')).toBe('const x = 1;');+ });+});++describe('the captured quote anchors back to the body', () => {+ // The two halves have to agree: a quote that reads well but cannot be found+ // is still a comment the server refuses to store.+ const CASES = [+ 'A ==marked== word.',+ 'A  word.',+ 'A [[page]] word.',+ 'A [[page|Alias]] word.',+ 'A [[page#sec]] word.',+ 'A #tagname word.',+ 'A $$x^2$$ word.',+ 'A claim[^1] word.',+ 'A <u>under</u> word.',+ 'A <http://x.com> word.',+ '```mermaid\ngraph TD;\n A-->B;\n```',+ 'Before.\n\n```mermaid\ngraph TD;\n```\n\nAfter.',+ 'Before.\n\n$$\nx = 1\n$$\n\nAfter.',+ 'See [[page]] and #tag with ==mark== plus $$y$$ and .',+ ];++ for (const md of CASES) {+ test(`anchors: ${JSON.stringify(md.slice(0, 44))}`, () => {+ expect(locate(md)).not.toBeNull();+ });+ }+});++describe('inlineOnly — for callers deciding whether text formatting applies', () => {+ function inlineQuote(md: string): string {+ const doc = schema.nodeFromJSON(mdManager.parse(md));+ return commentQuoteText(doc, 0, doc.content.size, ' ', { inlineOnly: true }).trim();+ }++ test('an inline atom still counts — it carries the marks a bar would apply', () => {+ expect(inlineQuote('A [[page]] word.')).toBe('A page word.');+ });++ test('a mermaid diagram does not count', () => {+ expect(inlineQuote('```mermaid\ngraph TD;\n```')).toBe('');+ });++ test('a math block does not count', () => {+ expect(inlineQuote('$$\nx = 1\n$$')).toBe('');+ });++ test('real block text is unaffected', () => {+ expect(inlineQuote('Just ordinary words.')).toBe('Just ordinary words.');+ });+});++describe('images and embeds', () => {+ test('an image reads as its alt text, not its markup', () => {+ expect(quote('')).toBe('a cat asleep');+ });++ test('an image with no alt falls back to its source', () => {+ expect(quote('')).toBe('');+ });++ test('an embed falls back to its source', () => {+ expect(quote('<Embed src="https://example.com/a" />')).toBe(+ '<Embed src="https://example.com/a" />',+ );+ });++ /**+ * The span covers the READABLE words, not the delimiters around them: a match+ * never opens on a syntax character, so an image quoted by its alt text+ * anchors over the alt text. An image with no alt has only its source to+ * quote, and that source opens with `!`, which the caller did select — so+ * there the span is the whole construct.+ */+ test('each resolves to the span its quote names', () => {+ expect(locate('')).toBe('a cat asleep');+ expect(locate('')).toBe('');+ expect(locate('<Embed src="https://x.co" />')).toBe('<Embed src="https://x.co" />');+ });+});++/**+ * Attached files.+ *+ * These reach the editor as childless components too, so they share the+ * diagram's shape — but a wiki file embed carries an EMPTY `sourceRaw`, which+ * made it the case that proved the fallback is not a floor: it quoted as+ * nothing at all, and the Ask AI button on its chrome opened a composer that+ * immediately declined.+ */+describe('attached files', () => {+ test('a file reads as its title when it has one', () => {+ expect(quote('<File src="docs/spec.pdf" name="The Spec" />')).toBe('The Spec');+ });++ test('a file with no title falls back to its source', () => {+ expect(quote('<File src="docs/spec.pdf" />')).toBe('<File src="docs/spec.pdf" />');+ });++ test('a wiki file embed reads as its target, having no source to fall back on', () => {+ expect(quote('![[report.pdf]]')).toBe('report.pdf');+ });++ test('a video falls back to its source', () => {+ expect(quote('<video src="clip.mp4" />')).toBe('<video src="clip.mp4" />');+ });++ test('each resolves to the span its quote names', () => {+ // Titled by a prop — the span is the title inside the markup.+ expect(locate('<File src="docs/spec.pdf" name="The Spec" />')).toBe('The Spec');+ expect(locate('![[report.pdf]]')).toBe('report.pdf');+ // Nothing readable but the source, so the span is the whole construct.+ expect(locate('<File src="docs/spec.pdf" />')).toBe('<File src="docs/spec.pdf" />');+ expect(locate('<video src="clip.mp4" />')).toBe('<video src="clip.mp4" />');+ });+});++/**+ * Block separation, including the empty textblock.+ *+ * A textblock opens a new line whether or not it turned out to hold anything —+ * ProseMirror's own rule, mirrored so a quote reads in the panel exactly as the+ * passage read on screen. Without a fixture that has an empty paragraph in the+ * middle, the `isTextblock` half of that condition is never what decides, and+ * dropping it would leave every other test green.+ */+describe('block separation', () => {+ // Built from JSON, not markdown: no markdown input produces an empty+ // paragraph, so the only way to put the `isTextblock` half of the rule under+ // test is to construct the document that has one.+ function para(text?: string) {+ return text === undefined+ ? { type: 'paragraph' }+ : { type: 'paragraph', content: [{ type: 'text', text }] };+ }++ test('an empty paragraph consumes a separator, as it does in textBetween', () => {+ const doc = schema.nodeFromJSON({+ type: 'doc',+ content: [para('First.'), para(), para('Second.')],+ });+ // Three textblocks, two separators — the empty one earns its own even+ // though it contributes no text. Drop `isTextblock` from the rule and this+ // collapses to a single break.+ expect(commentQuoteText(doc, 0, doc.content.size)).toBe('First.\n\nSecond.');+ });++ test('two paragraphs are separated by exactly one break', () => {+ expect(quote('First.\n\nSecond.')).toBe('First.\nSecond.');+ });+});++/**+ * Inline components.+ *+ * A registered inline descriptor destructures its paired body into+ * `props.children` and leaves the node childless, so it read as empty exactly+ * the way a diagram did — an inline `<Callout>` mid-sentence left a hole in the+ * quote and the comment on that sentence could not anchor. An UNREGISTERED tag+ * keeps its raw source as real text children, so it was never affected.+ */+describe('inline components', () => {+ test('a registered inline component reads as its body', () => {+ expect(quote('Read <Callout type="note">body text</Callout> here.')).toBe(+ 'Read body text here.',+ );+ });++ test('and that quote anchors over the whole construct', () => {+ expect(locate('Read <Callout type="note">body text</Callout> here.')).toBe(+ 'Read <Callout type="note">body text</Callout> here.',+ );+ });++ test('an unregistered tag still reads as its source', () => {+ expect(quote('Read <Unregistered>raw</Unregistered> here.')).toBe(+ 'Read <Unregistered>raw</Unregistered> here.',+ );+ });++ test('a self-closing inline tag still reads as its source', () => {+ expect(quote('Read <Icon name="x" /> here.')).toBe('Read <Icon name="x" /> here.');+ });+});diff --git a/packages/core/src/comments/leaf-text.ts b/packages/core/src/comments/leaf-text.tsnew file mode 100644index 000000000..56cf9ce60--- /dev/null+++ b/packages/core/src/comments/leaf-text.ts@@ -0,0 +1,176 @@+/**+ * The text a comment quote is made of.+ *+ * ProseMirror's `textBetween` reads node CONTENT, and two shapes in this schema+ * keep their reader-visible text somewhere else:+ *+ * - Inline atoms — wiki link, tag, image, inline math, footnote reference —+ * hold it in attributes. They define `renderText`, but that is a TipTap+ * concept `textBetween` never consults, and it returns a position-faithful+ * placeholder rather than anything readable.+ * - Promoted fences — ` ```mermaid `, ` ```math `, `$$…$$`, and the other+ * constructs `mermaid-promoter.ts` and its siblings rewrite into an+ * `mdxJsxFlowElement` — become a CHILDLESS `jsxComponent` carrying the+ * source in `props`. Those are not leaves (`content: 'block*'`), so+ * `textBetween`'s `leafText` hook does not fire for them at all.+ *+ * Both read as the empty string, which broke commenting twice over: selecting+ * one produced no quote and the composer declined to open, and selecting PROSE+ * AROUND one produced a quote with a hole in it that the anchor resolver could+ * not find in the markdown. The second is the costly half — in a wiki-style+ * document a paragraph containing a wiki link or a tag is the common case, not+ * the exotic one.+ *+ * The text each node yields is what a reader sees in its place, so a quote+ * reads as the passage did on screen. That is also what makes it resolvable:+ * the same characters sit in the markdown source, surrounded by syntax+ * `passage-match.ts` treats as elastic.+ *+ * Deliberately NOT `leafText` on the node specs. That would change+ * `textBetween` for every caller — clipboard, `getText`, the input-rule range+ * arithmetic `renderInlineObjectText` exists to keep faithful — and would still+ * miss the promoted fences, which are not leaves. Comments are the one caller+ * that wants readable text, so they ask for it explicitly and nothing else+ * moves.+ */++import type { Node as PMNode } from '@tiptap/pm/model';++function attr(node: PMNode, name: string): string {+ const value = node.attrs[name];+ return typeof value === 'string' ? value : '';+}++function prop(node: PMNode, name: string): string {+ const props = node.attrs.props;+ if (props === null || typeof props !== 'object') return '';+ const value = (props as Record<string, unknown>)[name];+ return typeof value === 'string' ? value : '';+}++/**+ * A childless `jsxComponent`'s source text.+ *+ * `children` first — a registered component's paired body, which is the text a+ * reader sees inside it. Then the named props, in the order a reader would name+ * the thing: a mermaid+ * diagram or a math block quotes as the thing itself (`graph TD; …`) rather+ * than as a fence with its delimiters, an image as its alt text, an attached+ * file as its title or its path.+ *+ * `sourceRaw` last, covering everything else — an image with no alt, MDX+ * expressions, components this schema has never heard of. It is a fallback and+ * not a floor: a wiki file embed (`![[report.pdf]]`) carries an EMPTY one, so+ * without `target` in the chain it quoted as nothing at all and its Ask AI+ * button opened a composer that immediately declined. Any construct whose+ * readable text lives in a prop belongs in this list.+ *+ * A component WITH children (a `<Callout>`) never reaches here: its text is+ * real content and the ordinary walk already collects it.+ */+const TEXT_BEARING_PROPS = [+ 'children',+ 'chart',+ 'formula',+ 'alt',+ 'name',+ 'alias',+ 'target',+] as const;++function componentText(node: PMNode): string {+ for (const name of TEXT_BEARING_PROPS) {+ const value = prop(node, name);+ if (value.length > 0) return value;+ }+ return attr(node, 'sourceRaw');+}++/** The text `node` contributes to a quote, or `''` when it contributes none. */+export function commentLeafText(node: PMNode): string {+ switch (node.type.name) {+ // The alias when there is one — `[[page|Nice Name]]` shows "Nice Name", and+ // quoting the target instead would name something the reader cannot see.+ case 'wikiLink':+ return attr(node, 'alias') || attr(node, 'target');+ case 'wikiLinkEmbed':+ return attr(node, 'target');+ // With the `#`, which IS on screen and is part of the word a reader would+ // say they selected. It also matches the source byte-for-byte, so tags need+ // no elastic-syntax rule at all.+ case 'tag':+ return `#${attr(node, 'value')}`;+ case 'mathInline':+ return attr(node, 'formula');+ case 'image':+ case 'imageReference':+ return attr(node, 'alt');+ // The label as authored (`[^note]` → "note"), falling back to the+ // identifier remark normalizes it to.+ case 'footnoteReference':+ return attr(node, 'label') || attr(node, 'identifier');+ // Both component nodes, block and inline, on the same rule: a registered+ // descriptor's paired body is destructured into `props.children` and the+ // node is left childless, so an INLINE `<Callout>body</Callout>` had the+ // very hole this exists to close. An unregistered tag keeps its raw source+ // as real text children and never reaches here.+ case 'jsxComponent':+ case 'jsxInline':+ return node.content.size === 0 ? componentText(node) : '';+ default:+ return '';+ }+}++/**+ * `doc.textBetween(from, to, blockSeparator)`, plus the text the two shapes+ * above keep in their attributes.+ *+ * Mirrors ProseMirror's own `textBetween` — including its rule that a block+ * separator is emitted before a block that actually contributes text, never+ * leading and never doubled — because a quote whose whitespace disagrees with+ * the editor's would still anchor (the matcher is elastic about whitespace) but+ * would READ wrong in the comments panel.+ */+export function commentQuoteText(+ doc: PMNode,+ from: number,+ to: number,+ blockSeparator = '\n',+ /**+ * `inlineOnly` ignores the text a BLOCK holds in its attributes — a mermaid+ * diagram's chart, a math block's formula. Callers deciding whether the text+ * FORMATTING bar applies want this: a diagram is not prose, it owns a chrome+ * bar of its own, and bold has nothing to say about it. Callers building a+ * quote want the default, which counts everything.+ */+ { inlineOnly = false }: { inlineOnly?: boolean } = {},+): string {+ let text = '';+ let first = true;+ doc.nodesBetween(+ from,+ to,+ (node, pos) => {+ const nodeText = node.isText+ ? (node.text ?? '').slice(Math.max(from, pos) - pos, to - pos)+ : inlineOnly && node.isBlock+ ? ''+ : commentLeafText(node);+ // A textblock opens a new line whether or not it turned out to hold+ // anything — matching ProseMirror, so an empty paragraph separates its+ // neighbours here exactly as it does there. A non-textblock block earns a+ // separator only by contributing text of its own (a mermaid fence). An+ // inline atom is not a block at all and must not break the line it was+ // typed into.+ if (node.isBlock && (node.isTextblock || nodeText.length > 0) && blockSeparator) {+ if (first) first = false;+ else text += blockSeparator;+ }+ text += nodeText;+ return true;+ },+ 0,+ );+ return text;+}diff --git a/packages/core/src/comments/passage-match.test.ts b/packages/core/src/comments/passage-match.test.tsindex 644b4683d..b3064ebea 100644--- a/packages/core/src/comments/passage-match.test.ts+++ b/packages/core/src/comments/passage-match.test.ts@@ -7,7 +7,7 @@*/import { describe, expect, test } from 'vitest';-import { findAllPassages, findPassage } from './passage-match.ts';+import { findAllPassages, findPassage, rewriteCeiling } from './passage-match.ts';const BODY = `## Ingredients@@ -330,3 +330,158 @@ Acceptance: the leaf validates.`;});});});++/**+ * Constructs whose markup renders as nothing, or as less than it spells.+ *+ * Each of these failed as a bare "the quoted passage is not in the document"+ * for any comment on a paragraph that merely CONTAINED one — which in a+ * wiki-style document is most paragraphs, since wiki links and tags are+ * everywhere. The quote is always what the editor renders; the body is what is+ * on disk.+ */+describe('markup that renders as less than it spells', () => {+ /** The passage the caller quoted, as it sits in the body. */+ function locate(body: string, quote: string): string | null {+ const hit = findPassage(body, quote, { syntaxIn: 'haystack' });+ return hit ? body.slice(hit.start, hit.end) : null;+ }++ test('a highlight, whose `==` delimiters render as nothing', () => {+ expect(locate('A ==marked== word.', 'A marked word.')).toBe('A ==marked== word.');+ });++ test('inline math, whose `$$` delimiters render as nothing', () => {+ expect(locate('A $$x^2$$ word.', 'A x^2 word.')).toBe('A $$x^2$$ word.');+ });++ test('an image, which renders as its alt text alone', () => {+ expect(locate('A  word.', 'A alt word.')).toBe('A  word.');+ });++ test('a reference-style image, which renders as its alt text alone', () => {+ expect(locate('A ![alt][ref] word.', 'A alt word.')).toBe('A ![alt][ref] word.');+ });++ test('a wiki link, which renders as its target', () => {+ expect(locate('A [[page]] word.', 'A page word.')).toBe('A [[page]] word.');+ });++ test('an aliased wiki link, which renders as the alias and hides the target', () => {+ expect(locate('A [[page|Alias]] word.', 'A Alias word.')).toBe('A [[page|Alias]] word.');+ });++ test('a wiki link with a heading fragment, which renders without it', () => {+ expect(locate('A [[page#sec]] word.', 'A page word.')).toBe('A [[page#sec]] word.');+ });++ test('a footnote reference, whose brackets render as nothing', () => {+ expect(locate('A claim[^1] word.', 'A claim1 word.')).toBe('A claim[^1] word.');+ });++ test('an inline HTML tag, which renders as its content alone', () => {+ expect(locate('A <u>under</u> word.', 'A under word.')).toBe('A <u>under</u> word.');+ });++ test('an autolink, which renders as the bare URL', () => {+ expect(locate('A <http://x.com> word.', 'A http://x.com word.')).toBe('A <http://x.com> word.');+ });++ test('a mermaid fence, whose delimiters render as nothing', () => {+ expect(+ locate('Before.\n\n```mermaid\ngraph TD;\n```\n\nAfter.', 'Before.\ngraph TD;\nAfter.'),+ ).toBe('Before.\n\n```mermaid\ngraph TD;\n```\n\nAfter.');+ });++ /**+ * `<` is elastic so an autolink can be crossed, but inline HTML that SURVIVES+ * into rendered text is quoted with its tags — and a match has to be allowed+ * to open on one. The start guard declines a syntax character only when the+ * caller's own quote does not begin with it.+ */+ test('opens a match on a `<` the caller actually quoted', () => {+ expect(locate('<div>\nbody\n</div>', '<div>\nbody\n</div>')).toBe('<div>\nbody\n</div>');+ });++ test('a bare `#` mid-sentence is still content, not a wiki-link fragment', () => {+ // Tags render WITH their `#`, so it must match literally rather than being+ // skipped — otherwise a quote could drift onto a different tag.+ expect(locate('A #alpha and #beta.', 'A #beta.')).toBeNull();+ });++ test('a line-leading `#` is still a heading marker', () => {+ expect(locate('## Heading here', 'Heading here')).toBe('Heading here');+ });++ test('a bare `!` is still content', () => {+ expect(locate('Wow! Amazing.', 'Wow Amazing.')).toBeNull();+ });++ /**+ * Sisters to the `!` rule above. None of these characters is markdown syntax+ * on its own — a highlight is always `==`, and `<` / `>` are invisible only+ * as the brackets of an autolink. Skipping a lone one would let a match cross+ * an operator the quote never contained.+ */+ test('a lone `=` is still content', () => {+ expect(locate('A = B', 'A B')).toBeNull();+ });++ test('a lone `>` is still content', () => {+ expect(locate('if x > y then', 'if x y then')).toBeNull();+ });++ test('a lone `<` is still content', () => {+ expect(locate('if x < y then', 'if x y then')).toBeNull();+ });++ test('an arrow is still content', () => {+ expect(locate('map a => b here', 'map a b here')).toBeNull();+ });++ test('a `>` that closes no autolink is still content', () => {+ expect(locate('read <docs> now', 'read docs now')).toBeNull();+ });++ test('an email autolink renders as the address', () => {+ expect(locate('Mail <[email protected]> now.', 'Mail [email protected] now.')).toBe('Mail <[email protected]> now.');+ });++ test('a line-leading `>` is still a blockquote marker', () => {+ expect(locate('> Quoted line.', 'Quoted line.')).toBe('Quoted line.');+ });+});++/**+ * The recovery ceiling, shared by both sides.+ *+ * It lived as two copies of two constants under a drift warning — the same+ * arrangement that had already let the context scorer diverge, fixed on one+ * side and silently stale on the other. These pin the policy itself so a change+ * to it is a deliberate edit to one visible contract.+ */+describe('rewriteCeiling', () => {+ test('allows a short passage to grow by the floor, not by the multiple', () => {+ // 4x of 5 is 20, which would refuse an ordinary edit to a short quote.+ expect(rewriteCeiling(5)).toBe(69);+ });++ test('allows a long passage to grow by the multiple', () => {+ expect(rewriteCeiling(100)).toBe(400);+ });++ test('never returns less than the passage itself', () => {+ for (const length of [0, 1, 21, 64, 500]) {+ expect(rewriteCeiling(length)).toBeGreaterThanOrEqual(length);+ }+ });++ test('grows monotonically', () => {+ let previous = -1;+ for (const length of [0, 10, 50, 100, 1000]) {+ const ceiling = rewriteCeiling(length);+ expect(ceiling).toBeGreaterThan(previous);+ previous = ceiling;+ }+ });+});diff --git a/packages/core/src/comments/passage-match.ts b/packages/core/src/comments/passage-match.tsindex 14de591a9..1641cb720 100644--- a/packages/core/src/comments/passage-match.ts+++ b/packages/core/src/comments/passage-match.ts@@ -32,8 +32,115 @@ export interface PassageMatchOptions {readonly syntaxIn: 'haystack' | 'needle';}-/** Emphasis, strong, code, strikethrough, escape — anywhere in a line. */-const INLINE_SYNTAX = new Set(['*', '_', '`', '~', '\\']);+/**+ * Emphasis, strong, code, strikethrough, escape, math — anywhere in a line.+ *+ * Only characters that are ALWAYS markup wherever they appear belong here, so+ * that skipping one can never cross something the quote actually contained.+ * `$` qualifies (`$$x^2$$` renders as its formula); `=`, `<` and `>` do not,+ * and each has its own narrower rule below — a lone `=` is prose (`A = B`), as+ * is a lone `<` or `>` (`if x > y`).+ */+const INLINE_SYNTAX = new Set(['*', '_', '`', '~', '\\', '$']);++/**+ * A highlight's delimiter: the `==` of `==marked==`.+ *+ * Two characters, never one. A lone `=` is ordinary prose (`A = B`, `=>`), and+ * making it skippable would let a match cross an equals sign the quote never+ * contained — the same hazard `IMAGE_BANG` below exists to avoid for `!`. A+ * setext underline is a whole line and is claimed earlier by+ * `INVISIBLE_RULE_LINE`, so it never reaches here.+ */+const HIGHLIGHT_DELIM = /^={2,}/;++/**+ * An autolink's brackets: the `<` and `>` of `<https://example.com>` or+ * `<[email protected]>`, which render as the bare address.+ *+ * Only the brackets are elastic — the address between them IS on screen and+ * must match. And only when they really do bracket an address: a bare `<` or+ * `>` is prose (`if x > y`), so neither is skippable on its own.+ */+const AUTOLINK = /^<(?:[A-Za-z][A-Za-z0-9+.-]*:[^<>\s]*|[^\s<>@]+@[^\s<>]+)>/;++/** Bounds the backward scan that pairs a `>` with its opening `<`. */+const AUTOLINK_WINDOW = 512;++/** The `<` at `i` opens an autolink, so the bracket alone is invisible. */+function opensAutolink(text: string, i: number): boolean {+ return AUTOLINK.test(text.slice(i, i + AUTOLINK_WINDOW));+}++/** The `>` at `i` closes an autolink opened within the scan window. */+function closesAutolink(text: string, i: number): boolean {+ const open = text.lastIndexOf('<', i);+ if (open < 0 || i - open > AUTOLINK_WINDOW) return false;+ return AUTOLINK.exec(text.slice(open, i + 1))?.[0].length === i + 1 - open;+}++/**+ * An HTML tag whose markup renders as nothing: `<u>`, `</u>`, `<Icon x="1" />`.+ *+ * Single-character elasticity cannot do this. `<u>under</u>` would skip the+ * `<`, then match the tag's own `u` against the content's `u` and desync from+ * there. The tag has to go as one run.+ *+ * The name is required to start with a letter, so `<https://example.com>` is+ * not a tag — its `:` ends the name and the pattern fails, leaving the autolink+ * to the single-character `<` / `>` rule above, which is what it needs.+ */+const HTML_TAG = /^<\/?[A-Za-z][A-Za-z0-9-]*(?:\s[^>]*?)?\/?>/;++/** Bounds the slice `HTML_TAG` runs on. */+const HTML_TAG_WINDOW = 512;++/**+ * The head of an image: the `!` of `` / `![alt][ref]`.+ *+ * Only ever elastic in front of a `[` — a bare `!` is ordinary prose and+ * skipping it would let a match drift across an exclamation mark.+ */+const IMAGE_BANG = /^!(?=\[)/;++/**+ * The head of a footnote reference: the `[^` of `[^1]`.+ *+ * `[` alone was already elastic, but the `^` behind it was read as content, so+ * a passage containing a footnote marker could not be matched.+ */+const FOOTNOTE_HEAD = /^\[\^/;++/**+ * The tail of a reference-style link or image: `][ref]` and `][]`.+ *+ * Sister to `LINK_TAIL` — a reference renders as its label alone, so the+ * bracketed reference behind it is invisible the same way `](target)` is.+ */+const REFERENCE_TAIL = /^\]\[[^\]]*\]/;++/**+ * The invisible head of an ALIASED wiki link: `[[page|` of `[[page|Alias]]`.+ *+ * An aliased link renders as the alias alone, so the target AND the pipe are+ * invisible — and unlike the brackets they are arbitrary content, so they have+ * to be skipped as one run rather than a character at a time. Unaliased links+ * need nothing here: their target IS what renders, so it matches directly.+ */+const WIKI_ALIAS_HEAD = /^\[\[[^\][|]*\|/;++/**+ * The heading fragment of a wiki link: the `#sec` of `[[page#sec]]`.+ *+ * The link renders as its page (or its alias), so the fragment is invisible.+ * Anchored to a following `]]` rather than matching any `#`, so an ordinary+ * `#tag` mid-sentence stays content — a tag's `#` IS on screen and quotes+ * carry it.+ */+const WIKI_ANCHOR_TAIL = /^#[^\][|]*(?=\]\])/;++/** Bounds the slice the bracket-construct patterns run on. */+const BRACKET_WINDOW = 512;/*** The tail of an inline link or image: `](target)`.@@ -144,15 +251,52 @@ function syntaxRunAt(text: string, i: number): number {if (line > 0) return line;}if (ch === TABLE_PIPE) return 1;+ // Multi-character runs before the single-character sets: `<u>` opens with a+ // character that is also inline syntax, and consuming it alone leaves the tag+ // name looking like content.+ if (ch === '<') {+ const tag = HTML_TAG.exec(text.slice(i, i + HTML_TAG_WINDOW))?.[0].length;+ if (tag !== undefined) return tag;+ // Just the bracket — the address behind it renders and has to match.+ return opensAutolink(text, i) ? 1 : 0;+ }+ // Both fall through when they are not what they might have been, so a+ // line-leading `>` still reaches the blockquote rule at the end.+ if (ch === '>' && closesAutolink(text, i)) return 1;+ if (ch === '=') {+ const highlight = HIGHLIGHT_DELIM.exec(text.slice(i, i + 3))?.[0].length;+ if (highlight !== undefined) return highlight;+ }+ if (ch === '!') return IMAGE_BANG.test(text.slice(i, i + 2)) ? 1 : 0;if (INLINE_SYNTAX.has(ch)) return 1;- // `[label](target)` and `[[target]]` both render as their label alone, so the- // brackets and the whole `](target)` tail are invisible to a caller working- // from rendered text.- if (ch === '[') return 1;+ // `[label](target)`, `[[target]]`, and `[^1]` all render as their label alone+ // (or as nothing), so the brackets, the `](target)` tail, and an aliased wiki+ // link's `target|` head are invisible to a caller working from rendered text.+ if (ch === '[') {+ const window = text.slice(i, i + BRACKET_WINDOW);+ const alias = WIKI_ALIAS_HEAD.exec(window)?.[0].length;+ if (alias !== undefined) return alias;+ if (FOOTNOTE_HEAD.test(window)) return 2;+ return 1;+ }+ if (ch === '#') {+ const anchor = WIKI_ANCHOR_TAIL.exec(text.slice(i, i + BRACKET_WINDOW))?.[0].length;+ // Falls through when it is not a wiki-link fragment, so a line-leading `#`+ // still reaches the heading rule at the end of this function.+ if (anchor !== undefined) return anchor;+ }if (ch === ']') {// Wiki-link close first: `]]` is never the start of a link tail.if (text[i + 1] === ']') return 2;- return LINK_TAIL.exec(text.slice(i, i + LINK_TAIL_WINDOW))?.[0].length ?? 0;+ const window = text.slice(i, i + Math.max(LINK_TAIL_WINDOW, BRACKET_WINDOW));+ const inline = LINK_TAIL.exec(window)?.[0].length;+ if (inline !== undefined) return inline;+ const reference = REFERENCE_TAIL.exec(window)?.[0].length;+ if (reference !== undefined) return reference;+ // A plain closer — the `]` of `[^1]`. Symmetric with the bare `[` above,+ // which has always been elastic; without it a footnote marker could not be+ // crossed even once its `[^` head was skippable.+ return 1;}if (!isLineStart(text, i)) return 0;return BLOCK_MARKER.exec(text.slice(i, i + BLOCK_MARKER_WINDOW))?.[0].length ?? 0;@@ -178,7 +322,11 @@ export function findAllPassages(// reported range would open with syntax the caller never selected.const first = haystack[start];if (first === undefined || isSpace(first)) continue;- if (syntaxInHaystack && syntaxRunAt(haystack, start) > 0) continue;+ // …unless the caller's own text opens with that character, in which case it+ // is content to them and the loop will match it literally. Inline HTML that+ // survives into rendered text is the case: a quote of `<div>` must still be+ // able to start on the `<` that `HTML_TAG` would otherwise call syntax.+ if (syntaxInHaystack && first !== needle[0] && syntaxRunAt(haystack, start) > 0) continue;let hi = start;let ni = 0;@@ -219,3 +367,130 @@ export function findPassage(): PassageMatch | null {return findAllPassages(haystack, needle, options)[0] ?? null;}++/**+ * How well the words around a candidate match the words captured around the+ * passage when the comment was written — the score that picks WHICH occurrence+ * of a repeated quote was meant.+ *+ * Shared by both sides deliberately. The client scores against rendered editor+ * text and the server against the markdown body, and for a while each kept its+ * own byte-exact copy of the comparison. Byte-exact is the one thing it cannot+ * be: the captured context joins blocks with a single `\n`, while the text+ * being scored separates them with `\n\n`, `- `, `> `, `#`. Every candidate+ * therefore scored zero the moment the context reached past its own block, the+ * ranking went inert, and the caller silently fell back to the first hit — so a+ * comment on the second of several identical list items anchored to the first.+ * Two copies also meant fixing one and leaving the other, which is how the+ * client and server came to disagree about the same thread.+ *+ * Whitespace is elastic on both sides, and markdown syntax on whichever side+ * carries it (`syntaxIn`) — the same tolerances {@link findAllPassages} applies+ * when locating the passage in the first place. Ranking only: the score orders+ * candidates and never moves a boundary, so condensing is safe here in a way it+ * would not be for a match.+ */+export interface ContextMatchOptions {+ /** `'haystack'` when `text` is markdown; `'none'` when it is rendered text. */+ readonly syntaxIn: 'haystack' | 'none';+}++/**+ * Text either side of a candidate worth condensing, as a multiple of the+ * context being matched. Generous because markdown spends characters that+ * render as nothing, so the same words occupy more of the body than of the+ * editor; bounded so a long document does not pay for the whole of itself on+ * every candidate.+ */+const CONTEXT_WINDOW_FACTOR = 8;+const CONTEXT_WINDOW_FLOOR = 64;++/** `text[from, to)` with whitespace — and optionally markdown syntax — removed. */+function condense(text: string, from: number, to: number, syntax: boolean): string {+ let out = '';+ let i = Math.max(0, from);+ const end = Math.min(text.length, to);+ while (i < end) {+ const ch = text[i] as string;+ if (isSpace(ch)) {+ i += 1;+ continue;+ }+ if (syntax) {+ const run = syntaxRunAt(text, i);+ if (run > 0) {+ i += run;+ continue;+ }+ }+ out += ch;+ i += 1;+ }+ return out;+}++function commonSuffixLen(a: string, b: string): number {+ const n = Math.min(a.length, b.length);+ let i = 0;+ while (i < n && a[a.length - 1 - i] === b[b.length - 1 - i]) i += 1;+ return i;+}++function commonPrefixLen(a: string, b: string): number {+ const n = Math.min(a.length, b.length);+ let i = 0;+ while (i < n && a[i] === b[i]) i += 1;+ return i;+}++export function contextMatchScore(+ text: string,+ span: { readonly start: number; readonly end: number },+ context: { readonly prefix?: string; readonly suffix?: string },+ { syntaxIn }: ContextMatchOptions,+): number {+ const prefix = context.prefix ?? '';+ const suffix = context.suffix ?? '';+ const syntax = syntaxIn === 'haystack';+ let score = 0;+ if (prefix.length > 0) {+ const window = prefix.length * CONTEXT_WINDOW_FACTOR + CONTEXT_WINDOW_FLOOR;+ score += commonSuffixLen(+ condense(prefix, 0, prefix.length, false),+ condense(text, span.start - window, span.start, syntax),+ );+ }+ if (suffix.length > 0) {+ const window = suffix.length * CONTEXT_WINDOW_FACTOR + CONTEXT_WINDOW_FLOOR;+ score += commonPrefixLen(+ condense(suffix, 0, suffix.length, false),+ condense(text, span.end, span.end + window, syntax),+ );+ }+ return score;+}++/**+ * The longest a passage may become when recovered from the context brackets+ * still surrounding it.+ *+ * Editing inside a commented passage is normal ("needs space" → "needs more+ * space"); replacing whole paragraphs between the same two boundaries is not,+ * and silently swallowing them would attach the comment to text nobody pointed+ * at. Generous but finite: past this, treat it as a replacement and orphan.+ *+ * Shared by both recovery paths — the editor's, over rendered text, and the+ * server's, over the markdown body. Those search different substrates and so+ * cannot share an implementation, but they must agree on this ceiling exactly:+ * a passage that recovers on one side and orphans on the other leaves a comment+ * highlighted against text the other half has already given up on. It lived as+ * two copies of two constants under a drift warning, which is the arrangement+ * that had already let `contextMatchScore` diverge — fixed on one side, silently+ * stale on the other — so the policy is a function here rather than a number+ * each side re-applies.+ */+export function rewriteCeiling(quoteLength: number): number {+ const MAX_GROWTH = 4;+ const GROWTH_FLOOR = 64;+ return Math.max(quoteLength * MAX_GROWTH, quoteLength + GROWTH_FLOOR);+}diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex e09c459d9..93bc7ff26 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -108,11 +108,15 @@ export {type MenuPlatform,type MenuSection,} from './commands/command-identity.ts';+export { commentLeafText, commentQuoteText } from './comments/leaf-text.ts';export {+ type ContextMatchOptions,+ contextMatchScore,findAllPassages,findPassage,type PassageMatch,type PassageMatchOptions,+ rewriteCeiling,} from './comments/passage-match.ts';// Sync-mode vocabulary + resolution rules (off/follow/full). Browser+node compatible.export {diff --git a/packages/core/src/markdown/callout-transformer.ts b/packages/core/src/markdown/callout-transformer.tsindex 83d47404a..a363dfd54 100644--- a/packages/core/src/markdown/callout-transformer.ts+++ b/packages/core/src/markdown/callout-transformer.ts@@ -1,3 +1,4 @@+import type { Blockquote, Paragraph, Root } from 'mdast';import type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';import { visit } from 'unist-util-visit';diff --git a/packages/core/src/markdown/code-fence.ts b/packages/core/src/markdown/code-fence.tsindex 9807a7e82..4499dbc92 100644--- a/packages/core/src/markdown/code-fence.ts+++ b/packages/core/src/markdown/code-fence.ts@@ -1,3 +1,4 @@+export function selectFenceChar(info: string): '`' | '~' {return info.includes('`') ? '~' : '`';}diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.tsindex 7415c474a..34704165b 100644--- a/packages/core/src/markdown/comment-promoter.ts+++ b/packages/core/src/markdown/comment-promoter.ts@@ -1,3 +1,4 @@+import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';import { SKIP, visit } from 'unist-util-visit';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/dedent-block-jsx-close.ts b/packages/core/src/markdown/dedent-block-jsx-close.tsindex ece5c02a1..b6299a948 100644--- a/packages/core/src/markdown/dedent-block-jsx-close.ts+++ b/packages/core/src/markdown/dedent-block-jsx-close.ts@@ -1,3 +1,4 @@+import { findFencedRegions, isInsideFence } from './fence-regions.ts';const INDENTED_BLOCK_JSX_CLOSE_RE = /^([ ]{1,3})(<\/[A-Z][A-Za-z0-9_]*\s*>)([ \t]*)$/gm;diff --git a/packages/core/src/markdown/details-accordion-promoter.ts b/packages/core/src/markdown/details-accordion-promoter.tsindex 54774764a..acd398aa6 100644--- a/packages/core/src/markdown/details-accordion-promoter.ts+++ b/packages/core/src/markdown/details-accordion-promoter.ts@@ -1,3 +1,4 @@+import type { Nodes, Paragraph, Parent, Root, Text } from 'mdast';import type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';import { visit } from 'unist-util-visit';diff --git a/packages/core/src/markdown/div-align-promoter.ts b/packages/core/src/markdown/div-align-promoter.tsindex 43ecdc709..ba9b56384 100644--- a/packages/core/src/markdown/div-align-promoter.ts+++ b/packages/core/src/markdown/div-align-promoter.ts@@ -1,3 +1,4 @@+import type { Nodes, Paragraph, Parent, Root, Text } from 'mdast';import type { MdxJsxFlowElement } from 'mdast-util-mdx';import { visit } from 'unist-util-visit';diff --git a/packages/core/src/markdown/fence-regions.ts b/packages/core/src/markdown/fence-regions.tsindex 6d3f3643e..4513da9bb 100644--- a/packages/core/src/markdown/fence-regions.ts+++ b/packages/core/src/markdown/fence-regions.ts@@ -1,3 +1,4 @@+const FENCE_RE = /^(`{3,}|~{3,})/gm;export function findFencedRegions(src: string): Array<[number, number]> {diff --git a/packages/core/src/markdown/fixtures/index.ts b/packages/core/src/markdown/fixtures/index.tsindex 00f5771ee..6931623c7 100644--- a/packages/core/src/markdown/fixtures/index.ts+++ b/packages/core/src/markdown/fixtures/index.ts@@ -1,3 +1,4 @@+import { readFileSync } from 'node:fs';import { dirname, resolve } from 'node:path';import { fileURLToPath } from 'node:url';@@ -8,6 +9,7 @@ function fixturePath(...segments: string[]): string {return resolve(FIXTURES_DIR, ...segments);}+interface GfmExample {section: string;markdown: string;@@ -17,6 +19,7 @@ export function loadGfmExamples(): GfmExample[] {return JSON.parse(readFileSync(fixturePath('gfm', 'examples.json'), 'utf8')) as GfmExample[];}+interface MdxCrashEntry {id: string;input: string;@@ -68,6 +71,7 @@ export function loadLargeEmbedFixtures(): LargeEmbedFixture[] {) as LargeEmbedFixture[];}+export function loadPrd6955Before(): string {return readFileSync(fixturePath('regression', 'prd-6955-before.md'), 'utf8');}@@ -76,6 +80,7 @@ export function loadPrd6955CorruptedTriplicated(): string {return readFileSync(fixturePath('regression', 'prd-6955-corrupted-triplicated.md'), 'utf8');}+export interface NgPinnedCase {id: string;name: string;@@ -92,6 +97,7 @@ export function loadNgPinnedCases(): NgPinnedCase[] {) as NgPinnedCase[];}+export function loadLargeRealistic(): string {return readFileSync(fixturePath('perf', 'large-realistic.md'), 'utf8');}diff --git a/packages/core/src/markdown/fixtures/perf/generate.ts b/packages/core/src/markdown/fixtures/perf/generate.tsindex 8b1eb8519..daa3ef82b 100644--- a/packages/core/src/markdown/fixtures/perf/generate.ts+++ b/packages/core/src/markdown/fixtures/perf/generate.ts@@ -1,3 +1,4 @@+import { writeFileSync } from 'node:fs';import { dirname, resolve } from 'node:path';import { fileURLToPath } from 'node:url';@@ -10,6 +11,7 @@ const BASELINE_ONLY_COUNTS = [500, 2500] as const;const SEED = 0xf1de1117;+function mulberry32(seed: number): () => number {let a = seed >>> 0;return () => {@@ -36,6 +38,7 @@ function pickWeighted<T>(rand: () => number, items: readonly [T, number][]): T {return items[items.length - 1][0];}+const WORDS = ['lorem','ipsum',@@ -153,6 +156,7 @@ function mdxBlock(rand: () => number): string {return `<${name}>\n\n${sentence(rand)}\n\n</${name}>`;}+type BlockKind = 'paragraph' | 'heading' | 'list' | 'code' | 'table' | 'mdx';const MIX: readonly [BlockKind, number][] = [@@ -191,6 +195,7 @@ function generateDocument(blockCount: number, seed: number): string {return `${blocks.join('\n\n')}\n`;}+function main(): void {for (const count of [...BLOCK_COUNTS, ...BASELINE_ONLY_COUNTS]) {const doc = generateDocument(count, SEED);diff --git a/packages/core/src/markdown/guard-flanking-matrix.ts b/packages/core/src/markdown/guard-flanking-matrix.tsindex 420dc0fe5..32c055c5b 100644--- a/packages/core/src/markdown/guard-flanking-matrix.ts+++ b/packages/core/src/markdown/guard-flanking-matrix.ts@@ -7,6 +7,7 @@ import {import { BACKSLASH_GUARD_SUBSTITUTIONS, encodeBackslashEscapes } from './backslash-escape-guard.ts';import { ENTITY_REF_GUARD_SUBSTITUTIONS, encodeEntityRefs } from './entity-ref-guard.ts';+export const ATTENTION_DELIMITERS = ['*', '_', '**', '~~', '=='] as const;export type FlankClass = 'whitespace' | 'punctuation' | 'other';diff --git a/packages/core/src/markdown/handler-shadow-audit.ts b/packages/core/src/markdown/handler-shadow-audit.tsindex 2653473da..283913255 100644--- a/packages/core/src/markdown/handler-shadow-audit.ts+++ b/packages/core/src/markdown/handler-shadow-audit.ts@@ -1,3 +1,4 @@+export interface HandlerShadowWitness {input: string;expect: 'byte' | 'byte-and-reparse-type';diff --git a/packages/core/src/markdown/highlight-promoter.ts b/packages/core/src/markdown/highlight-promoter.tsindex da0a81d23..5d1000cc4 100644--- a/packages/core/src/markdown/highlight-promoter.ts+++ b/packages/core/src/markdown/highlight-promoter.ts@@ -1,3 +1,4 @@+import type { Nodes, Parent, PhrasingContent, Root, Text } from 'mdast';import type { MdxJsxTextElement } from 'mdast-util-mdx';import { SKIP, visit } from 'unist-util-visit';diff --git a/packages/core/src/markdown/html-to-mdast.ts b/packages/core/src/markdown/html-to-mdast.tsindex 787315157..9681c0056 100644--- a/packages/core/src/markdown/html-to-mdast.ts+++ b/packages/core/src/markdown/html-to-mdast.ts@@ -1,3 +1,4 @@+import type { Element as HastElement, Root as HastRoot } from 'hast';import type { List, Nodes as MdastNodes, Root as MdastRoot } from 'mdast';import rehypeParse from 'rehype-parse';@@ -128,7 +129,8 @@ export function htmlToMdast(html: string, options?: HtmlToMdastOptions): MdastRothrow new HtmlPayloadTooLargeError(html.length, maxBytes);}- const processor = unified().use(rehypeParse, { fragment: true });+ const processor = unified()+ .use(rehypeParse, { fragment: true });for (const plugin of cleanupPlugins) {processor.use(plugin);diff --git a/packages/core/src/markdown/image-promoter.ts b/packages/core/src/markdown/image-promoter.tsindex 706fa9afb..994eb6917 100644--- a/packages/core/src/markdown/image-promoter.ts+++ b/packages/core/src/markdown/image-promoter.ts@@ -1,3 +1,4 @@+import type { Image, Paragraph, Root } from 'mdast';import type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';import { visit } from 'unist-util-visit';diff --git a/packages/core/src/markdown/index.ts b/packages/core/src/markdown/index.tsindex 9075aa7f1..69221627d 100644--- a/packages/core/src/markdown/index.ts+++ b/packages/core/src/markdown/index.ts@@ -1,3 +1,4 @@+import {type FromProseMirrorOptions,fromPmMark,@@ -206,6 +207,7 @@ export class MarkdownManager {}}+const registry = createRegistry();function destructureAttrs(@@ -254,6 +256,7 @@ function destructureAttrs(return result;}+function hasDirtyDescendant(node: PmNode): boolean {let found = false;node.descendants((child) => {@@ -276,6 +279,7 @@ function effectiveDirty(node: PmNode, freshnessChecker?: StructuralFreshnessChecreturn freshnessChecker ? freshnessChecker.isDiverged(node.toJSON()) : false;}+function isEmptyMdastParagraph(node: MdastNodes): boolean {if (node.type !== 'paragraph') return false;const children = node.children ?? [];@@ -344,6 +348,7 @@ function extractTextFromMdastNodes(nodes: MdastNodes[]): string {return out;}+import {AUDIO_EXTENSIONS,FILE_ATTACHMENT_EXTENSIONS,@@ -596,6 +601,7 @@ function buildMdastToPmHandlers(}));}+if (m.emphasis) {handlers.emphasis = toPmMark(m.emphasis, (node: Emphasis) => ({sourceDelimiter: node.data?.sourceDelimiter ?? '*',@@ -676,6 +682,7 @@ function buildMdastToPmHandlers(}));}+if (m.link) {const sourceLiteralMark = m.sourceLiteral;handlers.link = (node: Link, _parent: MdastParent, state: MdastToPmState) => {@@ -973,6 +980,7 @@ function buildMdastToPmHandlers(};}+const blockUnknownHandler = (node: {type: string;position?: { start: { offset: number }; end: { offset: number } };@@ -1098,6 +1106,7 @@ function buildMdastToPmHandlers(return handlers as RemarkProseMirrorOptions['handlers'];}+function buildPmToMdastHandlers(schema: Schema,freshness: FreshnessCheckerHolder,diff --git a/packages/core/src/markdown/lint/applies-to.ts b/packages/core/src/markdown/lint/applies-to.tsindex fa0be9e86..b79e9aef7 100644--- a/packages/core/src/markdown/lint/applies-to.ts+++ b/packages/core/src/markdown/lint/applies-to.ts@@ -1,3 +1,4 @@+import picomatch from 'picomatch';import { SUPPORTED_DOC_EXTENSIONS } from '../../constants/doc-extensions.ts';diff --git a/packages/core/src/markdown/lint/config-files.ts b/packages/core/src/markdown/lint/config-files.tsindex 312c9c74b..94b34dee1 100644--- a/packages/core/src/markdown/lint/config-files.ts+++ b/packages/core/src/markdown/lint/config-files.ts@@ -1,3 +1,4 @@+const MARKDOWNLINT_JSON_CONFIG_FILES: ReadonlySet<string> = new Set(['.markdownlint.json','.markdownlint.jsonc',diff --git a/packages/core/src/markdown/lint/config-schemas.ts b/packages/core/src/markdown/lint/config-schemas.tsindex 0ba33b7d8..e413367ef 100644--- a/packages/core/src/markdown/lint/config-schemas.ts+++ b/packages/core/src/markdown/lint/config-schemas.ts@@ -38,6 +38,7 @@ const fullPluginShape = Object.fromEntries(LINT_PLUGINS.map((plugin) => [plugin.id, plugin.sliceSchema]),) as z.ZodRawShape;+export const LinterConfigSchema = z.object({enabled: z.boolean(),plugins: z.object(fullPluginShape),diff --git a/packages/core/src/markdown/lint/default-config.ts b/packages/core/src/markdown/lint/default-config.tsindex d48eea5e4..a51f2ffb0 100644--- a/packages/core/src/markdown/lint/default-config.ts+++ b/packages/core/src/markdown/lint/default-config.ts@@ -6,6 +6,7 @@ export const DEFAULT_MARKDOWNLINT_CONFIG: Record<string, MarkdownlintRuleSettingMD013: false,};+export function resolveMarkdownlintConfig(rules: Record<string, MarkdownlintRuleSetting> | undefined,): Configuration {diff --git a/packages/core/src/markdown/lint/frontmatter-schema-edit.ts b/packages/core/src/markdown/lint/frontmatter-schema-edit.tsindex ef478e268..9c9542661 100644--- a/packages/core/src/markdown/lint/frontmatter-schema-edit.ts+++ b/packages/core/src/markdown/lint/frontmatter-schema-edit.ts@@ -1,3 +1,4 @@+import { CANONICAL_SCHEMA_DIALECT_URIS, DEFAULT_SCHEMA_DIALECT } from './frontmatter-validate.ts';export interface FrontmatterFieldConstraint {diff --git a/packages/core/src/markdown/lint/frontmatter-validate.ts b/packages/core/src/markdown/lint/frontmatter-validate.tsindex 0d4c9a0df..b96d19182 100644--- a/packages/core/src/markdown/lint/frontmatter-validate.ts+++ b/packages/core/src/markdown/lint/frontmatter-validate.ts@@ -1,3 +1,4 @@+import Ajv, { type ValidateFunction } from 'ajv';import Ajv2019 from 'ajv/dist/2019.js';import Ajv2020 from 'ajv/dist/2020.js';diff --git a/packages/core/src/markdown/lint/index.ts b/packages/core/src/markdown/lint/index.tsindex 00d08265a..48d260ed6 100644--- a/packages/core/src/markdown/lint/index.ts+++ b/packages/core/src/markdown/lint/index.ts@@ -1,3 +1,4 @@+import { LINT_PLUGINS, type LinterConfig } from './plugins.ts';import type { LintDiagnostic } from './types.ts';diff --git a/packages/core/src/markdown/lint/plugins.ts b/packages/core/src/markdown/lint/plugins.tsindex 5e832aa19..be5871ed9 100644--- a/packages/core/src/markdown/lint/plugins.ts+++ b/packages/core/src/markdown/lint/plugins.ts@@ -1,3 +1,4 @@+import { z } from 'zod';import { DEFAULT_MARKDOWNLINT_CONFIG, resolveMarkdownlintConfig } from './default-config.ts';import {@@ -13,6 +14,7 @@ import {type MarkdownlintSlice,} from './types.ts';+const MarkdownlintRuleSettingSchema = z.union([z.boolean(),z.enum(MARKDOWNLINT_RULE_SEVERITIES),diff --git a/packages/core/src/markdown/lint/types.ts b/packages/core/src/markdown/lint/types.tsindex 3918e657c..833c3a727 100644--- a/packages/core/src/markdown/lint/types.ts+++ b/packages/core/src/markdown/lint/types.ts@@ -1,3 +1,4 @@+export const LINT_PLUGIN_IDS = ['markdownlint', 'frontmatter'] as const;export type LintPluginId = (typeof LINT_PLUGIN_IDS)[number];@@ -71,6 +72,7 @@ export interface FrontmatterSlice {schemas: ResolvedFrontmatterSchemaEntry[];}+interface RuleOptionSpecBase {key: string;description: string;diff --git a/packages/core/src/markdown/lint/validation-counts.ts b/packages/core/src/markdown/lint/validation-counts.tsindex 98238656e..437cc539a 100644--- a/packages/core/src/markdown/lint/validation-counts.ts+++ b/packages/core/src/markdown/lint/validation-counts.ts@@ -1,3 +1,4 @@+export interface ValidationSourceCounts {errorCount: number;warningCount: number;diff --git a/packages/core/src/markdown/math-promoter.ts b/packages/core/src/markdown/math-promoter.tsindex a5b511f25..bd79424c7 100644--- a/packages/core/src/markdown/math-promoter.ts+++ b/packages/core/src/markdown/math-promoter.ts@@ -1,3 +1,4 @@+import type { Code, Root } from 'mdast';import type { Math as MdastMath } from 'mdast-util-math';import type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';diff --git a/packages/core/src/markdown/mdast-to-hast-handlers.ts b/packages/core/src/markdown/mdast-to-hast-handlers.tsindex ed89371d0..00fbc0035 100644--- a/packages/core/src/markdown/mdast-to-hast-handlers.ts+++ b/packages/core/src/markdown/mdast-to-hast-handlers.ts@@ -1,3 +1,4 @@+import type { Comment, Element, ElementContent, Properties } from 'hast';import type { FootnoteDefinition, FootnoteReference } from 'mdast';import type { MdxJsxFlowElement, MdxJsxTextElement } from 'mdast-util-mdx';diff --git a/packages/core/src/markdown/mdast-to-html.ts b/packages/core/src/markdown/mdast-to-html.tsindex 8ce2b2492..df788a80d 100644--- a/packages/core/src/markdown/mdast-to-html.ts+++ b/packages/core/src/markdown/mdast-to-html.ts@@ -1,3 +1,4 @@+import type { Element, Root as HastRoot, Text as HastText } from 'hast';import type { Root as MdastRoot, Text as MdastText } from 'mdast';import type { Handler } from 'mdast-util-to-hast';diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.tsindex bfd7e3109..fa3cfcd86 100644--- a/packages/core/src/markdown/merged-walker.ts+++ b/packages/core/src/markdown/merged-walker.ts@@ -1,3 +1,4 @@+import type { Nodes, Parent, Root } from 'mdast';import { SKIP, visit } from 'unist-util-visit';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/mermaid-promoter.ts b/packages/core/src/markdown/mermaid-promoter.tsindex 1a897fca8..17ed2b829 100644--- a/packages/core/src/markdown/mermaid-promoter.ts+++ b/packages/core/src/markdown/mermaid-promoter.ts@@ -1,3 +1,4 @@+import type { Code, Root } from 'mdast';import type { MdxJsxAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';import { visit } from 'unist-util-visit';diff --git a/packages/core/src/markdown/parse-with-fallback.ts b/packages/core/src/markdown/parse-with-fallback.tsindex d988f4b8b..4aa0490c4 100644--- a/packages/core/src/markdown/parse-with-fallback.ts+++ b/packages/core/src/markdown/parse-with-fallback.ts@@ -168,6 +168,7 @@ export function parseRecursive(}}+interface VFilePlace {offset?: number;start?: { offset?: number };@@ -186,6 +187,7 @@ function extractErrorOffset(err: unknown): number | undefined {return undefined;}+interface Region {start: number;end: number;@@ -209,6 +211,7 @@ function nearestBlankLineAfter(src: string, offset: number): number | null {return null;}+export interface TagEvent {kind: 'open' | 'close' | 'self-close';name: string;@@ -355,6 +358,7 @@ function findFallbackRegion(src: string, errorOffset: number): Region {return { start: blockStart, end: blockEnd };}+interface SourceBlock {src: string;start: number;@@ -452,6 +456,7 @@ function tryPerBlockFallback(};}+function wholeDocRawText(source: string): JSONContent {return {type: 'doc',diff --git a/packages/core/src/markdown/parser-drop-closure.ts b/packages/core/src/markdown/parser-drop-closure.tsindex e6ab4fd57..43d420187 100644--- a/packages/core/src/markdown/parser-drop-closure.ts+++ b/packages/core/src/markdown/parser-drop-closure.ts@@ -1,3 +1,4 @@+export type DroppedTokenAdjudication =| {kind: 'format-dof-axis';diff --git a/packages/core/src/markdown/pipeline.ts b/packages/core/src/markdown/pipeline.tsindex 449e6f12f..5af2f05f5 100644--- a/packages/core/src/markdown/pipeline.ts+++ b/packages/core/src/markdown/pipeline.ts@@ -1,3 +1,4 @@+import {type FromProseMirrorOptions,fromProseMirror,diff --git a/packages/core/src/markdown/position-aware-join.ts b/packages/core/src/markdown/position-aware-join.tsindex 91cdf658f..ccf06e94d 100644--- a/packages/core/src/markdown/position-aware-join.ts+++ b/packages/core/src/markdown/position-aware-join.ts@@ -1,3 +1,4 @@+import type { Join } from 'mdast-util-to-markdown';import type { Position } from 'unist';import { isBlankLineParagraph } from './zero-emission-blocks.ts';diff --git a/packages/core/src/markdown/position-slice.ts b/packages/core/src/markdown/position-slice.tsindex 0f399d34c..e5b4c9eba 100644--- a/packages/core/src/markdown/position-slice.ts+++ b/packages/core/src/markdown/position-slice.ts@@ -1,3 +1,4 @@+import type { Nodes, Root } from 'mdast';import { visit } from 'unist-util-visit';import type { VFile } from 'vfile';@@ -476,7 +477,8 @@ export function applyPositionSliceToNode(const first = source[startOff];if (first !== '[' && first !== '<' && !node.title) {node.data.sourceStyle = 'gfm-autolink';- } else if (first === '[') {+ }+ else if (first === '[') {const slice = source.slice(startOff, endOff);const closeBracketIdx = slice.lastIndexOf('](');if (closeBracketIdx !== -1) {diff --git a/packages/core/src/markdown/rehype-plugins/skip-notion-whitespace.ts b/packages/core/src/markdown/rehype-plugins/skip-notion-whitespace.tsindex db10fa54b..ca225ce9b 100644--- a/packages/core/src/markdown/rehype-plugins/skip-notion-whitespace.ts+++ b/packages/core/src/markdown/rehype-plugins/skip-notion-whitespace.ts@@ -1,3 +1,4 @@+import type { Comment, Element, Root, Text } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/rehype-plugins/strip-cocoa-meta.ts b/packages/core/src/markdown/rehype-plugins/strip-cocoa-meta.tsindex 34068d103..3a2524a9a 100644--- a/packages/core/src/markdown/rehype-plugins/strip-cocoa-meta.ts+++ b/packages/core/src/markdown/rehype-plugins/strip-cocoa-meta.ts@@ -1,3 +1,4 @@+import type { Element, ElementContent, Root } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/rehype-plugins/strip-gdocs-wrapper.ts b/packages/core/src/markdown/rehype-plugins/strip-gdocs-wrapper.tsindex 93151d43b..2279a0189 100644--- a/packages/core/src/markdown/rehype-plugins/strip-gdocs-wrapper.ts+++ b/packages/core/src/markdown/rehype-plugins/strip-gdocs-wrapper.ts@@ -1,3 +1,4 @@+import type { Element, Root } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/rehype-plugins/strip-github-hovercard.ts b/packages/core/src/markdown/rehype-plugins/strip-github-hovercard.tsindex 188f02d83..a0f467c42 100644--- a/packages/core/src/markdown/rehype-plugins/strip-github-hovercard.ts+++ b/packages/core/src/markdown/rehype-plugins/strip-github-hovercard.ts@@ -1,3 +1,4 @@+import type { Element, Root } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/rehype-plugins/strip-gmail-classes.ts b/packages/core/src/markdown/rehype-plugins/strip-gmail-classes.tsindex 4ba2c3418..4e8542a21 100644--- a/packages/core/src/markdown/rehype-plugins/strip-gmail-classes.ts+++ b/packages/core/src/markdown/rehype-plugins/strip-gmail-classes.ts@@ -1,3 +1,4 @@+import type { Element, Root } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/rehype-plugins/strip-gsheets-wrapper.ts b/packages/core/src/markdown/rehype-plugins/strip-gsheets-wrapper.tsindex 1a74b1272..a3c96b24a 100644--- a/packages/core/src/markdown/rehype-plugins/strip-gsheets-wrapper.ts+++ b/packages/core/src/markdown/rehype-plugins/strip-gsheets-wrapper.ts@@ -1,3 +1,4 @@+import type { Element, ElementContent, Root } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/rehype-plugins/strip-mso-styles.ts b/packages/core/src/markdown/rehype-plugins/strip-mso-styles.tsindex de35e7b79..e95cf43d6 100644--- a/packages/core/src/markdown/rehype-plugins/strip-mso-styles.ts+++ b/packages/core/src/markdown/rehype-plugins/strip-mso-styles.ts@@ -1,3 +1,4 @@+import type { Comment, Element, ElementContent, Root } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/rehype-plugins/strip-slack-classes.ts b/packages/core/src/markdown/rehype-plugins/strip-slack-classes.tsindex b4be1b2ea..ffdb4d5b5 100644--- a/packages/core/src/markdown/rehype-plugins/strip-slack-classes.ts+++ b/packages/core/src/markdown/rehype-plugins/strip-slack-classes.ts@@ -1,3 +1,4 @@+import type { Element, ElementContent, Root } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/rehype-plugins/strip-vscode-spans.ts b/packages/core/src/markdown/rehype-plugins/strip-vscode-spans.tsindex 3c8d88b7c..e2a96d1a7 100644--- a/packages/core/src/markdown/rehype-plugins/strip-vscode-spans.ts+++ b/packages/core/src/markdown/rehype-plugins/strip-vscode-spans.ts@@ -1,3 +1,4 @@+import type { Element, Root } from 'hast';import type { Plugin } from 'unified';diff --git a/packages/core/src/markdown/resolve-image-url.ts b/packages/core/src/markdown/resolve-image-url.tsindex cf2c9da8e..0389575ac 100644--- a/packages/core/src/markdown/resolve-image-url.ts+++ b/packages/core/src/markdown/resolve-image-url.ts@@ -1,3 +1,4 @@+import { isRelativeUrl } from './safe-url.ts';function isDevDiagnosticContext(): boolean {diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.tsindex 4d3250d3a..8be632ba4 100644--- a/packages/core/src/markdown/safe-url.ts+++ b/packages/core/src/markdown/safe-url.ts@@ -1,3 +1,4 @@+export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');diff --git a/packages/core/src/markdown/serialize-fidelity.test-helper.ts b/packages/core/src/markdown/serialize-fidelity.test-helper.tsindex 0dd54070e..96304e7a5 100644--- a/packages/core/src/markdown/serialize-fidelity.test-helper.ts+++ b/packages/core/src/markdown/serialize-fidelity.test-helper.ts@@ -1,3 +1,4 @@+import type { JSONContent } from '@tiptap/core';import type { Nodes } from 'mdast';import { expect } from 'vitest';diff --git a/packages/core/src/markdown/serialize-helpers.ts b/packages/core/src/markdown/serialize-helpers.tsindex 7cac5b042..93798cf83 100644--- a/packages/core/src/markdown/serialize-helpers.ts+++ b/packages/core/src/markdown/serialize-helpers.ts@@ -1,3 +1,4 @@+import type { Node as PmNode } from '@tiptap/pm/model';import type {MdxJsxAttribute,diff --git a/packages/core/src/markdown/single-dollar-math-promoter.ts b/packages/core/src/markdown/single-dollar-math-promoter.tsindex b012a5aff..8c08b7a04 100644--- a/packages/core/src/markdown/single-dollar-math-promoter.ts+++ b/packages/core/src/markdown/single-dollar-math-promoter.ts@@ -1,3 +1,4 @@+import type { PhrasingContent, Root, Text } from 'mdast';import type { InlineMath } from 'mdast-util-math';import { SKIP, visit } from 'unist-util-visit';diff --git a/packages/core/src/markdown/to-markdown-handlers.ts b/packages/core/src/markdown/to-markdown-handlers.tsindex 3e79f8cb6..959cc0bd2 100644--- a/packages/core/src/markdown/to-markdown-handlers.ts+++ b/packages/core/src/markdown/to-markdown-handlers.ts@@ -1,3 +1,4 @@+import type { Nodes, Parents } from 'mdast';import type { MdxJsxAttribute, MdxJsxExpressionAttribute, MdxJsxFlowElement } from 'mdast-util-mdx';import type { Handle, Info, State } from 'mdast-util-to-markdown';diff --git a/packages/core/src/markdown/underline-promoter.ts b/packages/core/src/markdown/underline-promoter.tsindex 0f1b0f27d..6e6d73400 100644--- a/packages/core/src/markdown/underline-promoter.ts+++ b/packages/core/src/markdown/underline-promoter.ts@@ -1,3 +1,4 @@+import type { Nodes, Parent, Root } from 'mdast';import type { MdxJsxTextElement } from 'mdast-util-mdx';import { visit } from 'unist-util-visit';diff --git a/packages/core/src/markdown/unknown-mdast-guard.ts b/packages/core/src/markdown/unknown-mdast-guard.tsindex 1a082bda7..932d7d2a8 100644--- a/packages/core/src/markdown/unknown-mdast-guard.ts+++ b/packages/core/src/markdown/unknown-mdast-guard.ts@@ -1,3 +1,4 @@+import type { Root as MdastRoot } from 'mdast';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/whitespace-char-ref.ts b/packages/core/src/markdown/whitespace-char-ref.tsindex d578476a7..ac9f5239b 100644--- a/packages/core/src/markdown/whitespace-char-ref.ts+++ b/packages/core/src/markdown/whitespace-char-ref.ts@@ -1,3 +1,4 @@+const INLINE_WHITESPACE_BY_CODE: ReadonlyMap<number, string> = new Map([[0x20, ' '],[0x09, '\t'],diff --git a/packages/core/src/markdown/wiki-link-micromark.ts b/packages/core/src/markdown/wiki-link-micromark.tsindex 6dad26200..afd9936fe 100644--- a/packages/core/src/markdown/wiki-link-micromark.ts+++ b/packages/core/src/markdown/wiki-link-micromark.ts@@ -17,6 +17,7 @@ declare module 'micromark-util-types' {}}+const CODE_BANG = 33; // !const CODE_LBRACKET = 91; // [const CODE_RBRACKET = 93; // ]@@ -260,6 +261,7 @@ export function wikiLinkSyntax(): Extension {};}+function enterWikiLink(this: CompileContext, token: Token) {this.enter({@@ -385,6 +387,7 @@ export const wikiLinkToMarkdown: {unsafe: [{ character: '[', inConstruct: ['phrasing'] }],};+const MICROMARK_EXT = wikiLinkSyntax();export function remarkWikiLink(this: Processor) {diff --git a/packages/server/src/comments/anchor.test.ts b/packages/server/src/comments/anchor.test.tsindex 587f99445..872b17a7c 100644--- a/packages/server/src/comments/anchor.test.ts+++ b/packages/server/src/comments/anchor.test.ts@@ -1,5 +1,11 @@import { describe, expect, test } from 'vitest';-import { assertAnchorConsistent, createAnchor, refind } from './anchor.ts';+import {+ assertAnchorConsistent,+ bestByContext,+ createAnchor,+ literalSpans,+ refind,+} from './anchor.ts';describe('createAnchor', () => {test('captures the exact quote and surrounding context', () => {@@ -191,3 +197,58 @@ describe('refind — a passage that was edited, not removed', () => {}});});++/**+ * Ranking a repeated passage whose only distinguishing context is in an+ * adjacent block.+ *+ * The captured context joins blocks with a single `\n`; the body separates them+ * with `\n\n` and spends `- ` on every list item. Scored byte-exact, that+ * disagreed at the first seam character and returned zero for every candidate,+ * so the ranking was inert and the caller took the first hit — persisting a+ * comment on the third item against the first.+ */+describe('bestByContext across a block seam', () => {+ const BODY = ['- hi', '- hi', '- hi', '', 'the marker paragraph', '', '- hi', '- hi'].join('\n');++ /** `[start, end)` of the nth (1-based) list item's text. */+ function nth(n: number): number {+ let at = -1;+ for (let i = 0; i < n; i += 1) at = BODY.indexOf('- hi', at + 1);+ return at + 2;+ }++ test('the fixture really does repeat (guards the guard)', () => {+ expect(literalSpans(BODY, 'hi').length).toBe(5);+ });++ test('ranks the item whose SUFFIX reaches the marker block', () => {+ const hits = literalSpans(BODY, 'hi');+ const ranked = bestByContext(BODY, hits, {+ prefix: 'hi\nhi\n',+ suffix: '\nthe marker paragraph\nhi\nhi',+ });+ expect(ranked[0]?.start).toBe(nth(3));+ });++ test('ranks the item whose PREFIX reaches the marker block', () => {+ const hits = literalSpans(BODY, 'hi');+ const ranked = bestByContext(BODY, hits, {+ prefix: 'hi\nhi\nhi\nthe marker paragraph\n',+ suffix: '\nhi',+ });+ expect(ranked[0]?.start).toBe(nth(4));+ });++ test('markdown emphasis inside the window is tolerated too', () => {+ const body = ['- hi', '- hi', '', 'a **bold** marker', '', '- hi'].join('\n');+ const hits = literalSpans(body, 'hi');+ const ranked = bestByContext(body, hits, {+ prefix: 'hi\n',+ suffix: '\na bold marker\nhi',+ });+ let at = body.indexOf('- hi');+ at = body.indexOf('- hi', at + 1);+ expect(ranked[0]?.start).toBe(at + 2);+ });+});diff --git a/packages/server/src/comments/anchor.ts b/packages/server/src/comments/anchor.tsindex 7f5a0e9eb..98d4c786d 100644--- a/packages/server/src/comments/anchor.ts+++ b/packages/server/src/comments/anchor.ts@@ -8,6 +8,7 @@* between multiple exact hits.*/+import { contextMatchScore, rewriteCeiling } from '@inkeep/open-knowledge-core';import type { Anchor } from './types.ts';/** Hypothesis uses 32 chars of context for web text; a reasonable markdown default. */@@ -32,17 +33,6 @@ export type RefindResult =}| { status: 'orphaned' };-/**- * How much the passage may grow when recovered from its brackets.- *- * Editing inside a commented passage is normal ("needs space" → "needs more- * space"); replacing whole paragraphs between the same two boundaries is not,- * and silently swallowing them would attach the comment to text nobody pointed- * at. Generous but finite: past this, treat it as a replacement and orphan.- */-const MAX_REWRITE_GROWTH = 4;-const REWRITE_GROWTH_FLOOR = 64;-/*** Build an anchor for the selection `[start, end)` against `body`. Captures the* exact quote plus context on each side, widening the context until@@ -105,11 +95,15 @@ export function literalSpans(haystack: string, needle: string): Span[] {* invents one, and with no context every candidate ties and the whole set comes* back untouched.*- * Scoring is a raw common-character run on each side, deliberately tolerant:- * the context may have been captured against RENDERED text while `body` is- * markdown, so an exact comparison would collapse to zero the moment a `**`- * falls inside the window. A partial run still ranks the right occurrence first,- * and every candidate is scored the same way.+ * Scoring is `contextMatchScore`, shared with the editor's copy of this+ * decision so the two cannot rank the same thread differently. It has to be+ * tolerant, and of more than this comment once claimed: the context was+ * captured against RENDERED text while `body` is markdown, so an exact+ * comparison collapses to zero not only when a `**` falls inside the window but+ * — far more often — at the very first block seam, where the captured text has+ * a single `\n` and the body has `\n\n`, `- `, `> ` or `#`. It scored zero for+ * every candidate on any context reaching past its own block, which is most of+ * them, and the ranking silently did nothing.*/export function bestByContext<T extends Span>(body: string,@@ -121,9 +115,8 @@ export function bestByContext<T extends Span>(let best: T[] = [];let bestScore = -1;for (const hit of hits) {- const score =- commonSuffixLen(prefix, body.slice(0, hit.start)) +- commonPrefixLen(suffix, body.slice(hit.end));+ // `body` is markdown, so syntax is elastic here as well as whitespace.+ const score = contextMatchScore(body, hit, { prefix, suffix }, { syntaxIn: 'haystack' });if (score > bestScore) {bestScore = score;best = [hit];@@ -213,7 +206,7 @@ function refindBetweenBrackets(body: string, anchor: Anchor): RefindResult {if (end < start) return { status: 'orphaned' };// A passage edited down to nothing is a removal, not an edit.if (end === start) return { status: 'orphaned' };- const ceiling = Math.max(exact.length * MAX_REWRITE_GROWTH, exact.length + REWRITE_GROWTH_FLOOR);+ const ceiling = rewriteCeiling(exact.length);if (end - start > ceiling) return { status: 'orphaned' };return { status: 'anchored', start, end, rewritten: true };}@@ -243,19 +236,3 @@ function allOccurrences(haystack: string, needle: string): number[] {function isUniqueTriple(body: string, prefix: string, exact: string, suffix: string): boolean {return countOccurrences(body, prefix + exact + suffix) === 1;}--/** Length of the longest common prefix of `a` and `b`. */-function commonPrefixLen(a: string, b: string): number {- const n = Math.min(a.length, b.length);- let i = 0;- while (i < n && a[i] === b[i]) i += 1;- return i;-}--/** Length of the longest common suffix of `a` and `b`. */-function commonSuffixLen(a: string, b: string): number {- const n = Math.min(a.length, b.length);- let i = 0;- while (i < n && a[a.length - 1 - i] === b[b.length - 1 - i]) i += 1;- return i;-}