-
-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathDoc.tsx
More file actions
148 lines (133 loc) · 4.17 KB
/
Doc.tsx
File metadata and controls
148 lines (133 loc) · 4.17 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import * as React from 'react'
import { FaEdit } from 'react-icons/fa'
import { marked } from 'marked'
import markedAlert from 'marked-alert'
import { gfmHeadingId, getHeadingList } from 'marked-gfm-heading-id'
import { DocTitle } from '~/components/DocTitle'
import { Markdown } from '~/components/Markdown'
import { Toc } from './Toc'
import { twMerge } from 'tailwind-merge'
import { TocMobile } from './TocMobile'
type DocProps = {
title: string
content: string
repo: string
branch: string
filePath: string
shouldRenderToc?: boolean
colorFrom?: string
colorTo?: string
}
export function Doc({
title,
content,
repo,
branch,
filePath,
shouldRenderToc = false,
colorFrom,
colorTo,
}: DocProps) {
const { markup, headings } = React.useMemo(() => {
const markup = marked.use(
{ gfm: true },
gfmHeadingId(),
markedAlert()
)(content) as string
const headings = getHeadingList()
return { markup, headings }
}, [content])
const isTocVisible = shouldRenderToc && headings && headings.length > 1
const markdownContainerRef = React.useRef<HTMLDivElement>(null)
const [activeHeadings, setActiveHeadings] = React.useState<Array<string>>([])
const headingElementRefs = React.useRef<
Record<string, IntersectionObserverEntry>
>({})
React.useEffect(() => {
const callback = (headingsList: Array<IntersectionObserverEntry>) => {
headingElementRefs.current = headingsList.reduce(
(map, headingElement) => {
map[headingElement.target.id] = headingElement
return map
},
headingElementRefs.current
)
const visibleHeadings: Array<IntersectionObserverEntry> = []
Object.keys(headingElementRefs.current).forEach((key) => {
const headingElement = headingElementRefs.current[key]
if (headingElement.isIntersecting) {
visibleHeadings.push(headingElement)
}
})
if (visibleHeadings.length >= 1) {
setActiveHeadings(visibleHeadings.map((h) => h.target.id))
}
}
const observer = new IntersectionObserver(callback, {
rootMargin: '0px',
threshold: 0.2,
})
const headingElements = Array.from(
markdownContainerRef.current?.querySelectorAll(
'h2[id], h3[id], h4[id], h5[id], h6[id]'
) ?? []
)
headingElements.forEach((el) => observer.observe(el))
return () => observer.disconnect()
}, [])
return (
<React.Fragment>
{shouldRenderToc ? <TocMobile headings={headings} /> : null}
<div
className={twMerge(
'w-full flex bg-white/70 dark:bg-black/40 mx-auto rounded-xl max-w-[936px]',
isTocVisible && 'max-w-full',
shouldRenderToc && 'lg:pt-0'
)}
>
<div
className={twMerge(
'flex overflow-auto flex-col w-full p-4 lg:p-6',
isTocVisible && '!pr-0'
)}
>
{title ? <DocTitle>{title}</DocTitle> : null}
<div className="h-4" />
<div className="h-px bg-gray-500 opacity-20" />
<div className="h-4" />
<div
ref={markdownContainerRef}
className={twMerge(
'prose prose-gray prose-sm prose-p:leading-7 dark:prose-invert max-w-none',
isTocVisible && 'pr-4 lg:pr-6',
'styled-markdown-content'
)}
>
<Markdown htmlMarkup={markup} />
</div>
<div className="h-12" />
<div className="w-full h-px bg-gray-500 opacity-30" />
<div className="py-4 opacity-70">
<a
href={`https://github.com/${repo}/tree/${branch}/${filePath}`}
className="flex items-center gap-2"
>
<FaEdit /> Edit on GitHub
</a>
</div>
<div className="h-24" />
</div>
{isTocVisible && (
<div className="border-l border-gray-500/20 max-w-52 w-full hidden 2xl:block transition-all">
<Toc
headings={headings}
activeHeadings={activeHeadings}
colorFrom={colorFrom}
colorTo={colorTo}
/>
</div>
)}
</div>
</React.Fragment>
)
}