Skip to content

Commit 33048f4

Browse files
fix: Parsing paragraphs without text content (#798)
* Made paragraphs only get parsed if they have text content * Added unit test
1 parent bb1bdce commit 33048f4

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"id": "1",
4+
"type": "image",
5+
"props": {
6+
"backgroundColor": "default",
7+
"textAlignment": "left",
8+
"name": "",
9+
"url": "exampleURL",
10+
"caption": "",
11+
"showPreview": true,
12+
"previewWidth": 512
13+
},
14+
"children": []
15+
}
16+
]

packages/core/src/api/parsers/html/parseHTML.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ describe("Parse HTML", () => {
295295
await parseHTMLAndCompareSnapshots(html, "parse-two-divs");
296296
});
297297

298+
it("Parse image in paragraph", async () => {
299+
const html = `<p>
300+
<img src="exampleURL">
301+
</p>`;
302+
303+
await parseHTMLAndCompareSnapshots(html, "parse-image-in-paragraph");
304+
});
305+
298306
it("Parse fake image caption", async () => {
299307
const html = `<div>
300308
<img src="exampleURL">

packages/core/src/blocks/ParagraphBlockContent/ParagraphBlockContent.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ export const ParagraphBlockContent = createStronglyTypedTiptapNode({
4141
{
4242
tag: "p",
4343
priority: 200,
44+
getAttrs: (element) => {
45+
if (typeof element === "string" || !element.textContent?.trim()) {
46+
return false;
47+
}
48+
49+
return {};
50+
},
4451
node: "paragraph",
4552
},
4653
];

packages/core/src/blocks/TableBlockContent/TableBlockContent.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,29 @@ const TableParagraph = Node.create({
4545
content: "inline*",
4646

4747
parseHTML() {
48-
return [{ tag: "p" }];
48+
return [
49+
{ tag: "td" },
50+
{
51+
tag: "p",
52+
getAttrs: (element) => {
53+
if (typeof element === "string" || !element.textContent) {
54+
return false;
55+
}
56+
57+
const parent = element.parentElement;
58+
59+
if (parent === null) {
60+
return false;
61+
}
62+
63+
if (parent.tagName === "TD") {
64+
return {};
65+
}
66+
67+
return false;
68+
},
69+
},
70+
];
4971
},
5072

5173
renderHTML({ HTMLAttributes }) {

0 commit comments

Comments
 (0)