Skip to content

Commit

Permalink
fix: update resolver to handle rendering empty headings and paragraph…
Browse files Browse the repository at this point in the history
…s inside list items

Signed-off-by: Edoardo Sandon <[email protected]>
  • Loading branch information
Edo-San authored and edvinasjurele committed Sep 6, 2023
1 parent f72a956 commit fa838c5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export type Heading = {
attrs: {
level: 1 | 2 | 3 | 4 | 5 | 6;
};
content: Text[];
content?: Text[];
};

type Blok = {
Expand Down
61 changes: 61 additions & 0 deletions lib/src/utils/resolveRichTextToNodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ describe("resolveNode", () => {
],
};

const emptyNode: SchemaNode = {
type: "heading",
attrs: {
level: 2,
},
};

// default
expect(resolveNode(node)).toStrictEqual({
component: "h1",
Expand Down Expand Up @@ -225,6 +232,11 @@ describe("resolveNode", () => {
},
content: [{ content: "Hello from rich text" }],
});

// empty content
expect(resolveNode(emptyNode)).toStrictEqual({
component: "br",
});
});

it("blok", () => {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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", () => {
Expand Down
10 changes: 9 additions & 1 deletion lib/src/utils/resolveRichTextToNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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)) || "",
};
}

Expand Down

0 comments on commit fa838c5

Please sign in to comment.