Skip to content
MSO CloudDocumentation

ADR 0030 — TipTap v3 as the insight editor: markdown end-to-end, never-bail tables

On this page
  • Status: Accepted (operator directive 2026-08-12 "one insight block is ONE markdown document"; shipped to prod 2026-08-13).
  • Date: 2026-08-14 (recorded after the fact).
  • Owners: Platform (Audience CI programme).
  • Related: ADR 0025 (narrative gates; editAndLock human-lock semantics), ADR 0029 (blockKinds static|insight), §44 (store verbatim), §76 (route bundle budget).

Context#

Insight blocks on delivered decks were edited through four per-section textareas over a wsna.v1 JSON envelope. The delivered artefacts are markdown documents — headings, prose, ordered lists and (critically) GFM tables up to eight columns wide — and analysts must edit them AS documents, seeing the structure the deck renders (components/markdown-view.tsx reads the same grammar). A plain textarea over raw markdown fails the non-technical analyst; a JSON envelope editor cannot represent a table at all.

Decisions#

D1 — TipTap v3 (ProseMirror) WYSIWYG over a markdown wire format

markdown-insight-editor.tsx (@tiptap/react v3 + @tiptap/starter-kit + table extensions + tiptap-markdown). Serialization is MARKDOWN end to end: content_insight_publications.body_vi stays a plain markdown string readable in psql — no HTML is ever stored (§44 posture: what is stored is what is reviewed). A legacy wsna.v1 envelope opens as markdown once (wsnaBodyToMarkdown); saving stores markdown and flips the row to authored_by='human' (editAndLock), which is exactly the semantics editing always had. The editor chunk is next/dynamic-deferred (§76): TipTap loads only when an editor opens.

D2 — Toolbar state via useEditorState (v3 breaking change)

useEditor in @tiptap/react v3 does NOT re-render on editor transactions (shouldRerenderOnTransaction defaults to false; it was true in v2). Every editor.isActive(…) read in JSX is frozen at mount — active states never flip and conditionally-rendered controls (table row/column buttons) never appear. All toolbar state therefore goes through useEditorState, which subscribes to exactly the flags the toolbar draws. The other three faults of the same incident (missing .yng-md-editor stylesheet under Tailwind preflight; mousedown-only toolbar commands breaking keyboard use; heading.levels excluding H1 silently demoting # on save) are documented in the component docstring with their fixes.

D3 — A table ALWAYS survives as a table (never-bail serializer)

tiptap-markdown@0.9 fails SILENTLY on the tables the delivered decks are made of: a cell with a second block or any merged cell flunks isMarkdownSerializable() and (with html: false) the whole table serializes to the literal text [table]; a raw | in a cell shifts the row; a line break becomes [hardBreak]. The rule: content a GFM cell cannot carry is FLATTENED to <br>-joined inline text with \|-escaped pipes — never dropped, never a placeholder — and the editor tells the analyst so while the caret is in a table. The lossy string transforms live DOM-free in apps/web/src/lib/insight-markdown.ts (pure functions, unit-tested — the app's vitest runs in environment: 'node', so keeping them out of the DOM layer is what makes the round-trip guardable); the TipTap extension is a thin adapter. INSIGHT_BODY_MAX_CHARS (20,000) caps a body editor-side.

D4 — Draft lifecycle is explicit

Manual saves publish directly (editAndLock → published); "Tạo lại" (AI regenerate) produces a DRAFT with a visible "Bản nháp — chưa hiển thị cho người xem" banner and a [Duyệt hiển thị] approval action; generateDeck reads only published publications, so a draft can never leak into a delivered deck. (A server-side draft_body column so a published block never leaves published during regeneration is recorded as the intended next step, not yet built.)

Consequences#

  • Round-trip fidelity is a tested invariant: open → save of an unchanged document changes zero characters (proved against the real ACB/Heineken seeds).
  • Table-shape edits are safe for non-technical analysts; the worst case is flattened cell content, visibly, never data loss.
  • Upgrading @tiptap majors requires re-checking D2 (render model) and D3 (serializer bail-outs) first — both are the exact places v3 broke.

Implementation status (declared 2026-08-14)#

Decision Status Evidence
D1 editor + markdown wire Landed markdown-insight-editor.tsx; @tiptap/*@^3.30, tiptap-markdown@^0.9 (apps/web package.json)
D2 useEditorState toolbar Landed component docstring "WHY THE TOOLBAR IS WIRED THE WAY IT IS (2026-08-13)"; QA authoring rig 12/12 PASS on prod 2026-08-13
D3 never-bail tables Landed insight-markdown.ts + unit tests (markdown-view-inline/insight-markdown suites); prod round-trip created:0 updated:0 on re-seed
D4 draft lifecycle Landed (server draft_body still open) editAndLock publish path; draft banner + [Duyệt hiển thị]; generateDeck filters published