update jsonGenerator

This commit is contained in:
somrat sorkar
2023-12-19 08:52:26 +06:00
parent 6efeb5cd0e
commit 92ec961f80
2 changed files with 23 additions and 35 deletions
+7 -7
View File
@@ -1,6 +1,6 @@
{ {
"name": "astroplate", "name": "astroplate",
"version": "3.0.0", "version": "3.0.1",
"description": "Astro and Tailwindcss boilerplate", "description": "Astro and Tailwindcss boilerplate",
"author": "zeon.studio", "author": "zeon.studio",
"license": "MIT", "license": "MIT",
@@ -19,7 +19,7 @@
"@astrojs/rss": "^4.0.1", "@astrojs/rss": "^4.0.1",
"@astrojs/sitemap": "^3.0.3", "@astrojs/sitemap": "^3.0.3",
"@astrojs/tailwind": "^5.0.3", "@astrojs/tailwind": "^5.0.3",
"astro": "^4.0.4", "astro": "^4.0.6",
"astro-auto-import": "^0.4.2", "astro-auto-import": "^0.4.2",
"date-fns": "^2.30.0", "date-fns": "^2.30.0",
"date-fns-tz": "^2.0.0", "date-fns-tz": "^2.0.0",
@@ -36,24 +36,24 @@
"react-lite-youtube-embed": "^2.4.0", "react-lite-youtube-embed": "^2.4.0",
"remark-collapse": "^0.1.2", "remark-collapse": "^0.1.2",
"remark-toc": "^9.0.0", "remark-toc": "^9.0.0",
"sharp": "^0.33.0", "sharp": "^0.33.1",
"swiper": "^11.0.5" "swiper": "^11.0.5"
}, },
"devDependencies": { "devDependencies": {
"@tailwindcss/forms": "^0.5.7", "@tailwindcss/forms": "^0.5.7",
"@tailwindcss/typography": "^0.5.10", "@tailwindcss/typography": "^0.5.10",
"@types/marked": "^5.0.2", "@types/marked": "^5.0.2",
"@types/node": "20.10.4", "@types/node": "20.10.5",
"@types/react": "18.2.45", "@types/react": "18.2.45",
"@types/react-dom": "18.2.17", "@types/react-dom": "18.2.18",
"autoprefixer": "^10.4.16", "autoprefixer": "^10.4.16",
"eslint": "^8.55.0", "eslint": "^8.56.0",
"postcss": "^8.4.32", "postcss": "^8.4.32",
"prettier": "^3.1.1", "prettier": "^3.1.1",
"prettier-plugin-astro": "^0.12.2", "prettier-plugin-astro": "^0.12.2",
"prettier-plugin-tailwindcss": "^0.5.9", "prettier-plugin-tailwindcss": "^0.5.9",
"sass": "^1.69.5", "sass": "^1.69.5",
"tailwindcss": "^3.3.6", "tailwindcss": "^3.3.7",
"tailwind-bootstrap-grid": "^5.1.0", "tailwind-bootstrap-grid": "^5.1.0",
"typescript": "5.3.3" "typescript": "5.3.3"
} }
+16 -28
View File
@@ -8,10 +8,10 @@ const BLOG_FOLDER = "src/content/blog";
// get data from markdown // get data from markdown
const getData = (folder, groupDepth) => { const getData = (folder, groupDepth) => {
const getPath = fs.readdirSync(path.join(folder)); const getPath = fs.readdirSync(folder);
const removeIndex = getPath.filter((item) => item.match(/^(?!-)/)); const removeIndex = getPath.filter((item) => !item.startsWith("-"));
const getPaths = removeIndex.map((filename) => { const getPaths = removeIndex.flatMap((filename) => {
const filepath = path.join(folder, filename); const filepath = path.join(folder, filename);
const stats = fs.statSync(filepath); const stats = fs.statSync(filepath);
const isFolder = stats.isDirectory(); const isFolder = stats.isDirectory();
@@ -19,18 +19,16 @@ const getData = (folder, groupDepth) => {
if (isFolder) { if (isFolder) {
return getData(filepath, groupDepth); return getData(filepath, groupDepth);
} else if (filename.endsWith(".md") || filename.endsWith(".mdx")) { } else if (filename.endsWith(".md") || filename.endsWith(".mdx")) {
const file = fs.readFileSync(path.join(folder, filename), "utf-8"); const file = fs.readFileSync(filepath, "utf-8");
const { data } = matter(file); const { data, content } = matter(file);
const content = matter(file).content; const pathParts = filepath.split(path.sep);
const removeExtension = filepath.replace(/\.[^/.]+$/, ""); const slug =
const slug = data.slug data.slug ||
? data.slug pathParts
: removeExtension .slice(CONTENT_DEPTH)
.split("/") .join("/")
.slice(CONTENT_DEPTH, removeExtension.split("/").length) .replace(/\.[^/.]+$/, "");
.join("/"); const group = pathParts[groupDepth];
const group = removeExtension.split("/")[Number(groupDepth)];
return { return {
group: group, group: group,
@@ -38,6 +36,8 @@ const getData = (folder, groupDepth) => {
frontmatter: data, frontmatter: data,
content: content, content: content,
}; };
} else {
return [];
} }
}); });
@@ -47,18 +47,6 @@ const getData = (folder, groupDepth) => {
return publishedPages; return publishedPages;
}; };
// flatten nested arrays
const flatten = (arr) => {
return arr.reduce((result, element) => {
if (Array.isArray(element)) {
result.push(...flatten(element));
} else {
result.push(element);
}
return result;
}, []);
};
try { try {
// create folder if it doesn't exist // create folder if it doesn't exist
if (!fs.existsSync(JSON_FOLDER)) { if (!fs.existsSync(JSON_FOLDER)) {
@@ -68,7 +56,7 @@ try {
// create json files // create json files
fs.writeFileSync( fs.writeFileSync(
`${JSON_FOLDER}/posts.json`, `${JSON_FOLDER}/posts.json`,
JSON.stringify(flatten(getData(BLOG_FOLDER, 2))), JSON.stringify(getData(BLOG_FOLDER, 2)),
); );
// merger json files for search // merger json files for search