-
-
Notifications
You must be signed in to change notification settings - Fork 440
/
genArticle.js
63 lines (53 loc) · 1.99 KB
/
genArticle.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
const fs = require('fs');
const translate = require('@iamtraction/google-translate');
const defaultLang = "en";
const targetLang = process.argv[2] || "en";
const filePath = process.argv[3];
const targetLangIso = targetLang == "pt" ? "pt-pt" : targetLang;
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
async function convert(text, from, to) {
var options = {
from: from,
to: to
};
var translated_text = await translate(text, options);
return translated_text.text;
}
console.log(filePath);
console.log(targetFilePath);
async function processFrontMatter(block) {
var array = block.split("\n");
var translatedBlock = "";
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf(":") > -1) {
var elements = array[i].split(":");
var newElement = "";
if (elements[0] == "title" ||
elements[0] == "description" ||
elements[0] == "summary" ||
elements[0] == "description" ||
elements[0] == "categories" ||
elements[0] == "tags") {
var translatedElement = elements[1] ? await convert(elements[1], defaultLang, targetLang) : elements[1];
newElement = elements[0] + ": " + translatedElement;
} else {
newElement = array[i];
}
} else {
newElement = array[i];
}
translatedBlock += newElement + "\n";
}
return translatedBlock;
}
async function main() {
const fileContent = fs.readFileSync(filePath, 'utf8');
var array = fileContent.split("---\n")
var frontMatter = array[1];
var content = array[2];
var translatedFrontMatter = await processFrontMatter(frontMatter);
var translatedContent = await convert(content, defaultLang, targetLang);
var newFileContent = "---\n" + translatedFrontMatter + "---\n" + translatedContent;
fs.writeFileSync(targetFilePath, newFileContent, 'utf8');
}
main();