Skip to content

Table node: detect GFM tables by header and delimiter row so they render on blur - #3856

Merged
mstomar125 merged 2 commits into
mainfrom
issue-3840-table-render-on-blur
Oct 20, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-3840-table-render-on-blur

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

What changed

Root cause: the table node already renders a <table> in display mode (and shows the markdown source in a textarea on focus), but the table plugin only detected a leading | (pipe + space). A table typed or pasted as |a|b| or a | b with a delimiter row stayed a text node and showed raw markdown.

  • table-markdown.ts (new): the GFM parser moved out of table-node.svelte, now splitting cells on unescaped pipes outside code spans (\| and `a|b` stay in the cell) and requiring a header row plus a matching delimiter row. Also exports TABLE_DETECT_PATTERN.
  • core-plugins.ts: the table plugin's detect now also matches a complete header + delimiter row, excluding lines that start like a quote, heading, code fence or list item. The | shortcut is unchanged.
  • table-node.svelte: when the content is not (yet) a complete table, display mode shows the source as plain text instead of an empty view.
  • Editing is unchanged: the node stores the markdown source verbatim; nothing rewrites it, so focus/edit/blur round-trips.

Tests

src/tests/components/table-node-render.test.ts (13 tests): detection (shortcut, |a|b|, no outer pipes, negatives, quote/fence not stolen), parsing (alignment, code-span and escaped pipes), display mode renders a table with bold, code span and link inside cells, focus swaps to a textarea holding the exact source and blur swaps back, re-render from edited source, partial content falls back to text.

Ran: the new test file, src/tests/plugins/table-plugin.test.ts, bun run test:changed (frontend + browser tiers, passed), bun run quality:fix, desktop-app quality:check (eslint, design tokens, svelte-check: 0 errors).

Closes #3840

🤖 Generated with Claude Code

@mstomar125 mstomar125 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Type: Initial Review

Verdict: APPROVE WITH NITS

Checked out the head in a throwaway worktree. table-node-render.test.ts passes (13/13) and eslint is clean on the touched files. I also ran probe tests that I did not commit.

Requirements (#3840)

  • Renders as a table on blur: met. Display mode shows <table>, 3 th, correct tbody rows.
  • Focus shows source, round-trip unchanged: met. The node stores the markdown verbatim and the parser only reads it. Nothing rewrites content on blur.
  • Inline formatting in cells: met. Bold, code and links render, and no literal ** is left.
  • Test covers detection, render and the focus/blur switch: met.

Findings

  1. Non-blocking: the detect regex is looser than isMarkdownTable, so some ordinary text flips to a table node that is not a table.

    • File: packages/desktop-app/src/lib/design/components/table-markdown.ts:83-84.
    • The regex does not compare the header and delimiter cell counts, and isMarkdownTable does. Probed with TABLE_DETECT_PATTERN.test() against isMarkdownTable:
      • "Use a | b\n-|" regex true, isMarkdownTable false
      • "a | b\n---|" true, false
      • "a|b\n:-|" true, false
      • "a | b\n|-|" true, false
    • Content is preserved, because cleanContent is false and the fallback renders the source. But the node type changes. The cells then render as plain text with markdown disabled (table-node.svelte:116), so ordinary prose loses its bold and links in display mode.
    • The reverse also happens: "x|y\n\n---|---" is a table by isMarkdownTable and not by the regex.
    • Suggested fix: make the detect agree with isMarkdownTable, for example by exposing the check as a function in the plugin pattern, or by tightening the regex.
    • What is not affected: a | b followed by --- (no pipe on line 2) is not detected, so a setext-style heading or horizontal rule stays text. Header lines starting with >, #, a fence, - , * or 1. are excluded. A single line with a pipe is never detected. A quote and a code fence containing a table-looking line are correctly left alone. Typing a pipe in a normal sentence does not flip the type mid-edit, because a delimiter line is required.
  2. Non-blocking: quadratic regex backtracking on a very long single line.

    • File: table-markdown.ts:83-84, the [^\n]*\|[^\n]*\n prefix.
    • detectPatternInContent runs on every keystroke and on load. Measured: a 100k-char line with 50k pipes takes 2.2s, and a 200k-char line takes 8.8s. Ordinary content is unaffected (~0 ms for many short lines).
    • Only pathological pasted data hits this. Capping the length of the first line, or checking the first line with a plain string function, removes it.
  3. Non-blocking: backtick pipes deviate from GFM.

    • File: table-markdown.ts:27-38.
    • GFM splits cells on a pipe even inside a code span unless it is escaped. This parser treats a pipe inside backticks as part of the cell, so a table that GitHub renders one way renders differently here.
    • An unbalanced backtick makes the rest of the row one cell. Probe: | its | x | y|gives a single cellits | x | y.
    • Source is not lost, only the rendering is off.
  4. Non-blocking: table end and ragged rows.

    • File: table-markdown.ts:55-72.
    • Blank lines are filtered out, so a paragraph after the table separated by a blank line renders as an extra one-cell body row. GFM ends the table at the blank line.
    • Extra cells beyond the header are rendered (GFM truncates). Short rows are not padded.
    • Display only; the stored source is unchanged.
  5. Non-blocking, pre-existing and now reachable from table cells: link hrefs are not scheme-filtered.

    • Files: view-mode-renderer.svelte link branch; app-shell.svelte:471-500.
    • [x](javascript:alert(1)) in a cell renders <a href="javascript:alert(1)">, and app-shell's click handler only intercepts http(s) and nodespace://.
    • Raw HTML is safe: <img onerror> and <script> come out as escaped text, and there is no {@html}.
    • Same renderer as every other node, so not introduced here. Worth a follow-up issue to allow-list schemes in view-mode-parser.ts.
  6. Nit: only the first line is checked when the node type is already something else.

    • A task node (whose content is stored without its - [ ] prefix) whose text contains x | y plus a delimiter-looking second line would flip to table. Very rare, and the same shape of risk as the old | rule. Mentioning it for awareness.
  7. Nit: test gaps.

    • There is no negative test for the mismatch cases in finding 1, and none for a pipe inside a code span at the render level.
    • There is no test that editing the source keystroke by keystroke through the textarea keeps the type stable. The round-trip test only toggles focus.

Performance (render): a 1500-row, 4-column table (6000 cells) mounts in about 0.7s in jsdom. Parsing 2000 rows takes about 1.6ms. Per-cell ViewModeRenderer instances are the cost. Acceptable.

Edge cases that behave correctly: empty cells, a leading ||, alignment colons and \| all parse and render as expected. Escaped pipes are unescaped in the rendered cell only; the source is untouched.

No blocking issues. Findings 1 and 2 are worth fixing here if cheap.

mstomar125 and others added 2 commits October 20, 2026 13:02
…e source as text

The table plugin only matched a leading '| ', so a table pasted as
'|a|b|' or 'a | b' stayed a text node and showed raw markdown. Detection
now also accepts a complete header plus delimiter row, and the parser
moves to table-markdown.ts with code-span and escaped-pipe handling.
TableNode falls back to plain source text when the content is not yet a
complete table instead of rendering nothing.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…r cell counts

The detect pattern backtracked quadratically on a long single line and
accepted a delimiter row with fewer cells than the header. A linear
prefilter now runs first, then a RegExp subclass checks that both rows
have the same cell count.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@mstomar125
mstomar125 force-pushed the issue-3840-table-render-on-blur branch from c5fd87e to 170dca9 Compare October 20, 2026 11:02
@mstomar125
mstomar125 merged commit cebc522 into main Oct 20, 2026
@mstomar125
mstomar125 deleted the issue-3840-table-render-on-blur branch October 20, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Table node: render a markdown table on blur, show the markdown on focus

1 participant