forked from casper-network/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-md-unused.js
200 lines (180 loc) · 6.95 KB
/
check-md-unused.js
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
//----------------------------------
// Imports
//----------------------------------
const glob = require("glob");
const path = require("path");
const style = require("ansi-styles");
const fs = require("fs");
//----------------------------------
// Constants
//----------------------------------
const excludedFolders = ["./source/blog/**/*", "./source/i18n/**/*"];
const excludedFiles = ["./source/docs/casper/concepts/about.md"];
//----------------------------------
// Private Vars
//----------------------------------
const slugMapping = {};
const fileMapping = {};
/**
* Unused Markdown Checker
* @author Sandro Ducceschi [Ditoy GmbH, Switzerland]
*/
class UnusedMarkdownChecker {
static async execute() {
console.log("Attempting to determine unused markdown files...");
console.log(`\n${style.cyanBright.open}Files are assumed unused based on the following criteria:`);
console.log(" - No other markdown file points to it");
console.log(` - The file is not present in the sidebar${style.cyanBright.close}`);
let filepaths = glob.sync("./source/**/@(*.md|*.mdx)", {
ignore: excludedFolders,
});
filepaths.forEach((filepath) => {
fileMapping[filepath] = 0;
const slug = this.retrieveInternalMapping(filepath);
if (slug && !slugMapping[slug]) {
slugMapping[slug] = path.dirname(filepath);
}
});
filepaths.forEach((filepath) => {
const urls = this.retrieveInternalUrls(filepath);
if (urls.length > 0) {
this.updateOccurrences(filepath, urls);
}
});
const sidebarMapping = require("./config/sidebar.config");
const keys = Object.keys(sidebarMapping);
const sidebarUrls = [];
keys.forEach((key) => {
const map = sidebarMapping[key];
map.forEach((entry) => {
if (entry.constructor === String) {
sidebarUrls.push(entry);
} else {
const items = entry.items;
items.forEach((item) => {
if (item.constructor === String) {
sidebarUrls.push(item);
} else {
sidebarUrls.push(...item.items);
}
});
}
});
});
sidebarUrls.forEach((url) => {
const slug = url.split("/")[0];
const path = slugMapping[`/${slug}`];
if (path) {
url = path + url.replace(slug, "");
}
if (fs.existsSync(url + ".md")) {
fileMapping[url + ".md"] += 1;
} else if (fs.existsSync(url + ".mdx")) {
fileMapping[url + ".mdx"] += 1;
} else if (fs.existsSync(url)) {
fileMapping[url] += 1;
}
});
let assumedUnused = [];
const paths = Object.keys(fileMapping);
paths.forEach((path) => {
if (fileMapping[path] === 0) {
assumedUnused.push(path);
}
});
assumedUnused = assumedUnused.filter((file) => !excludedFiles.includes(file));
if (assumedUnused.length > 0) {
console.log(
`\nThe following ${style.bold.open + assumedUnused.length + style.bold.close} files are ${style.bold.open}assumed${
style.bold.close
} to be unused:\n`,
);
assumedUnused.forEach((file) => {
console.log("━", file);
});
}
console.log(`\n${style.yellow.open}This test ${style.bold.open}may or may not${style.bold.close} have false-positives.${style.yellow.close}`);
console.log(
`${style.yellow.open}It is ${style.bold.open}adviced to review these findings${style.bold.close}, before deleting any of these files.${style.yellow.close}\n`,
);
process.exit(0);
}
//----------------------------------
// Methods
//----------------------------------
static retrieveInternalUrls(path) {
const urls = [];
const md = fs.readFileSync(path, { encoding: "utf-8" });
const urlMatches = md.match(/\[(.+)]\(((?!https?|mailto)[^ ]+?)( "(.+)")?\)/gm);
if (urlMatches) {
urlMatches.forEach((url) => {
const transformedUrl = url.replace(/\[.*]\(|\)|(#.*)/gm, "");
if (transformedUrl !== "") {
urls.push(transformedUrl);
}
});
}
return urls;
}
static retrieveInternalMapping(path) {
const md = fs.readFileSync(path, { encoding: "utf-8" });
const slugMatches = /^slug:\s+(?<slug>.+)$/gm.exec(md);
const slug = ((slugMatches || {}).groups || {}).slug;
if (slug && slug !== "/") {
return slug;
}
return null;
}
static updateOccurrences(originPath, urls) {
urls.forEach((url) => {
let targetPath;
let testUrl = url;
if (path.extname(url) === "") {
testUrl = `${url}.md`;
}
const extension = path.extname(testUrl);
const isMarkdown = extension === ".md" || extension === ".mdx";
const isAbsolute = testUrl[0] === "/";
if (isAbsolute && !isMarkdown) {
targetPath = `/static${testUrl}`;
} else if (isAbsolute && isMarkdown) {
targetPath = this.urlToDiskpath(testUrl);
} else {
targetPath = "./" + path.join(path.dirname(originPath), `/${testUrl}`);
}
if (fs.existsSync(targetPath) && isMarkdown) {
fileMapping[targetPath] += 1;
} else if (isMarkdown) {
testUrl = url.replace(/\/docs|\.{1,2}/, "");
const retryPath = this.urlToDiskpath(testUrl);
if (path.extname(retryPath) === "") {
if (fs.existsSync(retryPath + ".md")) {
fileMapping[retryPath + ".md"] += 1;
} else if (fs.existsSync(retryPath + ".mdx")) {
fileMapping[retryPath + ".mdx"] += 1;
}
} else if (fs.existsSync(retryPath)) {
fileMapping[retryPath] += 1;
}
}
});
}
//----------------------------------
// Helper Methods
//----------------------------------
static urlToDiskpath(url) {
if (url.indexOf("/docs") === 0) {
url = url.replace("/docs", "");
}
const slugs = Object.keys(slugMapping);
const slug = slugs.find((slug) => url.indexOf(slug) === 0);
const path = slugMapping[slug];
return path + url.replace(slug, "");
}
}
(async function run() {
await UnusedMarkdownChecker.execute();
})().catch((err) => {
console.error(err);
process.exit(1);
});