Skip to content

Commit

Permalink
feat!: bump minimum required typedoc-plugin-markdown version to `4.…
Browse files Browse the repository at this point in the history
…0.0`
  • Loading branch information
HiDeoo committed Jun 25, 2024
1 parent dc38bdb commit cac8ed0
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 73 deletions.
6 changes: 1 addition & 5 deletions docs/src/content/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import { PackageManagers } from '@hideoo/starlight-plugins-docs-components'

1. Starlight TypeDoc is a Starlight [plugin](https://starlight.astro.build/reference/plugins/). Install it and its peer dependencies using your favorite package manager:

<PackageManagers pkg="starlight-typedoc typedoc typedoc-plugin-markdown@next" />

:::note
The Starlight TypeDoc package requires at least the version `4.0.0-next.59` of `typedoc-plugin-markdown`, hence the `@next` tag in the installation command.
:::
<PackageManagers pkg="starlight-typedoc typedoc typedoc-plugin-markdown" />

2. Configure the plugin in your Starlight [configuration](https://starlight.astro.build/reference/configuration/#plugins) in the `astro.config.mjs` file.

Expand Down
4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"astro": "4.11.1",
"sharp": "0.32.6",
"starlight-typedoc": "workspace:*",
"typedoc": "0.25",
"typedoc-plugin-markdown": "4.0.0-next.59",
"typedoc": "0.26.2",
"typedoc-plugin-markdown": "4.1.0",
"typedoc-plugin-mdn-links": "3.0.3"
},
"engines": {
Expand Down
36 changes: 28 additions & 8 deletions packages/starlight-typedoc/libs/typedoc.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import fs from 'node:fs'
import path from 'node:path'

import type { AstroIntegrationLogger } from 'astro'
import {
Application,
type DeclarationReflection,
PageEvent,
TSConfigReader,
type TypeDocOptions,
ParameterType,
type Reflection,
RendererEvent,
} from 'typedoc'
import type { PluginOptions } from 'typedoc-plugin-markdown'

Expand Down Expand Up @@ -76,6 +78,8 @@ async function bootstrapApp(
base: string,
logger: AstroIntegrationLogger,
) {
const pagesToRemove: string[] = []

const app = await Application.bootstrapWithPlugins({
...defaultTypeDocConfig,
...markdownPluginConfig,
Expand All @@ -87,10 +91,15 @@ async function bootstrapApp(
})
app.logger = new StarlightTypeDocLogger(logger)
app.options.addReader(new TSConfigReader())
// @ts-expect-error - Invalid theme typing
app.renderer.defineTheme('starlight-typedoc', StarlightTypeDocTheme)
app.renderer.on(PageEvent.END, (event: PageEvent<DeclarationReflection>) => {
onRendererPageEnd(event, pagination)
app.renderer.on(PageEvent.END, (event: PageEvent<Reflection>) => {
const shouldRemovePage = onRendererPageEnd(event, pagination)
if (shouldRemovePage) {
pagesToRemove.push(event.filename)
}
})
app.renderer.on(RendererEvent.END, () => {
onRendererEnd(pagesToRemove)
})
app.options.addDeclaration({
defaultValue: getStarlightTypeDocOutputDirectory(outputDirectory, base),
Expand All @@ -102,13 +111,16 @@ async function bootstrapApp(
return app
}

function onRendererPageEnd(event: PageEvent<DeclarationReflection>, pagination: boolean) {
// Returning `true` will delete the page from the filesystem.
function onRendererPageEnd(event: PageEvent<Reflection>, pagination: boolean) {
if (!event.contents) {
return
return false
} else if (/^.+[/\\]README\.md$/.test(event.url)) {
// Do not save `README.md` files for multiple entry points.
event.preventDefault()
return
// It is no longer supported in TypeDoc 0.26.0 to call `event.preventDefault()` to prevent the file from being saved.
// https://github.com/TypeStrong/typedoc/commit/6e6b3b662c92b3d4bc24b6c6c0c6e227e063c759
// event.preventDefault()
return true
}

event.contents = addFrontmatter(event.contents, {
Expand All @@ -118,6 +130,14 @@ function onRendererPageEnd(event: PageEvent<DeclarationReflection>, pagination:
// Wrap in quotes to prevent issue with special characters in frontmatter
title: `"${event.model.name}"`,
})

return false
}

function onRendererEnd(pagesToRemove: string[]) {
for (const page of pagesToRemove) {
fs.rmSync(page, { force: true })
}
}

export type TypeDocConfig = Partial<Omit<TypeDocOptions, 'entryPoints' | 'tsconfig'> & PluginOptions>
4 changes: 2 additions & 2 deletions packages/starlight-typedoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"peerDependencies": {
"@astrojs/starlight": ">=0.15.0",
"astro": ">=4.0.0",
"typedoc": ">=0.25.0",
"typedoc-plugin-markdown": ">=4.0.0-next.59"
"typedoc": ">=0.26.0",
"typedoc-plugin-markdown": ">=4.0.0"
},
"engines": {
"node": ">=18.14.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test('should disable edit links', async ({ docPage }) => {
test('should support TypeDoc plugins', async ({ docPage }) => {
await docPage.goto('classes/foo')

const mdnLink = docPage.page.getByRole('link', { exact: true, name: 'HTMLElement ↗️' })
const mdnLink = docPage.page.getByRole('link', { exact: true, name: 'HTMLElement' })

await expect(mdnLink).toBeVisible()

Expand Down
9 changes: 5 additions & 4 deletions packages/starlight-typedoc/tests/unit/typedoc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const starlightTypeDocOptions = {

beforeAll(() => {
vi.spyOn(fs, 'mkdirSync').mockReturnValue(undefined)
vi.spyOn(fs, 'rmSync').mockReturnValue(undefined)
vi.spyOn(fs, 'writeFileSync').mockReturnValue(undefined)
})

Expand Down Expand Up @@ -89,11 +90,11 @@ test('should not add `README.md` module files for multiple entry points', async
entryPoints: ['../../fixtures/basics/src/Bar.ts', '../../fixtures/basics/src/Foo.ts'],
})

const writeFileSyncSpy = vi.mocked(fs.writeFileSync)
const filePaths = writeFileSyncSpy.mock.calls.map((call) => call[0].toString())
const rmSyncSpy = vi.mocked(fs.rmSync)
const filePaths = rmSyncSpy.mock.calls.map((call) => call[0].toString())

expect(writeFileSyncSpy).toHaveBeenCalled()
expect(filePaths.some((filePath) => /\/(?:Bar|Foo)\/README\.md$/.test(filePath))).toBe(false)
expect(rmSyncSpy).toHaveBeenCalled()
expect(filePaths.filter((filePath) => /\/(?:Bar|Foo)\/README\.md$/.test(filePath)).length).toBe(2)
})

test('should support overriding typedoc-plugin-markdown readme page generation', async () => {
Expand Down
116 changes: 65 additions & 51 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cac8ed0

Please sign in to comment.