From 7e96119c1a00c3bbd3c5e231c34b0ca58d50cc0b Mon Sep 17 00:00:00 2001 From: bosh-code Date: Mon, 24 Mar 2025 16:48:34 +1300 Subject: [PATCH] refactor jsonGenerator.js to be esmodule --- scripts/jsonGenerator.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/scripts/jsonGenerator.js b/scripts/jsonGenerator.js index 8aee1ac..28bc69b 100644 --- a/scripts/jsonGenerator.js +++ b/scripts/jsonGenerator.js @@ -1,6 +1,6 @@ -const fs = require("fs"); -const path = require("path"); -const matter = require("gray-matter"); +import fs from "node:fs"; +import path from "node:path"; +import matter from "gray-matter"; const CONTENT_DEPTH = 2; const JSON_FOLDER = "./.json"; @@ -41,10 +41,7 @@ const getData = (folder, groupDepth) => { } }); - const publishedPages = getPaths.filter( - (page) => !page.frontmatter?.draft && page, - ); - return publishedPages; + return getPaths.filter((page) => !page.frontmatter?.draft && page); }; try { @@ -60,7 +57,8 @@ try { ); // 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]; fs.writeFileSync(`${JSON_FOLDER}/search.json`, JSON.stringify(search)); } catch (err) {