Skip to content

Commit 6fc21e9

Browse files
Merge pull request #53 from storyblok/feature/anchor-links
Feature/anchor links
2 parents 74b3116 + c579429 commit 6fc21e9

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

source/schema.js

+5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ module.exports = {
115115
attrs.href = `mailto:${attrs.href}`
116116
}
117117

118+
if (attrs.anchor) {
119+
attrs.href = `${attrs.href}#${attrs.anchor}`
120+
delete attrs.anchor
121+
}
122+
118123
return {
119124
tag: [{
120125
tag: 'a',

tests/richTextResolver.test.js

+171
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,174 @@ test('escape html marks from text', () => {
175175

176176
expect(resolver.render(doc)).toBe('<p>&ltp&gtFooter data&lt/p&gt</p><p><b>Another footer data</b></p>')
177177
})
178+
179+
test('link to generate a tag with achor', () => {
180+
const doc = {
181+
type: 'doc',
182+
content: [
183+
{
184+
text: 'link text',
185+
type: 'text',
186+
marks: [
187+
{
188+
type: 'link',
189+
attrs: {
190+
href: '/link',
191+
target: '_blank',
192+
uuid: '300aeadc-c82d-4529-9484-f3f8f09cf9f5',
193+
anchor: 'anchor-text'
194+
}
195+
}
196+
]
197+
}
198+
]
199+
}
200+
201+
const result = resolver.render(doc)
202+
const expected = '<a href="/link#anchor-text" target="_blank" uuid="300aeadc-c82d-4529-9484-f3f8f09cf9f5">link text</a>'
203+
204+
expect(result).toBe(expected)
205+
})
206+
207+
test('Complex and immutability test', () => {
208+
const doc = {
209+
type: "doc",
210+
content: [
211+
{
212+
type: "paragraph",
213+
content: [
214+
{
215+
text: "Lorem",
216+
type: "text",
217+
marks: [
218+
{
219+
type: "bold"
220+
}
221+
]
222+
},
223+
{
224+
text: " ipsum, ",
225+
type: "text"
226+
},
227+
{
228+
text: "dolor",
229+
type: "text",
230+
marks: [
231+
{
232+
type: "strike"
233+
}
234+
]
235+
},
236+
{
237+
text: " sit amet ",
238+
type: "text"
239+
},
240+
{
241+
text: "consectetur",
242+
type: "text",
243+
marks: [
244+
{
245+
type: "underline"
246+
}
247+
]
248+
},
249+
{
250+
text: " adipisicing elit. ",
251+
type: "text"
252+
},
253+
{
254+
text: "Eos architecto",
255+
type: "text",
256+
marks: [
257+
{
258+
"type": "code"
259+
}
260+
]
261+
},
262+
{
263+
text: " asperiores temporibus ",
264+
type: "text"
265+
},
266+
{
267+
text: "suscipit harum ",
268+
type: "text",
269+
marks: [
270+
{
271+
type: "link",
272+
attrs: {
273+
href: "/test/our-service",
274+
uuid: "931e04b7-f701-4fe4-8ec0-78be0bee8809",
275+
anchor: "anchor-text",
276+
target: "_blank",
277+
linktype: "story"
278+
}
279+
}
280+
]
281+
},
282+
{
283+
text: "ut, fugit, cumque ",
284+
type: "text"
285+
},
286+
{
287+
text: "molestiae ",
288+
type: "text",
289+
marks: [
290+
{
291+
type: "link",
292+
attrs: {
293+
href: "asdfsdfasf",
294+
uuid: null,
295+
anchor: null,
296+
target: "_blank",
297+
linktype: "url"
298+
}
299+
}
300+
]
301+
},
302+
{
303+
text: "ratione non adipisci, ",
304+
type: "text"
305+
},
306+
{
307+
text: "facilis",
308+
type: "text",
309+
marks: [
310+
{
311+
"type": "italic"
312+
}
313+
]
314+
},
315+
{
316+
text: " inventore optio dolores. Rem, perspiciatis ",
317+
type: "text"
318+
},
319+
{
320+
text: "deserunt!",
321+
type: "text",
322+
marks: [
323+
{
324+
type: "link",
325+
attrs: {
326+
href: "/home",
327+
uuid: "fc6a453f-9aa6-4a00-a22d-49c5878f7983",
328+
anchor: null,
329+
target: "_self",
330+
linktype: "story"
331+
}
332+
}
333+
]
334+
},
335+
{
336+
text: " Esse, maiores!",
337+
type: "text"
338+
}
339+
]
340+
}
341+
]
342+
}
343+
344+
const result = resolver.render(doc)
345+
const expected = `<p><b>Lorem</b> ipsum, <strike>dolor</strike> sit amet <u>consectetur</u> adipisicing elit. <code>Eos architecto</code> asperiores temporibus <a href="/test/our-service#anchor-text" uuid="931e04b7-f701-4fe4-8ec0-78be0bee8809" target="_blank" linktype="story">suscipit harum </a>ut, fugit, cumque <a href="asdfsdfasf" target="_blank" linktype="url">molestiae </a>ratione non adipisci, <i>facilis</i> inventore optio dolores. Rem, perspiciatis <a href="/home" uuid="fc6a453f-9aa6-4a00-a22d-49c5878f7983" target="_self" linktype="story">deserunt!</a> Esse, maiores!</p>`
346+
347+
expect(result).toBe(expected)
348+
})

0 commit comments

Comments
 (0)