From c8a2b87b1c9d486e99f4e90a6b470e81a42eabdb Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 28 Dec 2022 04:46:03 +0400 Subject: [PATCH] add option for generating pages inside separate folders --- package.json | 2 +- src/backend/build-static.ts | 19 ++++++++++++++++++- src/backend/utils/appConfig.ts | 5 ++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index ea91a71..9cb812d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "codex.docs", "license": "Apache-2.0", - "version": "v2.2.0-rc.12", + "version": "v2.2.0-rc.13", "type": "module", "bin": { "codex.docs": "dist/backend/app.js" diff --git a/src/backend/build-static.ts b/src/backend/build-static.ts index 7d11e63..9349268 100644 --- a/src/backend/build-static.ts +++ b/src/backend/build-static.ts @@ -73,6 +73,11 @@ export default async function buildStatic(): Promise { */ async function renderPage(page: Page, isIndex?: boolean): Promise { console.log(`Rendering page ${page.uri}`); + const pageUri = page.uri; + + if (!pageUri) { + throw new Error('Page uri is not defined'); + } const pageParent = await page.getParent(); const pageId = page._id; @@ -98,7 +103,19 @@ export default async function buildStatic(): Promise { config: appConfig.frontend, }); - const filename = (isIndex || page.uri === '') ? 'index.html' : `${page.uri}.html`; + let filename: string; + + if (isIndex) { + filename = 'index.html'; + } else if (config?.pagesInsideFolders) { // create folder for each page if pagesInsideFolders is true + const pagePath = path.resolve(distPath, pageUri); + + await mkdirp(pagePath); + + filename = path.resolve(pagePath, 'index.html'); + } else { + filename = `${page.uri}.html`; + } await fs.writeFile(path.resolve(distPath, filename), result); console.log(`Page ${page.uri} rendered`); diff --git a/src/backend/utils/appConfig.ts b/src/backend/utils/appConfig.ts index 8d3e60e..34a5e61 100644 --- a/src/backend/utils/appConfig.ts +++ b/src/backend/utils/appConfig.ts @@ -90,7 +90,10 @@ const FrontendConfig = z.object({ */ const StaticBuildConfig = z.object({ outputDir: z.string(), // Output directory for static build - overwrite: z.boolean().optional().default(true), + overwrite: z.boolean().optional() // Overwrite output directory + .default(true), + pagesInsideFolders: z.boolean().optional() // Create separate folder for each page + .default(true), indexPage: z.object({ enabled: z.boolean(), // Is index page enabled uri: z.string(), // Index page uri