refactor jsonGenerator.js to be esmodule

This commit is contained in:
bosh-code
2025-03-24 16:48:34 +13:00
parent af4fb506c3
commit 7e96119c1a
+6 -8
View File
@@ -1,6 +1,6 @@
const fs = require("fs"); import fs from "node:fs";
const path = require("path"); import path from "node:path";
const matter = require("gray-matter"); import matter from "gray-matter";
const CONTENT_DEPTH = 2; const CONTENT_DEPTH = 2;
const JSON_FOLDER = "./.json"; const JSON_FOLDER = "./.json";
@@ -41,10 +41,7 @@ const getData = (folder, groupDepth) => {
} }
}); });
const publishedPages = getPaths.filter( return getPaths.filter((page) => !page.frontmatter?.draft && page);
(page) => !page.frontmatter?.draft && page,
);
return publishedPages;
}; };
try { try {
@@ -60,7 +57,8 @@ try {
); );
// merger json files for search // merger json files for search
const posts = require(`../${JSON_FOLDER}/posts.json`); const postsPath = new URL(`../${JSON_FOLDER}/posts.json`, import.meta.url);
const posts = JSON.parse(fs.readFileSync(postsPath, "utf8"));
const search = [...posts]; const search = [...posts];
fs.writeFileSync(`${JSON_FOLDER}/search.json`, JSON.stringify(search)); fs.writeFileSync(`${JSON_FOLDER}/search.json`, JSON.stringify(search));
} catch (err) { } catch (err) {