1
1
import type { ComponentProps } from "react" ;
2
+ import React from "react" ;
2
3
import NavLink from "next/link" ;
3
4
import { Code } from "bright" ;
4
5
import { CodeTabs } from "@/components/code-highlighter/code-tabs" ;
@@ -10,6 +11,7 @@ import {
10
11
import { ComponentSource } from "@/components/component-source" ;
11
12
import { DocsList , type DocsListProps } from "@/components/docs/docs-list" ;
12
13
import { IconsExplorer } from "@/components/icons-explorer" ;
14
+ import { slugify } from "@/utils/string" ;
13
15
import { cn } from "@/lib/utils/classes" ;
14
16
15
17
export const Link = ( {
@@ -42,52 +44,45 @@ export const Link = ({
42
44
) ;
43
45
} ;
44
46
47
+ function createHeading ( level : number , className ?: string ) {
48
+ const Component = ( {
49
+ children,
50
+ ...props
51
+ } : React . HTMLAttributes < HTMLHeadingElement > ) => {
52
+ const slug = slugify ( children as string ) ;
53
+ return React . createElement (
54
+ `h${ level } ` ,
55
+ { id : slug , className, ...props } ,
56
+ [
57
+ React . createElement ( "a" , {
58
+ href : `#${ slug } ` ,
59
+ key : `link-${ slug } ` ,
60
+ } ) ,
61
+ ] ,
62
+ children
63
+ ) ;
64
+ } ;
65
+
66
+ Component . displayName = `Heading${ level } ` ;
67
+ return Component ;
68
+ }
69
+
45
70
export const components = {
46
- h1 : ( { className, ...props } : React . HTMLAttributes < HTMLHeadingElement > ) => (
47
- < h1
48
- className = { cn ( "font-heading mt-2 scroll-m-20 text-4xl font-bold" , className ) }
49
- { ...props }
50
- />
51
- ) ,
52
- h2 : ( { className, ...props } : React . HTMLAttributes < HTMLHeadingElement > ) => (
53
- < h2
54
- className = { cn (
55
- "font-heading mt-12 scroll-m-20 border-b pb-2 text-xl font-semibold tracking-tight first:mt-0" ,
56
- className
57
- ) }
58
- { ...props }
59
- />
60
- ) ,
61
- h3 : ( { className, ...props } : React . HTMLAttributes < HTMLHeadingElement > ) => (
62
- < h3
63
- className = { cn (
64
- "font-heading mt-8 scroll-m-20 text-xl font-semibold tracking-tight" ,
65
- className
66
- ) }
67
- { ...props }
68
- />
69
- ) ,
70
- h4 : ( { className, ...props } : React . HTMLAttributes < HTMLHeadingElement > ) => (
71
- < h4
72
- className = { cn (
73
- "font-heading mt-8 scroll-m-20 text-lg font-semibold tracking-tight" ,
74
- className
75
- ) }
76
- { ...props }
77
- />
71
+ h1 : createHeading ( 1 , "font-heading mt-2 scroll-m-20 text-4xl font-bold" ) ,
72
+ h2 : createHeading (
73
+ 2 ,
74
+ "font-heading mt-12 scroll-m-20 border-b pb-2 text-xl font-semibold tracking-tight first:mt-0"
78
75
) ,
79
- h5 : ( { className, ...props } : React . HTMLAttributes < HTMLHeadingElement > ) => (
80
- < h5
81
- className = { cn ( "mt-8 scroll-m-20 text-lg font-semibold tracking-tight" , className ) }
82
- { ...props }
83
- />
76
+ h3 : createHeading (
77
+ 3 ,
78
+ "font-heading mt-8 scroll-m-20 text-xl font-semibold tracking-tight"
84
79
) ,
85
- h6 : ( { className, ...props } : React . HTMLAttributes < HTMLHeadingElement > ) => (
86
- < h6
87
- className = { cn ( "mt-8 scroll-m-20 text-base font-semibold tracking-tight" , className ) }
88
- { ...props }
89
- />
80
+ h4 : createHeading (
81
+ 4 ,
82
+ "font-heading mt-8 scroll-m-20 text-lg font-semibold tracking-tight"
90
83
) ,
84
+ h5 : createHeading ( 5 , "mt-8 scroll-m-20 text-lg font-semibold tracking-tight" ) ,
85
+ h6 : createHeading ( 6 , "mt-8 scroll-m-20 text-base font-semibold tracking-tight" ) ,
91
86
a : Link ,
92
87
p : ( { className, ...props } : ComponentProps < "p" > ) => (
93
88
< p className = { cn ( "leading-7 [&:not(:first-child)]:mt-4" , className ) } { ...props } />
0 commit comments