diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/builder/pageDirectory.ts | 63 |
1 files changed, 34 insertions, 29 deletions
diff --git a/app/builder/pageDirectory.ts b/app/builder/pageDirectory.ts index 1aed94e..82c14fa 100644 --- a/app/builder/pageDirectory.ts +++ b/app/builder/pageDirectory.ts @@ -1,9 +1,10 @@ -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'; +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 markedFootnote from "marked-footnote"; +import matter from "gray-matter"; marked.use(gfmHeadingId()); @@ -11,19 +12,21 @@ export async function parsePage(page: Page) { try { const frontmatter = matter(page.raw); const config = frontmatter.data; - const html = marked.parse(frontmatter.content, { mangle: false }); + const html = marked + .use(markedFootnote()) + .parse(frontmatter.content, { mangle: false }); page.html = html; page.config = config; - page.view = config.view || 'index'; + page.view = config.view || "index"; page.buildTime = Date.now(); - } catch (e) { + } catch (e) { logger.error(`Failed to parse page ${page.originalPath}: ${e.message}`); } } function loadRaw(path: string): string { - return readFileSync(`${path}`, 'utf-8'); + return readFileSync(`${path}`, "utf-8"); } export class PageDirectory { @@ -31,7 +34,7 @@ export class PageDirectory { private pages: Record<string, Page> = {}; private lastFullBuild: number; - + constructor(pagesPath: string) { this.pagesPath = pagesPath; @@ -39,22 +42,22 @@ export class PageDirectory { delete this.pages[page]; } } - + public init = async (): Promise<void> => { - const localPages = glob.sync(`**/*.{md,html}`, { cwd: this.pagesPath }) + 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 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 fullPath = `${this.pagesPath}/${page}`; + let buildPath = `${process.env.OUTPUT_DIR}/${route}.html`; + let view = `${route}`; let raw: string; try { raw = loadRaw(fullPath); @@ -72,18 +75,18 @@ export class PageDirectory { view: view, raw: raw, buildTime: 0, - config: {} - } - + config: {}, + }; + await parsePage(this.pages[route]); - + return this.pages[route]; - } - + }; + public removePage = (page: string): void => { - let route = page.replace(/\.[^.]*$/,'') + let route = page.replace(/\.[^.]*$/, ""); delete this.pages[route]; - } + }; public get(name: string): Page { const page = this.pages[name]; @@ -97,9 +100,11 @@ export class PageDirectory { public getPages(): Page[] { return Object.values(this.pages); } - + public getPagesBeginningWith(prefix: string): Page[] { - return Object.values(this.pages).filter(page => page.route.startsWith(prefix)); + return Object.values(this.pages).filter((page) => + page.route.startsWith(prefix) + ); } } |
