Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): correct input-rule replacement ranges across inline objects (#3180)

Nick Gomez05db8df6d6a0
diff --git a/.changeset/input-rule-atom-ranges.md b/.changeset/input-rule-atom-ranges.md
new file mode 100644
index 000000000..05bffb40a
--- /dev/null
+++ b/.changeset/input-rule-atom-ranges.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Typing a markdown shortcut around an image, wiki link, tag, inline math, footnote reference, or inline JSX no longer breaks the editor. Closing a shortcut whose text spanned one of these — typing the last `*` of `**see ![img](a.png) here**`, for example — computed a replacement range that was five positions too wide per object, so the keystroke either threw and was lost, or, in a longer paragraph, silently rewrote the wrong span and ate the text in front of it. Inline JSX was skewed by an amount that grew with its own length, so a short one could work and a realistic one could not. Every inline object now reports its width to the shortcut matcher as the single position it actually occupies, which fixes the range for every shortcut rather than any one of them.
diff --git a/packages/app/tests/dom/input-rule-atom-ranges.dom.test.tsx b/packages/app/tests/dom/input-rule-atom-ranges.dom.test.tsx
new file mode 100644
index 000000000..b90aa6995
--- /dev/null
+++ b/packages/app/tests/dom/input-rule-atom-ranges.dom.test.tsx
@@ -0,0 +1,182 @@
+/**
+ * Input-rule replacement ranges across inline objects, at the mounted rung.
+ *
+ * The range a rule replaces is arithmetic over the length of the string the
+ * runner matched (`from - (match[0].length - text.length)`), so it is only
+ * correct while every node before the caret contributed as many characters as
+ * it occupies positions. When one does not, a completion keystroke either
+ * throws `RangeError` out of `handleTextInput` — the keystroke crashes — or,
+ * with enough text ahead of it to keep the range positive, silently rewrites
+ * the wrong span. Both are reachable through the shipped `**bold**` rule.
+ *
+ * The schema-level contract is pinned in core. This tier is the one that can
+ * fail the way a user does: the real app roster, a real mounted editor with
+ * focus, and the same `handleTextInput` walk the view performs on text input.
+ *
+ */
+
+import { cleanup } from '@testing-library/react';
+import { Editor, getSchema } from '@tiptap/core';
+import type { MarkType, Node as PMNode } from '@tiptap/pm/model';
+import type { EditorView } from '@tiptap/pm/view';
+import { afterEach, describe, expect, test } from 'vitest';
+import { sharedExtensions } from '../../src/editor/extensions/shared';
+
+type TextInputHandler = (view: EditorView, from: number, to: number, text: string) => boolean;
+
+const editors: Editor[] = [];
+const containers: HTMLElement[] = [];
+
+afterEach(() => {
+ // Every mount is torn down here rather than at each call site: the
+ // parametrised cases below mount one editor per inline node, and a zombie
+ // left in `document.body` would be visible to any later test that reads
+ // `document.activeElement` or walks the body's children.
+ for (const editor of editors.splice(0)) editor.destroy();
+ for (const container of containers.splice(0)) container.remove();
+ cleanup();
+});
+
+function mountEditor(): Editor {
+ const container = document.createElement('div');
+ document.body.appendChild(container);
+ containers.push(container);
+ const editor = new Editor({ element: container, extensions: sharedExtensions, editable: true });
+ editors.push(editor);
+ // ProseMirror's own focus rather than `commands.focus()`, which defers the
+ // real DOM focus into a frame that lands after every synchronous step here.
+ editor.view.focus();
+ return editor;
+}
+
+/**
+ * Feed one character the way the view does on text input: walk every plugin's
+ * `handleTextInput` until one claims it, and insert plainly when none does.
+ */
+function typeCharacter(editor: Editor, character: string): void {
+ const { from, to } = editor.state.selection;
+ const handled =
+ editor.view.someProp('handleTextInput', (handler) =>
+ (handler as TextInputHandler)(editor.view, from, to, character),
+ ) ?? false;
+ if (!handled) editor.view.dispatch(editor.state.tr.insertText(character));
+}
+
+/** One instance of `name`, filled with text when the node takes content. */
+function inlineObject(editor: Editor, name: string, body: string): PMNode | null {
+ const type = editor.schema.nodes[name];
+ if (!type) return null;
+ return type.isLeaf ? type.createAndFill() : type.createAndFill(null, [editor.schema.text(body)]);
+}
+
+/** Seed `<lead><object><tail>` in one paragraph and park the caret at its end. */
+function seedParagraph(editor: Editor, lead: string, object: PMNode, tail: string): void {
+ const { schema } = editor;
+ const paragraph = schema.nodes.paragraph?.create(null, [
+ schema.text(lead),
+ object,
+ schema.text(tail),
+ ]);
+ if (!paragraph) throw new Error('paragraph missing from schema');
+ const doc = schema.nodes.doc?.createAndFill(null, [paragraph]);
+ if (!doc) throw new Error('doc missing from schema');
+ editor.view.dispatch(editor.state.tr.replaceWith(0, editor.state.doc.content.size, doc.content));
+ editor.commands.setTextSelection(editor.state.doc.content.size - 1);
+}
+
+/**
+ * Names of every inline node the roster can put in a rule's match window.
+ *
+ * Built from the schema rather than a mounted editor: this runs at module
+ * scope, before any `afterEach` exists to tear a mount down, and the names are
+ * a property of the roster rather than of any editor over it.
+ */
+const INLINE_OBJECT_NAMES: string[] = Object.entries(getSchema(sharedExtensions).nodes)
+ .filter(([name, type]) => type.isInline && name !== 'text')
+ .map(([name]) => name);
+
+describe('a completion keystroke whose match window spans an inline object', () => {
+ test('the roster still has inline objects to guard', () => {
+ // Guards the parametrised cases below against silently becoming vacuous if
+ // the roster ever loses its inline nodes.
+ expect(INLINE_OBJECT_NAMES.length).toBeGreaterThanOrEqual(8);
+ });
+
+ test.each(INLINE_OBJECT_NAMES)('completes `**bold**` across %s', (name) => {
+ const editor = mountEditor();
+ const object = inlineObject(editor, name, 'x');
+ expect(object).not.toBeNull();
+ seedParagraph(editor, '**see ', object as PMNode, ' here*');
+
+ expect(() => typeCharacter(editor, '*')).not.toThrow();
+
+ const paragraph = editor.state.doc.firstChild;
+ expect(paragraph).not.toBeNull();
+ // The delimiters are consumed, not left stranded in the text...
+ expect((paragraph as PMNode).textContent).not.toContain('*');
+ // ...the surviving text is exactly what the user wrote between them (a
+ // node with children contributes its own text, a leaf contributes none)...
+ expect((paragraph as PMNode).textContent).toBe(`see ${(object as PMNode).textContent} here`);
+ // ...the object itself survived the replacement...
+ let objectSurvived = false;
+ (paragraph as PMNode).forEach((child) => {
+ if (child.type.name === name) objectSurvived = true;
+ });
+ expect(objectSurvived).toBe(true);
+ // ...and the mark covers the delimited span exactly. `rangeHasMark` would
+ // not show this — it is an ANY check, so it also holds when the mark starts
+ // several characters too far left, which is the pre-fix shape.
+ const strong = editor.schema.marks.strong;
+ expect(strong).toBeDefined();
+ const unmarked: string[] = [];
+ // Walk to the leaves: a mark applied over a range lands on the text inside
+ // a node with children, not on the wrapper, so checking direct children
+ // alone would report a correctly-marked inline element as unmarked.
+ (paragraph as PMNode).descendants((node) => {
+ if (!node.isText && !node.isLeaf) return true;
+ if (!strong?.isInSet(node.marks)) unmarked.push(node.type.name);
+ return false;
+ });
+ expect(unmarked, 'every leaf of the delimited span carries strong').toEqual([]);
+ });
+
+ test.each(
+ INLINE_OBJECT_NAMES,
+ )('leaves preceding text untouched when the range stays positive (%s)', (name) => {
+ // With a short paragraph a skewed range runs off the document and throws.
+ // A long lead keeps it in bounds, where the same skew is silent: the rule
+ // rewrites a span that starts too far left and eats the text before it.
+ const editor = mountEditor();
+ const object = inlineObject(editor, name, 'x');
+ expect(object).not.toBeNull();
+ const lead = `${'lorem ipsum dolor sit amet '.repeat(2)}**see `;
+ seedParagraph(editor, lead, object as PMNode, ' here*');
+
+ expect(() => typeCharacter(editor, '*')).not.toThrow();
+
+ const text = editor.state.doc.firstChild?.textContent ?? '';
+ expect(text.startsWith('lorem ipsum dolor sit amet '.repeat(2))).toBe(true);
+ expect(text).not.toContain('*');
+ // The silent form of the skew marks text the user never delimited, so pin
+ // that negative directly rather than inferring it from the text alone.
+ const strong = editor.schema.marks.strong;
+ expect(strong).toBeDefined();
+ const leadEnd = 1 + 'lorem ipsum dolor sit amet '.repeat(2).length;
+ expect(editor.state.doc.rangeHasMark(1, leadEnd, strong as MarkType)).toBe(false);
+ });
+
+ test('a node with children is faithful regardless of how much it holds', () => {
+ // A wrapper's skew scales with its content, so a short body can land on the
+ // correct range by coincidence while a realistic one runs off the document.
+ for (const body of ['a', 'ab', 'abc', '<Foo bar="baz" />']) {
+ const editor = mountEditor();
+ const object = inlineObject(editor, 'jsxInline', body);
+ expect(object).not.toBeNull();
+ seedParagraph(editor, '**see ', object as PMNode, ' here*');
+
+ expect(() => typeCharacter(editor, '*')).not.toThrow();
+
+ expect(editor.state.doc.firstChild?.textContent).toBe(`see ${body} here`);
+ }
+ });
+});
diff --git a/packages/app/tests/dom/plain-text-clipboard.dom.test.tsx b/packages/app/tests/dom/plain-text-clipboard.dom.test.tsx
new file mode 100644
index 000000000..2223f08de
--- /dev/null
+++ b/packages/app/tests/dom/plain-text-clipboard.dom.test.tsx
@@ -0,0 +1,133 @@
+/**
+ * Plain-text copy from an editor that adds no `editorProps` of its own.
+ *
+ * `renderText` makes every inline node contribute one character per document
+ * position so the input-rule runner computes correct replacement ranges. TipTap
+ * stores that on the NodeSpec as `toText`, which its built-in
+ * `clipboardTextSerializer` also reads — with the opposite need. `getTextBetween`
+ * swaps the serializer's output in for the node and stops descending, so without
+ * a counterweight a copied image would carry the placeholder and a copied inline
+ * JSX element would lose its text.
+ *
+ * The read-only viewers mount the shared roster with no `editorProps`, so this
+ * exercises the roster ALONE — no overrides, no disabled core extensions. That
+ * is what makes it a pin for editors added later rather than for today's two.
+ *
+ */
+
+import { cleanup } from '@testing-library/react';
+import { Editor } from '@tiptap/core';
+import type { Node as PMNode } from '@tiptap/pm/model';
+import { Slice } from '@tiptap/pm/model';
+import { afterEach, describe, expect, test } from 'vitest';
+import { INLINE_OBJECT_PLACEHOLDER } from '../../../core/src/extensions/input-rule-text.ts';
+import { sharedExtensions } from '../../src/editor/extensions/shared';
+
+const editors: Editor[] = [];
+const containers: HTMLElement[] = [];
+
+afterEach(() => {
+ for (const editor of editors.splice(0)) editor.destroy();
+ for (const container of containers.splice(0)) container.remove();
+ cleanup();
+});
+
+/** An editor with the shared roster and nothing else — a viewer's shape. */
+function mountBareEditor(content?: PMNode): Editor {
+ const container = document.createElement('div');
+ document.body.appendChild(container);
+ containers.push(container);
+ const editor = new Editor({
+ element: container,
+ ...(content ? { content: content.toJSON() } : {}),
+ extensions: sharedExtensions,
+ });
+ editors.push(editor);
+ return editor;
+}
+
+/**
+ * The `text/plain` payload the view would put on the clipboard for the doc.
+ *
+ * Selects the document first: the serializer this guards against reads the
+ * SELECTION rather than the slice it is handed, so serializing a freshly
+ * mounted editor would return the empty string and every absence assertion
+ * below would hold vacuously.
+ */
+function copyWholeDocAsPlainText(editor: Editor): string {
+ editor.commands.selectAll();
+ const { doc } = editor.state;
+ const slice = new Slice(doc.content, 0, 0);
+ const serializer = editor.view.someProp('clipboardTextSerializer');
+ expect(serializer, 'no clipboardTextSerializer on a bare roster').toBeDefined();
+ const text = (serializer as (s: Slice, v: unknown) => string)(slice, editor.view);
+ expect(text, 'empty payload would make the assertions vacuous').not.toBe('');
+ return text;
+}
+
+describe('plain-text copy from an editor with no editorProps of its own', () => {
+ test('an inline leaf contributes no placeholder', () => {
+ const probe = mountBareEditor();
+ const { schema } = probe;
+ const image = schema.nodes.image?.create({ src: 'a.png', alt: 'a' });
+ expect(image).toBeDefined();
+ const doc = schema.nodes.doc?.createAndFill(null, [
+ schema.nodes.paragraph?.create(null, [
+ schema.text('before '),
+ image as PMNode,
+ schema.text(' after'),
+ ]) as PMNode,
+ ]);
+ expect(doc).not.toBeNull();
+
+ const text = copyWholeDocAsPlainText(mountBareEditor(doc as PMNode));
+
+ expect(text).not.toContain(INLINE_OBJECT_PLACEHOLDER);
+ expect(text).toBe('before after');
+ });
+
+ test('a node with children keeps its visible text', () => {
+ // The sharper half: the serializer path stops descending once it has
+ // handled a node, so a placeholder here would not merely be added — it
+ // would REPLACE the element's text.
+ const probe = mountBareEditor();
+ const { schema } = probe;
+ const jsx = schema.nodes.jsxInline?.create(null, [schema.text('<Foo bar="baz" />')]);
+ expect(jsx).toBeDefined();
+ const doc = schema.nodes.doc?.createAndFill(null, [
+ schema.nodes.paragraph?.create(null, [schema.text('see '), jsx as PMNode]) as PMNode,
+ ]);
+ expect(doc).not.toBeNull();
+
+ const text = copyWholeDocAsPlainText(mountBareEditor(doc as PMNode));
+
+ expect(text).toContain('<Foo bar="baz" />');
+ expect(text).not.toContain(INLINE_OBJECT_PLACEHOLDER);
+ });
+
+ test('every inline node in the roster stays out of the payload', () => {
+ const probe = mountBareEditor();
+ const { schema } = probe;
+ for (const [name, type] of Object.entries(schema.nodes)) {
+ if (!type.isInline || name === 'text') continue;
+ const node = type.isLeaf
+ ? type.createAndFill()
+ : type.createAndFill(null, [schema.text('x')]);
+ if (!node) continue;
+ const doc = schema.nodes.doc?.createAndFill(null, [
+ schema.nodes.paragraph?.create(null, [schema.text('a'), node, schema.text('b')]) as PMNode,
+ ]);
+ if (!doc) continue;
+
+ const text = copyWholeDocAsPlainText(mountBareEditor(doc));
+
+ expect(text, `${name} leaked the placeholder into text/plain`).not.toContain(
+ INLINE_OBJECT_PLACEHOLDER,
+ );
+ // The surrounding text must survive too: the serializer path replaces a
+ // node AND stops descending, so a regression can drop text, not just add.
+ expect(text.startsWith('a'), `${name} lost the text before it`).toBe(true);
+ expect(text.endsWith('b'), `${name} lost the text after it`).toBe(true);
+ }
+ });
+});
diff --git a/packages/core/src/checkpoint-kind-registry.test.ts b/packages/core/src/checkpoint-kind-registry.test.ts
index 0631f138f..4449f2774 100644
--- a/packages/core/src/checkpoint-kind-registry.test.ts
+++ b/packages/core/src/checkpoint-kind-registry.test.ts
@@ -5,95 +5,10 @@ import {
type CheckpointKind,
formatCheckpointBodyLine,
isSurfacedCheckpointKind,
- type ParsedCheckpoint,
parseCheckpoint,
+ CHECKPOINT_SAMPLE_BY_KIND as SAMPLE_BY_KIND,
} from './shadow-repo-layout.ts';
-// One representative checkpoint per kind. Typed `satisfies Record<CheckpointKind,
-// …>` so a kind added to ParsedCheckpoint without a sample here fails to
-// compile — the round-trip test below then proves the parser and the registry
-// agree on that new kind at runtime, not just at the type level.
-const SAMPLE_BY_KIND = {
- 'bridge-merge-loss': {
- kind: 'bridge-merge-loss',
- docName: 'notes',
- size: 12,
- metadata: { lostSubstrings: ['dropped'] },
- },
- 'producer-guard-loss': {
- kind: 'producer-guard-loss',
- docName: 'notes',
- size: 12,
- metadata: { construct: 'tableCell' },
- },
- 'observer-a-duplication': {
- kind: 'observer-a-duplication',
- docName: 'notes',
- size: 12,
- metadata: { duplicatedLineCount: 2 },
- },
- 'external-change-rescue': {
- kind: 'external-change-rescue',
- docName: 'notes',
- size: 12,
- metadata: { incomingDiskSha: 'abc123' },
- },
- 'defer-exhaustion-loss': {
- kind: 'defer-exhaustion-loss',
- docName: 'notes',
- size: 12,
- metadata: { deferCount: 8 },
- },
- 'observer-a-apply-loss': {
- kind: 'observer-a-apply-loss',
- docName: 'notes',
- size: 12,
- metadata: { lostSubstrings: ['dropped'] },
- },
- 'bridge-derive-loss': {
- kind: 'bridge-derive-loss',
- docName: 'notes',
- size: 12,
- metadata: { lostSubstrings: ['dropped'] },
- },
- 'bridge-backstop-trip': {
- kind: 'bridge-backstop-trip',
- docName: 'notes',
- size: 12,
- metadata: { rounds: 8 },
- },
- 'persistence-reconcile-loss': {
- kind: 'persistence-reconcile-loss',
- docName: 'notes',
- size: 12,
- metadata: { atRiskLines: 1, witnessAvailable: true },
- },
- 'persistence-duplication-reset': {
- kind: 'persistence-duplication-reset',
- docName: 'notes',
- size: 12,
- metadata: { copies: 2, fragmentChildren: 18 },
- },
- 'persistence-divergence-realign': {
- kind: 'persistence-divergence-realign',
- docName: 'notes',
- size: 12,
- metadata: { diskBytes: 37, discardedBytes: 79 },
- },
- 'managed-artifact-reconcile': {
- kind: 'managed-artifact-reconcile',
- docName: '.ok/templates/daily',
- size: 12,
- metadata: { diskBytes: 41, discardedBytes: 66 },
- },
- 'auto-consolidation': {
- kind: 'auto-consolidation',
- docName: null,
- size: null,
- metadata: { foldedRefs: 3, trigger: 'dead-chain' },
- },
-} satisfies Record<CheckpointKind, ParsedCheckpoint>;
-
describe('checkpoint-kind registry', () => {
it('registers every kind the parser can produce, and no phantom kinds', () => {
for (const sample of Object.values(SAMPLE_BY_KIND)) {
diff --git a/packages/core/src/checkpoint-kinds.ts b/packages/core/src/checkpoint-kinds.ts
index 8b58d168b..151e48c55 100644
--- a/packages/core/src/checkpoint-kinds.ts
+++ b/packages/core/src/checkpoint-kinds.ts
@@ -611,6 +611,106 @@ export const CHECKPOINT_KIND_REGISTRY = {
/** Every registered checkpoint kind, for exhaustive iteration. */
export const CHECKPOINT_KINDS = Object.keys(CHECKPOINT_KIND_REGISTRY) as CheckpointKind[];
+/**
+ * One representative checkpoint per kind, in the shape `parseCheckpoint`
+ * returns for it.
+ *
+ * This lives beside the kinds rather than in the round-trip test because a
+ * `satisfies` clause only binds where TypeScript reads it, and every package
+ * tsconfig excludes `**\/*.test.ts` — a guard written there compiles nothing
+ * and fails nothing. Sited here it is load-bearing: a kind added to
+ * `ParsedCheckpoint` with no sample, or a sample whose metadata does not match
+ * the kind it is keyed under, fails `tsc` in the same file as the registry
+ * entry it is missing. The mapped type is what makes the second half true:
+ * `Record<CheckpointKind, ParsedCheckpoint>` would accept any union member
+ * under any key, so a sample keyed under one kind while carrying another
+ * would compile.
+ *
+ * The parser and registry still have to AGREE at runtime, which no type can
+ * show; the round-trip test drives these samples through
+ * `formatCheckpointBodyLine` and `parseCheckpoint` to prove it.
+ */
+export const CHECKPOINT_SAMPLE_BY_KIND = {
+ 'bridge-merge-loss': {
+ kind: 'bridge-merge-loss',
+ docName: 'notes',
+ size: 12,
+ metadata: { lostSubstrings: ['dropped'] },
+ },
+ 'producer-guard-loss': {
+ kind: 'producer-guard-loss',
+ docName: 'notes',
+ size: 12,
+ metadata: { construct: 'tableCell' },
+ },
+ 'observer-a-duplication': {
+ kind: 'observer-a-duplication',
+ docName: 'notes',
+ size: 12,
+ metadata: { duplicatedLineCount: 2 },
+ },
+ 'external-change-rescue': {
+ kind: 'external-change-rescue',
+ docName: 'notes',
+ size: 12,
+ metadata: { incomingDiskSha: 'abc123' },
+ },
+ 'defer-exhaustion-loss': {
+ kind: 'defer-exhaustion-loss',
+ docName: 'notes',
+ size: 12,
+ metadata: { deferCount: 8 },
+ },
+ 'observer-a-apply-loss': {
+ kind: 'observer-a-apply-loss',
+ docName: 'notes',
+ size: 12,
+ metadata: { lostSubstrings: ['dropped'] },
+ },
+ 'bridge-derive-loss': {
+ kind: 'bridge-derive-loss',
+ docName: 'notes',
+ size: 12,
+ metadata: { lostSubstrings: ['dropped'] },
+ },
+ 'bridge-backstop-trip': {
+ kind: 'bridge-backstop-trip',
+ docName: 'notes',
+ size: 12,
+ metadata: { rounds: 8 },
+ },
+ 'persistence-reconcile-loss': {
+ kind: 'persistence-reconcile-loss',
+ docName: 'notes',
+ size: 12,
+ metadata: { atRiskLines: 1, witnessAvailable: true },
+ },
+ 'persistence-duplication-reset': {
+ kind: 'persistence-duplication-reset',
+ docName: 'notes',
+ size: 12,
+ metadata: { copies: 2, fragmentChildren: 18 },
+ },
+ 'persistence-divergence-realign': {
+ kind: 'persistence-divergence-realign',
+ docName: 'notes',
+ size: 12,
+ metadata: { diskBytes: 37, discardedBytes: 79 },
+ },
+ 'managed-artifact-reconcile': {
+ kind: 'managed-artifact-reconcile',
+ docName: '.ok/templates/daily',
+ size: 12,
+ metadata: { diskBytes: 41, discardedBytes: 66 },
+ },
+ 'auto-consolidation': {
+ kind: 'auto-consolidation',
+ docName: null,
+ size: null,
+ metadata: { foldedRefs: 3, trigger: 'dead-chain' },
+ },
+} satisfies { [K in CheckpointKind]: Extract<ParsedCheckpoint, { kind: K }> };
+
/**
* Whether a checkpoint kind surfaces as an ordinary timeline row. `null` /
* `undefined` (a non-checkpoint row, or a checkpoint whose body line was
diff --git a/packages/core/src/extensions/footnote-reference.ts b/packages/core/src/extensions/footnote-reference.ts
index 9a0d45b74..e83762d11 100644
--- a/packages/core/src/extensions/footnote-reference.ts
+++ b/packages/core/src/extensions/footnote-reference.ts
@@ -18,6 +18,7 @@
*/
import { Node } from '@tiptap/core';
+import { renderInlineObjectText } from './input-rule-text.ts';
declare module '@tiptap/core' {
interface Commands<ReturnType> {
@@ -142,6 +143,8 @@ export const FootnoteReference = Node.create({
// before the inline-unknown fallback path.
priority: 60,
+ renderText: renderInlineObjectText,
+
addAttributes() {
return {
identifier: { default: '' },
diff --git a/packages/core/src/extensions/image-reference-fidelity.ts b/packages/core/src/extensions/image-reference-fidelity.ts
index d7b30e220..e892700b4 100644
--- a/packages/core/src/extensions/image-reference-fidelity.ts
+++ b/packages/core/src/extensions/image-reference-fidelity.ts
@@ -13,6 +13,7 @@
*/
import { Node } from '@tiptap/core';
+import { renderInlineObjectText } from './input-rule-text.ts';
export const ImageReferenceFidelity = Node.create({
name: 'imageReference',
@@ -22,6 +23,8 @@ export const ImageReferenceFidelity = Node.create({
atom: true,
priority: 60,
+ renderText: renderInlineObjectText,
+
addAttributes() {
return {
// Original alt text from `![alt][ref]`. Empty for shortcut/collapsed
diff --git a/packages/core/src/extensions/image-src-fidelity.ts b/packages/core/src/extensions/image-src-fidelity.ts
index 56a069f97..9af744f28 100644
--- a/packages/core/src/extensions/image-src-fidelity.ts
+++ b/packages/core/src/extensions/image-src-fidelity.ts
@@ -22,11 +22,14 @@
import { mergeAttributes } from '@tiptap/core';
import Image from '@tiptap/extension-image';
import { toDesktopAssetHref } from '../utils/asset-href.ts';
+import { renderInlineObjectText } from './input-rule-text.ts';
export const ImageSrcFidelity = Image.extend({
marks: '_', // legal mark carrier; see wiki-link.ts
priority: 60,
+ renderText: renderInlineObjectText,
+
addAttributes() {
return {
...this.parent?.(),
diff --git a/packages/core/src/extensions/input-rule-text.test.ts b/packages/core/src/extensions/input-rule-text.test.ts
new file mode 100644
index 000000000..232bced2a
--- /dev/null
+++ b/packages/core/src/extensions/input-rule-text.test.ts
@@ -0,0 +1,83 @@
+import { getSchema, getTextContentFromNodes } from '@tiptap/core';
+import type { NodeType, Node as PMNode } from '@tiptap/pm/model';
+import { describe, expect, it } from 'vitest';
+import { INLINE_OBJECT_PLACEHOLDER, renderInlineObjectText } from './input-rule-text.ts';
+import { sharedExtensions } from './shared.ts';
+
+const schema = getSchema(sharedExtensions);
+
+/** Every inline node that is not text — the nodes the runner needs a chunk for. */
+function inlineObjectTypes(): NodeType[] {
+ return Object.values(schema.nodes).filter((type) => type.isInline && type.name !== 'text');
+}
+
+/** One instance of `type`, filled with text when the node takes content. */
+function sample(type: NodeType): PMNode | null {
+ return type.isLeaf ? type.createAndFill() : type.createAndFill(null, [schema.text('foo')]);
+}
+
+describe('INLINE_OBJECT_PLACEHOLDER', () => {
+ it('is one UTF-16 unit, the unit match lengths are counted in', () => {
+ expect(INLINE_OBJECT_PLACEHOLDER).toHaveLength(1);
+ expect(INLINE_OBJECT_PLACEHOLDER.codePointAt(0)).toBe(0xfffc);
+ });
+
+ it('is not a delimiter any shipped input rule matches on', () => {
+ expect(INLINE_OBJECT_PLACEHOLDER).not.toMatch(/[*_$[\]()#~`\-+>!\\\s]/);
+ });
+});
+
+describe('renderInlineObjectText', () => {
+ it('yields one placeholder for a leaf — the single position it owns', () => {
+ const image = schema.nodes.image?.createAndFill();
+ expect(image).not.toBeNull();
+ expect(renderInlineObjectText({ node: image as PMNode })).toBe(INLINE_OBJECT_PLACEHOLDER);
+ });
+
+ it('yields two for a node with children — its open and close positions', () => {
+ const jsx = schema.nodes.jsxInline?.createAndFill(null, [schema.text('foo')]);
+ expect(jsx).not.toBeNull();
+ expect(renderInlineObjectText({ node: jsx as PMNode })).toBe(
+ INLINE_OBJECT_PLACEHOLDER.repeat(2),
+ );
+ // The children are walked separately, so the wrapper must not speak for them.
+ expect(renderInlineObjectText({ node: jsx as PMNode })).toHaveLength(
+ (jsx as PMNode).nodeSize - (jsx as PMNode).content.size,
+ );
+ });
+});
+
+describe('input-rule matching text is position-faithful', () => {
+ // The contract the range arithmetic `from - (match[0].length - text.length)`
+ // depends on: the derived string is as long as the span it describes. A node
+ // that breaks it skews the range for EVERY rule whose match window covers it,
+ // so this walks the whole schema rather than the nodes known to have broken.
+ it.each(
+ inlineObjectTypes().map((type) => [type.name, type] as const),
+ )('%s contributes exactly the positions it occupies', (_name, type) => {
+ const node = sample(type);
+ expect(node).not.toBeNull();
+ const paragraph = schema.nodes.paragraph?.create(null, [
+ schema.text('x'),
+ node as PMNode,
+ schema.text('y'),
+ ]);
+ expect(paragraph).toBeDefined();
+ const doc = schema.nodes.doc?.createAndFill(null, [paragraph as PMNode]);
+ expect(doc).not.toBeNull();
+ const $end = (doc as PMNode).resolve(1 + (paragraph as PMNode).content.size);
+
+ expect(getTextContentFromNodes($end)).toHaveLength($end.parentOffset);
+ });
+
+ it('never falls back to the six-character %leaf% sentinel', () => {
+ for (const type of inlineObjectTypes()) {
+ const node = sample(type);
+ if (!node) continue;
+ const paragraph = schema.nodes.paragraph?.create(null, [node]);
+ const doc = schema.nodes.doc?.createAndFill(null, [paragraph as PMNode]);
+ const $end = (doc as PMNode).resolve(1 + (paragraph as PMNode).content.size);
+ expect(getTextContentFromNodes($end)).not.toContain('%leaf%');
+ }
+ });
+});
diff --git a/packages/core/src/extensions/input-rule-text.ts b/packages/core/src/extensions/input-rule-text.ts
new file mode 100644
index 000000000..a33203fab
--- /dev/null
+++ b/packages/core/src/extensions/input-rule-text.ts
@@ -0,0 +1,60 @@
+/**
+ * The text a non-text inline node contributes when the input-rule runner
+ * derives the string it matches rules against.
+ *
+ * `@tiptap/core` builds that string by concatenating one chunk per node before
+ * the caret, then computes the replacement range as pure arithmetic:
+ * `from - (match[0].length - text.length)`. That arithmetic is correct only
+ * while every node contributes exactly as many characters as it occupies
+ * document positions, and two node shapes break it:
+ *
+ * - A leaf with no text falls back to the literal `"%leaf%"` — six characters
+ * for one position. A rule whose body spans one computes a range five
+ * positions too far left: far enough to land before the document start and
+ * throw `RangeError` out of `handleTextInput`, crashing the keystroke, or —
+ * when enough text precedes it to keep the range positive — to silently
+ * rewrite the wrong span.
+ * - A node with children is skewed the other way, and by an amount that varies
+ * with its content: the runner reads the wrapper AND descends into it, so
+ * the children's text is counted twice while the wrapper's own two positions
+ * are counted not at all.
+ *
+ * Both are one defect — a node's contributed length must equal the positions it
+ * owns — so one rule fixes both: contribute a placeholder per structural
+ * position and let the children speak for themselves.
+ */
+
+import type { Node as PMNode } from '@tiptap/pm/model';
+
+/**
+ * U+FFFC OBJECT REPLACEMENT CHARACTER — Unicode's stand-in for an embedded
+ * object in a text stream, and exactly one UTF-16 unit wide, which is the unit
+ * `match[0].length` counts in.
+ *
+ * Preferred over an ASCII filler because it cannot collide with a delimiter a
+ * rule matches on, and over the empty string because the runner treats a falsy
+ * chunk as absent and falls back to `"%leaf%"`.
+ *
+ * It also gives rule authors a lever they did not have: a rule that must not
+ * match across an embedded object can exclude this character from its body.
+ */
+export const INLINE_OBJECT_PLACEHOLDER = '\uFFFC';
+
+/**
+ * `renderText` for an inline node that is not itself text.
+ *
+ * Returns one placeholder per position the node owns outside its content — one
+ * for a leaf, two (open + close) for a node with children. Deriving that count
+ * from the node instead of hard-coding it per extension is what lets every
+ * inline node share one implementation, and what keeps the count right if a
+ * node's shape changes later.
+ *
+ * For a node with children the placeholders lead rather than bracket the
+ * content, because the runner appends the wrapper's chunk before descending.
+ * Only the total length feeds the range arithmetic, so that ordering is inert
+ * there; it is visible to a rule that matches across such a node, which is a
+ * separate question from the one this module answers.
+ */
+export function renderInlineObjectText({ node }: { node: PMNode }): string {
+ return INLINE_OBJECT_PLACEHOLDER.repeat(node.nodeSize - node.content.size);
+}
diff --git a/packages/core/src/extensions/jsx-inline.ts b/packages/core/src/extensions/jsx-inline.ts
index 6d4c3a7d7..7201cf8ca 100644
--- a/packages/core/src/extensions/jsx-inline.ts
+++ b/packages/core/src/extensions/jsx-inline.ts
@@ -1,4 +1,5 @@
import { Node } from '@tiptap/core';
+import { renderInlineObjectText } from './input-rule-text.ts';
/**
* jsxInline — inline PM node for MDX inline JSX (`mdxJsxTextElement`).
@@ -32,6 +33,8 @@ export const JsxInline = Node.create({
selectable: true,
priority: 60,
+ renderText: renderInlineObjectText,
+
addAttributes() {
return {
// JSX tag name (e.g., 'Callout'). Empty string = legacy thin-shape path
diff --git a/packages/core/src/extensions/math-inline.ts b/packages/core/src/extensions/math-inline.ts
index 4f44f1a00..26b1944b3 100644
--- a/packages/core/src/extensions/math-inline.ts
+++ b/packages/core/src/extensions/math-inline.ts
@@ -1,4 +1,5 @@
import { Node } from '@tiptap/core';
+import { renderInlineObjectText } from './input-rule-text.ts';
declare module '@tiptap/core' {
interface Commands<ReturnType> {
@@ -52,6 +53,8 @@ export const MathInline = Node.create({
selectable: true,
priority: 60,
+ renderText: renderInlineObjectText,
+
addAttributes() {
return {
formula: { default: '' },
diff --git a/packages/core/src/extensions/plain-text-clipboard.ts b/packages/core/src/extensions/plain-text-clipboard.ts
new file mode 100644
index 000000000..0a7f3db38
--- /dev/null
+++ b/packages/core/src/extensions/plain-text-clipboard.ts
@@ -0,0 +1,46 @@
+/**
+ * Keeps the input-rule placeholder out of the plain-text clipboard.
+ *
+ * `renderText` (see `input-rule-text.ts`) exists so the input-rule runner can
+ * compute replacement ranges correctly, which requires every inline node to
+ * contribute exactly as many characters as it occupies positions. `@tiptap/core`
+ * files that function on the NodeSpec as `toText`, and reads `toText` from a
+ * SECOND place with an incompatible contract: `getTextSerializersFromSchema`,
+ * which feeds the built-in `clipboardTextSerializer`. There, `getTextBetween`
+ * substitutes the serializer's output for the node AND stops descending into it,
+ * so plain-text copy would emit the placeholder for a leaf and would replace a
+ * node's visible text with it entirely.
+ *
+ * One hook, two contracts: position-faithful for range arithmetic, human-readable
+ * for the clipboard. This restores the second by serializing plain text the way
+ * ProseMirror does natively — `textBetween`, which consults `leafText` and never
+ * `toText`.
+ *
+ * Registered in `sharedExtensions` rather than at each editor's call site so an
+ * editor added later inherits it; a mounted-editor test asserts a bare roster
+ * (no `editorProps` overrides) still copies clean text. The document editor's
+ * own `editorProps.clipboardTextSerializer` continues to win over this: a view
+ * prop is consulted before any plugin's.
+ */
+
+import { Extension } from '@tiptap/core';
+import { Plugin, PluginKey } from '@tiptap/pm/state';
+
+export const PlainTextClipboard = Extension.create({
+ name: 'plainTextClipboard',
+ // Above the default so this plugin precedes the built-in serializer's, which
+ // is the one whose output has to lose.
+ priority: 1000,
+
+ addProseMirrorPlugins() {
+ return [
+ new Plugin({
+ key: new PluginKey('okPlainTextClipboard'),
+ props: {
+ clipboardTextSerializer: (slice) =>
+ slice.content.textBetween(0, slice.content.size, '\n\n'),
+ },
+ }),
+ ];
+ },
+});
diff --git a/packages/core/src/extensions/shared.ts b/packages/core/src/extensions/shared.ts
index 86558542b..43895f0a4 100644
--- a/packages/core/src/extensions/shared.ts
+++ b/packages/core/src/extensions/shared.ts
@@ -27,6 +27,7 @@ import { LinkRefDefFidelity } from './link-ref-def-fidelity.ts';
import { List, ListItem } from './list.ts';
import { ListBoundaryMerge } from './list-boundary-merge.ts';
import { MathInline } from './math-inline.ts';
+import { PlainTextClipboard } from './plain-text-clipboard.ts';
import { RawMdxFallback } from './raw-mdx-fallback.ts';
import { SourceLiteralMark } from './source-literal-mark.ts';
import { StrikeFidelity } from './strike-fidelity.ts';
@@ -101,6 +102,8 @@ export const sharedExtensions = [
DocFidelity,
// Verbatim source text for unsupported inline markdown constructs.
SourceLiteralMark,
+ // Keeps `renderText`'s position-faithful placeholder out of plain-text copy.
+ PlainTextClipboard,
StarterKit.configure({
undoRedo: false,
bulletList: false,
diff --git a/packages/core/src/extensions/tag.ts b/packages/core/src/extensions/tag.ts
index c02a0333a..06f79676d 100644
--- a/packages/core/src/extensions/tag.ts
+++ b/packages/core/src/extensions/tag.ts
@@ -18,6 +18,7 @@
*/
import { Node } from '@tiptap/core';
+import { renderInlineObjectText } from './input-rule-text.ts';
declare module '@tiptap/core' {
interface Commands<ReturnType> {
@@ -39,6 +40,8 @@ export const Tag = Node.create({
// before anything that might conflict on parseHTML.
priority: 60,
+ renderText: renderInlineObjectText,
+
addAttributes() {
return {
value: { default: '' },
diff --git a/packages/core/src/extensions/wiki-link-embed.ts b/packages/core/src/extensions/wiki-link-embed.ts
index 1ac3147bb..0d7fc56d8 100644
--- a/packages/core/src/extensions/wiki-link-embed.ts
+++ b/packages/core/src/extensions/wiki-link-embed.ts
@@ -29,6 +29,7 @@
import { Node } from '@tiptap/core';
import { IMAGE_EXTENSIONS } from '../constants/upload.ts';
import { extensionOf } from '../utils/extension.ts';
+import { renderInlineObjectText } from './input-rule-text.ts';
import { normalizeNullableString } from './wiki-link.ts';
export interface WikiLinkEmbedAttrs {
@@ -67,6 +68,8 @@ export const WikiLinkEmbed = Node.create({
atom: true,
priority: 60,
+ renderText: renderInlineObjectText,
+
addAttributes() {
return {
target: { default: '' },
diff --git a/packages/core/src/extensions/wiki-link.ts b/packages/core/src/extensions/wiki-link.ts
index 9362ec2be..ffed47524 100644
--- a/packages/core/src/extensions/wiki-link.ts
+++ b/packages/core/src/extensions/wiki-link.ts
@@ -1,4 +1,5 @@
import { Node } from '@tiptap/core';
+import { renderInlineObjectText } from './input-rule-text.ts';
export interface WikiLinkAttrs {
target: string;
@@ -70,6 +71,8 @@ export const WikiLink = Node.create({
atom: true,
priority: 60,
+ renderText: renderInlineObjectText,
+
addAttributes() {
return {
target: {
diff --git a/packages/core/src/schema-invariant.test.ts b/packages/core/src/schema-invariant.test.ts
index 93189ed7c..d7b7a37a9 100644
--- a/packages/core/src/schema-invariant.test.ts
+++ b/packages/core/src/schema-invariant.test.ts
@@ -10,8 +10,8 @@
* - sharedExtensions ordering changed
*
* Adding new nodes/attrs with defaults causes a snapshot mismatch — regenerate
- * via `bun run generate-schema-snapshot` (or manually update schema-snapshot.json)
- * and verify the diff is purely additive before committing.
+ * via `pnpm --dir packages/core run regen-schema-snapshot` (or manually update
+ * schema-snapshot.json) and verify the diff is purely additive before committing.
*
* Rationale: [email protected] destructively deletes Y.Items whose
* schema.node() throws. The delete is multi-peer replicated and undo-resistant.
diff --git a/packages/core/src/schema-snapshot.json b/packages/core/src/schema-snapshot.json
index dcd98de11..0da70a84d 100644
--- a/packages/core/src/schema-snapshot.json
+++ b/packages/core/src/schema-snapshot.json
@@ -685,6 +685,7 @@
"escapeMark",
"docFidelity",
"sourceLiteral",
+ "plainTextClipboard",
"starterKit",
"listBoundaryMerge",
"table",
diff --git a/packages/core/src/shadow-repo-layout.ts b/packages/core/src/shadow-repo-layout.ts
index ded2e09dd..330a15709 100644
--- a/packages/core/src/shadow-repo-layout.ts
+++ b/packages/core/src/shadow-repo-layout.ts
@@ -479,6 +479,7 @@ export type {
export {
CHECKPOINT_KIND_REGISTRY,
CHECKPOINT_KINDS,
+ CHECKPOINT_SAMPLE_BY_KIND,
formatCheckpointBodyLine,
isChainAnchorCheckpointKind,
isSurfacedCheckpointKind,