diff --git a/index.js b/index.js index 02d03cb..c10192b 100644 --- a/index.js +++ b/index.js @@ -4,8 +4,12 @@ import { fileURLToPath } from 'node:url' import { Application, PageEvent, TSConfigReader } from 'typedoc' const __dirname = dirname(fileURLToPath(import.meta.url)) +const objectToFrontmatter = (object = {}) => + Object.entries(object) + .map(([key, value]) => `${key}: ${value}`) + .join('\n') -const onRendererPageEnd = event => { +const onRendererPageEnd = frontmatterObject => event => { if (!event.contents) { return } else if (/README\.md$/.test(event.url)) { @@ -13,14 +17,14 @@ const onRendererPageEnd = event => { return } - let frontmatter = `--- + let prependix = `--- title: '${event.model.name}' -layout: '../../../layouts/DocLayout.astro' +${objectToFrontmatter(frontmatterObject)} --- ` - event.contents = frontmatter + event.contents + event.contents = prependix + event.contents } const getNavigationFromProject = (baseUrl = '', project) => { @@ -79,11 +83,17 @@ export const initAstroTypedoc = async ({ }) app.options.addReader(new TSConfigReader()) - app.renderer.on(PageEvent.END, event => onRendererPageEnd(event)) let getReflections = async () => await app.convert() - let generateDocs = async (project, pagesDirectory = 'src/pages/docs') => - await app.generateDocs(project, pagesDirectory) + let generateDocs = async ({ + frontmatter, + outputFolder = 'src/pages/docs', + project + }) => { + app.renderer.on(PageEvent.END, onRendererPageEnd(frontmatter)) + + await app.generateDocs(project, outputFolder) + } let generateNavigationJSON = async (project, outputFolder) => { let navigation = getNavigationFromProject(baseUrl, project) diff --git a/test/website-nanostores/astro.config.mjs b/test/website-nanostores/astro.config.mjs index 4f4ca52..eb2ad4f 100644 --- a/test/website-nanostores/astro.config.mjs +++ b/test/website-nanostores/astro.config.mjs @@ -12,13 +12,16 @@ const astroTypedoc = await initAstroTypedoc({ tsconfig: resolve(__dirname, '../../../nanostores/tsconfig.json') }) -const reflections = await astroTypedoc.getReflections() +const project = await astroTypedoc.getReflections() -await astroTypedoc.generateDocs(reflections, 'src/pages/docs') -await astroTypedoc.generateNavigationJSON( - reflections, - resolve(__dirname, './src/') -) +await astroTypedoc.generateDocs({ + frontmatter: { + layout: resolve(__dirname, './src/layouts/DocLayout.astro') + }, + outputFolder: 'src/pages/docs', + project +}) +await astroTypedoc.generateNavigationJSON(project, resolve(__dirname, './src/')) // https://astro.build/config export default defineConfig({}) diff --git a/test/website-types/astro.config.mjs b/test/website-types/astro.config.mjs index 06092a3..e0c99d6 100644 --- a/test/website-types/astro.config.mjs +++ b/test/website-types/astro.config.mjs @@ -12,13 +12,16 @@ const astroTypedoc = await initAstroTypedoc({ tsconfig: resolve(__dirname, '../types/tsconfig.json') }) -const reflections = await astroTypedoc.getReflections() +const project = await astroTypedoc.getReflections() -await astroTypedoc.generateDocs(reflections, 'src/pages/docs') -await astroTypedoc.generateNavigationJSON( - reflections, - resolve(__dirname, './src/') -) +await astroTypedoc.generateDocs({ + frontmatter: { + layout: resolve(__dirname, './src/layouts/DocLayout.astro') + }, + outputFolder: 'src/pages/docs', + project +}) +await astroTypedoc.generateNavigationJSON(project, resolve(__dirname, './src/')) // https://astro.build/config export default defineConfig({})