diff options
| author | Leonardo Bishop <me@leonardobishop.com> | 2023-08-28 22:02:57 +0100 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.com> | 2023-08-28 22:02:57 +0100 |
| commit | 7c9abacf956c0e135c1094e38087e018dd572965 (patch) | |
| tree | 866c0a3c27cdaf0bd97ba2403a33349793f3652d /app/builder/pages.ts | |
| parent | 7c8ca1c5cf0067a350578d5589139abf076ef1ec (diff) | |
Rename files to more descriptive names
Diffstat (limited to 'app/builder/pages.ts')
| -rw-r--r-- | app/builder/pages.ts | 117 |
1 files changed, 0 insertions, 117 deletions
diff --git a/app/builder/pages.ts b/app/builder/pages.ts deleted file mode 100644 index 1aed94e..0000000 --- a/app/builder/pages.ts +++ /dev/null @@ -1,117 +0,0 @@ -import { readFileSync } from 'fs'; -import glob from 'glob'; -import { logger } from '../logger.js' -import { marked } from 'marked'; -import { gfmHeadingId } from 'marked-gfm-heading-id'; -import matter from 'gray-matter'; - -marked.use(gfmHeadingId()); - -export async function parsePage(page: Page) { - try { - const frontmatter = matter(page.raw); - const config = frontmatter.data; - const html = marked.parse(frontmatter.content, { mangle: false }); - - page.html = html; - page.config = config; - page.view = config.view || 'index'; - page.buildTime = Date.now(); - } catch (e) { - logger.error(`Failed to parse page ${page.originalPath}: ${e.message}`); - } -} - -function loadRaw(path: string): string { - return readFileSync(`${path}`, 'utf-8'); -} - -export class PageDirectory { - private pagesPath: string; - - private pages: Record<string, Page> = {}; - private lastFullBuild: number; - - constructor(pagesPath: string) { - this.pagesPath = pagesPath; - - for (const page in this.pages) { - delete this.pages[page]; - } - } - - public init = async (): Promise<void> => { - const localPages = glob.sync(`**/*.{md,html}`, { cwd: this.pagesPath }) - for (const page in localPages) { - await this.loadPage(localPages[page]); - } - this.lastFullBuild = Date.now(); - } - - public loadPage = async (page: string): Promise<Page> => { - let route = `/${page.replace(/\.[^.]*$/,'')}`; - let name = /[^/]*$/.exec(route)[0]; - let originalPath = page; - let fullPath = `${this.pagesPath}/${page}` - let buildPath = `${process.env.OUTPUT_DIR}/${route}.html` - let view = `${route}` - let raw: string; - try { - raw = loadRaw(fullPath); - } catch (e) { - logger.error(`Failed to read page ${originalPath}: ${e.message}`); - return undefined; - } - - this.pages[route] = { - route: route, - name: name, - originalPath: originalPath, - fullPath: fullPath, - buildPath: buildPath, - view: view, - raw: raw, - buildTime: 0, - config: {} - } - - await parsePage(this.pages[route]); - - return this.pages[route]; - } - - public removePage = (page: string): void => { - let route = page.replace(/\.[^.]*$/,'') - delete this.pages[route]; - } - - public get(name: string): Page { - const page = this.pages[name]; - if (!page) { - return undefined; - } - - return page; - } - - public getPages(): Page[] { - return Object.values(this.pages); - } - - public getPagesBeginningWith(prefix: string): Page[] { - return Object.values(this.pages).filter(page => page.route.startsWith(prefix)); - } -} - -export type Page = { - html?: string; - raw?: string; - route: string; - name: string; - originalPath: string; - fullPath: string; - buildPath: string; - view: string; - buildTime: number; - config: any; -}; |
