返回 marp
Heading.tsx
根目录 / website / components / markdown / Heading.tsx
1 import { createContext, useContext } from 'react'
2
3 // eslint-disable-next-line @typescript-eslint/ban-types
4 type HOCProps<P extends {} = Record<string, any>> = React.PropsWithChildren<P>
5
6 const anchorLinkContext = createContext(true)
7
8 const Heading: React.FC<HOCProps<{ level: number; id?: string }>> = ({
9 children,
10 level,
11 id,
12 ...rest
13 }) => {
14 const anchorLink = useContext(anchorLinkContext)
15 const HeadingTag: any = 'h' + level
16
17 return (
18 <HeadingTag id={id} {...rest}>
19 {id && anchorLink && (
20 <a
21 aria-hidden
22 className="anchor-link"
23 href={`#${id}`}
24 tabIndex={-1}
25 ></a>
26 )}
27 {children}
28 </HeadingTag>
29 )
30 }
31
32 export const H1: React.FC<HOCProps> = ({ children, ...rest }) => (
33 <Heading level={1} {...rest}>
34 <span>
35 {children}
36 <style jsx>{`
37 & {
38 box-shadow: inset 0 -0.2em theme('colors.marp.light');
39 }
40 `}</style>
41 </span>
42 </Heading>
43 )
44
45 export const H2: React.FC<HOCProps> = ({ children, ...rest }) => (
46 <Heading level={2} {...rest}>
47 <span className="headingLv2">
48 <span className="content">{children}</span>
49 <span className="divider"></span>
50 </span>
51 <style jsx>{`
52 .headingLv2 {
53 @apply flex items-center;
54 }
55
56 .content {
57 @apply flex-initial;
58 }
59
60 .divider {
61 @apply ml-6 h-0 flex-1 border-t border-gray-400;
62 }
63 `}</style>
64 </Heading>
65 )
66
67 // export const H2: React.FC = (props) => <Heading level={2} {...props} />
68 export const H3: React.FC<HOCProps> = (props) => (
69 <Heading level={3} {...props} />
70 )
71 export const H4: React.FC<HOCProps> = (props) => (
72 <Heading level={4} {...props} />
73 )
74 export const H5: React.FC<HOCProps> = (props) => (
75 <Heading level={5} {...props} />
76 )
77 export const H6: React.FC<HOCProps> = (props) => (
78 <Heading level={6} {...props} />
79 )
80
81 export const AnchorLinkProvider = anchorLinkContext.Provider
82
82 lines Plain Text