mirror of
https://gitee.com/butubb8/blowfish.git
synced 2025-12-05 14:17:50 +08:00
fix: improve scripts and js
This commit is contained in:
@@ -7,7 +7,7 @@ const oid_likes =
|
|||||||
pageScriptElement && pageScriptElement.getAttribute("data-oid-likes")
|
pageScriptElement && pageScriptElement.getAttribute("data-oid-likes")
|
||||||
? pageScriptElement.getAttribute("data-oid-likes")
|
? pageScriptElement.getAttribute("data-oid-likes")
|
||||||
: (console.error("data-oid-likes is null"), null);
|
: (console.error("data-oid-likes is null"), null);
|
||||||
const liked_page = false;
|
let liked_page = false;
|
||||||
const id = oid ? oid.replaceAll("/", "-") : oid;
|
const id = oid ? oid.replaceAll("/", "-") : oid;
|
||||||
const id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes;
|
const id_likes = oid_likes ? oid_likes.replaceAll("/", "-") : oid_likes;
|
||||||
|
|
||||||
|
|||||||
@@ -156,7 +156,6 @@ function executeQuery(term) {
|
|||||||
|
|
||||||
if (results.length > 0) {
|
if (results.length > 0) {
|
||||||
results.forEach(function (value, key) {
|
results.forEach(function (value, key) {
|
||||||
console.log(value.item.summary);
|
|
||||||
var html = value.item.summary;
|
var html = value.item.summary;
|
||||||
var div = document.createElement("div");
|
var div = document.createElement("div");
|
||||||
div.innerHTML = html;
|
div.innerHTML = html;
|
||||||
|
|||||||
@@ -6,14 +6,33 @@ function _getDefaultPackeryOptions() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _getPackeryOptions(nodeGallery) {
|
||||||
|
const defaults = _getDefaultPackeryOptions();
|
||||||
|
const {
|
||||||
|
packeryGutter,
|
||||||
|
packeryPercentPosition,
|
||||||
|
packeryResize,
|
||||||
|
} = nodeGallery.dataset;
|
||||||
|
|
||||||
|
return {
|
||||||
|
percentPosition:
|
||||||
|
packeryPercentPosition !== undefined
|
||||||
|
? packeryPercentPosition === "true"
|
||||||
|
: defaults.percentPosition,
|
||||||
|
gutter:
|
||||||
|
packeryGutter !== undefined ? parseInt(packeryGutter, 10) : defaults.gutter,
|
||||||
|
resize:
|
||||||
|
packeryResize !== undefined ? packeryResize === "true" : defaults.resize,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
(function init() {
|
(function init() {
|
||||||
$(window).on("load", function () {
|
$(window).on("load", function () {
|
||||||
let packeries = [];
|
let packeries = [];
|
||||||
let nodeGalleries = document.querySelectorAll(".gallery");
|
let nodeGalleries = document.querySelectorAll(".gallery");
|
||||||
|
|
||||||
nodeGalleries.forEach((nodeGallery) => {
|
nodeGalleries.forEach((nodeGallery) => {
|
||||||
// TODO : implement a reader of Packery configuration _getPackeryOptions; for example by reading data-attribute
|
let packery = new Packery(nodeGallery, _getPackeryOptions(nodeGallery));
|
||||||
let packery = new Packery(nodeGallery, _getDefaultPackeryOptions());
|
|
||||||
packeries.push(packery);
|
packeries.push(packery);
|
||||||
});
|
});
|
||||||
console.groupEnd();
|
console.groupEnd();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const fs = require("fs");
|
const fs = require("fs/promises");
|
||||||
const translate = require("@iamtraction/google-translate");
|
const translate = require("@iamtraction/google-translate");
|
||||||
|
|
||||||
const defaultLang = "en";
|
const defaultLang = "en";
|
||||||
@@ -8,11 +8,8 @@ const targetLangIso = targetLang == "pt" ? "pt-pt" : targetLang;
|
|||||||
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
|
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
|
||||||
|
|
||||||
async function convert(text, from, to) {
|
async function convert(text, from, to) {
|
||||||
var options = {
|
const options = { from, to };
|
||||||
from: from,
|
const translated_text = await translate(text, options);
|
||||||
to: to,
|
|
||||||
};
|
|
||||||
var translated_text = await translate(text, options);
|
|
||||||
return translated_text.text;
|
return translated_text.text;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,12 +17,12 @@ console.log(filePath);
|
|||||||
console.log(targetFilePath);
|
console.log(targetFilePath);
|
||||||
|
|
||||||
async function processFrontMatter(block) {
|
async function processFrontMatter(block) {
|
||||||
var array = block.split("\n");
|
const array = block.split("\n");
|
||||||
var translatedBlock = "";
|
let translatedBlock = "";
|
||||||
for (var i = 0; i < array.length; i++) {
|
for (const line of array) {
|
||||||
if (array[i].indexOf(":") > -1) {
|
let newElement = line;
|
||||||
var elements = array[i].split(":");
|
if (line.indexOf(":") > -1) {
|
||||||
var newElement = "";
|
const elements = line.split(":");
|
||||||
if (
|
if (
|
||||||
elements[0] == "title" ||
|
elements[0] == "title" ||
|
||||||
elements[0] == "description" ||
|
elements[0] == "description" ||
|
||||||
@@ -34,13 +31,11 @@ async function processFrontMatter(block) {
|
|||||||
elements[0] == "categories" ||
|
elements[0] == "categories" ||
|
||||||
elements[0] == "tags"
|
elements[0] == "tags"
|
||||||
) {
|
) {
|
||||||
var translatedElement = elements[1] ? await convert(elements[1], defaultLang, targetLang) : elements[1];
|
const translatedElement = elements[1]
|
||||||
|
? await convert(elements[1], defaultLang, targetLang)
|
||||||
|
: elements[1];
|
||||||
newElement = elements[0] + ": " + translatedElement;
|
newElement = elements[0] + ": " + translatedElement;
|
||||||
} else {
|
|
||||||
newElement = array[i];
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
newElement = array[i];
|
|
||||||
}
|
}
|
||||||
translatedBlock += newElement + "\n";
|
translatedBlock += newElement + "\n";
|
||||||
}
|
}
|
||||||
@@ -48,17 +43,19 @@ async function processFrontMatter(block) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const fileContent = fs.readFileSync(filePath, "utf8");
|
const fileContent = await fs.readFile(filePath, "utf8");
|
||||||
|
|
||||||
var array = fileContent.split("---\n");
|
const array = fileContent.split("---\n");
|
||||||
var frontMatter = array[1];
|
const frontMatter = array[1];
|
||||||
var content = array[2];
|
const content = array[2];
|
||||||
|
|
||||||
var translatedFrontMatter = await processFrontMatter(frontMatter);
|
const translatedFrontMatter = await processFrontMatter(frontMatter);
|
||||||
var translatedContent = await convert(content, defaultLang, targetLang);
|
const translatedContent = await convert(content, defaultLang, targetLang);
|
||||||
|
|
||||||
var newFileContent = "---\n" + translatedFrontMatter + "---\n" + translatedContent;
|
const newFileContent =
|
||||||
fs.writeFileSync(targetFilePath, newFileContent, "utf8");
|
"---\n" + translatedFrontMatter + "---\n" + translatedContent;
|
||||||
|
await fs.writeFile(targetFilePath, newFileContent, "utf8");
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
||||||
|
|||||||
92
genLang.js
92
genLang.js
@@ -1,4 +1,4 @@
|
|||||||
const fs = require("fs");
|
const fs = require("fs/promises");
|
||||||
const translate = require("@iamtraction/google-translate");
|
const translate = require("@iamtraction/google-translate");
|
||||||
|
|
||||||
const configDir = "./exampleSite/config/_default";
|
const configDir = "./exampleSite/config/_default";
|
||||||
@@ -7,35 +7,39 @@ const defaultLang = "en";
|
|||||||
const targetLang = process.argv[2] || "en";
|
const targetLang = process.argv[2] || "en";
|
||||||
const targetLangIso = targetLang == "pt" ? "pt-pt" : targetLang;
|
const targetLangIso = targetLang == "pt" ? "pt-pt" : targetLang;
|
||||||
|
|
||||||
function createConfigs() {
|
async function createConfigs() {
|
||||||
const files = fs.readdirSync(configDir);
|
const files = await fs.readdir(configDir);
|
||||||
files.forEach((file) => {
|
for (const file of files) {
|
||||||
const filePath = `${configDir}/${file}`;
|
const filePath = `${configDir}/${file}`;
|
||||||
if (filePath.indexOf("languages.en.toml") > -1 || filePath.indexOf("menus.en.toml") > -1) {
|
if (
|
||||||
var fileContent = fs.readFileSync(filePath, "utf8");
|
filePath.indexOf("languages.en.toml") > -1 ||
|
||||||
fs.writeFileSync(filePath.replace(".en.toml", "." + targetLangIso + ".toml"), fileContent, "utf8");
|
filePath.indexOf("menus.en.toml") > -1
|
||||||
|
) {
|
||||||
|
const fileContent = await fs.readFile(filePath, "utf8");
|
||||||
|
await fs.writeFile(
|
||||||
|
filePath.replace(".en.toml", "." + targetLangIso + ".toml"),
|
||||||
|
fileContent,
|
||||||
|
"utf8"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function convert(text, from, to) {
|
async function convert(text, from, to) {
|
||||||
var options = {
|
const options = { from, to };
|
||||||
from: from,
|
const translated_text = await translate(text, options).catch((err) => {
|
||||||
to: to,
|
|
||||||
};
|
|
||||||
var translated_text = await translate(text, options).catch((err) => {
|
|
||||||
console.error(err);
|
console.error(err);
|
||||||
});
|
});
|
||||||
return translated_text && translated_text.text ? translated_text.text : "";
|
return translated_text && translated_text.text ? translated_text.text : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processFrontMatter(block) {
|
async function processFrontMatter(block) {
|
||||||
var array = block.split("\n");
|
const array = block.split("\n");
|
||||||
var translatedBlock = "";
|
let translatedBlock = "";
|
||||||
for (var i = 0; i < array.length; i++) {
|
for (const line of array) {
|
||||||
if (array[i].indexOf(":") > -1) {
|
let newElement = line;
|
||||||
var elements = array[i].split(":");
|
if (line.indexOf(":") > -1) {
|
||||||
var newElement = "";
|
const elements = line.split(":");
|
||||||
if (
|
if (
|
||||||
elements[0] == "title" ||
|
elements[0] == "title" ||
|
||||||
elements[0] == "description" ||
|
elements[0] == "description" ||
|
||||||
@@ -44,13 +48,11 @@ async function processFrontMatter(block) {
|
|||||||
elements[0] == "categories" ||
|
elements[0] == "categories" ||
|
||||||
elements[0] == "tags"
|
elements[0] == "tags"
|
||||||
) {
|
) {
|
||||||
var translatedElement = elements[1] ? await convert(elements[1], defaultLang, targetLang) : elements[1];
|
const translatedElement = elements[1]
|
||||||
|
? await convert(elements[1], defaultLang, targetLang)
|
||||||
|
: elements[1];
|
||||||
newElement = elements[0] + ": " + translatedElement;
|
newElement = elements[0] + ": " + translatedElement;
|
||||||
} else {
|
|
||||||
newElement = array[i];
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
newElement = array[i];
|
|
||||||
}
|
}
|
||||||
translatedBlock += newElement + "\n";
|
translatedBlock += newElement + "\n";
|
||||||
}
|
}
|
||||||
@@ -60,29 +62,34 @@ async function processFrontMatter(block) {
|
|||||||
async function processFile(filePath) {
|
async function processFile(filePath) {
|
||||||
console.log("translating", filePath);
|
console.log("translating", filePath);
|
||||||
if (filePath.indexOf("index.md") > -1) {
|
if (filePath.indexOf("index.md") > -1) {
|
||||||
const targetFilePath = filePath.replace(".md", "." + targetLangIso + ".md");
|
const targetFilePath = filePath.replace(
|
||||||
|
".md",
|
||||||
|
"." + targetLangIso + ".md"
|
||||||
|
);
|
||||||
|
|
||||||
const fileContent = fs.readFileSync(filePath, "utf8");
|
const fileContent = await fs.readFile(filePath, "utf8");
|
||||||
|
|
||||||
var array = fileContent.split("---\n");
|
const array = fileContent.split("---\n");
|
||||||
var frontMatter = array[1];
|
const frontMatter = array[1];
|
||||||
var content = array[2];
|
const content = array[2];
|
||||||
|
|
||||||
var translatedFrontMatter = await processFrontMatter(frontMatter);
|
const translatedFrontMatter = await processFrontMatter(frontMatter);
|
||||||
var translatedContent = await convert(content, defaultLang, targetLang);
|
const translatedContent = await convert(content, defaultLang, targetLang);
|
||||||
|
|
||||||
var newFileContent = "---\n" + translatedFrontMatter + "---\n" + translatedContent;
|
const newFileContent =
|
||||||
fs.writeFileSync(targetFilePath, newFileContent, "utf8");
|
"---\n" + translatedFrontMatter + "---\n" + translatedContent;
|
||||||
} else return;
|
await fs.writeFile(targetFilePath, newFileContent, "utf8");
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processFolder(folderPath) {
|
async function processFolder(folderPath) {
|
||||||
const files = fs.readdirSync(folderPath);
|
const files = await fs.readdir(folderPath);
|
||||||
|
|
||||||
for (var i in files) {
|
for (const file of files) {
|
||||||
const file = files[i];
|
|
||||||
const filePath = `${folderPath}/${file}`;
|
const filePath = `${folderPath}/${file}`;
|
||||||
const isDir = fs.lstatSync(filePath).isDirectory();
|
const isDir = (await fs.lstat(filePath)).isDirectory();
|
||||||
console.log(filePath, isDir);
|
console.log(filePath, isDir);
|
||||||
if (isDir) {
|
if (isDir) {
|
||||||
await processFolder(filePath);
|
await processFolder(filePath);
|
||||||
@@ -93,8 +100,11 @@ async function processFolder(folderPath) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createContent() {
|
async function createContent() {
|
||||||
processFolder(contentDir);
|
await processFolder(contentDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
createConfigs();
|
(async () => {
|
||||||
createContent();
|
await createConfigs();
|
||||||
|
await createContent();
|
||||||
|
})();
|
||||||
|
|
||||||
|
|||||||
@@ -1,52 +1,57 @@
|
|||||||
const fs = require("fs");
|
const fs = require("fs/promises");
|
||||||
|
|
||||||
const configDir = "./exampleSite/config/_default";
|
const configDir = "./exampleSite/config/_default";
|
||||||
const contentDir = "./exampleSite/content";
|
const contentDir = "./exampleSite/content";
|
||||||
const defaultLang = "en";
|
const defaultLang = "en";
|
||||||
|
|
||||||
var targetLangs = [];
|
const targetLangs = [];
|
||||||
|
|
||||||
function readConfigs() {
|
async function readConfigs() {
|
||||||
const files = fs.readdirSync(configDir);
|
const files = await fs.readdir(configDir);
|
||||||
files.forEach((file) => {
|
for (const file of files) {
|
||||||
console.log(file);
|
console.log(file);
|
||||||
if (file.indexOf("languages.") > -1) {
|
if (file.indexOf("languages.") > -1) {
|
||||||
var lang = file.split(".")[1];
|
const lang = file.split(".")[1];
|
||||||
console.log(lang);
|
console.log(lang);
|
||||||
if (lang != defaultLang) {
|
if (lang != defaultLang) {
|
||||||
targetLangs.push(lang);
|
targetLangs.push(lang);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processFile(filePath, file) {
|
async function processFile(filePath, file) {
|
||||||
if (filePath.indexOf("index.md") > -1) {
|
if (filePath.indexOf("index.md") > -1) {
|
||||||
console.log("processing", filePath);
|
console.log("processing", filePath);
|
||||||
|
|
||||||
for (var i in targetLangs) {
|
for (const targetLang of targetLangs) {
|
||||||
const targetLang = targetLangs[i];
|
const targetFilePath = filePath.replace(".md", "." + targetLang + ".md");
|
||||||
var targetFilePath = filePath.replace(".md", "." + targetLang + ".md");
|
|
||||||
//var targetFileName = file.replace(".md", "." + targetLang + ".md");
|
|
||||||
|
|
||||||
if (fs.existsSync(targetFilePath)) {
|
let exists = true;
|
||||||
|
try {
|
||||||
|
await fs.access(targetFilePath);
|
||||||
|
} catch {
|
||||||
|
exists = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
console.log("file already exists", targetFilePath);
|
console.log("file already exists", targetFilePath);
|
||||||
} else {
|
} else {
|
||||||
console.log("creating file", targetFilePath);
|
console.log("creating file", targetFilePath);
|
||||||
//fs.symlinkSync(file, targetFilePath, 'junction');
|
await fs.copyFile(filePath, targetFilePath);
|
||||||
fs.copyFileSync(filePath, targetFilePath);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else return;
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processFolder(folderPath) {
|
async function processFolder(folderPath) {
|
||||||
const files = fs.readdirSync(folderPath);
|
const files = await fs.readdir(folderPath);
|
||||||
|
|
||||||
for (var i in files) {
|
for (const file of files) {
|
||||||
const file = files[i];
|
|
||||||
const filePath = `${folderPath}/${file}`;
|
const filePath = `${folderPath}/${file}`;
|
||||||
const isDir = fs.lstatSync(filePath).isDirectory();
|
const isDir = (await fs.lstat(filePath)).isDirectory();
|
||||||
if (isDir) {
|
if (isDir) {
|
||||||
await processFolder(filePath);
|
await processFolder(filePath);
|
||||||
} else {
|
} else {
|
||||||
@@ -56,8 +61,11 @@ async function processFolder(folderPath) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createLinks() {
|
async function createLinks() {
|
||||||
processFolder(contentDir);
|
await processFolder(contentDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
readConfigs();
|
(async () => {
|
||||||
createLinks();
|
await readConfigs();
|
||||||
|
await createLinks();
|
||||||
|
})();
|
||||||
|
|
||||||
|
|||||||
218
processUsers.js
218
processUsers.js
@@ -1,151 +1,139 @@
|
|||||||
const fs = require("fs");
|
const fs = require("fs/promises");
|
||||||
const crypto = require("crypto");
|
const crypto = require("crypto");
|
||||||
const puppeteer = require("puppeteer");
|
const puppeteer = require("puppeteer");
|
||||||
|
const translate = require("@iamtraction/google-translate");
|
||||||
|
|
||||||
const configDir = "./exampleSite/config/_default";
|
const configDir = "./exampleSite/config/_default";
|
||||||
const defaultLang = "en";
|
const defaultLang = "en";
|
||||||
const usersFolderPath = "./exampleSite/content/users/";
|
const usersFolderPath = "./exampleSite/content/users/";
|
||||||
const translate = require("@iamtraction/google-translate");
|
|
||||||
|
|
||||||
var targetLangs = [];
|
let cache = {};
|
||||||
|
|
||||||
const configFiles = fs.readdirSync(configDir);
|
|
||||||
configFiles.forEach((file) => {
|
|
||||||
if (file.indexOf("languages.") > -1) {
|
|
||||||
var lang = file.split(".")[1];
|
|
||||||
if (lang != defaultLang) {
|
|
||||||
targetLangs.push(lang);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const indexFiles = fs.readdirSync(usersFolderPath);
|
|
||||||
for (var i in targetLangs) {
|
|
||||||
var targetFile = "_index." + targetLangs[i] + ".md";
|
|
||||||
if (indexFiles.indexOf(targetFile) == -1) {
|
|
||||||
fs.copyFileSync(usersFolderPath + "_index.md", usersFolderPath + targetFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let rawdata = fs.readFileSync(usersFolderPath + "users.json");
|
|
||||||
let users = JSON.parse(rawdata);
|
|
||||||
let userDict = {};
|
|
||||||
for (var i in users) {
|
|
||||||
userDict[generateDirName(users[i].url)] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const files = fs.readdirSync(usersFolderPath);
|
|
||||||
|
|
||||||
for (file in files) {
|
|
||||||
let stats = fs.statSync(usersFolderPath + files[file]);
|
|
||||||
if (files[file] != "users.json" && files[file].indexOf("_index.") == -1) {
|
|
||||||
if (stats.isDirectory()) {
|
|
||||||
if (!userDict[files[file]]) {
|
|
||||||
console.log("deleting: ", files[file]);
|
|
||||||
fs.rmdirSync(usersFolderPath + files[file], { recursive: true, force: true });
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log("deleting: ", files[file]);
|
|
||||||
fs.unlinkSync(usersFolderPath + files[file]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var cache = {};
|
|
||||||
|
|
||||||
function generateDirName(seed) {
|
function generateDirName(seed) {
|
||||||
var hash = crypto.createHash("md5");
|
const hash = crypto.createHash("md5");
|
||||||
hash.update(seed);
|
hash.update(seed);
|
||||||
var dir = hash.digest("hex");
|
return hash.digest("hex");
|
||||||
return dir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function convert(text, from, to) {
|
async function convert(text, from, to) {
|
||||||
var options = {
|
const options = { from, to };
|
||||||
from: from,
|
|
||||||
to: to,
|
|
||||||
};
|
|
||||||
if (!cache[to]) cache[to] = {};
|
if (!cache[to]) cache[to] = {};
|
||||||
if (cache[to][text]) return cache[to][text];
|
if (cache[to][text]) return cache[to][text];
|
||||||
var translated_text = await translate(text, options);
|
const translated_text = await translate(text, options);
|
||||||
cache[to][text] = translated_text.text;
|
cache[to][text] = translated_text.text;
|
||||||
return translated_text.text;
|
return translated_text.text;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function translateFrontMatterTags(block, targetLang, tags) {
|
async function translateFrontMatterTags(block, targetLang, tags) {
|
||||||
var array = block.split("\n");
|
const array = block.split("\n");
|
||||||
var translatedBlock = "";
|
let translatedBlock = "";
|
||||||
for (var i = 0; i < array.length; i++) {
|
for (const line of array) {
|
||||||
if (array[i].indexOf(":") > -1) {
|
let newElement = line;
|
||||||
var elements = array[i].split(":");
|
if (line.indexOf(":") > -1) {
|
||||||
var newElement = "";
|
const elements = line.split(":");
|
||||||
if (elements[0].indexOf("tags") != -1) {
|
if (elements[0].indexOf("tags") != -1) {
|
||||||
translatedTags = [];
|
const translatedTags = [];
|
||||||
for (var j in tags) {
|
for (const tag of tags) {
|
||||||
var tempTag = await convert(tags[j], defaultLang, targetLang);
|
const tempTag = await convert(tag, defaultLang, targetLang);
|
||||||
translatedTags.push(tempTag);
|
translatedTags.push(tempTag);
|
||||||
}
|
}
|
||||||
var trasnlatedTagsString = translatedTags.join(", ");
|
const trasnlatedTagsString = translatedTags.join(", ");
|
||||||
newElement = elements[0] + ": [" + trasnlatedTagsString + "]";
|
newElement = elements[0] + ": [" + trasnlatedTagsString + "]";
|
||||||
} else {
|
|
||||||
newElement = array[i];
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
newElement = array[i];
|
|
||||||
}
|
}
|
||||||
translatedBlock += newElement + "\n";
|
translatedBlock += newElement + "\n";
|
||||||
}
|
}
|
||||||
return translatedBlock;
|
return translatedBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
puppeteer
|
(async () => {
|
||||||
.launch({
|
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 = {};
|
||||||
|
for (const user of users) {
|
||||||
|
userDict[generateDirName(user.url)] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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: {
|
defaultViewport: {
|
||||||
width: 1280,
|
width: 1280,
|
||||||
height: 800,
|
height: 800,
|
||||||
},
|
},
|
||||||
})
|
|
||||||
.then(async (browser) => {
|
|
||||||
const page = await browser.newPage();
|
|
||||||
|
|
||||||
for (var i in users) {
|
|
||||||
var userMDFile =
|
|
||||||
'---\n\
|
|
||||||
title: "' +
|
|
||||||
users[i].title +
|
|
||||||
'"\n\
|
|
||||||
tags: [' +
|
|
||||||
users[i].tags +
|
|
||||||
']\n\
|
|
||||||
externalUrl: "' +
|
|
||||||
users[i].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";
|
|
||||||
|
|
||||||
var dir = usersFolderPath + generateDirName(users[i].url);
|
|
||||||
|
|
||||||
if (!fs.existsSync(dir)) {
|
|
||||||
fs.mkdirSync(dir);
|
|
||||||
console.log(i, users[i].title);
|
|
||||||
fs.writeFileSync(dir + "/index.md", userMDFile);
|
|
||||||
for (var j in targetLangs) {
|
|
||||||
var content = await translateFrontMatterTags(userMDFile, targetLangs[j], users[i].tags);
|
|
||||||
fs.writeFileSync(dir + "/index." + targetLangs[j] + ".md", content);
|
|
||||||
}
|
|
||||||
await page.goto(users[i].url);
|
|
||||||
await page.screenshot({ path: dir + "/feature.jpg", type: "webp", quality: 50 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await browser.close();
|
|
||||||
});
|
});
|
||||||
|
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);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(dir);
|
||||||
|
} catch {
|
||||||
|
await fs.mkdir(dir);
|
||||||
|
}
|
||||||
|
console.log(i, user.title);
|
||||||
|
await fs.writeFile(dir + "/index.md", userMDFile);
|
||||||
|
for (const lang of targetLangs) {
|
||||||
|
const content = await translateFrontMatterTags(userMDFile, lang, user.tags);
|
||||||
|
await fs.writeFile(dir + `/index.${lang}.md`, content);
|
||||||
|
}
|
||||||
|
await page.goto(user.url);
|
||||||
|
await page.screenshot({ path: dir + "/feature.jpg", type: "webp", quality: 50 });
|
||||||
|
}
|
||||||
|
|
||||||
|
await browser.close();
|
||||||
|
})();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user