Repository navigation
Table node: detect GFM tables by header and delimiter row so they render on blur - #3856
Conversation
mstomar125
left a comment
There was a problem hiding this comment.
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>, 3th, correcttbodyrows. - 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
-
Non-blocking: the detect regex is looser than
isMarkdownTable, so some ordinary text flips to atablenode 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
isMarkdownTabledoes. Probed withTABLE_DETECT_PATTERN.test()againstisMarkdownTable:"Use a | b\n-|"regex true,isMarkdownTablefalse"a | b\n---|"true, false"a|b\n:-|"true, false"a | b\n|-|"true, false
- Content is preserved, because
cleanContentis 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 byisMarkdownTableand 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 | bfollowed 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,-,*or1.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.
- File:
-
Non-blocking: quadratic regex backtracking on a very long single line.
- File:
table-markdown.ts:83-84, the[^\n]*\|[^\n]*\nprefix. detectPatternInContentruns 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.
- File:
-
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.
- File:
-
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.
- File:
-
Non-blocking, pre-existing and now reachable from table cells: link hrefs are not scheme-filtered.
- Files:
view-mode-renderer.sveltelink branch;app-shell.svelte:471-500. [x](javascript:alert(1))in a cell renders<a href="javascript:alert(1)">, andapp-shell's click handler only intercepts http(s) andnodespace://.- 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.
- Files:
-
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 containsx | yplus 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.
- A task node (whose content is stored without its
-
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.
…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]>
c5fd87e to
170dca9
Compare
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|ora | bwith a delimiter row stayed a text node and showed raw markdown.table-markdown.ts(new): the GFM parser moved out oftable-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 exportsTABLE_DETECT_PATTERN.core-plugins.ts: the table plugin'sdetectnow 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.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-appquality:check(eslint, design tokens, svelte-check: 0 errors).Closes #3840
🤖 Generated with Claude Code