forked from denoland/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
198 lines (170 loc) · 5.01 KB
/
Copy pathcommon.ts
File metadata and controls
198 lines (170 loc) · 5.01 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type { HrefResolver, ShortPath } from "@deno/doc";
import { dirname, join } from "@std/path";
import markdownit from "markdown-it";
import Prism from "../prism.ts";
import admonitionPlugin from "../markdown-it/admonition.ts";
import codeblockCopyPlugin from "../markdown-it/codeblock-copy.ts";
const titleOnlyAllowedTypes = new Set([
"inline",
"paragraph_open",
"paragraph_close",
"heading_open",
"heading_close",
"text",
"code_inline",
"html_inline",
"em_open",
"em_close",
"strong_open",
"strong_close",
"s_open",
"s_close",
"sup_open",
"sup_close",
"link_open",
"link_close",
"math_inline",
"softbreak",
"underline_open",
"underline_close",
]);
function walkTitleTokens(tokens) {
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (titleOnlyAllowedTypes.has(token.type)) {
if (token.children) {
walkTitleTokens(token.children);
}
} else {
tokens.splice(i, 1);
}
}
}
function titleOnlyPlugin(md) {
md.core.ruler.push("titleOnly", function titleOnly(state) {
walkTitleTokens(state.tokens);
const paragraphEnd = state.tokens.findIndex((token) =>
token.type === "paragraph_close"
);
if (paragraphEnd !== -1) {
state.tokens.splice(paragraphEnd);
}
if (
state.tokens.length === 1 && state.tokens[0].type === "paragraph_open"
) {
state.tokens = [];
}
});
}
function createAnchorizePlugin(
anchorizer: (content: string, depthLevel: number) => string,
) {
return function anchorizePlugin(md) {
md.core.ruler.push("anchorize", function anchorize(state) {
const tokens = state.tokens;
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (token.type === "heading_open") {
const level = parseInt(token.tag.substring(1));
const nextToken = tokens[i + 1];
if (nextToken && nextToken.type === "inline") {
const content = nextToken.children
.filter((t) => t.type === "text" || t.type === "code_inline")
.map((t) => t.content)
.join("");
const id = anchorizer(content, level);
token.attrSet("id", id);
}
}
}
});
};
}
function createRenderer(
anchorizer: (content: string, depthLevel: number) => string,
) {
return markdownit({
html: true,
linkify: true,
langPrefix: "highlight notranslate language-",
highlight(content: string, lang: string) {
if (Prism.languages[lang]) {
return Prism.highlight(content, Prism.languages[lang], lang);
} else {
return "";
}
},
})
.disable("code")
.use(admonitionPlugin)
.use(codeblockCopyPlugin)
.use(createAnchorizePlugin(anchorizer));
}
export function renderMarkdown(
md: string,
titleOnly: boolean,
_filePath: ShortPath | undefined,
anchorizer: (content: string, depthLevel: number) => string,
): string | undefined {
const renderer = createRenderer(anchorizer);
if (titleOnly) {
const titleOnlyRenderer = renderer.use(titleOnlyPlugin);
const parsed = titleOnlyRenderer.parse(md, {});
if (parsed.length === 0) {
return undefined;
}
return `<div data-color-mode="dark" data-light-theme="light" data-dark-theme="dark" class="markdown-body markdown-summary">${
titleOnlyRenderer.renderer.render(parsed, titleOnlyRenderer.options, {})
}</div>`;
} else {
return `<div data-color-mode="dark" data-light-theme="light" data-dark-theme="dark" class="markdown-body">${
renderer.render(md)
}</div>`;
}
}
function strip(tokens) {
let out = "";
for (const token of tokens) {
if (token.type === "text" || token.type === "code_inline") {
out += token.content;
} else if (token.type === "fence") {
out += token.content + "\n";
} else if (token.type === "softbreak") {
out += "\n";
} else if (
token.type === "heading_close" || token.type === "paragraph_close"
) {
out += "\n\n";
} else if (token.children && token.children.length) {
out += strip(token.children);
}
}
return out;
}
export function stripMarkdown(md: string): string {
const renderer = createRenderer(() => "");
const tokens = renderer.parse(md, {});
return strip(tokens);
}
export const hrefResolver: HrefResolver = {
resolvePath(_current, _target, defaultResolve) {
let path = defaultResolve();
if (path.endsWith("index.html")) {
path = path.slice(0, -"index.html".length);
} else if (path.endsWith(".html")) {
path = path.slice(0, -".html".length);
}
return path;
},
};
export async function writeFiles(root: string, files: Record<string, string>) {
await Deno.remove(root, { recursive: true });
await Promise.all(
Object.entries(files).map(async ([path, content]) => {
const joined = join(root, path);
await Deno.mkdir(dirname(joined), { recursive: true });
await Deno.writeTextFile(joined, content);
}),
);
console.log(`Written ${Object.keys(files).length} files`);
}