From d70977f0ef430d253c950522df3ac2ae8f40d5dc Mon Sep 17 00:00:00 2001 From: Raphael Fakhri Date: Mon, 23 Nov 2026 17:02:27 +0200 Subject: [PATCH] fix(core): index doc references inside table cells (#15713) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Closes #15468. A doc linked with `@` inside a table block (`/table`, not a database view) never appears in Bidirectional Links on either doc. The indexer's `affine:table` branch in `@affine/reader` only collects cell text, so the block document has no `refDocId`/`ref`, and the backlink query on `refDocId` finds nothing. The `affine:database` branch already collects inline references from rich-text cells. This change reads the inline references from each `prop:cells.*.text` value with the existing `getTextDeltasFromCellValue` and `collectInlineReferences` helpers, and sets `refDocId` and `ref` on the table block, the same way the database branch does. Scope: this fixes the TypeScript crawler, which the web app (IndexedDB storage) uses and which the desktop and mobile apps use as the fallback. The native crawler in `affine_doc_loader` (`read/block.rs`, `affine:table` branch) has the same gap, and this PR leaves it unchanged. Test: `should index references from table cells` in `packages/common/reader/__tests__/reader.spec.ts` fails without the fix (`expected undefined to deeply equal [ 'target-doc' ]`) and passes with it. ## Checklist - [x] I have signed the [AFFiNE Contributor License Agreement](https://cla-assistant.io/toeverything/AFFiNE) — required before merge; the `license/cla` check must be green ([how it works](https://github.com/toeverything/AFFiNE/blob/canary/docs/BUILDING.md#sign-the-cla-first)) - [x] The PR targets the `canary` branch and its title follows [Conventional Commits](https://www.conventionalcommits.org/) - [x] Tests are added or updated where it makes sense - [x] `yarn lint` and `yarn typecheck` pass locally - [x] If the PR code includes AI-generated edits, I have carefully reviewed it to ensure that the scope of the PR is consistent with what is claimed in the title and description, and that there are no redundant or invalid implementations. - [x] I agree that maintainers may close the PR without discussion if they consider the code quality to be too low. ## Summary by CodeRabbit - **Bug Fixes** - Table cell text is now included when reading document blocks, and references within cells are captured for indexing. --- .../common/reader/__tests__/reader.spec.ts | 61 +++++++++++++++++++ packages/common/reader/src/reader.ts | 31 +++++++--- 2 files changed, 84 insertions(+), 8 deletions(-) diff --git a/packages/common/reader/__tests__/reader.spec.ts b/packages/common/reader/__tests__/reader.spec.ts index be304d62bc..41d1452583 100644 --- a/packages/common/reader/__tests__/reader.spec.ts +++ b/packages/common/reader/__tests__/reader.spec.ts @@ -280,3 +280,64 @@ test('should index references from database rich-text cells', async () => { JSON.stringify({ docId: 'target-doc', mode: 'page' }), ]); }); + +test('should index references from table cells', async () => { + const doc = new YDoc({ + guid: 'table-doc', + }); + const blocks = doc.getMap('blocks'); + + const pageTitle = new YText(); + pageTitle.insert(0, 'Page'); + const page = new YMap(); + page.set('sys:id', 'page'); + page.set('sys:flavour', 'affine:page'); + page.set('sys:children', YArray.from(['note'])); + page.set('prop:title', pageTitle); + blocks.set('page', page); + + const note = new YMap(); + note.set('sys:id', 'note'); + note.set('sys:flavour', 'affine:note'); + note.set('sys:children', YArray.from(['table'])); + note.set('prop:displayMode', 'page'); + blocks.set('note', note); + + const table = new YMap(); + table.set('sys:id', 'table'); + table.set('sys:flavour', 'affine:table'); + table.set('sys:children', new YArray()); + + const plainText = new YText(); + plainText.insert(0, 'Plain'); + table.set('prop:cells.row1:col1.text', plainText); + + const refText = new YText(); + refText.applyDelta([ + { insert: 'See ' }, + { + insert: 'Target', + attributes: { + reference: { + pageId: 'target-doc', + params: { mode: 'page' }, + }, + }, + }, + ]); + table.set('prop:cells.row1:col2.text', refText); + + blocks.set('table', table); + + const result = await readAllBlocksFromDoc({ + ydoc: doc, + spaceId: 'test-space', + }); + + const tableBlock = result?.blocks.find(block => block.blockId === 'table'); + expect(tableBlock?.content).toEqual(['Plain', 'See Target']); + expect(tableBlock?.refDocId).toEqual(['target-doc']); + expect(tableBlock?.ref).toEqual([ + JSON.stringify({ docId: 'target-doc', mode: 'page' }), + ]); +}); diff --git a/packages/common/reader/src/reader.ts b/packages/common/reader/src/reader.ts index 8b9f0ab25d..d87d0f10e3 100644 --- a/packages/common/reader/src/reader.ts +++ b/packages/common/reader/src/reader.ts @@ -847,17 +847,32 @@ export async function readAllBlocksFromDoc({ content: block.get('prop:latex')?.toString() ?? '', }); } else if (flavour === 'affine:table') { - const contents = Array.from(block.keys()) - .map(key => { - if (key.startsWith('prop:cells.') && key.endsWith('.text')) { - return block.get(key)?.toString() ?? ''; - } - return ''; - }) - .filter(Boolean); + const contents: string[] = []; + const tableRefs: { refDocId: string; ref: string }[] = []; + for (const key of block.keys()) { + if (!key.startsWith('prop:cells.') || !key.endsWith('.text')) { + continue; + } + const cellText = block.get(key); + const text = cellText?.toString() ?? ''; + if (text) { + contents.push(text); + } + const deltas = getTextDeltasFromCellValue(cellText); + if (deltas?.length) { + tableRefs.push(...collectInlineReferences(deltas)); + } + } + const refs = uniqBy(tableRefs, item => item.ref); blockDocuments.push({ ...commonBlockProps, content: contents, + ...(refs.length + ? { + refDocId: refs.map(item => item.refDocId), + ref: refs.map(item => item.ref), + } + : {}), }); } else if (bookmarkFlavours.has(flavour)) { blockDocuments.push({ ...commonBlockProps });