diff --git a/apps/website/app/[locale]/blog/[slug]/components/Headings.tsx b/apps/website/app/[locale]/blog/[slug]/components/Headings.tsx new file mode 100644 index 0000000..540c247 --- /dev/null +++ b/apps/website/app/[locale]/blog/[slug]/components/Headings.tsx @@ -0,0 +1,100 @@ +"use client"; + +import { useRouter } from "next/navigation"; +import type { DetailedHTMLProps, HTMLAttributes } from "react"; +import slugify from "slugify"; + +type HeadingProps = DetailedHTMLProps< + HTMLAttributes, + HTMLHeadingElement +>; + +function LinkIcon() { + return ( + + + + ); +} + +export function H1({ children, ...props }: HeadingProps) { + const router = useRouter(); + const id = slugify(children?.toString() || "", { + lower: true, + strict: true, + }); + + const handleClick = () => { + router.push(`#${id}`); + }; + + return ( +

+ {children} + +

+ ); +} + +export function H2({ children, ...props }: HeadingProps) { + const router = useRouter(); + const id = slugify(children?.toString() || "", { + lower: true, + strict: true, + }); + + const handleClick = () => { + router.push(`#${id}`); + }; + + return ( +

+ {children} + +

+ ); +} + +export function H3({ children, ...props }: HeadingProps) { + const router = useRouter(); + const id = slugify(children?.toString() || "", { + lower: true, + strict: true, + }); + + const handleClick = () => { + router.push(`#${id}`); + }; + + return ( +

+ {children} + +

+ ); +} diff --git a/apps/website/app/[locale]/blog/[slug]/page.tsx b/apps/website/app/[locale]/blog/[slug]/page.tsx index 5f36113..f0d0d9f 100644 --- a/apps/website/app/[locale]/blog/[slug]/page.tsx +++ b/apps/website/app/[locale]/blog/[slug]/page.tsx @@ -19,6 +19,7 @@ import slugify from "slugify"; import TurndownService from "turndown"; // @ts-ignore import * as turndownPluginGfm from "turndown-plugin-gfm"; +import { H1, H2, H3 } from "./components/Headings"; import { TableOfContents } from "./components/TableOfContents"; import { ZoomableImage } from "./components/ZoomableImage"; @@ -172,55 +173,17 @@ export default async function BlogPostPage({ params }: Props) { }); const components: Partial = { - h1: ({ node, ...props }) => { - const id = slugify(props.children?.toString() || "", { - lower: true, - strict: true, - }); - return ( -

- ); - }, - h2: ({ node, ...props }) => { - const id = slugify(props.children?.toString() || "", { - lower: true, - strict: true, - }); - return ( -

- ); - }, - h3: ({ node, ...props }) => { - const id = slugify(props.children?.toString() || "", { - lower: true, - strict: true, - }); - return ( -

- ); - }, - p: ({ node, children, ...props }) => { - return ( -

- {children} -

- ); - }, + h1: H1, + h2: H2, + h3: H3, + p: ({ node, children, ...props }) => ( +

+ {children} +

+ ), a: ({ node, href, ...props }) => (