/
plugin.ts
82 lines (69 loc) · 2.12 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { LoadContext } from '@docusaurus/types';
import * as path from 'path';
import { Application } from 'typedoc';
import { load } from 'typedoc-plugin-markdown';
import { getPluginOptions } from './options';
import { bootstrap, removeDir } from './render';
import { DocusaurusTheme } from './theme';
import { PluginOptions } from './types';
// store list of plugin ids when running multiple instances
const apps: string[] = [];
export default function pluginDocusaurus(
context: LoadContext,
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: LoadContext,
opts: Partial<PluginOptions>,
) {
const { siteDir } = context;
const options = getPluginOptions(opts);
const outputDir = path.resolve(siteDir, options.docsRoot, options.out);
if (opts.cleanOutputDir) {
removeDir(outputDir);
}
const app = new Application();
app.renderer.defineTheme('docusaurus', DocusaurusTheme);
load(app);
bootstrap(app, options);
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);
}
}