diff --git a/README.md b/README.md index 83a8f83..37e8573 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ It's super easy to install and use. - 🤩 [Editor.js](https://editorjs.io/) ecosystem powered - 📂 Docs nesting — create any structure you need -- 💎 Static rendering - 📱 Nice look on Desktop and Mobile - 🔥 Beautiful page URLs. Human-readable and SEO-friendly. - 🦅 [Hawk](https://hawk.so/?from=docs-demo) is hunting. Errors tracking integrated diff --git a/package.json b/package.json index 4e6ae91..490b138 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "codex.docs", "license": "Apache-2.0", - "version": "2.2.3", + "version": "v2.2.0-rc.11", "type": "module", "bin": { "codex.docs": "dist/backend/app.js" diff --git a/src/backend/build-static.ts b/src/backend/build-static.ts index ca0894a..2b17fa4 100644 --- a/src/backend/build-static.ts +++ b/src/backend/build-static.ts @@ -13,7 +13,6 @@ import fse from 'fs-extra'; import appConfig from './utils/appConfig.js'; import Aliases from './controllers/aliases.js'; import Pages from './controllers/pages.js'; -import { downloadFavicon } from './utils/downloadFavicon.js'; /** * Build static pages from database @@ -55,33 +54,7 @@ export default async function buildStatic(): Promise { const pagesOrder = await PagesOrder.getAll(); const allPages = await Page.getAll(); - try { - console.log('Create dist folder'); - await mkdirp(distPath); - } catch (e) { - console.log('Error while creating dist folder', e); - - return; - } - - console.log('Copy public directory'); - const publicDir = path.resolve(dirname, '../../public'); - - console.log(`Copy from ${publicDir} to ${distPath}`); - - try { - await fse.copy(publicDir, distPath); - console.log('Public directory copied'); - } catch (e) { - console.log('Error while copying public directory'); - console.error(e); - } - - const favicon = appConfig.favicon ? await downloadFavicon(appConfig.favicon, distPath, '') : { - destination: '/favicon.png', - type: 'image/png', - }; - + await mkdirp(distPath); /** * Renders single page @@ -91,11 +64,6 @@ 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; @@ -106,30 +74,16 @@ export default async function buildStatic(): Promise { const previousPage = await PagesFlatArray.getPageBefore(pageId); const nextPage = await PagesFlatArray.getPageAfter(pageId); const menu = createMenuTree(parentIdOfRootPages, allPages, pagesOrder, 2); - const result = await renderTemplate('./views/pages/page.twig', { page, pageParent, previousPage, nextPage, menu, - favicon, config: appConfig.frontend, }); - 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`; - } + const filename = (isIndex || page.uri === '') ? 'index.html' : `${page.uri}.html`; await fs.writeFile(path.resolve(distPath, filename), result); console.log(`Page ${page.uri} rendered`); @@ -165,6 +119,19 @@ export default async function buildStatic(): Promise { } console.log('Static files built'); + console.log('Copy public directory'); + const publicDir = path.resolve(dirname, '../../public'); + + console.log(`Copy from ${publicDir} to ${distPath}`); + + try { + await fse.copy(publicDir, distPath); + console.log('Public directory copied'); + } catch (e) { + console.log('Error while copying public directory'); + console.error(e); + } + if (appConfig.uploads.driver === 'local') { console.log('Copy uploads directory'); await fse.copy(path.resolve(cwd, appConfig.uploads.local.path), path.resolve(distPath, 'uploads')); diff --git a/src/backend/utils/appConfig.ts b/src/backend/utils/appConfig.ts index 34a5e61..8d3e60e 100644 --- a/src/backend/utils/appConfig.ts +++ b/src/backend/utils/appConfig.ts @@ -90,10 +90,7 @@ const FrontendConfig = z.object({ */ const StaticBuildConfig = z.object({ outputDir: z.string(), // Output directory for static build - overwrite: z.boolean().optional() // Overwrite output directory - .default(true), - pagesInsideFolders: z.boolean().optional() // Create separate folder for each page - .default(true), + overwrite: z.boolean().optional().default(true), indexPage: z.object({ enabled: z.boolean(), // Is index page enabled uri: z.string(), // Index page uri diff --git a/src/backend/utils/downloadFavicon.ts b/src/backend/utils/downloadFavicon.ts index ed10cdb..73aa2c0 100644 --- a/src/backend/utils/downloadFavicon.ts +++ b/src/backend/utils/downloadFavicon.ts @@ -1,5 +1,5 @@ import path from 'path'; -import fs from 'fs/promises'; +import fs from 'fs'; import fetch, { RequestInit } from 'node-fetch'; /** @@ -32,10 +32,9 @@ function checkIsUrl(str: string): boolean { * * @param destination - url or path of favicon * @param faviconFolder - folder to save favicon - * @param subRoute - subroute from which the favicon will be served * @returns { Promise } - Promise with data about favicon */ -export async function downloadFavicon(destination: string, faviconFolder: string, subRoute = '/favicon'): Promise { +export async function downloadFavicon(destination: string, faviconFolder: string): Promise { // Check of destination is empty if (!destination) { throw Error('Favicon destination is empty'); @@ -49,10 +48,8 @@ export async function downloadFavicon(destination: string, faviconFolder: string // Check if string is url if (!checkIsUrl(destination)) { - await fs.copyFile(destination, path.join(faviconFolder, filename)); - return { - destination: `${subRoute}/${filename}`, + destination: `/${filename}`, type: `image/${format}`, } as FaviconData; } @@ -75,10 +72,14 @@ export async function downloadFavicon(destination: string, faviconFolder: string const filePath = path.join(faviconFolder, `favicon.${format}`); // Save file - await fs.writeFile(filePath, fileData); + await fs.writeFile(filePath, fileData, (err) => { + if (err) { + console.log(err); + } + }); return { - destination: `${subRoute}/favicon.${format}`, + destination: `/favicon/favicon.${format}`, type: `image/${format}`, } as FaviconData; }