-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathcreate-i18n-tree.mjs
92 lines (85 loc) · 2.32 KB
/
create-i18n-tree.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
import { existsSync } from "fs";
import path from "path";
import { COLLECTIONS_ROOT } from "./collections/index.mjs";
import { getFrontMatterData } from "./collections/utils/get-frontmatter.mjs";
/**
* @typedef {Object} Leaf
* @property {"markdown"} type - The type of the child item.
* @property {string} file - The markdown file associated with the child item.
* @property {string} path - The path to the child item.
* @property {string} slug - The slug for the child item.
* @property {string} parent - The parent section of the child item.
* @property {string} title - The title of the child item.
*/
/**
* @typedef {Object} Branch
* @property {"section" | "markdown"} type - The type of reference item.
* @property {string} title - The title of the reference section.
* @property {string[]} pages - List of pages related to the section.
* @property {(Leaf | Branch)[]} children - List of child items under the section.
*/
/**
* @typedef {Object} Tree
* @property {Branch[]} reference - List of reference items.
* @property {Branch[]} learn - List of learn items.
*/
/**
*
* @param {Branch["children"]} children
*/
function traverseTree(children) {
for (const child of children) {
if (child.type === "section") {
return traverseTree(child.children);
} else {
/**
* check if exist i18n
*/
return child;
}
}
}
/**
*
* @param {Tree["reference" | "learn"]} entryList
* @param {string} locale
* @param {string | undefined} project
*/
export async function createI18nTree(entryList, locale, project = "") {
const entries = [];
for (const entry of entryList) {
if (entry.type === "section") {
const nested = await createI18nTree(entry.children, locale, project);
entries.push({
...entry,
children: nested.filter(Boolean),
});
} else {
const i18nEntryPath = path.join(
process.cwd(),
COLLECTIONS_ROOT,
project,
locale,
entry?.path?.endsWith("/") ? entry?.path + "index" : entry.path
);
if (existsSync(i18nEntryPath + ".mdx")) {
const { title, isDeprecated } = await getFrontMatterData(
i18nEntryPath + ".mdx"
);
entries.push({
...entry,
isDeprecated,
isTranslated: true,
title,
path: `/${locale}${entry.path}`,
});
} else {
entries.push({
...entry,
isTranslated: false,
});
}
}
}
return entries;
}