-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsync-heading-ids.mjs
111 lines (92 loc) · 3.26 KB
/
sync-heading-ids.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
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
import fs from 'node:fs/promises';
import path from 'node:path';
import arg from 'arg';
import picocolors from 'picocolors';
import { docsBaseDir, i18nBaseDir, translateDir, walk, exit } from './translate.shared.mjs';
const args = arg({
'--locale': String,
});
/**
* The target locale to translate the files to. Note that the locale must exist in the `i18n`
* directory. It's recommended to run the Docusaurus write translation command before running this
* script.
*
* @type {string}
*/
const locale = args['--locale'];
if (!locale) {
exit('No locale specified. Use --locale to specify the target locale.');
}
const headingRegex = /^(#+ .*)$/gm;
const headingLevelRegex = /^(#+) /;
const headingIdRegex = /(\\?{#[^}]+})/;
const files = await walk(docsBaseDir);
for (const file of files) {
const i18nFile = file.replace(docsBaseDir, path.join(i18nBaseDir, locale, translateDir));
console.log(picocolors.blue('Processing'), file, '->', i18nFile);
// eslint-disable-next-line no-await-in-loop
const [content, i18nContent] = await Promise.all([
fs.readFile(file, 'utf8'),
fs.readFile(i18nFile, 'utf8'),
]);
// Find all headings in the file.
const headings = content.match(headingRegex);
const i18nHeadings = i18nContent.match(headingRegex);
const length = headings?.length ?? 0;
const i18nLength = i18nHeadings?.length ?? 0;
if (length !== i18nLength) {
console.error(
picocolors.red(
`Mismatched heading count in ${file}.\n\n -> Original: ${length} headings in ${file}\n -> Translated: ${
i18nLength
} headings in ${i18nFile}`
)
);
throw new Error('Mismatched heading count');
}
if (!headings) {
console.log(picocolors.dim(' 🏃 No headings found, skipping'));
continue;
}
// Ensure heading levels are the same.
for (const [i, heading] of headings.entries()) {
const i18nHeading = i18nHeadings[i];
const level = heading.match(headingLevelRegex)[1].length;
const i18nLevel = i18nHeading.match(headingLevelRegex)[1].length;
if (level !== i18nLevel) {
console.error(
picocolors.red(
`Mismatched heading level in ${file}. Original: ${level}, Translated: ${i18nLevel}`
)
);
throw new Error('Mismatched heading level');
}
}
// Append heading IDs to i18n headings if they don't exist.
// eslint-disable-next-line @silverhand/fp/no-let
let index = -1;
// eslint-disable-next-line no-await-in-loop
await fs.writeFile(
i18nFile,
i18nContent.replaceAll(headingRegex, (match) => {
// eslint-disable-next-line @silverhand/fp/no-mutation
index += 1;
const idMatch = match.match(headingIdRegex);
if (idMatch) {
console.log(picocolors.dim(` 🙅 ID exists, skipping ${match}`));
return match;
}
const headingId = headings[index].match(headingIdRegex);
if (!headingId) {
console.log(picocolors.dim(` 🏃 No ID found in the original content, skipping ${match}`));
return match;
}
const newHeading = `${match} ${headingId[0]}`;
console.log(picocolors.dim(` ✅ Appended ID ${newHeading}`));
return newHeading;
})
);
}
console.log(
picocolors.green(`✓ Completed processing heading IDs for ${locale} with ${files.length} files.`)
);