-
Notifications
You must be signed in to change notification settings - Fork 7
fix(editor): paste markdown tables that contain blank cells #1484
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
Merged
+143
−0
Merged
Changes from 1 commit
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
83 changes: 83 additions & 0 deletions
83
apps/web/src/specs/features/tiptap-editor/empty-table-cell-paste.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,83 @@ | ||
| import { vi } from "vitest"; | ||
|
|
||
| vi.mock("@/features/tiptap-editor/extensions", () => ({ | ||
| HIVE_POST_PURE_REGEX: /$a^/, | ||
| LOOM_REGEX: /$a^/, | ||
| TAG_MENTION_PURE_REGEX: /$a^/, | ||
| 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 { parseAllExtensionsToDoc } from "@/features/tiptap-editor/functions/parse-all-extensions-to-doc"; | ||
|
|
||
| const TABLE_EXTENSIONS = [StarterKit, Table, TableRow, TableCell, TableHeader]; | ||
|
|
||
| /** Pastes markdown exactly as the clipboard text strategy does. */ | ||
| function pasteMarkdown(markdown: string): string { | ||
| const editor = new Editor({ extensions: TABLE_EXTENSIONS, content: "<p></p>" }); | ||
| try { | ||
| editor.chain().insertContent(parseAllExtensionsToDoc(simpleMarkdownToHTML(markdown))).run(); | ||
| return editor.getHTML(); | ||
| } finally { | ||
| editor.destroy(); | ||
| } | ||
| } | ||
|
|
||
| const rowCount = (html: string) => (html.match(/<tr/g) || []).length; | ||
|
|
||
| describe("pasting a markdown table with blank cells", () => { | ||
| // Regression: a blank cell renders as <td></td>, which the ProseMirror | ||
| // tableCell schema rejects with "Invalid content for node tableCell: <>". | ||
| // insertContent throws, so nothing is inserted and the table never appears. | ||
| // One blank cell anywhere was enough to lose the whole table. | ||
| it("does not throw on a trailing blank cell", () => { | ||
| expect(() => pasteMarkdown("| A | B |\n| --- | --- |\n| 1 | |")).not.toThrow(); | ||
| }); | ||
|
|
||
| it("keeps every row when a cell is blank", () => { | ||
| expect(rowCount(pasteMarkdown("| A | B |\n| --- | --- |\n| 1 | |"))).toBe(2); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["trailing blank", "| A | B |\n| --- | --- |\n| 1 | |"], | ||
| ["leading blank", "| A | B |\n| --- | --- |\n| | 2 |"], | ||
| ["middle blank", "| A | B | C |\n| --- | --- | --- |\n| 1 | | 3 |"], | ||
| ["whole row blank", "| A | B |\n| --- | --- |\n| | |"], | ||
| ["blank header cell", "| A | |\n| --- | --- |\n| 1 | 2 |"], | ||
| ["several blank rows", "| A | B |\n| --- | --- |\n| 1 | |\n| | 4 |\n| | |"] | ||
| ])("survives a %s", (_label, markdown) => { | ||
| const html = pasteMarkdown(markdown); | ||
| expect(html).toContain("<table"); | ||
| expect(rowCount(html)).toBe(markdown.split("\n").length - 1); | ||
| }); | ||
|
|
||
| it("preserves the content of the cells that are not blank", () => { | ||
| const html = pasteMarkdown("| Date | Memo |\n| --- | --- |\n| 2019-02-11 | |\n| 2023-01-06 | closed off |"); | ||
|
|
||
| expect(html).toContain("2019-02-11"); | ||
| expect(html).toContain("2023-01-06"); | ||
| expect(html).toContain("closed off"); | ||
| }); | ||
|
|
||
| it("still pastes a table with no blank cells at all", () => { | ||
| const html = pasteMarkdown("| A | B |\n| --- | --- |\n| 1 | 2 |"); | ||
|
|
||
| expect(rowCount(html)).toBe(2); | ||
| expect(html).toContain("1"); | ||
| expect(html).toContain("2"); | ||
| }); | ||
|
|
||
| it("fills a blank cell with an empty paragraph rather than dropping it", () => { | ||
| const doc = parseAllExtensionsToDoc("<table><tbody><tr><td>1</td><td></td></tr></tbody></table>"); | ||
|
|
||
| expect(doc).toContain("<td><p></p></td>"); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.