From e6d474c45da7a0410594a4ded1dc13ea96f2adaf Mon Sep 17 00:00:00 2001 From: Baheya Khalifa Date: Thu, 3 Aug 2023 15:25:46 +0300 Subject: [PATCH 1/2] fix: isRichTextEmpty now checks every node in a given RichText structure --- lib/fixtures/richTextObject.json | 231 ++++++++++++++++++++- lib/index.ts | 2 +- lib/tests/__snapshots__/index.test.js.snap | 4 +- 3 files changed, 232 insertions(+), 5 deletions(-) diff --git a/lib/fixtures/richTextObject.json b/lib/fixtures/richTextObject.json index 19c77647..dba2609e 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,232 @@ } ] } + }, + { + "type": "paragraph" + }, + { + "type": "paragraph", + "content": [ + { + "text": "paragraph after empty line", + "type": "text" + } + ] + }, + { + "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 one", + "type": "text" + } + ] + } + ] + } + ] + }, + { + "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 one", + "type": "text" + } + ] + } + ] + } + ] + }, + { + "type": "blockquote", + "content": [ + { + "type": "paragraph", + "content": [ + { + "text": "this is a quote", + "type": "text" + } + ] + } + ] + }, + { + "type": "horizontal_rule" + }, + { + "type": "paragraph", + "content": [ + { + "text": "some word after an
", + "type": "text" + } + ] + }, + { + "type": "blok", + "attrs": { + "id": "12fcf3e3-7f45-4d45-b3a4-dbaf4c50414b", + "body": [ + { + "_uid": "i-86ae8c08-6e61-477b-b1fc-16165bd14dc3", + "code": "a random blok with code", + "title": "", + "caption": { + "type": "doc", + "content": [ + { + "type": "paragraph" + } + ] + }, + "language": "", + "component": "codeblock", + "enable_diff": false, + "_editable": "" + } + ] + } + }, + { + "type": "paragraph", + "content": [ + { + "text": "italic text", + "type": "text", + "marks": [ + { + "type": "italic" + } + ] + } + ] + }, + { + "type": "paragraph", + "content": [ + { + "text": "strikethrough", + "type": "text", + "marks": [ + { + "type": "strike" + } + ] + } + ] + }, + { + "type": "paragraph", + "content": [ + { + "text": "underlined", + "type": "text", + "marks": [ + { + "type": "underline" + } + ] + } + ] + }, + { + "type": "paragraph", + "content": [ + { + "text": "superscript", + "type": "text", + "marks": [ + { + "type": "superscript" + } + ] + } + ] + }, + { + "type": "paragraph", + "content": [ + { + "text": "subscript", + "type": "text", + "marks": [ + { + "type": "subscript" + } + ] + } + ] + }, + { + "type": "paragraph", + "content": [ + { + "text": "inline code", + "type": "text", + "marks": [ + { + "type": "code" + } + ] + }, + { + "text": " ", + "type": "text" + } + ] + }, + { + "type": "paragraph" + }, + { + "type": "paragraph" } ] -} +} \ No newline at end of file diff --git a/lib/index.ts b/lib/index.ts index a0f6d29c..dc5dcadd 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 99c9492a..885bf215 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 one

this is a quote


some word after an <hr>

italic text

strikethrough

underlined

superscript

subscript

inline code

"`; From 8825822e6100469c69bf364bebceb6e1416edf1a Mon Sep 17 00:00:00 2001 From: Baheya Khalifa Date: Thu, 3 Aug 2023 15:53:22 +0300 Subject: [PATCH 2/2] updated richtext tests --- lib/cypress/e2e/index.cy.js | 6 +- lib/fixtures/richTextObject.json | 66 +++++++++++----------- lib/tests/__snapshots__/index.test.js.snap | 2 +- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/lib/cypress/e2e/index.cy.js b/lib/cypress/e2e/index.cy.js index 17758dfe..b6b53d1f 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 dba2609e..a17f6bae 100644 --- a/lib/fixtures/richTextObject.json +++ b/lib/fixtures/richTextObject.json @@ -51,6 +51,9 @@ } ] }, + { + "type": "paragraph" + }, { "type": "bullet_list", "content": [ @@ -75,7 +78,7 @@ "type": "paragraph", "content": [ { - "text": "another one", + "text": "another item", "type": "text" } ] @@ -84,6 +87,9 @@ } ] }, + { + "type": "paragraph" + }, { "type": "ordered_list", "attrs": { @@ -111,7 +117,7 @@ "type": "paragraph", "content": [ { - "text": "another one", + "text": "another item", "type": "text" } ] @@ -120,6 +126,9 @@ } ] }, + { + "type": "paragraph" + }, { "type": "blockquote", "content": [ @@ -134,42 +143,26 @@ } ] }, + { + "type": "paragraph" + }, { "type": "horizontal_rule" }, + { + "type": "paragraph" + }, { "type": "paragraph", "content": [ { - "text": "some word after an
", + "text": "some words after an
", "type": "text" } ] }, { - "type": "blok", - "attrs": { - "id": "12fcf3e3-7f45-4d45-b3a4-dbaf4c50414b", - "body": [ - { - "_uid": "i-86ae8c08-6e61-477b-b1fc-16165bd14dc3", - "code": "a random blok with code", - "title": "", - "caption": { - "type": "doc", - "content": [ - { - "type": "paragraph" - } - ] - }, - "language": "", - "component": "codeblock", - "enable_diff": false, - "_editable": "" - } - ] - } + "type": "paragraph" }, { "type": "paragraph", @@ -185,6 +178,9 @@ } ] }, + { + "type": "paragraph" + }, { "type": "paragraph", "content": [ @@ -199,6 +195,9 @@ } ] }, + { + "type": "paragraph" + }, { "type": "paragraph", "content": [ @@ -213,6 +212,9 @@ } ] }, + { + "type": "paragraph" + }, { "type": "paragraph", "content": [ @@ -227,6 +229,9 @@ } ] }, + { + "type": "paragraph" + }, { "type": "paragraph", "content": [ @@ -241,6 +246,9 @@ } ] }, + { + "type": "paragraph" + }, { "type": "paragraph", "content": [ @@ -258,12 +266,6 @@ "type": "text" } ] - }, - { - "type": "paragraph" - }, - { - "type": "paragraph" } ] } \ No newline at end of file diff --git a/lib/tests/__snapshots__/index.test.js.snap b/lib/tests/__snapshots__/index.test.js.snap index 885bf215..9a0755b7 100644 --- a/lib/tests/__snapshots__/index.test.js.snap +++ b/lib/tests/__snapshots__/index.test.js.snap @@ -1,3 +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 valid RichText object 1`] = `"

Holain bold

paragraph after empty line

  1. item in another list

  2. another one

this is a quote


some word after an <hr>

italic text

strikethrough

underlined

superscript

subscript

inline code

"`; +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

"`;