Created
May 10, 2023 08:48
-
-
Save SebbeJohansson/467244f90bec9e3e8994d4bacd00f794 to your computer and use it in GitHub Desktop.
Storyblok Richtext formatting for proper translated internal links and prevents "Cannot read properties of undefined (reading 'color')" error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Our app should never allow for internal links that has a language prefix (since we dont use that) | |
| export const removeLanguageFromLink = (link) => { | |
| if (!link) return link; | |
| const linkWithoutLanguage = link.replace(/^[a-z]{2}\/|[a-z]{2}-[a-z]{2}\//, ''); | |
| return linkWithoutLanguage; | |
| }; | |
| // Our app has specific prefix for some channels, these should be added to internal links. | |
| export const addChannelRootPathToLink = (link, channelRootPath) => { | |
| const linkWithRootPath = `${channelRootPath}${link || ''}`; | |
| return linkWithRootPath; | |
| }; | |
| // If the link doesnt have http in the link, it is propably an internal link. | |
| export const isInternalLink = (link) => { | |
| if (!link) return false; | |
| if (link.startsWith('http')) return false; | |
| return true; | |
| }; | |
| // Method for handling internal links coming from storyblok so that they are properly converted. | |
| export const toInternalLink = (link, channelRootPath) => { | |
| if (!isInternalLink(link)) return link; | |
| const linkWithoutStartingSlash = link.replace(/^\//, ''); | |
| const linkWithRootPath = addChannelRootPathToLink( | |
| removeLanguageFromLink(linkWithoutStartingSlash), | |
| channelRootPath, | |
| ); | |
| return linkWithRootPath; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { renderRichText } from '@storyblok/nuxt-2'; | |
| import { toInternalLink } from '~/utils/storyblok/link-helpers'; | |
| export const renderStoryblokRichText = (richText, channelRootPath) => { | |
| const richTextJson = JSON.stringify(richText); | |
| const richTextObject = JSON.parse(richTextJson); | |
| // This functions goes through the nodes and removes empty links and properly handles internal links. | |
| const loopThroughNodes = (node) => { | |
| const localNode = { ...node }; | |
| localNode.content.map((content) => { | |
| if (content.content) { | |
| content = loopThroughNodes(content); | |
| } | |
| if (content.marks) { | |
| content.marks = content.marks | |
| .filter((mark) => mark.type !== 'link' || (mark.type === 'link' && !!mark.attrs)) // Remove empty links | |
| .map((mark) => { | |
| // Prevents issue with marks without attributes. | |
| mark.attrs = mark.attrs ?? {}; | |
| if (mark.type === 'link' && mark.attrs?.href) { | |
| mark.attrs.href = | |
| toInternalLink(mark.attrs.story?.full_slug, channelRootPath) ?? | |
| mark.attrs.story?.url ?? | |
| mark.attrs.href; | |
| } | |
| return mark; | |
| }); | |
| } | |
| return content; | |
| }); | |
| return localNode; | |
| }; | |
| const rendered = renderRichText(loopThroughNodes(richTextObject)); | |
| return rendered; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment