forked from pingcap/docs-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_ja.js
More file actions
161 lines (143 loc) · 4.32 KB
/
index_ja.js
File metadata and controls
161 lines (143 loc) · 4.32 KB
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
import * as fs from "fs";
import { fromMarkdown } from "mdast-util-from-markdown";
import { toMarkdown } from "mdast-util-to-markdown";
import { frontmatter } from "micromark-extension-frontmatter";
import {
frontmatterFromMarkdown,
frontmatterToMarkdown,
} from "mdast-util-frontmatter";
// import { mdxjs } from "micromark-extension-mdxjs";
// import { mdxFromMarkdown, mdxToMarkdown } from "mdast-util-mdx";
// import { gfmTable } from "micromark-extension-gfm-table";
// import { gfmTableFromMarkdown, gfmTableToMarkdown } from "mdast-util-gfm-table";
import { gfm } from "micromark-extension-gfm";
import { gfmFromMarkdown, gfmToMarkdown } from "mdast-util-gfm";
import { visit } from "unist-util-visit";
// import {
// comment,
// commentFromMarkdown,
// commentToMarkdown,
// } from "remark-comment-o";
import { getMdFileList, writeFileSync, handleAstNode } from "./lib.js";
import { loadVariables, variablesReplace } from "./variables.js";
import { postProcessFileFrontmatterAliases } from "./frontmatterAliases.js";
import { postProcessFileGithubMentions } from "./GithubMentions.js";
import { normalizeStandaloneCustomContentIndentation } from "./customContentIndentation.js";
const pSum = {
sum: 0,
_data: [],
};
const translateSingleMdToJa = async (filePath, outputFilePath) => {
const mdFileContent = fs.readFileSync(filePath);
const mdAst = fromMarkdown(mdFileContent, {
// extensions: [frontmatter(["yaml", "toml"]), gfmTable, gfm()],
extensions: [frontmatter(["yaml", "toml"]), gfm()],
mdastExtensions: [
frontmatterFromMarkdown(["yaml", "toml"]),
// gfmTableFromMarkdown,
gfmFromMarkdown(),
],
});
const treeNodes = [];
visit(mdAst, (node) => {
treeNodes.push(node);
});
// treeNodes.forEach((node) => {
// if (node.type === "paragraph") {
// pSum.sum = pSum.sum + 1;
// const children = node.children;
// children?.forEach((c) => {
// const type = c.type;
// pSum[type] = pSum[type] ? pSum[type] + 1 : 1;
// if (type === "linkReference") pSum._data.push(node);
// });
// }
// });
// treeNodes.forEach((node) => {
// node.type === "html" && console.log(node);
// });
await Promise.all(
treeNodes.map(async (d) => {
await handleAstNode(d);
})
);
const newFile = toMarkdown(mdAst, {
bullet: "-",
extensions: [
frontmatterToMarkdown(["yaml", "toml"]),
// gfmTableToMarkdown(),
gfmToMarkdown(),
],
});
const result = normalizeStandaloneCustomContentIndentation(
newFile.replaceAll(/(#+.+)(\\{)(#.+})/g, `$1{$3`)
);
writeFileSync(outputFilePath, result);
};
const copyable = /{{< copyable\s+(.+)\s+>}}\r?\n/g;
const replaceDeprecatedContent = (path) => {
const mdFileContent = fs.readFileSync(path).toString();
fs.writeFileSync(path, mdFileContent.replace(copyable, ""));
};
// root
// paragraph
// heading
// thematicBreak
// blockquote
// list
// listItem
// table
// tableRow
// tableCell
// html
// code
// yaml
// definition
// footnoteDefinition
// text
// emphasis
// strong
// delete
// inlineCode
// break
// link
// image
// linkReference
// imageReference
// footnote
// footnoteReference
const main = async (dir = "markdowns", outputDir = "output") => {
const srcList = getMdFileList(dir, outputDir);
// console.log(srcList);
// Load variables from variables.json
const variables = loadVariables(dir);
console.log("Loaded variables:", variables);
for (let { filePath, outputFilePath } of srcList) {
console.log(filePath);
variablesReplace(variables, filePath);
replaceDeprecatedContent(filePath);
await translateSingleMdToJa(filePath, outputFilePath);
postProcessFileFrontmatterAliases(outputFilePath, "ja");
postProcessFileGithubMentions(outputFilePath);
// break;
}
// console.log(pSum);
};
// Parse command line arguments
const getInputDir = () => {
const inputIndex = process.argv.indexOf("--input-dir");
if (inputIndex !== -1 && process.argv[inputIndex + 1]) {
return process.argv[inputIndex + 1];
}
return "markdowns";
};
const getOutputDir = () => {
const outputIndex = process.argv.indexOf("--output-dir");
if (outputIndex !== -1 && process.argv[outputIndex + 1]) {
return process.argv[outputIndex + 1];
}
return "output";
};
const dir = getInputDir();
const outputDir = getOutputDir();
main(dir, outputDir);