forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint-slugcheck.mjs
54 lines (50 loc) · 1.71 KB
/
lint-slugcheck.mjs
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
import glob from 'fast-glob';
import kleur from 'kleur';
/** Makes sure that all translations’ slugs match the English slugs. */
class SlugChecker {
async run() {
const errors = await this.#findMismatchedSlugs();
this.#outputResult(errors);
}
/** Load all Markdown pages and check non-English pages have counterparts in `pages/en/`. */
async #findMismatchedSlugs() {
const enSlugs = new Set();
/** @type {Record<string, string[]>} */
const errorMap = {};
(await glob('./src/content/docs/**/*.{md,mdx}'))
.filter((file) => !file.endsWith('404.mdx'))
.map((file) => {
const [, lang, slug] = file.replace('./src/content/docs/', '').match(/^([^/]+)\/(.+)$/);
if (lang === 'en') enSlugs.add(slug);
return [lang, slug];
})
.forEach(([lang, slug]) => {
if (enSlugs.has(slug)) return;
if (!errorMap[lang]) errorMap[lang] = [];
errorMap[lang].push(slug);
});
return Object.entries(errorMap);
}
/**
* Print the result of the slug check to the console.
* @param {[lang: string, slugs: string[]][]} errors
*/
#outputResult(errors) {
if (errors.length === 0) {
console.log(kleur.green().bold(`\n*** Found no translations with mismatched slugs\n`));
return;
}
const prefix = kleur.gray(` [${kleur.red().bold(' ✖ ')}] `);
let errorCount = 0;
for (const [lang, slugs] of errors) {
errorCount += slugs.length;
const summary = [`\n/${lang}/`, ...slugs.map((slug) => prefix + slug)];
console.error(summary.join('\n'));
}
console.error(kleur.red().bold(`\n*** Found ${errorCount} translations with mismatched slugs`));
console.error(' Rename the files listed above to match English slugs\n');
process.exit(1);
}
}
const checker = new SlugChecker();
checker.run();