Skip to content

Commit

Permalink
Merge pull request #490 from storyblok/task/int-841
Browse files Browse the repository at this point in the history
feat(int 841): Adding support for anchor in the richtext resolver
  • Loading branch information
Thiago Saife authored Mar 16, 2023
2 parents 4756f33 + d2e9efe commit 93a94a9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export interface ISbError {
}

export interface ISbNode extends Element {
content: object[]
attrs: {
anchor?: string
body: Array<ISbComponentType<any>>
Expand Down
14 changes: 14 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const heading: NodeSchema = (node: ISbNode) => {
tag: `h${node.attrs.level}`,
}
}

const image: NodeSchema = (node: ISbNode) => {
return {
singleTag: [
Expand Down Expand Up @@ -157,6 +158,7 @@ const link: MarkSchema = (node: ISbNode) => {
],
}
}

const styled: MarkSchema = (node: ISbNode) => {
return {
tag: [
Expand All @@ -180,6 +182,17 @@ const superscript: MarkSchema = () => {
}
}

const anchor: MarkSchema = (node: ISbNode) => {
return {
tag: [
{
tag: 'span',
attrs: node.attrs,
},
],
}
}

const highlight: MarkSchema = (node: ISbNode) => {
const attrs = {
['style']: `background-color:${node.attrs.color};`,
Expand Down Expand Up @@ -233,6 +246,7 @@ export default {
styled,
subscript,
superscript,
anchor,
highlight,
textStyle,
},
Expand Down
54 changes: 54 additions & 0 deletions tests/constants/richTextResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,60 @@ 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"
}
}
]
}
]
}
]
}

export const TEXT_COLOR_DATA = {
type: 'doc',
content: [
Expand Down
58 changes: 57 additions & 1 deletion tests/richTextResolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
TEXT_COLOR_DATA,
HIGLIGHT_COLOR_DATA,
BOLD_TEXT,
Expand Down Expand Up @@ -325,13 +327,60 @@ 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 middle 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 = '<h1>Title with <span id="test1">Anchor</span> in the midle</h1>'
expect(result).toBe(expected)
})

test('should render a text with text color', () => {
const result = resolver.render(TEXT_COLOR_DATA)
const expected = '<span style="background-color:#E72929">Colored text</span>'

expect(result).toBe(expected)
})

test('should render a anchor in the text', () => {
const result = resolver.render(PARAGRAPH_WITH_ANCHOR)
const expected = '<p><span id="test">Paragraph with anchor in the midle</span></p>'

expect(result).toBe(expected)
})

test('should render a text with highlight color', () => {
const result = resolver.render(HIGLIGHT_COLOR_DATA)
const expected =
Expand All @@ -340,9 +389,16 @@ test('should render a text with highlight color', () => {
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 = '<p>a long text with a super nice <span id="test2">anchor here</span>, and at the end of the text is a normal tag</p>'

expect(result).toBe(expected)
})

test('should render a text with bold', () => {
const result = resolver.render(BOLD_TEXT)
const expected = '<b>Lorem Ipsum</b>'

expect(result).toBe(expected)
})
})

0 comments on commit 93a94a9

Please sign in to comment.