Skip to content

Commit

Permalink
fix: Updated fileoutput for 'packages' entryPointStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed May 5, 2023
1 parent c6a9551 commit 420f19e
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 227 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-taxis-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'docusaurus-plugin-typedoc': patch
---

- Updated fileoutput for 'packages' entryPointStrategy
5 changes: 1 addition & 4 deletions examples/docusaurus/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ const config = {
autoConfiguration: true,
position: 0,
categoryLabel: 'API-1',
indexLabel: 'Overview',
},
indexPageTitle: 'Overview',
cleanOutputDir: true,
groupByKinds: true,
plugin: ['typedoc-plugin-frontmatter'],
frontmatterGlobals: {
pagination_prev: null,
pagination_next: null,
},
},
],
[
Expand Down
135 changes: 69 additions & 66 deletions packages/docusaurus-plugin-typedoc/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# docusaurus-plugin-typedoc

A [Docusaurus v2](https://v2.docusaurus.io/) plugin to build documentation with [TypeDoc](https://github.com/TypeStrong/typedoc).
A [Docusaurus](https://v2.docusaurus.io/) plugin to build TypeScript API documentation with [TypeDoc](https://github.com/TypeStrong/typedoc).

[![npm](https://img.shields.io/npm/v/docusaurus-plugin-typedoc.svg)](https://www.npmjs.com/package/docusaurus-plugin-typedoc)
![CI](https://github.com/tgreyuk/typedoc-plugin-markdown/actions/workflows/ci.yml/badge.svg?branch=master)

## What it does?
## What does it do?

Generates static TypeDoc pages in Markdown with frontmatter as part of the Docusaurus build.
- Presets relevant options of [typedoc-plugin-markdown](https://github.com/tgreyuk/typedoc-plugin-markdown/tree/master/packages/typedoc-plugin-markdown#readme).
- Runs TypeDoc from the Docusaurus CLI.
- Adds some basic frontmatter to pages and exposes the ability to configure frontmatter further.

## Installation

> Install [Docusaurus](https://v2.docusaurus.io/docs/installation) in the root of your project and install the plugin dependencies in the same location as the Docusaurus website directory.
> [typedoc](https://github.com/TypeStrong/typedoc), [typedoc-plugin-markdown](https://github.com/tgreyuk/typedoc-plugin-markdown) and [typedoc-plugin-frontmatter]() are required peer dependencies.
```shell
npm install typedoc typedoc-plugin-markdown docusaurus-plugin-typedoc --save-dev
npm install docusaurus-plugin-typedoc typedoc typedoc-plugin-markdown@next typedoc-plugin-frontmatter@next --save-dev
```

## Usage
Expand Down Expand Up @@ -65,42 +69,65 @@ Once built the docs will be available at `/docs/api` (or equivalent out director

## Options

Options can be declared:

- Passing arguments via the command line.
- Using a `typedoc.json` file.
- Under the `typedocOptions` key in `tsconfig.json`.

Please see https://typedoc.org/options/configuration for general TypeDoc option configuration.

### TypeDoc options

To configure TypeDoc, pass any relevant [TypeDoc options](https://typedoc.org/guides/options/) to the config.
The following TypeDoc / Markdown plugin options can be passed to config:

At a minimum the `entryPoints` and `tsconfig` options will need to be set.
- [typedoc](https://typedoc.org/options) options (HTML specific output options that will be ignored).
- [typedoc-plugin-markdown ](https://github.com/tgreyuk/typedoc-plugin-markdown/tree/next/packages/typedoc-plugin-markdown#options) options (Some options are already preset to target Docusaurus).

```js
entryPoints: ['../src/index.ts'],
tsconfig: '../tsconfig.json'
```
#### Docusaurus presets

Additional TypeDoc plugins will need to be explicitly set:
The following options are preset with the plugin.

```js
plugin: ['typedoc-plugin-xyz'];
```json
{
"out": "api",
"hideInPageTOC": true,
"hideBreadcrumbs": true,
"hidePageHeader": true,
"entryDocument": "index.md",
"numberPrefixOutput": true
}
```

#### Other config options
### Plugin options

TypeDoc options can also be declared:
Options specific to the plugin should also be declared in the same object.

- Using a `typedoc.json` file.
- Under the `typedocOptions` key in `tsconfig.json`.
#### `--sidebar`

> Note: Options declared in this manner will take priority and overwrite options declared in `docusaurus.config.js`.
`sidebar.autoConfiguration`

### Plugin options
Set to `false` to disable sidebar generation. Defaults to `true`.

Options specific to the plugin should also be declared in the same object.
`sidebar.categoryLabel`

The sidebar main parent category label. Defaults to `API`.

`sidebar.indexLabel`

The label of the main index page. Please note if `readme=none` this option has no effect because the index page becomes the root page. Defaults to `Index`.

`sidebar.position`

The position of the sidebar in the tree.

#### `--docsRoot`

| Name | Default | Description |
| :---------------------- | :------ | :------------------------------------------------------- |
| `out` | `"api"` | Output dir relative to docs dir (use `.` for no subdir). |
| `sidebar.categoryLabel` | `API` | The sidebar parent category label. |
| `sidebar.fullNames` | `false` | Display full names with module path. |
| `sidebar.position` | `auto` | The position of the sidebar in the tree. |
The Docusaurus docs folder root. Use `./` if no root folder specified. Defaults to `./docs`.

```shell
--docsRoot <path/to/vitepress-docs/>
```

### An example configuration

Expand Down Expand Up @@ -128,6 +155,21 @@ module.exports = {
};
```

## Frontmatter

Additional frontmatter options can be added to the config. Please see [typedoc-plugin-frontmatter](https://github.com/tgreyuk/typedoc-plugin-frontmatter#typedoc-plugin-frontmatter).

For example:

```json
{
"frontmatterGlobals": {
"pagination_prev": null,
"pagination_next": null
}
}
```

## Recipes

### Sidebar and Navbar
Expand Down Expand Up @@ -190,27 +232,6 @@ A navbar item can be configured in `themeConfig` options in `docusaurus.config.j

Please see https://docusaurus.io/docs/api/themes/configuration#navbar-items for navbar documentation.

### Frontmatter

By default the plugin will configure minimal required [Frontmatter](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-docs#markdown-front-matter) configuration.
Additionally required global Frontmatter options can be passed in using the `frontmatterGlobals` options object

`docusaurus.config.js`:

```js
plugins: [
[
'docusaurus-plugin-typedoc',
{
// .... other plugin option
frontmatterGlobals: {
pagination_prev: null,
pagination_next: null
}
]
]
```
### Multi instance

It is possible to build multi TypeDoc instances by passing in multiple configs with unique ids:
Expand Down Expand Up @@ -272,27 +293,9 @@ module.exports = {
};
```

### Monorepo setup
`docusaurus.config.js`
```js
module.exports = {
plugins: [
[
'docusaurus-plugin-typedoc',
{
entryPoints: ['../packages/package-a', '../packages/package-b'],
entryPointStrategy: 'packages',
sidebar: {
fullNames: true,
},
},
],
],
};
```
## License
[MIT](https://github.com/tgreyuk/typedoc-plugin-markdown/blob/master/packages/docusaurus-plugin-typedoc/LICENSE)
```
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-typedoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"homepage": "https://github.com/tgreyuk/typedoc-plugin-markdown/tree/master/packages/docusaurus-plugin-typedoc",
"peerDependencies": {
"typedoc-plugin-frontmatter": "^0.0.2",
"typedoc-plugin-markdown": ">=4.0.0-next.9"
},
"scripts": {
Expand Down
88 changes: 86 additions & 2 deletions packages/docusaurus-plugin-typedoc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,86 @@
export { default } from './plugin.docusaurus';
export { load } from './plugin.typedoc';
import * as path from 'path';
import {
Application,
TSConfigReader,
TypeDocOptions,
TypeDocReader,
} from 'typedoc';
import { load as loadTypedocPluginFrontmatter } from 'typedoc-plugin-frontmatter';
import { load as loadTypedocPluginMarkdown } from 'typedoc-plugin-markdown';
import { PluginOptions } from './models';
import { getPluginOptions } from './options';
import { loadDocusaurusTypedocPlugin } from './typedoc-plugin';

// store list of plugin ids when running multiple instances
const apps: string[] = [];

export default function pluginDocusaurus(
context: any,
opts: Partial<PluginOptions>,
) {
return {
name: 'docusaurus-plugin-typedoc',
async loadContent() {
if (opts.id && !apps.includes(opts.id)) {
apps.push(opts.id);
generateTypedoc(context, opts);
}
},
extendCli(cli) {
cli
.command('generate-typedoc')
.description(
'(docusaurus-plugin-typedoc) Generate TypeDoc docs independently of the Docusaurus build process.',
)
.action(async () => {
context.siteConfig?.plugins.forEach((pluginConfig) => {
// Check PluginConfig is typed to [string, PluginOptions]
if (pluginConfig && typeof pluginConfig[1] === 'object') {
generateTypedoc(context, pluginConfig[1]);
}
});
});
},
};
}

/**
* Initiates a new typedoc Application bootstraped with plugin options
*/
async function generateTypedoc(context: any, opts: Partial<PluginOptions>) {
const { siteDir } = context;

const options = getPluginOptions(opts);

const { id, docsRoot, ...optionsPassedToTypeDoc } = options;

const outputDir = path.resolve(siteDir, options.docsRoot, options.out);

const app = new Application();

app.options.addReader(new TypeDocReader());
app.options.addReader(new TSConfigReader());

loadTypedocPluginMarkdown(app);
loadTypedocPluginFrontmatter(app);
loadDocusaurusTypedocPlugin(app);

await app.bootstrapWithPlugins(
optionsPassedToTypeDoc as unknown as Partial<TypeDocOptions>,
);

const project = app.convert();

// if project is undefined typedoc has a problem - error logging will be supplied by typedoc.
if (!project) {
return;
}

if (options.watch) {
app.convertAndWatch(async (project) => {
await app.generateDocs(project, outputDir);
});
} else {
await app.generateDocs(project, outputDir);
}
}
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-typedoc/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export interface PluginOptions extends TypedocPluginMarkdownOptions {

export interface SidebarOptions {
categoryLabel: string;
collapsed: boolean;
position: number | null;
autoConfiguration: boolean;
indexLabel: string;
}

export interface SidebarCategory {
Expand Down
10 changes: 9 additions & 1 deletion packages/docusaurus-plugin-typedoc/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ const DEFAULT_PLUGIN_OPTIONS: Partial<PluginOptions> = {
out: 'api',
sidebar: {
categoryLabel: 'API',
collapsed: true,
position: null,
autoConfiguration: true,
indexLabel: 'Index',
},
hideInPageTOC: true,
hideBreadcrumbs: true,
hidePageHeader: true,
entryDocument: 'index.md',
watch: false,
numberPrefixOutput: true,
plugin: [],
};

export const getPluginOptions = (
Expand All @@ -27,6 +29,12 @@ export const getPluginOptions = (
...DEFAULT_PLUGIN_OPTIONS.sidebar,
...opts.sidebar,
},
plugin: opts.plugin?.filter(
(plugin) =>
!['typedoc-plugin-markdown', 'typedoc-plugin-frontmatter'].includes(
plugin,
),
),
};
return options as PluginOptions;
};

0 comments on commit 420f19e

Please sign in to comment.