-
Notifications
You must be signed in to change notification settings - Fork 7
fix(editor): keep markdown tables intact through the publish editor #1482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
apps/web/src/specs/features/tiptap-editor/table-roundtrip.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { vi } from "vitest"; | ||
|
|
||
| vi.mock("@/features/tiptap-editor/extensions", () => ({ | ||
| HIVE_POST_PURE_REGEX: /$a^/, | ||
| LOOM_REGEX: /$a^/, | ||
| TAG_MENTION_PURE_REGEX: /$a^/, | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
| USER_MENTION_PURE_REGEX: /$a^/, | ||
| YOUTUBE_REGEX: /$a^/ | ||
| })); | ||
|
|
||
| import { Editor } from "@tiptap/core"; | ||
| import StarterKit from "@tiptap/starter-kit"; | ||
| import Table from "@tiptap/extension-table"; | ||
| import TableCell from "@tiptap/extension-table-cell"; | ||
| import TableHeader from "@tiptap/extension-table-header"; | ||
| import TableRow from "@tiptap/extension-table-row"; | ||
| import { simpleMarkdownToHTML } from "@ecency/render-helper"; | ||
|
|
||
| import { markdownToHtml } from "@/features/tiptap-editor/functions/markdown-to-html"; | ||
| import { parseAllExtensionsToDoc } from "@/features/tiptap-editor/functions/parse-all-extensions-to-doc"; | ||
|
|
||
| const TABLE_MARKDOWN = [ | ||
| "| Date | Account | Amount |", | ||
| "| --- | --- | --- |", | ||
| "| 2023-01-06 | valueplan | 52,239.55 |", | ||
| "| 2020-04-03 | ecency | 941.94 |" | ||
| ].join("\n"); | ||
|
|
||
| const TABLE_EXTENSIONS = [StarterKit, Table, TableRow, TableCell, TableHeader]; | ||
|
|
||
| /** | ||
| * Mirrors what the publish editor actually does: paste plain text (converted by | ||
| * the clipboard strategy), then serialize the document back to markdown the way | ||
| * `use-publish-editor` does on every update. | ||
| */ | ||
| function pasteThenSerialize(markdown: string): string { | ||
| const editor = new Editor({ extensions: TABLE_EXTENSIONS, content: "<p></p>" }); | ||
| try { | ||
| editor.chain().insertContent(parseAllExtensionsToDoc(simpleMarkdownToHTML(markdown))).run(); | ||
| return markdownToHtml(editor.getHTML()); | ||
| } finally { | ||
| editor.destroy(); | ||
| } | ||
| } | ||
|
|
||
| /** Runs `build` against a live editor, then serializes exactly as publish does. */ | ||
| function buildThenSerialize(build: (editor: Editor) => void): string { | ||
| const editor = new Editor({ extensions: TABLE_EXTENSIONS, content: "<p></p>" }); | ||
| try { | ||
| build(editor); | ||
| return markdownToHtml(editor.getHTML()); | ||
| } finally { | ||
| editor.destroy(); | ||
| } | ||
| } | ||
|
|
||
| describe("markdown table round-trip through the publish editor", () => { | ||
| // Regression: Turndown was built with only the `strikethrough` GFM plugin, so | ||
| // it had no rule for <table> and flattened every pasted table into loose text | ||
| // on the first serialization pass. | ||
| it("keeps a pasted table a table", () => { | ||
| const result = pasteThenSerialize(TABLE_MARKDOWN); | ||
|
|
||
| expect(result).toContain("| Date | Account | Amount |"); | ||
| expect(result).toContain("| 2023-01-06 | valueplan | 52,239.55 |"); | ||
| expect(result).toContain("| 2020-04-03 | ecency | 941.94 |"); | ||
| }); | ||
|
|
||
| it("keeps every row on its own line with a delimiter row", () => { | ||
| const lines = pasteThenSerialize(TABLE_MARKDOWN) | ||
| .split("\n") | ||
| .map((line) => line.trim()) | ||
| .filter((line) => line.startsWith("|")); | ||
|
|
||
| // header + delimiter + 2 body rows | ||
| expect(lines).toHaveLength(4); | ||
| expect(lines[1]).toMatch(/^\|[-\s|:]+\|$/); | ||
| lines.forEach((line) => expect(line.split("|")).toHaveLength(5)); | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| // Regression: TipTap emits <colgroup> before <tbody> and keeps the header | ||
| // cells as <th> inside that <tbody>, which made the GFM rule miss the heading | ||
| // row and prepend an empty one ("| | | |") above the real headers. | ||
| it("uses the real header row rather than prepending an empty one", () => { | ||
| const lines = pasteThenSerialize(TABLE_MARKDOWN) | ||
| .split("\n") | ||
| .map((line) => line.trim()) | ||
| .filter((line) => line.startsWith("|")); | ||
|
|
||
| expect(lines[0]).toContain("Date"); | ||
| expect(lines[0]).toContain("Account"); | ||
| expect(lines[0]).not.toMatch(/^\|(\s*\|)+$/); | ||
| }); | ||
|
|
||
| it("does not flatten cells into loose paragraphs", () => { | ||
| const result = pasteThenSerialize(TABLE_MARKDOWN); | ||
|
|
||
| // the pre-fix output was "Date\n\nAccount\n\nAmount\n\n2023-01-06\n\n..." | ||
| expect(result).not.toMatch(/^Date\s*$/m); | ||
| expect(result).not.toMatch(/^valueplan\s*$/m); | ||
| }); | ||
|
|
||
| it("serializes a table built with the editor's own insertTable command", () => { | ||
| const result = buildThenSerialize((editor) => { | ||
| editor.chain().focus().insertTable({ rows: 2, cols: 2, withHeaderRow: true }).run(); | ||
| }); | ||
|
|
||
| expect(result).toMatch(/\|.*\|/); | ||
| }); | ||
| }); | ||
|
|
||
| describe("single-cell tables the GFM rule cannot express", () => { | ||
| // Regression: the GFM table rule deliberately skips single-cell tables, so a | ||
| // 1x1 built from the toolbar serialized to bare text, or to nothing at all | ||
| // when empty, and vanished on the next draft load or publish. | ||
| const shrinkToSingleCell = (editor: Editor) => { | ||
| editor.chain().focus().insertTable({ rows: 2, cols: 2, withHeaderRow: true }).run(); | ||
| editor.chain().focus().deleteColumn().run(); | ||
| editor.chain().focus().deleteRow().run(); | ||
| }; | ||
|
|
||
| it("keeps an empty 1x1 table instead of serializing it away", () => { | ||
| const result = buildThenSerialize(shrinkToSingleCell); | ||
|
|
||
| expect(result.trim()).not.toBe(""); | ||
| expect(result).toContain("<table"); | ||
| }); | ||
|
|
||
| it("keeps a populated 1x1 table instead of flattening it to text", () => { | ||
| const result = buildThenSerialize((editor) => { | ||
| shrinkToSingleCell(editor); | ||
| editor.commands.insertContent("solo"); | ||
| }); | ||
|
|
||
| expect(result).toContain("<table"); | ||
| expect(result).toContain("solo"); | ||
| // pre-fix this was the bare string "solo" with no table markup at all | ||
| expect(result.trim()).not.toBe("solo"); | ||
| }); | ||
|
|
||
| it("still uses markdown syntax once the table has more than one cell", () => { | ||
| const result = buildThenSerialize((editor) => { | ||
| editor.chain().focus().insertTable({ rows: 2, cols: 2, withHeaderRow: true }).run(); | ||
| }); | ||
|
|
||
| expect(result).not.toContain("<table"); | ||
| expect(result).toContain("|"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an existing post contains a header table with block content in a cell, such as a list or blockquote, registering
tablesafter the custom table rule makes the plugin take precedence and converts the table to GFM even though GFM cells only support inline content. The conversion replaces the block structure with line-break-delimited Markdown, so merely editing the post silently changes or exposes that cell content on republish; the previoustablerule preserved such tables as HTML. Only route GFM-compatible tables through this plugin and retain the HTML fallback for cells with unsupported structure.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, and this one made me re-check the whole premise. The pre-existing
tablerule returningouterHTMLmeans develop already preserves tables losslessly, including cells with block content. Registeringtablesafter it trades that for GFM, which only holds inline content, so lists and blockquotes in cells degrade to<br>.Since the round-trip was never broken, that trade buys nothing. I have marked the PR draft and recommended closing it rather than adding a compatibility check, since the fix it claimed to make was not needed. Full write-up in the PR comment.