-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathportable-text.tsx
More file actions
84 lines (80 loc) · 2.65 KB
/
portable-text.tsx
File metadata and controls
84 lines (80 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* This component uses Portable Text to render a post body.
*
* You can learn more about Portable Text on:
* https://www.sanity.io/docs/block-content
* https://github.com/portabletext/react-portabletext
* https://portabletext.org/
*
*/
import {
PortableText,
type PortableTextComponents,
type PortableTextBlock,
} from "next-sanity";
import Link from "next/link";
import dynamic from "next/dynamic";
const BlockImage = dynamic(() => import("@/components/block-image"));
const BlockCode = dynamic(() => import("@/components/block-code"));
const TwitterEmbed = dynamic(() => import("@/components/twitter-embed"));
const CodePenEmbed = dynamic(() => import("@/components/codepen-embed"));
const CodeSandboxEmbed = dynamic(() => import("./codesandbox-embed"));
const HTMLEmbed = dynamic(() => import("@/components/html-embed"));
const QuoteEmbed = dynamic(() => import("@/components/quote-embed"));
const BlockTable = dynamic(() => import("@/components/block-table"));
const BlockYoutube = dynamic(() =>
import("@/components/youtube").then((mod) => mod.YouTube),
);
const YouTubeShorts = dynamic(() => import("./youtube-shorts").then((mod) => mod.YouTubeShorts));
export default function CustomPortableText({
className,
value,
}: {
className?: string;
value: PortableTextBlock[];
}) {
const components: PortableTextComponents = {
// TODO: make this more dynamic
types: {
image: ({ value }) => <BlockImage image={value} />,
code: ({ value }) => <BlockCode {...value} />,
codepen: ({ value }) => <CodePenEmbed {...value} />,
codesandbox: ({ value }) => <CodeSandboxEmbed {...value} />,
youtube: ({ value }) => <div className="not-prose"><BlockYoutube {...value} /></div>,
youtubeShorts: ({ value }) => <div className="not-prose"><YouTubeShorts {...value} /></div>,
twitter: ({ value }) => <TwitterEmbed {...value} />,
htmlBlock: ({ value }) => <HTMLEmbed {...value} />,
quote: ({ value }) => <QuoteEmbed {...value} />,
table: ({ value }) => <BlockTable value={value} />,
},
block: {
h5: ({ children }) => (
<h5 className="mb-2 text-sm font-semibold">{children}</h5>
),
h6: ({ children }) => (
<h6 className="mb-1 text-xs font-semibold">{children}</h6>
),
},
marks: {
link: ({ children, value }) => {
return (
<Link
href={value?.href || "#"}
rel="noreferrer noopener"
target="_blank"
>
{children}
</Link>
);
},
internalLink: ({ children, value }) => {
return <Link href={value?.href || "#"}>{children}</Link>;
},
},
};
return (
<div className={["prose", className].filter(Boolean).join(" ")}>
<PortableText components={components} value={value} />
</div>
);
}