Skip to content

Commit 510968f

Browse files
authored
chore(examples): fix JSX rendering of internal links (#10303)
fix #10197 The template was missing defining internalDocToHref. This callback is necessary because Payload does not know which URLs each document will be displayed on.
1 parent a000e4a commit 510968f

File tree

1 file changed

+16
-1
lines changed
  • templates/website/src/components/RichText

1 file changed

+16
-1
lines changed

templates/website/src/components/RichText/index.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { MediaBlock } from '@/blocks/MediaBlock/Component'
2-
import { DefaultNodeTypes, SerializedBlockNode } from '@payloadcms/richtext-lexical'
2+
import {
3+
DefaultNodeTypes,
4+
SerializedBlockNode,
5+
SerializedLinkNode,
6+
} from '@payloadcms/richtext-lexical'
37
import { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
48
import {
59
JSXConvertersFunction,
10+
LinkJSXConverter,
611
RichText as RichTextWithoutBlocks,
712
} from '@payloadcms/richtext-lexical/react'
813

@@ -21,8 +26,18 @@ type NodeTypes =
2126
| DefaultNodeTypes
2227
| SerializedBlockNode<CTABlockProps | MediaBlockProps | BannerBlockProps | CodeBlockProps>
2328

29+
const internalDocToHref = ({ linkNode }: { linkNode: SerializedLinkNode }) => {
30+
const { value, relationTo } = linkNode.fields.doc!
31+
if (typeof value !== 'object') {
32+
throw new Error('Expected value to be an object')
33+
}
34+
const slug = value.slug
35+
return relationTo === 'posts' ? `/posts/${slug}` : `/${slug}`
36+
}
37+
2438
const jsxConverters: JSXConvertersFunction<NodeTypes> = ({ defaultConverters }) => ({
2539
...defaultConverters,
40+
...LinkJSXConverter({ internalDocToHref }),
2641
blocks: {
2742
banner: ({ node }) => <BannerBlock className="col-start-2 mb-4" {...node.fields} />,
2843
mediaBlock: ({ node }) => (

0 commit comments

Comments
 (0)