From fe7b63fc4c48c2e413c096f163f359a8d0526171 Mon Sep 17 00:00:00 2001 From: Antal Orcsik Date: Thu, 1 Aug 2024 01:11:17 +0200 Subject: [PATCH] fix build script --- src/js/BlogPage.js | 4 ++-- src/js/build.js | 26 ++++++++++++-------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/js/BlogPage.js b/src/js/BlogPage.js index b893698..619d484 100644 --- a/src/js/BlogPage.js +++ b/src/js/BlogPage.js @@ -49,7 +49,7 @@ class BlogPage let markdownString = markdownContent.toString(); const titleMatch = markdownString.match(/## (.*)/); - if (!titleMatch) throw new Error("Invalid Blog Post - Missing title"); + if (!titleMatch) throw new Error(`Invalid Blog Post (${markdownPath}) - Missing title`); data.title = titleMatch[1].trim(); markdownString = markdownString.replace(titleMatch[0], ""); @@ -60,7 +60,7 @@ class BlogPage } const metaDataMatch = markdownString.match(//s); - if (!metaDataMatch) throw new Error("Invalid Blog Post - Missing meta data"); + if (!metaDataMatch) throw new Error(`Invalid Blog Post (${markdownPath}) - Missing meta data`); markdownString = markdownString.replace(metaDataMatch[0], ''); const metaData = JSON.parse(metaDataMatch[1]); Object.keys(metaData).forEach(key => { diff --git a/src/js/build.js b/src/js/build.js index de9e64e..bbf1393 100644 --- a/src/js/build.js +++ b/src/js/build.js @@ -23,24 +23,22 @@ async function buildPages(configPath) { const markdownPages = await getPages(config.markdownDir); for (let markdownPage of markdownPages) { - try { - - const pagePath = markdownPage.split("/"); - let filename = pagePath.pop(); - const directory = pagePath.join("/"); + if (markdownPage.match(/\/README.md$/)) continue; + try { + const directory = markdownPage.split("/").slice(0, -1).join("/"); if (directory) { const targetDirectory = `${config.webDir}/${directory}`; - await fs.promises.mkdir(targetDirectory, { recursive: true }); - console.log("+", targetDirectory); - - filename = `${directory}/${filename}`; + const result = await fs.promises.mkdir(targetDirectory, { recursive: true }); + if (result) console.log("+", result); } if (directory == "blog") { + const templatePath = `${config.templateDir}/_blog_post.ejs`; const blogPage = await BlogPage.fromFile(config, markdownPage); - const targetFile = `${config.webDir}/${filename.replace(/\.md$/, ".html")}`; - const content = await renderTemplate(`${config.templateDir}/_blog_post.ejs`, {context: {...context, ...blogPage, bundle: ['client']}}); + const content = await renderTemplate(templatePath, {context: {...context, ...blogPage, bundle: ['client']}}); + + const targetFile = `${config.webDir}/${markdownPage.replace(/\.md$/, ".html")}`; await writeFile(targetFile, content); console.log("+", targetFile); } @@ -56,15 +54,15 @@ async function buildPages(configPath) { const directory = page.split("/").slice(0, -1).join("/"); if (directory) { const targetDirectory = `${config.webDir}/${directory}`; - await fs.promises.mkdir(targetDirectory, { recursive: true }); - console.log("+", targetDirectory); + const result = console.log(await fs.promises.mkdir(targetDirectory, { recursive: true })); + if (result) console.log("+", result); } const templatePath = `${config.templateDir}/${page}`; context.path = `/${page.replace(/\.ejs$/, ".html")}`; const content = await renderTemplate(templatePath, {context: {...context, bundle: ["client"]}}); - const targetFile = `${config.webDir}/${context.path}`; + const targetFile = `${config.webDir}${context.path}`; await writeFile(targetFile, content); console.log("+", targetFile);