-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
render-markdown.ts
56 lines (54 loc) · 1.47 KB
/
render-markdown.ts
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
import { DocItem, FileInfo, ParseOptions } from "./interface.ts";
import {
Content,
fromMarkdown,
render,
TableRow,
toMarkdown,
visit,
} from "./deps.ts";
import { childrenToRoot, getDomain } from "./util.ts";
import _log from "./log.ts";
import {
gfm,
gfmFromMarkdown,
gfmToMarkdown,
remarkEmoji,
remarkGemoji,
} from "./deps.ts";
import { CONTENT_DIR, INDEX_MARKDOWN_PATH } from "./constant.ts";
export default function renderMarkdown(content: string): string {
const domain = getDomain();
const tree = fromMarkdown(content, "utf8", {
// @ts-ignore: remarkInlineLinks is not typed
extensions: [gfm()],
mdastExtensions: [gfmFromMarkdown()],
});
// @ts-ignore: node function
const remarkEmojiPlugin = remarkEmoji();
// @ts-ignore: node function
remarkEmojiPlugin(tree);
const remarkGemojiPlugin = remarkGemoji();
// @ts-ignore: node function
remarkGemojiPlugin(tree);
visit(tree, "link", (node) => {
const { url } = node;
if (
url &&
(url.startsWith("/" + CONTENT_DIR + "/")) &&
url.endsWith(INDEX_MARKDOWN_PATH)
) {
node.url = url.slice(CONTENT_DIR.length + 1, -INDEX_MARKDOWN_PATH.length);
} else if (
url && (url.startsWith("/")) && url.endsWith(INDEX_MARKDOWN_PATH)
) {
node.url = url.slice(0, -INDEX_MARKDOWN_PATH.length);
}
});
const markdownDist = toMarkdown(tree, {
extensions: [gfmToMarkdown()],
});
return render(markdownDist, {
allowIframes: true,
});
}