forked from denoland/dotland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdown.tsx
134 lines (115 loc) · 3.23 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
132
133
134
/* Copyright 2020 the Deno authors. All rights reserved. MIT license. */
import React, { useEffect } from "react";
import ReactMarkdown from "react-markdown";
import CodeBlock from "./CodeBlock";
import InlineCode from "./InlineCode";
import { useRouter } from "next/router";
interface HeadingRendererProps {
source: string;
children?: any;
level?: any;
}
function flatten(text: any, child: any): any {
return typeof child === "string"
? text + child
: React.Children.toArray(child.props.children).reduce(flatten, text);
}
function slugify(text: string): string {
text = text.toLowerCase();
text = text.split(" ").join("-");
text = text.split(/\t/).join("--");
text = text.split(/[|$&`~=\\/@+*!?({[\]})<>=.,;:'"^]/).join("");
text = text
.split(/[。?!,、;:“”【】()〔〕[]﹃﹄“ ”‘’﹁﹂—…-~《》〈〉「」]/)
.join("");
return text;
}
function HeadingRenderer(props: HeadingRendererProps) {
const children = React.Children.toArray(props.children);
const text = children.reduce(flatten, "");
const id = slugify(text);
return React.createElement(
"h" + props.level,
{ id },
<a href={"#" + id} className="hover:underline">
{props.children}
</a>
);
}
interface LinkRendererProps {
children?: any;
href?: string;
}
function LinkRenderer(props: LinkRendererProps) {
const { asPath } = useRouter();
const currentPath = new URL(asPath, "https://deno.land").pathname;
let href: string | undefined = undefined;
if (
props.href &&
(props.href.startsWith("./") || props.href.startsWith("../")) &&
currentPath.startsWith("/manual")
) {
href = props.href.replace(/\.md$/, "");
} else {
href = props.href;
}
// TODO(lucacasonato): Use next.js Link
return (
<a href={href} className="link">
{props.children}
</a>
);
}
function CodeRenderer(props: any) {
return <CodeBlock {...{ ...props, code: props.value, value: undefined }} />;
}
function ImageRenderer(props: { src: string; canonicalURL: string }) {
let src = props.src;
if (src?.startsWith("./") || src?.startsWith("../")) {
const url = new URL(props.canonicalURL);
const parts = url.pathname.split("/");
parts.pop();
url.pathname = parts.join("/") + "/" + src;
src = url.href;
}
return (
<a href={src}>
<img src={src} className="max-w-full inline-block" />
</a>
);
}
const renderers = (canonicalURL: string) => ({
inlineCode: InlineCode,
code: CodeRenderer,
heading: HeadingRenderer,
link: LinkRenderer,
image: function ImageRendererWrapper(props: any) {
return <ImageRenderer {...props} canonicalURL={canonicalURL} />;
},
});
interface MarkdownProps {
source: string;
canonicalURL: string;
}
function Markdown(props: MarkdownProps) {
useEffect(() => {
let { hash } = location;
hash = hash && hash.substring(1);
if (!hash) return;
const el = document.getElementById(hash);
if (!el) return;
setTimeout(() => el.scrollIntoView(), 0);
}, []);
if (!props.source) {
return null;
}
return (
<ReactMarkdown
source={props.source}
renderers={renderers(props.canonicalURL)}
skipHtml={true}
className="markdown"
/>
);
}
export default Markdown;