-
Notifications
You must be signed in to change notification settings - Fork 0
/
website.ts
290 lines (251 loc) · 11.4 KB
/
website.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { DOMParser } from "./deps/deno_dom.ts";
import { anchorPlugin } from "./deps/markdown-it/anchor.ts";
import { MarkdownIt } from "./deps/markdown-it/mod.ts";
import { slugifyPlugin } from "./deps/markdown-it/slugify.ts";
import { basename, dirname, extname, join, relative, resolve } from "./deps/std/path.ts";
import { blue, dim, magenta } from "./deps/std/fmt.ts";
import { checkExternalUrl, isValidAnchor, transformURL } from "./fetch.ts";
import { Issue, MissingAnchorIssue } from "./types.ts";
import { getAnchors, parseLink, parseMarkdownContent } from "./utilities.ts";
import { findGroupedLinksIssues, GroupedLinksResolved, groupLinks, resolveGroupedLinks } from "./group_links.ts";
import { IGNORED_DIRECTORIES } from "./constants.ts";
const domParser = new DOMParser();
// markdown-it configured to generate similar results to the content
// generated by Vitepress' underlying markdown-it instance.
const mdit = MarkdownIt({
html: true,
linkify: true,
}).use(anchorPlugin, { slugify: slugifyPlugin });
mdit.linkify.set({ fuzzyLink: false });
export async function readMarkdownFiles(
rootDirectory: string,
options: {
isCleanUrl: boolean;
indexFile: string;
allowHtmlExtension: boolean;
includeRefDirectory: boolean;
},
) {
const issues: Record<string, Issue[]> = {};
const allAnchors: Record<string, Set<string>> = {};
const usedAnchors: Record<string, Record<string, Set<[anchor: string, reference: string]>>> = {};
// Checker tries to avoid fetching the same link again. When ignoring the
// fetching after the first time, those files with the reference won't
// contain the issues of the link, if there's any. So, we store and push
// those issues to the issues of the file before completely ignoring it.
const externalLinkIssues: Record<string, Issue[]> = {};
const resolvedGroupedLinks: GroupedLinksResolved = {
githubRenderableFiles: {},
};
function isDirectoryIgnored(directory: string): boolean {
// if (directory == "ref" && options.includeRefDirectory) return false;
return IGNORED_DIRECTORIES.includes(directory);
}
async function readDirectoryFiles(directory: string) {
for await (const entry of Deno.readDir(directory)) {
if (!entry.isFile && !entry.isDirectory) continue;
const filepath = join(directory, entry.name);
if (entry.isDirectory) {
if (isDirectoryIgnored(entry.name)) continue;
await readDirectoryFiles(filepath);
continue;
}
if (extname(entry.name) !== ".md") continue;
console.log(dim(`${magenta("reading")} ${filepath}`));
const parsed = await parseMarkdownFile(filepath);
if (parsed.issues.length > 0) {
issues[filepath] ??= [];
issues[filepath].push(...parsed.issues);
}
// --- Anchors ---
allAnchors[filepath] = parsed.anchors.all;
if (parsed.anchors.used.size > 0) {
usedAnchors[filepath] ??= {};
usedAnchors[filepath][filepath] ??= new Set();
}
for (const anchor of parsed.anchors.used) {
usedAnchors[filepath][filepath].add([anchor, "#" + anchor]);
}
// if this file resides under the "ref" section and if /ref reporting is not enabled, then continue.
if ((basename(directory) === "ref" || basename(dirname(directory)) === "ref") && !options.includeRefDirectory) {
continue; // no need for checking the status of the links inside "ref" files.
}
// --- Relative Links (Local files) ---
for (const localLink of parsed.links.local) {
const linkedFile = await checkRelativeLink(localLink, {
root: rootDirectory,
current: directory,
}, {
indexFile: options.indexFile,
isCleanUrl: options.isCleanUrl,
allowHtmlExtension: options.allowHtmlExtension,
});
if (linkedFile.issues.length > 0) {
issues[filepath] ??= [];
issues[filepath].push(...linkedFile.issues);
}
if (linkedFile.anchor != null) {
if (linkedFile.anchor === "") {
issues[filepath] ??= [];
issues[filepath].push({ type: "empty_anchor", reference: localLink });
continue;
}
usedAnchors[linkedFile.path] ??= {};
usedAnchors[linkedFile.path][filepath] ??= new Set();
usedAnchors[linkedFile.path][filepath].add([linkedFile.anchor, localLink]);
}
}
const groupedLinks = groupLinks(parsed.links.external);
await resolveGroupedLinks(groupedLinks, resolvedGroupedLinks, { domParser });
const groupedLinksIssues = findGroupedLinksIssues(groupedLinks, resolvedGroupedLinks);
if (groupedLinksIssues.length > 0) {
issues[filepath] ??= [];
issues[filepath].push(...groupedLinksIssues);
}
// --- External Links ---
for (const externalLink of groupedLinks.other) {
const { root, anchor } = parseLink(externalLink);
if (externalLinkIssues[root] != null && externalLinkIssues[root].length > 0) {
issues[filepath] ??= [];
issues[filepath].push(...externalLinkIssues[root]);
}
// Force to use new API references
const url = new URL(root);
if (url.host === "deno.land" && /\/x\/grammy[a-z0-9_]*@?\/.+/.test(url.pathname)) {
issues[filepath] ??= [];
issues[filepath].push({
type: "local_alt_available",
reference: externalLink,
reason: "Replace the remote API reference link with the native API reference.",
});
}
if (usedAnchors[root] != null) {
usedAnchors[root][filepath] ??= new Set();
if (anchor != null) {
usedAnchors[root][filepath].add([anchor, externalLink]);
}
continue;
}
usedAnchors[root] = {
[filepath]: new Set(anchor != null ? [[anchor, externalLink]] : []),
};
console.log(blue("fetch"), decodeURIComponent(transformURL(root)));
const checkedExternalLink = await checkExternalUrl(root, { domParser });
if (checkedExternalLink.issues.length > 0) {
externalLinkIssues[root] = checkedExternalLink.issues;
issues[filepath] ??= [];
issues[filepath].push(...checkedExternalLink.issues);
}
if (checkedExternalLink.anchors != null) {
allAnchors[root] = checkedExternalLink.anchors;
}
}
}
}
await readDirectoryFiles(rootDirectory);
const missingAnchors = findMissingAnchors(allAnchors, usedAnchors);
for (const filepath in missingAnchors) {
issues[filepath] ??= [];
issues[filepath].push(...missingAnchors[filepath]);
}
return issues;
}
async function parseMarkdownFile(filepath: string) {
const content = await Deno.readTextFile(filepath);
const parsed = parseMarkdownContent(mdit, content);
const document = domParser.parseFromString(parsed.html, "text/html")!;
const allAnchors = getAnchors(document, { includeHref: false });
const issues: Issue[] = [];
const anchors = { all: allAnchors, used: new Set<string>() };
const links = { external: new Set<string>(), local: new Set<string>() };
for (const link of parsed.links) {
if ((/^https?:/).test(link) && URL.canParse(link)) {
links.external.add(link);
} else if (link.startsWith(".") || link.startsWith("/")) {
links.local.add(link);
} else if (link.startsWith("#")) {
anchors.used.add(link.slice(1));
} else if (link.startsWith("mailto:")) {
continue;
} else {
issues.push({ type: "unknown_link_format", reference: link });
}
}
return { anchors, links, issues };
}
async function checkRelativeLink(
localLink: string,
dirInfo: { root: string; current: string },
options: {
indexFile: string;
isCleanUrl: boolean;
allowHtmlExtension: boolean;
},
) {
localLink = decodeURIComponent(localLink);
const issues: Issue[] = [];
const normalizedLocalLink = decodeURIComponent(
localLink.startsWith("/") ? relative(resolve(dirInfo.current), resolve(join(dirInfo.root, "./", localLink))) : localLink,
);
let { root, anchor } = parseLink(normalizedLocalLink);
if (options.isCleanUrl) {
if (extname(root) === ".html") {
issues.push({ type: "disallow_extension", reference: localLink, extension: "html" });
root = root.slice(0, -4) + "md";
} else if (extname(root) === ".md") {
issues.push({ type: "disallow_extension", reference: localLink, extension: "md" });
} else if (root.endsWith("/")) {
root += options.indexFile;
} else if ((!localLink.includes("#") && localLink.endsWith("/")) || localLink.includes("/#")) {
root += "/" + options.indexFile;
} else {
root += ".md";
}
} else {
if (extname(root) === ".html") {
if (!options.allowHtmlExtension) {
issues.push({ type: "wrong_extension", actual: ".html", expected: ".md", reference: localLink });
}
root = root.slice(0, -4) + "md";
}
if (extname(root) !== ".md") {
if (!root.endsWith("/")) root += "/";
root += options.indexFile;
}
}
const path = join(dirInfo.current, root);
try {
if (root.includes("//")) {
throw new Deno.errors.NotFound();
}
await Deno.lstat(path);
return { anchor, issues, path };
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
issues.push({
type: "linked_file_not_found",
filepath: resolve(path),
reference: decodeURIComponent(localLink),
});
return { anchor, issues, path };
}
throw error;
}
}
function findMissingAnchors(
allAnchors: Record<string, Set<string>>,
usedAnchors: Record<string, Record<string, Set<[anchor: string, reference: string]>>>,
): Record<string, MissingAnchorIssue[]> {
const issues: Record<string, MissingAnchorIssue[]> = {};
for (const link in usedAnchors) {
const all = allAnchors[link] ?? new Set<string>();
for (const fileWithAnchorMention in usedAnchors[link]) {
for (const [anchor, reference] of usedAnchors[link][fileWithAnchorMention]) {
if (isValidAnchor(all, link, anchor)) continue;
issues[fileWithAnchorMention] ??= [];
issues[fileWithAnchorMention].push({ type: "missing_anchor", anchor, reference, allAnchors: all });
}
}
}
return issues;
}