forked from reactiflux/reactiflux.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[md-page].tsx
95 lines (90 loc) · 2.45 KB
/
[md-page].tsx
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
import React from "react";
import { FocusBoundary, Layout, Link } from "@components";
import { getAnchor } from "@utils/anchor";
import {
loadAllMd,
loadMdBySlug,
MdPage,
processMd,
} from "@helpers/retrieveMdPages";
import { pick } from "@helpers/object";
export default function MarkdownPage({
html,
title,
description,
headings,
sidebar,
}: Awaited<ReturnType<typeof getStaticProps>>["props"]) {
return (
<Layout
title={title}
sidebar={sidebar}
as={undefined}
description={description}
>
{(setSidebar: any) => (
<>
<h1>{title}</h1>
{sidebar ? (
<FocusBoundary
onChange={setSidebar}
onEnter={undefined}
onExit={undefined}
>
<nav>
<ol>
{headings
.filter((heading) => heading.depth < 3)
.map(({ value, depth }) => (
<li key={value}>
<p style={{ paddingLeft: `${depth - 1}rem` }}>
<Link
href={getAnchor(value)}
onClick={() => {
setSidebar(false);
const heading = document.getElementById(
getAnchor(value).replace("#", ""),
);
heading?.querySelector("a")?.focus();
}}
>
{value}
</Link>
</p>
</li>
))}
</ol>
</nav>
</FocusBoundary>
) : null}
<div
className="markdown"
dangerouslySetInnerHTML={{ __html: html }}
/>
</>
)}
</Layout>
);
}
export const getStaticProps = async ({
params,
}: {
params: { ["md-page"]: string };
}) => {
const doc = await loadMdBySlug<MdPage>("src/md-pages", params["md-page"]);
const { html, headings } = processMd(doc.content);
return {
props: {
...pick(["title", "sidebar", "description"], doc),
headings,
html,
},
};
};
export const getStaticPaths = async () => {
const pages = await loadAllMd<MdPage>("src/md-pages");
return {
paths: pages.map(({ slug }) => ({ params: { ["md-page"]: slug } })),
fallback: false,
};
};