mirror of
https://github.com/10h30/astroplate.git
synced 2026-06-05 15:08:00 +09:00
remove multilingual and create a new multilingual branch
This commit is contained in:
+37
-54
@@ -1,66 +1,49 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const matter = require("gray-matter");
|
||||
const languages = require("../src/config/language.json");
|
||||
|
||||
const CONTENT_DEPTH = 2;
|
||||
const JSON_FOLDER = "./.json";
|
||||
const CONTENT_ROOT = "src/content";
|
||||
const CONTENT_DEPTH = 3;
|
||||
const BLOG_FOLDER = "blog";
|
||||
const BLOG_FOLDER = "src/content/blog";
|
||||
|
||||
// get data from markdown
|
||||
const getData = (folder, groupDepth, langIndex = 0) => {
|
||||
const getPaths = languages
|
||||
.map((lang, index) => {
|
||||
const langFolder = lang.contentDir ? lang.contentDir : lang.languageCode;
|
||||
const dir = path.join(CONTENT_ROOT, folder, langFolder);
|
||||
return fs
|
||||
.readdirSync(dir)
|
||||
.filter(
|
||||
(filename) =>
|
||||
!filename.startsWith("-") &&
|
||||
(filename.endsWith(".md") || filename.endsWith(".mdx")),
|
||||
)
|
||||
.flatMap((filename) => {
|
||||
const filepath = path.join(dir, filename);
|
||||
const stats = fs.statSync(filepath);
|
||||
const isFolder = stats.isDirectory();
|
||||
const getData = (folder, groupDepth) => {
|
||||
const getPath = fs.readdirSync(folder);
|
||||
const removeIndex = getPath.filter((item) => !item.startsWith("-"));
|
||||
|
||||
if (isFolder) {
|
||||
return getData(filepath, groupDepth, index);
|
||||
} else {
|
||||
const file = fs.readFileSync(filepath, "utf-8");
|
||||
const { data, content } = matter(file);
|
||||
const pathParts = filepath.split(path.sep);
|
||||
const getPaths = removeIndex.flatMap((filename) => {
|
||||
const filepath = path.join(folder, filename);
|
||||
const stats = fs.statSync(filepath);
|
||||
const isFolder = stats.isDirectory();
|
||||
|
||||
let slug;
|
||||
if (data.slug) {
|
||||
const slugParts = data.slug.split("/");
|
||||
slugParts[0] = BLOG_FOLDER;
|
||||
slug = slugParts.join("/");
|
||||
} else {
|
||||
slug = pathParts
|
||||
.slice(CONTENT_DEPTH)
|
||||
.join("/")
|
||||
.replace(/\.[^/.]+$/, "");
|
||||
slug = `${BLOG_FOLDER}/${slug.split("/").slice(1).join("/")}`;
|
||||
}
|
||||
data.slug = slug;
|
||||
const group = "blog";
|
||||
if (isFolder) {
|
||||
return getData(filepath, groupDepth);
|
||||
} else if (filename.endsWith(".md") || filename.endsWith(".mdx")) {
|
||||
const file = fs.readFileSync(filepath, "utf-8");
|
||||
const { data, content } = matter(file);
|
||||
const pathParts = filepath.split(path.sep);
|
||||
const slug =
|
||||
data.slug ||
|
||||
pathParts
|
||||
.slice(CONTENT_DEPTH)
|
||||
.join("/")
|
||||
.replace(/\.[^/.]+$/, "");
|
||||
const group = pathParts[groupDepth];
|
||||
|
||||
return {
|
||||
lang: languages[index].languageCode, // Set the correct language code dynamically
|
||||
group: group,
|
||||
slug: data.slug,
|
||||
frontmatter: data,
|
||||
content: content,
|
||||
};
|
||||
}
|
||||
});
|
||||
})
|
||||
.flat();
|
||||
return {
|
||||
group: group,
|
||||
slug: slug,
|
||||
frontmatter: data,
|
||||
content: content,
|
||||
};
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
const publishedPages = getPaths.filter((page) => !page.frontmatter?.draft);
|
||||
const publishedPages = getPaths.filter(
|
||||
(page) => !page.frontmatter?.draft && page,
|
||||
);
|
||||
return publishedPages;
|
||||
};
|
||||
|
||||
@@ -73,10 +56,10 @@ try {
|
||||
// create json files
|
||||
fs.writeFileSync(
|
||||
`${JSON_FOLDER}/posts.json`,
|
||||
JSON.stringify(getData(BLOG_FOLDER, 3)),
|
||||
JSON.stringify(getData(BLOG_FOLDER, 2)),
|
||||
);
|
||||
|
||||
// merge json files for search
|
||||
// merger json files for search
|
||||
const posts = require(`../${JSON_FOLDER}/posts.json`);
|
||||
const search = [...posts];
|
||||
fs.writeFileSync(`${JSON_FOLDER}/search.json`, JSON.stringify(search));
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const languages = require("../src/config/language.json");
|
||||
|
||||
// Filter out the English language
|
||||
const englishLang = languages.filter((item) => item.languageCode === "en");
|
||||
const filterLangs = languages.filter((item) => item.languageCode !== "en");
|
||||
const contentDir = "src/content";
|
||||
const configDir = "src/config";
|
||||
const i18nDir = "src/i18n";
|
||||
|
||||
// Update language.json to only include the English language
|
||||
fs.writeFileSync(
|
||||
path.join(configDir, "language.json"),
|
||||
JSON.stringify(englishLang, null, 2),
|
||||
);
|
||||
|
||||
// Remove content directories for languages other than English
|
||||
filterLangs.forEach((lang) => {
|
||||
const langContentDir = path.join(contentDir, lang.contentDir);
|
||||
fs.rm(langContentDir, { recursive: true, force: true }, (err) => {
|
||||
if (err) {
|
||||
console.error(`Error deleting folder ${langContentDir}:`, err);
|
||||
return;
|
||||
}
|
||||
console.log(`Folder ${langContentDir} deleted successfully`);
|
||||
});
|
||||
});
|
||||
|
||||
// Remove other menu.{lang}.json files except menu.en.json
|
||||
fs.readdir(configDir, (err, files) => {
|
||||
if (err) {
|
||||
console.error("Error reading config directory:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
files.forEach((file) => {
|
||||
if (file.startsWith("menu.") && file !== "menu.en.json") {
|
||||
const filePath = path.join(configDir, file);
|
||||
fs.unlink(filePath, (err) => {
|
||||
if (err) {
|
||||
console.error(`Error deleting file ${filePath}:`, err);
|
||||
return;
|
||||
}
|
||||
console.log(`File ${filePath} deleted successfully`);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Remove other language files from i18n folder except en.json
|
||||
fs.readdir(i18nDir, (err, files) => {
|
||||
if (err) {
|
||||
console.error("Error reading i18n directory:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
files.forEach((file) => {
|
||||
if (file !== "en.json") {
|
||||
const filePath = path.join(i18nDir, file);
|
||||
fs.unlink(filePath, (err) => {
|
||||
if (err) {
|
||||
console.error(`Error deleting file ${filePath}:`, err);
|
||||
return;
|
||||
}
|
||||
console.log(`File ${filePath} deleted successfully`);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
console.log("Cleanup completed.");
|
||||
Reference in New Issue
Block a user