diff --git a/lib/cypress/e2e/index.cy.js b/lib/cypress/e2e/index.cy.js index 17758df..b6b53d1 100644 --- a/lib/cypress/e2e/index.cy.js +++ b/lib/cypress/e2e/index.cy.js @@ -27,7 +27,7 @@ describe("@storyblok/js", () => { cy.get("@consoleError").should("not.be.called"); cy.get("#rich-text-container").should( "have.html", - "

Holain bold

" + "

Holain bold

paragraph after empty line

  1. item in another list

  2. another item

this is a quote


some words after an <hr>

italic text

strikethrough

underlined

superscript

subscript

inline code

" ); }); @@ -38,7 +38,7 @@ describe("@storyblok/js", () => { cy.get(".render-rich-text").click(); cy.get("#rich-text-container").should( "have.html", - 'Holain bold
hey John
' + 'Holain bold
hey John
paragraph after empty linean item in a listanother itemitem in another listanother itemthis is a quotesome words after an <hr>italic textstrikethroughunderlinedsuperscriptsubscriptinline code ' ); }); @@ -49,7 +49,7 @@ describe("@storyblok/js", () => { cy.get(".render-rich-text-options").click(); cy.get("#rich-text-container").should( "have.html", - 'Holain bold
hey John
' + 'Holain bold
hey John
paragraph after empty linean item in a listanother itemitem in another listanother itemthis is a quotesome words after an <hr>italic textstrikethroughunderlinedsuperscriptsubscriptinline code ' ); }); }); diff --git a/lib/fixtures/richTextObject.json b/lib/fixtures/richTextObject.json index 19c7764..a17f6ba 100644 --- a/lib/fixtures/richTextObject.json +++ b/lib/fixtures/richTextObject.json @@ -1,6 +1,9 @@ { "type": "doc", "content": [ + { + "type": "paragraph" + }, { "type": "paragraph", "content": [ @@ -35,6 +38,234 @@ } ] } + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "paragraph after empty line", + "type": "text" + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "bullet_list", + "content": [ + { + "type": "list_item", + "content": [ + { + "type": "paragraph", + "content": [ + { + "text": "an item in a list", + "type": "text" + } + ] + } + ] + }, + { + "type": "list_item", + "content": [ + { + "type": "paragraph", + "content": [ + { + "text": "another item", + "type": "text" + } + ] + } + ] + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "ordered_list", + "attrs": { + "order": 1 + }, + "content": [ + { + "type": "list_item", + "content": [ + { + "type": "paragraph", + "content": [ + { + "text": "item in another list", + "type": "text" + } + ] + } + ] + }, + { + "type": "list_item", + "content": [ + { + "type": "paragraph", + "content": [ + { + "text": "another item", + "type": "text" + } + ] + } + ] + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "blockquote", + "content": [ + { + "type": "paragraph", + "content": [ + { + "text": "this is a quote", + "type": "text" + } + ] + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "horizontal_rule" + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "some words after an
", + "type": "text" + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "italic text", + "type": "text", + "marks": [ + { + "type": "italic" + } + ] + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "strikethrough", + "type": "text", + "marks": [ + { + "type": "strike" + } + ] + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "underlined", + "type": "text", + "marks": [ + { + "type": "underline" + } + ] + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "superscript", + "type": "text", + "marks": [ + { + "type": "superscript" + } + ] + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "subscript", + "type": "text", + "marks": [ + { + "type": "subscript" + } + ] + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "inline code", + "type": "text", + "marks": [ + { + "type": "code" + } + ] + }, + { + "text": " ", + "type": "text" + } + ] } ] -} +} \ No newline at end of file diff --git a/lib/index.ts b/lib/index.ts index a0f6d29..dc5dcad 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -108,7 +108,7 @@ const setComponentResolver = (resolver, resolveFn) => { }; export const isRichTextEmpty = (data?: ISbRichtext) => { -return !data || (data?.content?.[0].type !== "blok" && !data?.content?.[0].content); + return !data || !data?.content.some((node) => node.content || node.type === 'blok' || node.type === 'horizontal_rule'); } export const renderRichText = ( diff --git a/lib/tests/__snapshots__/index.test.js.snap b/lib/tests/__snapshots__/index.test.js.snap index 99c9492..9a0755b 100644 --- a/lib/tests/__snapshots__/index.test.js.snap +++ b/lib/tests/__snapshots__/index.test.js.snap @@ -1,5 +1,3 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`@storyblok/js > Rich Text Resolver > should return the rendered HTML when passing a RichText object 1`] = `"

Holain bold

"`; - -exports[`@storyblok/js > Rich Text Resolver > should return the rendered HTML when passing a valid RichText object 1`] = `"

Holain bold

"`; +exports[`@storyblok/js > Rich Text Resolver > should return the rendered HTML when passing a valid RichText object 1`] = `"

Holain bold

paragraph after empty line

  1. item in another list

  2. another item

this is a quote


some words after an <hr>

italic text

strikethrough

underlined

superscript

subscript

inline code

"`;