Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(open-knowledge): copy plain text from inside a table cell (#2724)
Andrew Mikofalvycc557b0ef5cf
diff --git a/.changeset/table-cell-copy-plain-text.md b/.changeset/table-cell-copy-plain-text.mdnew file mode 100644index 000000000..2efc2db8e--- /dev/null+++ b/.changeset/table-cell-copy-plain-text.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++Copying text from inside a table cell now copies just that text, not the whole Markdown table. Previously, drag-highlighting content in a table cell and copying it yielded the entire table — the `|` pipes and the `| -- |` delimiter row — because the selection's content included the enclosing table structure. A text selection confined to a single cell now serializes to only the cell's content, with its inline formatting (inline code, bold, links, etc.) preserved exactly as it would be when copying the same text from a paragraph, and the table structure dropped.diff --git a/packages/app/src/editor/clipboard/serialize.test.ts b/packages/app/src/editor/clipboard/serialize.test.tsindex 9a260b642..9fbf5c271 100644--- a/packages/app/src/editor/clipboard/serialize.test.ts+++ b/packages/app/src/editor/clipboard/serialize.test.ts@@ -15,15 +15,15 @@* mechanically rather than relying on E2E to surface it.*/-import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test';-import { sharedExtensions } from '@inkeep/open-knowledge-core';+import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';import type { JSONContent } from '@tiptap/core';import { getSchema } from '@tiptap/core';import type { Fragment } from '@tiptap/pm/model';import { Fragment as PmFragment, type Node as PmNode, Schema } from '@tiptap/pm/model';-import { EditorState, type TextSelection } from '@tiptap/pm/state';+import { EditorState, TextSelection } from '@tiptap/pm/state';import { CellSelection, TableMap } from '@tiptap/pm/tables';import type { EditorView } from '@tiptap/pm/view';+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';import {createClipboardHtmlSerializer,@@ -58,11 +58,11 @@ function makeSlice(text: string) {// receives that JSON shape and reaches into `doc > paragraph > text`.function fakeMdManager() {return {- serialize: mock((doc: JSONContent) => {+ serialize: vi.fn((doc: JSONContent) => {const p = doc.content?.[0]?.content?.[0]?.text ?? '';return `# ${p}`;}),- parse: mock(() => ({ type: 'doc', content: [] })),+ parse: vi.fn(() => ({ type: 'doc', content: [] })),};}@@ -93,7 +93,7 @@ describe('createClipboardTextSerializer', () => {test('falls through to PM textBetween on serialize throw', () => {const md = fakeMdManager();- md.serialize = mock(() => {+ md.serialize = vi.fn(() => {throw new Error('boom');});// biome-ignore lint/suspicious/noExplicitAny: fake md manager shape@@ -748,7 +748,7 @@ describe('createClipboardTextSerializer — CellSelection routing decision', ()// returns the sentinel. A passing test proves the CellSelection branch// fired instead.const md = fakeMdManager();- md.serialize = mock(() => 'MARKDOWN-PATH-FALLTHROUGH');+ md.serialize = vi.fn(() => 'MARKDOWN-PATH-FALLTHROUGH');// biome-ignore lint/suspicious/noExplicitAny: fake md manager shapeconst serializer = createClipboardTextSerializer({ mdManager: md as any });// Real state carries the CellSelection; the slice arg is unused by@@ -761,3 +761,140 @@ describe('createClipboardTextSerializer — CellSelection routing decision', ()expect(md.serialize).not.toHaveBeenCalled();});});++// Real MarkdownManager so the assertions reflect the actual clipboard bytes,+// not a stubbed serializer. Reuses `tableSchema` (real table nodes) above.+const realMd = new MarkdownManager({ extensions: sharedExtensions });+const realTextSerializer = createClipboardTextSerializer({ mdManager: realMd });++function viewFor(state: EditorState) {+ return { state } as unknown as Parameters<typeof realTextSerializer>[1];+}++// Build `doc > table > tableRow > tableHeader > paragraph > <inline>` where the+// header cell holds a single text node, optionally with an inline mark.+function singleCellTableDoc(text: string, markName?: string): PmNode {+ const marks = markName ? [tableSchema.marks[markName].create()] : undefined;+ const textNode = tableSchema.text(text, marks);+ const p = tableSchema.nodes.paragraph.create(null, [textNode]);+ const th = tableSchema.nodes.tableHeader.createChecked(null, p);+ const row = tableSchema.nodes.tableRow.createChecked(null, [th]);+ const table = tableSchema.nodes.table.createChecked(null, [row]);+ return tableSchema.nodes.doc.create(null, [table]);+}++// Set a TextSelection over the `[subFrom, subTo)` char offsets within the first+// text node whose content includes `needle`, then run the production text+// serializer.+function copyTextIn(doc: PmNode, needle: string, subFrom = 0, subTo = needle.length): string {+ const state = EditorState.create({ schema: tableSchema, doc });+ let from = -1;+ let to = -1;+ doc.descendants((node, pos) => {+ if (from === -1 && node.isText && node.text?.includes(needle)) {+ const base = pos + node.text.indexOf(needle);+ from = base + subFrom;+ to = base + subTo;+ }+ });+ const sel = TextSelection.create(doc, from, to);+ const st = state.apply(state.tr.setSelection(sel));+ return realTextSerializer(st.selection.content(), viewFor(st));+}++describe('createClipboardTextSerializer — text selection inside one table cell', () => {+ // Drag-highlighting text inside a single cell yields a TextSelection (not a+ // CellSelection). `selection.content()` returns the enclosing `table` with+ // open depths, so the markdown path would emit the whole table's pipe syntax+ // and delimiter row. The fix strips only the table/row/cell wrappers, so the+ // cell's inner content serializes to Markdown with its inline formatting+ // intact — exactly as the same content copies from a paragraph.++ test('inline-code cell → `command`, formatting kept, no table markup', () => {+ const out = copyTextIn(singleCellTableDoc('command', 'code'), 'command');+ expect(out.trimEnd()).toBe('`command`');+ expect(out).not.toContain('|');+ });++ test('sub-word selection inside an inline-code cell → the sub-word, still code', () => {+ const out = copyTextIn(singleCellTableDoc('npm run build', 'code'), 'npm run build', 4, 7);+ expect(out.trimEnd()).toBe('`run`');+ expect(out).not.toContain('|');+ });++ test('mixed inline marks in a cell are preserved, table markup is not', () => {+ const doc = tableSchema.nodes.doc.create(null, [+ tableSchema.nodes.table.createChecked(null, [+ tableSchema.nodes.tableRow.createChecked(null, [+ tableSchema.nodes.tableHeader.createChecked(+ null,+ tableSchema.nodes.paragraph.create(null, [+ tableSchema.text('run '),+ tableSchema.text('now', [tableSchema.marks.strong.create()]),+ ]),+ ),+ ]),+ ]),+ ]);+ const out = copyTextIn(doc, 'run ', 0, 7); // "run now" across both text nodes+ expect(out.trimEnd()).toBe('run **now**');+ expect(out).not.toContain('|');+ });++ test('plain (unmarked) cell text → the text, no pipes', () => {+ const out = copyTextIn(singleCellTableDoc('hello world'), 'hello world');+ expect(out.trimEnd()).toBe('hello world');+ expect(out).not.toContain('|');+ expect(out).not.toContain('`');+ });++ test('selecting text in one body cell of a multi-row table → only that cell', () => {+ // tableNode row 0 is the header; select text in a body cell.+ const doc = tableSchema.nodes.doc.create(+ null,+ tableNode([+ ['H1', 'H2'],+ ['alpha', 'bravo'],+ ['charlie', 'delta'],+ ]),+ );+ const out = copyTextIn(doc, 'bravo');+ expect(out.trimEnd()).toBe('bravo');+ expect(out).not.toContain('|');+ expect(out).not.toContain('alpha');+ expect(out).not.toContain('delta');+ });++ test('control: a text selection in a plain paragraph is untouched (markdown path)', () => {+ const doc = tableSchema.nodes.doc.create(null, [+ tableSchema.nodes.paragraph.create(null, [tableSchema.text('some prose here')]),+ ]);+ const out = copyTextIn(doc, 'some prose here');+ expect(out.trimEnd()).toBe('some prose here');+ });++ test('control: whole-doc selection spanning paragraph + table + paragraph keeps the table markup', () => {+ // Non-firing case: a select-all-style TextSelection covers the table as a+ // whole node, so `selection.content()` yields a multi-child fragment whose+ // table child is closed on both ends. Both strip-loop break conditions+ // (childCount !== 1; openStart/openEnd exhausted at the closed table) must+ // hold, or a full-document copy would lose its table markup.+ const doc = tableSchema.nodes.doc.create(null, [+ tableSchema.nodes.paragraph.create(null, [tableSchema.text('before')]),+ tableNode([+ ['H1', 'H2'],+ ['a', 'b'],+ ]),+ tableSchema.nodes.paragraph.create(null, [tableSchema.text('after')]),+ ]);+ const state = EditorState.create({ schema: tableSchema, doc });+ const sel = TextSelection.create(doc, 1, doc.content.size - 1);+ const st = state.apply(state.tr.setSelection(sel));+ const out = realTextSerializer(st.selection.content(), viewFor(st));+ expect(out).toContain('before');+ expect(out).toContain('after');+ expect(out).toContain('| H1 | H2 |');+ expect(out).toContain('| a | b |');+ expect(out).toMatch(/\|\s*-+\s*\|/); // delimiter row intact+ });+});diff --git a/packages/app/src/editor/clipboard/serialize.ts b/packages/app/src/editor/clipboard/serialize.tsindex 4799deafa..3e778e457 100644--- a/packages/app/src/editor/clipboard/serialize.ts+++ b/packages/app/src/editor/clipboard/serialize.ts@@ -32,8 +32,9 @@import type { MarkdownManager } from '@inkeep/open-knowledge-core';import { markdownToHtml } from '@inkeep/open-knowledge-core';import type { JSONContent } from '@tiptap/core';-import type { Node, Schema, Slice } from '@tiptap/pm/model';+import type { Node, ResolvedPos, Schema, Slice } from '@tiptap/pm/model';import { DOMSerializer, Fragment, Slice as SliceCtor } from '@tiptap/pm/model';+import { TextSelection } from '@tiptap/pm/state';import { CellSelection } from '@tiptap/pm/tables';import type { EditorView } from '@tiptap/pm/view';import {@@ -92,6 +93,36 @@ export function createClipboardTextSerializer(deps: WysiwygSerializerDeps) {// Fall through to the markdown path below.}}+ // A text selection *inside* a single table cell (drag-highlighting text+ // within one cell, not the cell-select gesture that yields a+ // CellSelection). `selection.content()` returns the whole enclosing+ // `table` node with open depths, and the markdown path below discards+ // those depths, so it would emit the entire table's pipe syntax ++ // delimiter row instead of the highlighted text. Peel off the+ // table/row/cell wrappers and serialize just the cell's inner content:+ // inline formatting (`code`, **strong**, …) is preserved exactly as it is+ // when copying from a paragraph, only the table structure is dropped. So+ // copying `` `cmd` `` out of a docs table yields `` `cmd` ``, not the+ // whole table.+ if (+ view.state.selection instanceof TextSelection &&+ isSelectionInsideSingleCell(view.state.selection)+ ) {+ try {+ return sliceToMarkdown(+ stripEnclosingTableWrapper(slice),+ view.state.schema,+ deps.mdManager,+ );+ } catch (err) {+ logSerializeFail({+ view: 'wysiwyg',+ kind: 'text',+ reason: `incell:${(err as Error)?.message ?? 'unknown'}`,+ });+ // Fall through to the markdown path below.+ }+ }try {return sliceToMarkdown(slice, view.state.schema, deps.mdManager);} catch (err) {@@ -135,6 +166,58 @@ function cellText(cell: Node): string {);}+const CELL_NODE_TYPES = new Set(['tableCell', 'tableHeader']);+const TABLE_WRAPPER_TYPES = new Set(['table', 'tableRow', 'tableCell', 'tableHeader']);++/**+ * True when both ends of a text selection resolve into the same table cell.+ * `$pos.before(depth)` at the cell depth is the position immediately before+ * that cell node, unique per cell, so equal values on both ends prove a+ * single-cell selection. A selection spanning two cells is normalized to a+ * `CellSelection` by the tables plugin and never reaches here; the same-cell+ * check also rejects the defensive edge where one somehow does.+ */+function isSelectionInsideSingleCell(selection: TextSelection): boolean {+ const fromDepth = cellDepth(selection.$from);+ const toDepth = cellDepth(selection.$to);+ if (fromDepth === null || toDepth === null) return false;+ return selection.$from.before(fromDepth) === selection.$to.before(toDepth);+}++/** Depth of the nearest table-cell ancestor of `$pos`, or `null` if none. */+function cellDepth($pos: ResolvedPos): number | null {+ for (let d = $pos.depth; d > 0; d--) {+ if (CELL_NODE_TYPES.has($pos.node(d).type.name)) return d;+ }+ return null;+}++/**+ * Peel `table` → `tableRow` → `tableCell`/`tableHeader` off the front of an+ * open slice, returning a slice of the cell's inner content with the open+ * depths adjusted. Only descends while the slice is a single wrapper that is+ * open on both ends — the shape `selection.content()` produces for a selection+ * *inside* one cell — so a fully-selected table (a closed slice) or a+ * multi-cell fragment is returned unchanged and still serializes as a real+ * table. The unwrapped slice serializes to the cell's inner Markdown, marks+ * and all, exactly as the same content would from a paragraph.+ */+function stripEnclosingTableWrapper(slice: Slice): Slice {+ let content = slice.content;+ let openStart = slice.openStart;+ let openEnd = slice.openEnd;+ while (openStart > 0 && openEnd > 0) {+ const only = content.firstChild;+ if (content.childCount !== 1 || only === null || !TABLE_WRAPPER_TYPES.has(only.type.name)) {+ break;+ }+ content = only.content;+ openStart -= 1;+ openEnd -= 1;+ }+ return new SliceCtor(content, openStart, openEnd);+}+/*** Encode one TSV field per the Excel / Google Sheets clipboard convention* (RFC 4180 §2.5-2.7 with a tab delimiter): a field containing a newline, a