-
-
Notifications
You must be signed in to change notification settings - Fork 698
Expand file tree
/
Copy pathprocessUsers.js
More file actions
147 lines (129 loc) · 4.29 KB
/
processUsers.js
File metadata and controls
147 lines (129 loc) · 4.29 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
const fs = require("fs/promises");
const crypto = require("crypto");
const puppeteer = require("puppeteer");
const translate = require("@iamtraction/google-translate");
const configDir = "./exampleSite/config/_default";
const defaultLang = "en";
const usersFolderPath = "./exampleSite/content/users/";
let cache = {};
function generateDirName(seed, index) {
const hash = crypto.createHash("md5");
hash.update(seed);
return index + "-" + hash.digest("hex");
}
async function convert(text, from, to) {
const options = { from, to };
if (!cache[to]) cache[to] = {};
if (cache[to][text]) return cache[to][text];
const translated_text = await translate(text, options);
cache[to][text] = translated_text.text;
return translated_text.text;
}
async function translateFrontMatterTags(block, targetLang, tags) {
const array = block.split("\n");
let translatedBlock = "";
for (const line of array) {
let newElement = line;
if (line.indexOf(":") > -1) {
const elements = line.split(":");
if (elements[0].indexOf("tags") != -1) {
const translatedTags = [];
for (const tag of tags) {
const tempTag = await convert(tag, defaultLang, targetLang);
translatedTags.push(tempTag);
}
const trasnlatedTagsString = translatedTags.join(", ");
newElement = elements[0] + ": [" + trasnlatedTagsString + "]";
}
}
translatedBlock += newElement + "\n";
}
return translatedBlock;
}
(async () => {
const targetLangs = [];
const configFiles = await fs.readdir(configDir);
for (const file of configFiles) {
if (file.indexOf("languages.") > -1) {
const lang = file.split(".")[1];
if (lang !== defaultLang) {
targetLangs.push(lang);
}
}
}
const indexFiles = await fs.readdir(usersFolderPath);
for (const lang of targetLangs) {
const targetFile = `_index.${lang}.md`;
if (!indexFiles.includes(targetFile)) {
await fs.copyFile(
usersFolderPath + "_index.md",
usersFolderPath + targetFile
);
}
}
const rawdata = await fs.readFile(usersFolderPath + "users.json", "utf8");
const users = JSON.parse(rawdata);
const userDict = {};
var index = 0;
for (const user of users) {
userDict[generateDirName(user.url, index)] = true;
index++;
}
const files = await fs.readdir(usersFolderPath);
for (const file of files) {
const stats = await fs.stat(usersFolderPath + file);
if (file !== "users.json" && file.indexOf("_index.") === -1) {
if (stats.isDirectory()) {
if (!userDict[file]) {
console.log("deleting: ", file);
await fs.rm(usersFolderPath + file, { recursive: true, force: true });
}
} else {
console.log("deleting: ", file);
await fs.unlink(usersFolderPath + file);
}
}
}
const browser = await puppeteer.launch({
defaultViewport: {
width: 1280,
height: 800,
},
});
const page = await browser.newPage();
for (let i = 0; i < users.length; i++) {
const user = users[i];
const userMDFile =
"---\n" +
` title: "${user.title}"\n` +
` tags: [${user.tags}]\n` +
` externalUrl: "${user.url}"\n` +
` weight: ${i + 1}\n` +
" showDate: false\n" +
" showAuthor: false\n" +
" showReadingTime: false\n" +
" showEdit: false\n" +
" showLikes: false\n" +
" showViews: false\n" +
" layoutBackgroundHeaderSpace: false\n" +
" \r---\n";
const dir = usersFolderPath + generateDirName(user.url, i);
try {
await fs.access(dir);
} catch {
await fs.mkdir(dir);
console.log(i, user.title, dir);
await fs.writeFile(dir + "/index.md", userMDFile);
for (var lang of targetLangs) {
const langfilename = lang
if (lang == "pt-br" || lang == "pt-pt")
lang = "pt"
const content = await translateFrontMatterTags(userMDFile, lang, user.tags);
await fs.writeFile(dir + `/index.${langfilename}.md`, content);
}
await page.goto(user.url);
await page.screenshot({ path: dir + "/feature.webp", type: "webp", quality: 50 });
}
}
await browser.close();
})();