Skip to content

Commit

Permalink
Merge pull request #31 from evilmartians/29-add-ability-to-modify-fro…
Browse files Browse the repository at this point in the history
…ntmatter

Add configurable frontmatter
  • Loading branch information
tatarianBarbarian authored Sep 11, 2023
2 parents 150ed9b + 002446f commit 39cc380
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
24 changes: 17 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ 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)) {
event.preventDefault()
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) => {
Expand Down Expand Up @@ -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)

Expand Down
15 changes: 9 additions & 6 deletions test/website-nanostores/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
15 changes: 9 additions & 6 deletions test/website-types/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({})

0 comments on commit 39cc380

Please sign in to comment.