diff --git a/build.js b/build.js index f8c26afc73781231f1a98447ca315936d386872f..19beddf2bd67301f484c6a85775d5b7128b57db1 100644 --- a/build.js +++ b/build.js @@ -87,9 +87,8 @@ function getPageHtml(pageName, pageMeta) { defaultValue: (function () { const urlContent = getDefaultOgMetaContent("url"); - return `${urlContent}${ - urlContent.charAt(urlContent.length - 1) !== "/" ? "/" : "" - }${pageName}`; + return `${urlContent}${urlContent.charAt(urlContent.length - 1) !== "/" ? "/" : "" + }${pageName}`; })(), }, { @@ -110,8 +109,8 @@ function getPageHtml(pageName, pageMeta) { else { return Array.isArray(customValue) ? customValue - .map(alt => `<meta property="og:${key}" content="${alt}"/>`) - .join("\n") + .map(alt => `<meta property="og:${key}" content="${alt}"/>`) + .join("\n") : `<meta property="og:${key}" content="${customValue}"/>`; } })() @@ -151,13 +150,12 @@ function getPageHtml(pageName, pageMeta) { // set twitter image html = html.replace( html.match(new RegExp(`<meta\\s*property="twitter:image"[^>]+>`, "g"))[0], - `<meta property="twitter:image" content="${ - pageMeta.image || - html - .match(new RegExp(`<meta\\s*name="image"[^>]+>`, "g"))[0] - .match(new RegExp(`content=".+"`, "g"))[0] - .match(new RegExp(`".+"`, "g")) - .replace(/"/g, "") + `<meta property="twitter:image" content="${pageMeta.image || + html + .match(new RegExp(`<meta\\s*name="image"[^>]+>`, "g"))[0] + .match(new RegExp(`content=".+"`, "g"))[0] + .match(new RegExp(`".+"`, "g")) + .replace(/"/g, "") }"/>` ); @@ -165,43 +163,92 @@ function getPageHtml(pageName, pageMeta) { return html; } -const pages = fs.readdirSync(`${curDir}/src/pages`); +function createPages(rootdir, destdir) { + const pages = fs.readdirSync(rootdir); -for (const p of pages) { - const fPath = `${curDir}/src/pages/${p}`; - const targetDirPath = `${curDir}/public/${p}`; + for (const p of pages) { + const fPath = `${rootdir}/${p}`; + const targetDirPath = `${destdir}/${p}`; - if (!fs.existsSync(targetDirPath)) { - fs.mkdirSync(targetDirPath); - } + if (!fs.existsSync(targetDirPath)) { + fs.mkdirSync(targetDirPath); + } - const b = browserify(); + const b = browserify(); - b.add(fPath) - .bundle() - .pipe(fs.createWriteStream(`${targetDirPath}/${p}.js`)); + b.add(fPath) + .bundle() + .pipe(fs.createWriteStream(`${targetDirPath}/${p}.js`)); - const page = fs.createWriteStream(`${targetDirPath}/index.html`); - const pageMeta = JSON.parse(fs.readFileSync(`${fPath}/meta.json`, "utf-8")); - page.write(getPageHtml(p, pageMeta)); -} + const page = fs.createWriteStream(`${targetDirPath}/index.html`); + const pageMeta = JSON.parse(fs.readFileSync(`${fPath}/meta.json`, "utf-8")); + page.write(getPageHtml(p, pageMeta)); -// If pages have been deleted in source, remove them in output directory too. -for (const dir of fs.readdirSync(`${curDir}/public`).filter(f => { - if (build_conf.protected_dirs.includes(f)) return false; - const stats = fs.statSync(`${curDir}/public/${f}`); - return stats.isDirectory(); -})) { - if (!pages.includes(dir)) { - const dPath = `${curDir}/public/${dir}`; - try { - const nestedFiles = fs.readdirSync(dPath); - for (const nf of nestedFiles) { - fs.unlinkSync(`${dPath}/${nf}`); + if (fs.existsSync(`${fPath}/subpages`)) { + createPages(`${fPath}/subpages`, targetDirPath) + } + } + + // If pages have been deleted in source, remove them in output directory too. + for (const dir of fs.readdirSync(destdir).filter(f => { + if (build_conf.protected_dirs.includes(f)) return false; + const stats = fs.statSync(`${destdir}/${f}`); + return stats.isDirectory(); + })) { + if (!pages.includes(dir)) { + const dPath = `${destdir}/${dir}`; + try { + const nestedFiles = fs.readdirSync(dPath); + for (const nf of nestedFiles) { + fs.unlinkSync(`${dPath}/${nf}`); + } + fs.rmdirSync(dPath); + } catch (error) { + console.error(error); } - fs.rmdirSync(dPath); - } catch (error) { - console.error(error); } } } + +createPages(`${curDir}/src/pages`, `${curDir}/public`); + +// const pages = fs.readdirSync(`${curDir}/src/pages`); + +// for (const p of pages) { +// const fPath = `${curDir}/src/pages/${p}`; +// const targetDirPath = `${curDir}/public/${p}`; + +// if (!fs.existsSync(targetDirPath)) { +// fs.mkdirSync(targetDirPath); +// } + +// const b = browserify(); + +// b.add(fPath) +// .bundle() +// .pipe(fs.createWriteStream(`${targetDirPath}/${p}.js`)); + +// const page = fs.createWriteStream(`${targetDirPath}/index.html`); +// const pageMeta = JSON.parse(fs.readFileSync(`${fPath}/meta.json`, "utf-8")); +// page.write(getPageHtml(p, pageMeta)); +// } + +// // If pages have been deleted in source, remove them in output directory too. +// for (const dir of fs.readdirSync(`${curDir}/public`).filter(f => { +// if (build_conf.protected_dirs.includes(f)) return false; +// const stats = fs.statSync(`${curDir}/public/${f}`); +// return stats.isDirectory(); +// })) { +// if (!pages.includes(dir)) { +// const dPath = `${curDir}/public/${dir}`; +// try { +// const nestedFiles = fs.readdirSync(dPath); +// for (const nf of nestedFiles) { +// fs.unlinkSync(`${dPath}/${nf}`); +// } +// fs.rmdirSync(dPath); +// } catch (error) { +// console.error(error); +// } +// } +// }