@@ -3,6 +3,7 @@ import path from 'node:path'
33import url from 'node:url'
44
55import type { AstroConfig , AstroIntegrationLogger } from 'astro'
6+ import { slug } from 'github-slugger'
67import {
78 Application ,
89 PageEvent ,
@@ -18,7 +19,7 @@ import type { StarlightTypeDocOptions } from '..'
1819
1920import { StarlightTypeDocLogger } from './logger'
2021import { addFrontmatter } from './markdown'
21- import { getStarlightTypeDocOutputDirectory } from './starlight'
22+ import { getRelativeURL , getStarlightTypeDocOutputDirectory } from './starlight'
2223import { StarlightTypeDocTheme } from './theme'
2324
2425const defaultTypeDocConfig : TypeDocConfig = {
@@ -91,6 +92,7 @@ async function bootstrapApp(
9192 logger : AstroIntegrationLogger ,
9293) {
9394 const pagesToRemove : string [ ] = [ ]
95+ const outputDirectory = getStarlightTypeDocOutputDirectory ( output . directory , output . base )
9496
9597 const app = await Application . bootstrapWithPlugins ( {
9698 ...defaultTypeDocConfig ,
@@ -106,10 +108,10 @@ async function bootstrapApp(
106108 app . options . addReader ( new TSConfigReader ( ) )
107109 app . renderer . defineTheme ( 'starlight-typedoc' , StarlightTypeDocTheme )
108110 app . renderer . on ( PageEvent . BEGIN , ( event ) => {
109- onRendererPageBegin ( event as MarkdownPageEvent , pagination )
111+ onRendererPageBegin ( event as MarkdownPageEvent , outputDirectory , pagination )
110112 } )
111113 app . renderer . on ( PageEvent . END , ( event ) => {
112- const shouldRemovePage = onRendererPageEnd ( event as MarkdownPageEvent , pagination )
114+ const shouldRemovePage = onRendererPageEnd ( event as MarkdownPageEvent , outputDirectory , pagination )
113115 if ( shouldRemovePage ) {
114116 pagesToRemove . push ( event . filename )
115117 }
@@ -118,7 +120,7 @@ async function bootstrapApp(
118120 onRendererEnd ( pagesToRemove )
119121 } )
120122 app . options . addDeclaration ( {
121- defaultValue : getStarlightTypeDocOutputDirectory ( output . directory , output . base ) ,
123+ defaultValue : outputDirectory ,
122124 help : 'The starlight-typedoc output directory containing the generated documentation markdown files relative to the `src/content/docs/` directory.' ,
123125 name : 'starlight-typedoc-output' ,
124126 type : ParameterType . String ,
@@ -127,17 +129,20 @@ async function bootstrapApp(
127129 return app
128130}
129131
130- function onRendererPageBegin ( event : MarkdownPageEvent , pagination : boolean ) {
132+ function onRendererPageBegin ( event : MarkdownPageEvent , outputDirectory : string , pagination : boolean ) {
131133 if ( event . frontmatter ) {
132- event . frontmatter [ 'editUrl' ] = false
133- event . frontmatter [ 'next' ] = pagination
134- event . frontmatter [ 'prev' ] = pagination
135- event . frontmatter [ 'title' ] = event . model . name
134+ event . frontmatter = getModelFrontmatter ( event , outputDirectory , {
135+ ...event . frontmatter ,
136+ editUrl : false ,
137+ next : pagination ,
138+ prev : pagination ,
139+ title : event . model . name ,
140+ } )
136141 }
137142}
138143
139144// Returning `true` will delete the page from the filesystem.
140- function onRendererPageEnd ( event : MarkdownPageEvent , pagination : boolean ) {
145+ function onRendererPageEnd ( event : MarkdownPageEvent , outputDirectory : string , pagination : boolean ) {
141146 if ( ! event . contents ) {
142147 return false
143148 } else if ( / ^ .+ [ / \\ ] R E A D M E \. m d $ / . test ( event . url ) ) {
@@ -149,13 +154,16 @@ function onRendererPageEnd(event: MarkdownPageEvent, pagination: boolean) {
149154 }
150155
151156 if ( ! event . frontmatter ) {
152- event . contents = addFrontmatter ( event . contents , {
153- editUrl : false ,
154- next : pagination ,
155- prev : pagination ,
156- // Wrap in quotes to prevent issue with special characters in frontmatter
157- title : `"${ event . model . name } "` ,
158- } )
157+ event . contents = addFrontmatter (
158+ event . contents ,
159+ getModelFrontmatter ( event , outputDirectory , {
160+ editUrl : false ,
161+ next : pagination ,
162+ prev : pagination ,
163+ // Wrap in quotes to prevent issue with special characters in frontmatter
164+ title : `"${ event . model . name } "` ,
165+ } ) ,
166+ )
159167 }
160168
161169 return false
@@ -167,6 +175,20 @@ function onRendererEnd(pagesToRemove: string[]) {
167175 }
168176}
169177
178+ function getModelFrontmatter (
179+ event : MarkdownPageEvent ,
180+ outputDirectory : string ,
181+ frontmatter : NonNullable < MarkdownPageEvent [ 'frontmatter' ] > ,
182+ ) {
183+ const defaultSlug = slug ( event . model . name )
184+
185+ if ( defaultSlug . length === 0 ) {
186+ frontmatter [ 'slug' ] = getRelativeURL ( event . url , outputDirectory , event . url ) . replaceAll ( / ^ \/ | \/ $ / g, '' )
187+ }
188+
189+ return frontmatter
190+ }
191+
170192export class NoReflectionsError extends Error {
171193 constructor ( ) {
172194 super ( 'Failed to generate TypeDoc documentation.' )
0 commit comments