4b2af3d34a
Аудит при подготовке #413 нашёл дрейфующие дубли между packages/mcp и packages/prosemirror-markdown. Четыре дедупа (поведение тулов не меняется): 1. node-ops: форк ~960 строк сведён в ОДНУ копию в prosemirror-markdown (живая mcp-версия — строгое надмножество замороженного #293-seed'а пакета; сверено по git-истории, новая пакет-копия байт-в-байт == прежней mcp-копии). Barrel-экспорт полной поверхности; mcp/client.ts/page-search.ts/transforms.ts/collaboration.ts импортируют из пакета; тесты переехали. node-ops тянет stripInlineMarkdown -> пакет-локальная text-normalize.ts несёт только этот примитив (mcp-версия — домен #408; заголовок документирует дубликацию + источник истины). 2. footnote-lex/footnote-analyze (vestigial legacy [^id]: диагностика): сведены к одному fence-aware предупреждению 'reference-style footnotes -> use ^[...]' (полезно для класса #410); footnote-lex удалён. 3. footnote-authoring -> примитивы (footnoteContentKey/makeFootnoteDefinition/ generateFootnoteId) перенесены в пакетный footnote.ts, одна реализация конвенции. 4. parse-node-arg -> перенесён в prosemirror-markdown (не mcp: сервер CommonJS не импортирует ESM-only @docmost/mcp, но нативно импортирует пакет), обе копии удалены, консьюмеры перенаправлены. canonicalizeFootnotes/ENFORCEMENT RULE #228 и comment-anchor/json-edit/text-normalize (mcp) не тронуты. API-поверхность node-ops оставлена чистой для #409/#413. closes #414 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
100 lines
4.0 KiB
TypeScript
100 lines
4.0 KiB
TypeScript
/**
|
|
* Locator normalization: strip inline markdown wrappers and trailing
|
|
* decoration from a LOCATOR string so a find/anchor that the model wrote with
|
|
* markdown (or a stray emoji) can still match the document's plain text.
|
|
*
|
|
* This is used ONLY as a fallback for LOCATING (after an exact match fails);
|
|
* it is never applied to replacement text or inserted node content, so no
|
|
* formatting is ever lost.
|
|
*
|
|
* Scope note (#414): this package-local copy exists so `node-ops.ts` — which
|
|
* lives here now (the single canonical copy) — can resolve its markdown-tolerant
|
|
* anchor fallback without a circular dependency back on `@docmost/mcp`. It
|
|
* intentionally carries ONLY `stripInlineMarkdown` (the primitive `node-ops`
|
|
* needs); the mcp-side `text-normalize.ts` (which additionally serves
|
|
* `json-edit.ts` via `stripBalancedWrappers`) is the subject of a separate
|
|
* dedup task and is left untouched here.
|
|
*/
|
|
|
|
/** Maximum unwrap passes, so pathological/nested input cannot loop forever. */
|
|
const MAX_PASSES = 8;
|
|
|
|
/**
|
|
* Inline emphasis/code/strikethrough wrappers, strong BEFORE emphasis so
|
|
* `**x**` collapses to `x` rather than leaving a stray `*x*`. Each pattern is
|
|
* non-greedy and capture group 1 is the inner text. Applied repeatedly until
|
|
* the string stops changing (nested wrappers like `**_x_**`).
|
|
*/
|
|
const WRAPPER_PATTERNS: RegExp[] = [
|
|
/\*\*([^*]+?)\*\*/g, // **x**
|
|
/__([^_]+?)__/g, // __x__
|
|
/~~([^~]+?)~~/g, // ~~x~~
|
|
/\*([^*]+?)\*/g, // *x*
|
|
/_([^_]+?)_/g, // _x_
|
|
/``([^`]+?)``/g, // ``x``
|
|
/`([^`]+?)`/g, // `x`
|
|
];
|
|
|
|
/** Links/images -> their visible text. `!?` covers both `[t](u)` and ``. */
|
|
const LINK_IMAGE_RE = /!?\[([^\]]*)\]\([^)]*\)/g;
|
|
|
|
/**
|
|
* Apply the two balanced/link passes: first collapse links/images to their
|
|
* visible text, then collapse balanced inline wrappers repeatedly until stable.
|
|
* Does NOT trim decoration, does NOT guard against an empty result — it returns
|
|
* exactly the transformed string.
|
|
*/
|
|
function stripWrappersAndLinks(s: string): string {
|
|
// 1. Links/images -> their visible text.
|
|
let out = s.replace(LINK_IMAGE_RE, "$1");
|
|
|
|
// 2. Strip balanced wrappers, repeating until the string is stable so nested
|
|
// wrappers (`**_x_**`) and adjacent runs both collapse.
|
|
for (let pass = 0; pass < MAX_PASSES; pass++) {
|
|
const before = out;
|
|
for (const re of WRAPPER_PATTERNS) {
|
|
out = out.replace(re, "$1");
|
|
}
|
|
if (out === before) break;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Conservatively strip inline markdown from a locator string.
|
|
*
|
|
* Deterministic, order-fixed steps:
|
|
* 1. Links/images: `[text](url)` -> `text`, `` -> `alt`.
|
|
* 2. Balanced inline wrappers (strong before emphasis, code, strikethrough),
|
|
* applied repeatedly until stable for nested cases.
|
|
* 3. Trim leading/trailing decoration only: whitespace, leftover marker chars
|
|
* (`* _ ~ \``) and emoji. Letters/digits and sentence punctuation (`.`/`,`
|
|
* etc.) are NEVER trimmed.
|
|
*
|
|
* If the result is empty (e.g. the input was only markers like `***`), the
|
|
* ORIGINAL string is returned so a locator can never normalize down to "" and
|
|
* match everything.
|
|
*/
|
|
export function stripInlineMarkdown(s: string): string {
|
|
if (typeof s !== "string" || s.length === 0) return s;
|
|
|
|
// 1 + 2. Shared link/image and balanced-wrapper passes.
|
|
let out = stripWrappersAndLinks(s);
|
|
|
|
// 3. Trim leading/trailing decoration: whitespace, leftover markdown markers,
|
|
// and emoji (Extended_Pictographic plus the VS16 / ZWJ joiners, plus the
|
|
// regional-indicator range U+1F1E6–U+1F1FF for flag emoji, which are NOT
|
|
// Extended_Pictographic). The `u` flag enables the Unicode property escape.
|
|
// Anchored runs only — interior text and sentence punctuation are untouched.
|
|
const DECORATION =
|
|
"[\\s*_~\\x60\\p{Extended_Pictographic}\\u{1F1E6}-\\u{1F1FF}\\u{FE0F}\\u{200D}]+";
|
|
out = out
|
|
.replace(new RegExp("^" + DECORATION, "u"), "")
|
|
.replace(new RegExp(DECORATION + "$", "u"), "");
|
|
|
|
// 4. Never normalize a locator down to nothing.
|
|
if (out.length === 0) return s;
|
|
|
|
return out;
|
|
}
|