From a2b75c60655773c91c3e4fe6ca80b589f7e43c30 Mon Sep 17 00:00:00 2001 From: ademarCardoso Date: Thu, 9 Mar 2023 10:54:08 -0300 Subject: [PATCH 1/4] feat(int-841): add initial tests for anchor support --- src/interfaces.ts | 1 + src/schema.ts | 14 ++++++++++++ tests/richTextResolver.test.js | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/interfaces.ts b/src/interfaces.ts index 829383ae..8b896125 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -199,6 +199,7 @@ export interface ISbError { } export interface ISbNode extends Element { + content: any attrs: { anchor?: string body: Array> diff --git a/src/schema.ts b/src/schema.ts index c1db2d55..d9cd3654 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -55,6 +55,7 @@ const heading: NodeSchema = (node: ISbNode) => { tag: `h${node.attrs.level}`, } } + const image: NodeSchema = (node: ISbNode) => { return { singleTag: [ @@ -157,6 +158,7 @@ const link: MarkSchema = (node: ISbNode) => { ], } } + const styled: MarkSchema = (node: ISbNode) => { return { tag: [ @@ -180,6 +182,17 @@ const superscript: MarkSchema = () => { } } +const anchor: MarkSchema = (node: ISbNode) => { + return { + tag: [ + { + tag: 'span', + attrs: node.attrs, + }, + ], + } +} + export default { nodes: { horizontal_rule, @@ -205,5 +218,6 @@ export default { styled, subscript, superscript, + anchor }, } diff --git a/tests/richTextResolver.test.js b/tests/richTextResolver.test.js index 0823d2a5..e145949f 100644 --- a/tests/richTextResolver.test.js +++ b/tests/richTextResolver.test.js @@ -321,3 +321,44 @@ test('should render a text with links, subscripts and superscripts', () => { expect(result).toBe(expected) }) + +test('should render a h1 title with a anchor in the midlle of the text', () => { + const sentenceWithAnchor = { + type: 'doc', + content: [ + { + type: 'heading', + attrs: { + level: '1' + }, + content: [ + { + text: 'Title with ', + type: 'text' + }, + { + text: 'Anchor', + type: 'text', + marks: [ + { + type: 'anchor', + attrs: { + id: 'test1' + } + } + ] + }, + { + text: ' in the midle', + type: 'text' + } + ] + } + ] + } + + const result = resolver.render(sentenceWithAnchor) + const expected = '

Title with Anchor in the midle

' + + expect(result).toBe(expected) +}) From 051cfa4e148c7059ae48ce7f2fb5b271a9ec140e Mon Sep 17 00:00:00 2001 From: ademarCardoso Date: Mon, 13 Mar 2023 14:18:58 -0300 Subject: [PATCH 2/4] feat(int-841): add tests for anchor support --- tests/constants/richTextResolver.js | 54 +++++++++++++++++++++++++++++ tests/richTextResolver.test.js | 16 +++++++++ 2 files changed, 70 insertions(+) diff --git a/tests/constants/richTextResolver.js b/tests/constants/richTextResolver.js index 2f8cde7a..b3778a88 100644 --- a/tests/constants/richTextResolver.js +++ b/tests/constants/richTextResolver.js @@ -350,4 +350,58 @@ export const LONG_TEXT_WITH_LINKS_SUB_SUP_SCRIPTS = { ] } ] +} + +export const PARAGRAPH_WITH_ANCHOR_IN_THE_MIDDLE = { + "type": "doc", + "content": [ + { + "type": "paragraph", + "content": [ + { + "text": "a long text with a super nice ", + "type": "text" + }, + { + "text": "anchor here", + "type": "text", + "marks": [ + { + "type": "anchor", + "attrs": { + "id": "test2" + } + } + ] + }, + { + "text": ", and at the end of the text is a normal tag", + "type": "text" + } + ] + } + ] +} + +export const PARAGRAPH_WITH_ANCHOR = { + "type": "doc", + "content": [ + { + "type": "paragraph", + "content": [ + { + "text": "Paragraph with anchor in the midle", + "type": "text", + "marks": [ + { + "type": "anchor", + "attrs": { + "id": "test" + } + } + ] + } + ] + } + ] } \ No newline at end of file diff --git a/tests/richTextResolver.test.js b/tests/richTextResolver.test.js index e145949f..b7a3896c 100644 --- a/tests/richTextResolver.test.js +++ b/tests/richTextResolver.test.js @@ -11,6 +11,8 @@ import { CUSTOM_ATTRIBUTE_DATA, LONG_TEXT_WITH_LINKS_SUB_SUP_SCRIPTS, LINK_WITH_ANCHOR_FOR_CUSTOM_SCHEMA, + PARAGRAPH_WITH_ANCHOR_IN_THE_MIDDLE, + PARAGRAPH_WITH_ANCHOR, } from './constants/richTextResolver' const TOKEN = 'w0yFvs04aKF2rpz6F8OfIQtt' @@ -362,3 +364,17 @@ test('should render a h1 title with a anchor in the midlle of the text', () => { expect(result).toBe(expected) }) + +test('should render a anchor in the text', () => { + const result = resolver.render(PARAGRAPH_WITH_ANCHOR) + const expected = '

Paragraph with anchor in the midle

' + + expect(result).toBe(expected) +}) + +test('should render a anchor in the middle of a text', () => { + const result = resolver.render(PARAGRAPH_WITH_ANCHOR_IN_THE_MIDDLE) + const expected = '

a long text with a super nice anchor here, and at the end of the text is a normal tag

' + + expect(result).toBe(expected) +}) From f5f4f9618be79a3020a0e240d5cfd4d22eba8801 Mon Sep 17 00:00:00 2001 From: ademarCardoso Date: Mon, 13 Mar 2023 14:23:17 -0300 Subject: [PATCH 3/4] feat(int-841): fix typo --- tests/richTextResolver.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/richTextResolver.test.js b/tests/richTextResolver.test.js index b7a3896c..12f4cc38 100644 --- a/tests/richTextResolver.test.js +++ b/tests/richTextResolver.test.js @@ -324,7 +324,7 @@ test('should render a text with links, subscripts and superscripts', () => { expect(result).toBe(expected) }) -test('should render a h1 title with a anchor in the midlle of the text', () => { +test('should render a h1 title with a anchor in the middle of the text', () => { const sentenceWithAnchor = { type: 'doc', content: [ From d2e9efe2aea23cc29585c770446051408905cb05 Mon Sep 17 00:00:00 2001 From: ademarCardoso Date: Thu, 16 Mar 2023 10:28:04 -0300 Subject: [PATCH 4/4] feat(int-841): improve interfaces --- src/interfaces.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 8b896125..f550d0a4 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -199,7 +199,7 @@ export interface ISbError { } export interface ISbNode extends Element { - content: any + content: object[] attrs: { anchor?: string body: Array>