-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathindex.tsx
More file actions
72 lines (64 loc) · 1.75 KB
/
index.tsx
File metadata and controls
72 lines (64 loc) · 1.75 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
import type { Heading } from '@vcarl/remark-headings';
import { Fragment, useMemo } from 'react';
import type { FC } from 'react';
import type { LinkLike } from '#ui/types';
import styles from './index.module.css';
type MetaBarProps = {
items: Partial<Record<string, React.ReactNode>>;
headings?: {
items: Array<Heading>;
minDepth?: number;
};
as?: LinkLike;
heading: string;
};
const MetaBar: FC<MetaBarProps> = ({
items,
headings,
as: Component = 'a',
heading,
}) => {
// The default depth of headings to display in the table of contents.
const { minDepth = 2, items: headingItems = [] } = headings || {};
const filteredHeadings = useMemo(
() => headingItems.filter(({ depth }) => depth >= minDepth && depth <= 4),
[minDepth, headingItems]
);
return (
<div className={styles.wrapper}>
<dl>
{Object.entries(items)
.filter(([, value]) => !!value)
.map(([key, value]) => (
<Fragment key={key}>
<dt>{key}</dt>
<dd>{value}</dd>
</Fragment>
))}
{filteredHeadings.length > 0 && (
<>
<dt>{heading}</dt>
<dd>
<ol>
{filteredHeadings.map(head => (
<li
key={head.value}
className={
head.depth === 3 ? 'pl-2' : head.depth === 4 ? 'pl-4' : ''
}
>
<Component href={`#${head?.data?.id}`}>
{' '}
{head.value}
</Component>
</li>
))}
</ol>
</dd>
</>
)}
</dl>
</div>
);
};
export default MetaBar;