Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

spaces: a link in a table cell is a link (#347)

nyblnetb576930191aa
diff --git a/scripts/test-spaces-model.ts b/scripts/test-spaces-model.ts
index bf5087a4..36ec2beb 100644
--- a/scripts/test-spaces-model.ts
+++ b/scripts/test-spaces-model.ts
@@ -2719,5 +2719,46 @@ function fsTable(f: string): string {
}
+// ---- A LINK IN A TABLE CELL IS A LINK --------------------------------------
+// A table keeps its content in `rows` and mirrors it into `html`; writeTable is
+// the one writer that keeps the two in step. So a table the EDITOR produced
+// back-links from its cells, and a table that arrives any other way — hand
+// written, an agent calling updateBlock, an import — does not: the link works
+// when clicked and appears in no "Linked from". Silent, and exactly the one
+// failure a backlink index can have.
+//
+// `rows` is read ONLY when `html` is absent. Extending this to always scan
+// `rows` was tried during the starter work and reverted, because with both
+// fields present every cell link is counted twice.
+{
+ const cell = '<a href="#p/b">B</a>'
+ const docWith = (blk: Record<string, unknown>) => ({
+ format: FORMAT, version: 1, docId: 'd', title: 't', home: 'a',
+ theme: {}, pages: [
+ { id: 'a', title: 'A', blocks: [blk] },
+ { id: 'b', title: 'B', blocks: [] },
+ ],
+ }) as unknown as SpacesDoc
+ const backlinksToB = (blk: Record<string, unknown>) =>
+ (buildIndex(docWith(blk)).backlinks?.get('b') ?? []).length
+
+ ok(backlinksToB({ id: 'k', type: 'table', rows: [['x', cell]] }) === 1,
+ 'a hand-authored table cell back-links, with no html fallback present')
+
+ const written: Record<string, unknown> = { id: 'k', type: 'table' }
+ writeTable(written as never, tableOf({ ...written, rows: [['x', cell]] } as never))
+ ok(backlinksToB(written) === 1, 'a table written by the editor back-links exactly once')
+
+ ok(backlinksToB({ id: 'k', type: 'table', rows: [[cell]], html: cell }) === 1,
+ '…and with BOTH fields present the same link is not counted twice')
+
+ // buildIndex runs on documents nobody has validated yet
+ let threw = false
+ try { backlinksToB({ id: 'k', type: 'table', rows: ['not-a-row', [null, 7, cell]] }) }
+ catch { threw = true }
+ ok(!threw, 'a malformed rows array does not take the index down')
+}
+
+
console.log(`\n${checks - failures}/${checks} checks passed`)
if (failures) process.exit(1)
diff --git a/spaces/src/model.ts b/spaces/src/model.ts
index e30cc2d9..f40d7e8e 100644
--- a/spaces/src/model.ts
+++ b/spaces/src/model.ts
@@ -550,6 +550,23 @@ export interface SpaceIndex {
backlinks: Map<string, Array<{ pageId: string; blockId: string }>>
}
+/**
+ * A table's cells as one string, for scanning only.
+ *
+ * Deliberately not `tableFallbackHtml`: this is never written anywhere, so it
+ * needs no separator, no escaping and no shape — it exists so one regex can
+ * find hrefs in cells the same way it finds them in prose. Tolerant of a
+ * malformed `rows` because it runs on documents nobody has validated yet.
+ */
+function tableCellsText(rows: unknown[]): string {
+ let out = ''
+ for (const row of rows) {
+ if (!Array.isArray(row)) continue
+ for (const cell of row) if (typeof cell === 'string') out += cell + '\n'
+ }
+ return out
+}
+
/** Every `#p/<id>` href in a block's html. */
const LINK_RE = /href="#p\/([^"]+)"/g
@@ -649,8 +666,25 @@ export function buildIndex(doc: SpacesDoc): SpaceIndex {
pushInto(children, p.parent && page.has(p.parent) ? p.parent : '', p)
for (const b of p.blocks) {
block.set(b.id, { block: b, pageId: p.id })
- if (b.html) {
- for (const m of b.html.matchAll(LINK_RE)) {
+ // WHERE A BLOCK'S LINKS LIVE. Normally in `html`. A table keeps its
+ // content in `rows` and mirrors it into `html` as a readable fallback,
+ // and `writeTable` is the one writer that keeps the two in step — so for
+ // a table the editor produced, scanning `html` finds the cells' links.
+ //
+ // It does not for a table that arrives any other way. A hand-written
+ // document, an agent calling updateBlock, an import: `rows` is set,
+ // `html` is absent, and a link in a cell then WORKS WHEN CLICKED and
+ // appears in no "Linked from" — the one failure a backlink index has,
+ // and silent. Measured before this: 0 backlinks, against 1 for the same
+ // table through writeTable.
+ //
+ // `rows` is read ONLY when `html` is absent. With both present they are
+ // the same links by construction, and scanning both would report every
+ // cell link twice — which is why extending this to always scan `rows`
+ // was tried during the starter work and reverted.
+ const linkSrc = b.html || (Array.isArray(b.rows) ? tableCellsText(b.rows) : '')
+ if (linkSrc) {
+ for (const m of linkSrc.matchAll(LINK_RE)) {
pushInto(backlinks, linkTarget(m[1], page), { pageId: p.id, blockId: b.id })
}
}