From fa838c5e5bbdfcf8df11185e387c9cad49c5d3de Mon Sep 17 00:00:00 2001 From: Edoardo Sandon Date: Wed, 6 Sep 2023 17:35:13 +0200 Subject: [PATCH] fix: update resolver to handle rendering empty headings and paragraphs inside list items Signed-off-by: Edoardo Sandon --- lib/src/types.ts | 2 +- lib/src/utils/resolveRichTextToNodes.spec.ts | 61 ++++++++++++++++++++ lib/src/utils/resolveRichTextToNodes.ts | 10 +++- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lib/src/types.ts b/lib/src/types.ts index 9e53878..a54ca9f 100644 --- a/lib/src/types.ts +++ b/lib/src/types.ts @@ -124,7 +124,7 @@ export type Heading = { attrs: { level: 1 | 2 | 3 | 4 | 5 | 6; }; - content: Text[]; + content?: Text[]; }; type Blok = { diff --git a/lib/src/utils/resolveRichTextToNodes.spec.ts b/lib/src/utils/resolveRichTextToNodes.spec.ts index 8fb9354..5195187 100644 --- a/lib/src/utils/resolveRichTextToNodes.spec.ts +++ b/lib/src/utils/resolveRichTextToNodes.spec.ts @@ -195,6 +195,13 @@ describe("resolveNode", () => { ], }; + const emptyNode: SchemaNode = { + type: "heading", + attrs: { + level: 2, + }, + }; + // default expect(resolveNode(node)).toStrictEqual({ component: "h1", @@ -225,6 +232,11 @@ describe("resolveNode", () => { }, content: [{ content: "Hello from rich text" }], }); + + // empty content + expect(resolveNode(emptyNode)).toStrictEqual({ + component: "br", + }); }); it("blok", () => { @@ -362,6 +374,29 @@ describe("resolveNode", () => { ], }; + const nodeWithEmptyParagraph: SchemaNode = { + type: "list_item", + content: [ + { + type: "paragraph", + }, + ], + }; + + const nodeWithParagraphContainingHardBreaksBeforeText: SchemaNode = { + type: "list_item", + content: [ + { + type: "paragraph", + content: [ + { type: "hard_break" }, + { type: "hard_break" }, + { text: "some text", type: "text" }, + ], + }, + ], + }; + // default expect(resolveNode(node)).toStrictEqual({ component: "li", @@ -398,6 +433,32 @@ describe("resolveNode", () => { }, ], }); + + // empty content + expect(resolveNode(nodeWithEmptyParagraph)).toStrictEqual({ + component: "li", + content: [ + { + content: "", + }, + ], + }); + + // hard breaks before text + expect( + resolveNode(nodeWithParagraphContainingHardBreaksBeforeText) + ).toStrictEqual({ + component: "li", + content: [ + { + content: [ + { component: "br" }, + { component: "br" }, + { content: "some text" }, + ], + }, + ], + }); }); it("ordered_list", () => { diff --git a/lib/src/utils/resolveRichTextToNodes.ts b/lib/src/utils/resolveRichTextToNodes.ts index b631ffa..f025cc3 100644 --- a/lib/src/utils/resolveRichTextToNodes.ts +++ b/lib/src/utils/resolveRichTextToNodes.ts @@ -170,6 +170,13 @@ export const resolveNode = ( if (node.type === "heading") { const { content, attrs } = node; + // empty line + if (!content) { + return { + component: "br", + }; + } + return { component: `h${attrs.level}`, content: content.map((node) => resolveNode(node, options)), @@ -252,7 +259,8 @@ export const resolveNode = ( // skip rendering p tag inside li if (node.type === "paragraph") { return { - content: node.content.map((node) => resolveNode(node, options)), + content: + node.content?.map((node) => resolveNode(node, options)) || "", }; }