-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
Markdown.tsx
131 lines (114 loc) · 2.97 KB
/
Markdown.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
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
/* eslint-disable react/no-danger */
import React, {
HTMLAttributes,
MutableRefObject,
ReactElement,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import cn from "classnames";
import { useRouter } from "next/router";
import styles from "./Markdown.module.scss";
import { markdownToHTML } from "./utils";
function useMarkdownResolver(markdown: MarkdownProps["children"]): string {
/* eslint-disable react-hooks/rules-of-hooks */
// i will never swap between strings and promises
if (typeof markdown === "string") {
return markdown;
}
const [resolved, setResolved] = useState("");
useEffect(() => {
markdown().then((md) => {
if (typeof md === "string") {
setResolved(md);
} else if (typeof md.default === "string") {
setResolved(md.default);
}
});
}, [markdown]);
return resolved;
}
interface DangerHTML {
__html: string;
}
function useHTML(children: MarkdownChildren): DangerHTML {
const markdown = useMarkdownResolver(children);
const html = useMemo(
() => ({
__html: markdownToHTML(markdown),
}),
[markdown]
);
return html;
}
function useCustomMarkdownBehavior({
__html: html,
}: DangerHTML): MutableRefObject<HTMLDivElement | null> {
const ref = useRef<HTMLDivElement | null>(null);
const router = useRouter();
useEffect(() => {
const instance = ref.current;
if (!instance) {
return;
}
const { origin } = window.location;
const links = Array.from(
instance.querySelectorAll<HTMLAnchorElement>("a[href]")
);
links.forEach((link) => {
if (link.href.startsWith(origin)) {
link.onclick = (event) => {
event.preventDefault();
const href = link.href.replace(origin, "");
router.push(href).then((success) => {
if (success) {
const [, hash] = href.split("#");
if (hash) {
const el = document.getElementById(hash);
if (el && typeof el.focus === "function") {
el.focus();
return;
}
}
window.scrollTo(0, 0);
}
});
};
}
});
}, [html, router]);
return ref;
}
export type ResolveMarkdown = () => Promise<string | { default: string }>;
export type MarkdownChildren = string | ResolveMarkdown;
export interface MarkdownProps extends HTMLAttributes<HTMLDivElement> {
children: ResolveMarkdown | string;
disableSinglePMargin?: boolean;
}
export default function Markdown({
className,
children,
disableSinglePMargin,
...props
}: MarkdownProps): ReactElement {
const html = useHTML(children);
const ref = useCustomMarkdownBehavior(html);
return (
<>
<div
{...props}
ref={ref}
className={cn(
styles.container,
{
[styles.marginless]: disableSinglePMargin,
},
className
)}
dangerouslySetInnerHTML={html}
/>
</>
);
}