docusaurus-plugin-typedoc 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -2,4 +2,4 @@
2
2
  * @module core
3
3
  */
4
4
  export { PluginOptions } from './models.js';
5
- export { default } from './plugins/docusaurus.js';
5
+ export { default } from './plugin/docusaurus.js';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { default } from './plugins/docusaurus.js';
1
+ export { default } from './plugin/docusaurus.js';
@@ -1,26 +1,22 @@
1
1
  import { DeclarationOption } from 'typedoc';
2
- /**
3
- * Used internally to pass options from docusaurus.config to TypeDoc.
4
- *
5
- * @internal
6
- *
7
- * @hidden
8
- */
9
- export declare const docusaurusConfigOptions: Partial<DeclarationOption>;
10
2
  /**
11
3
  * **autoConfiguration**
12
4
  *
13
5
  * Set to `false` to disable sidebar generation. Defaults to `true`.
14
6
  *
7
+ * **typescript**
8
+ *
9
+ * Set to `true` to generate a TypeScript file. Defaults to `false` (CommonJs).
10
+ *
15
11
  * **pretty**
16
12
  *
17
- * Pretty format the sidebar JSON.
13
+ * Pretty format the sidebar JSON. Defaults to `false`.
18
14
  *
19
15
  * **deprecatedItemClassName**
20
16
  *
21
17
  * The class name to apply to deprecated items in the sidebar. Defaults to `"typedoc-sidebar-item-deprecated"`.
22
18
  *
23
- * Please see the [sidebar guide](https:/typedoc-plugin-markdown.org/plugins/docusaurus/sidebar) for additional information on sidebar setup.
19
+ * Please see the [sidebar guide](/plugins/docusaurus/guides/sidebar) for additional information on sidebar setup.
24
20
  *
25
21
  */
26
22
  export declare const sidebar: Partial<DeclarationOption>;
@@ -1,31 +1,23 @@
1
1
  import { ParameterType } from 'typedoc';
2
2
  import { DEFAULT_SIDEBAR_OPTIONS } from './options.js';
3
- /**
4
- * Used internally to pass options from docusaurus.config to TypeDoc.
5
- *
6
- * @internal
7
- *
8
- * @hidden
9
- */
10
- export const docusaurusConfigOptions = {
11
- help: 'docusaurus.config options - should not be used if running as a docusaurus plugin.',
12
- type: ParameterType.String,
13
- defaultValue: '{}',
14
- };
15
3
  /**
16
4
  * **autoConfiguration**
17
5
  *
18
6
  * Set to `false` to disable sidebar generation. Defaults to `true`.
19
7
  *
8
+ * **typescript**
9
+ *
10
+ * Set to `true` to generate a TypeScript file. Defaults to `false` (CommonJs).
11
+ *
20
12
  * **pretty**
21
13
  *
22
- * Pretty format the sidebar JSON.
14
+ * Pretty format the sidebar JSON. Defaults to `false`.
23
15
  *
24
16
  * **deprecatedItemClassName**
25
17
  *
26
18
  * The class name to apply to deprecated items in the sidebar. Defaults to `"typedoc-sidebar-item-deprecated"`.
27
19
  *
28
- * Please see the [sidebar guide](https:/typedoc-plugin-markdown.org/plugins/docusaurus/sidebar) for additional information on sidebar setup.
20
+ * Please see the [sidebar guide](/plugins/docusaurus/guides/sidebar) for additional information on sidebar setup.
29
21
  *
30
22
  */
31
23
  export const sidebar = {
@@ -1,6 +1,7 @@
1
1
  export declare const DEFAULT_SIDEBAR_OPTIONS: {
2
2
  autoConfiguration: boolean;
3
3
  pretty: boolean;
4
+ typescript: boolean;
4
5
  deprecatedItemClassName: string;
5
6
  };
6
7
  export declare function getPluginOptions(context: any, opts: Record<string, any>): Record<string, any>;
@@ -2,6 +2,7 @@ import { presets } from './presets.js';
2
2
  export const DEFAULT_SIDEBAR_OPTIONS = {
3
3
  autoConfiguration: true,
4
4
  pretty: false,
5
+ typescript: false,
5
6
  deprecatedItemClassName: 'typedoc-sidebar-item-deprecated',
6
7
  };
7
8
  const DEFAULT_PLUGIN_OPTIONS = {
@@ -26,10 +27,7 @@ export function getPluginOptions(context, opts) {
26
27
  ...opts.sidebar,
27
28
  },
28
29
  plugin: [
29
- ...new Set([
30
- ...['typedoc-plugin-markdown', 'docusaurus-plugin-typedoc/typedoc'],
31
- ...(opts.plugin || []),
32
- ]),
30
+ ...new Set([...['typedoc-plugin-markdown'], ...(opts.plugin || [])]),
33
31
  ],
34
32
  };
35
33
  return options;
@@ -1,8 +1,6 @@
1
- import { spawnSync } from 'child_process';
2
1
  import * as fs from 'fs';
3
- import * as path from 'path';
4
2
  import { getPluginOptions } from '../options/options.js';
5
- import { presets } from '../options/presets.js';
3
+ import { writeSidebar } from './sidebar.js';
6
4
  export default async function pluginDocusaurus(context, opts) {
7
5
  await generateTypedoc(context, opts);
8
6
  return {
@@ -26,19 +24,19 @@ export default async function pluginDocusaurus(context, opts) {
26
24
  * Initiates a new typedoc Application bootstrapped with plugin options
27
25
  */
28
26
  async function generateTypedoc(context, opts) {
27
+ // get plugin options
28
+ const options = getPluginOptions(context, opts);
29
29
  // create outDir if it doesn't exist
30
- const outputDir = path.join(context?.siteDir, presets.out);
31
- if (!fs.existsSync(outputDir)) {
32
- fs.mkdirSync(outputDir, { recursive: true });
30
+ if (!fs.existsSync(options.out)) {
31
+ fs.mkdirSync(options.out, { recursive: true });
33
32
  }
34
- const { plugin, ...options } = getPluginOptions(context, opts);
35
- // spawn typedoc process and pass docusaurus.config options as a string
36
- const typedocExecutable = process.platform === 'win32' ? 'typedoc.cmd' : 'typedoc';
37
- spawnSync(typedocExecutable, [
38
- ...plugin.flatMap((plugin) => ['--plugin', plugin]),
39
- '--docusaurusConfigOptions',
40
- `${JSON.stringify(options)}`,
41
- ], {
42
- stdio: 'inherit',
33
+ // configure options for typedoc
34
+ const {
35
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
36
+ id, siteDir, numberPrefixParser, docsPresetPath, sidebar, ...optionsPassedToTypeDoc } = options;
37
+ const typeDocApp = await import('./typedoc.cjs');
38
+ // bootstrap typedoc with options
39
+ await typeDocApp.bootstrap(optionsPassedToTypeDoc, async (renderer) => {
40
+ writeSidebar(renderer.navigation, renderer.outputDirectory, sidebar, siteDir, docsPresetPath, numberPrefixParser);
43
41
  });
44
42
  }
@@ -0,0 +1,3 @@
1
+ import { NavigationItem } from 'typedoc-plugin-markdown';
2
+ import { Sidebar } from '../types/options.js';
3
+ export declare function writeSidebar(navigation: NavigationItem[], outputDir: string, sidebar: Sidebar, siteDir: string, docsPresetPath: string, numberPrefixParser: any): void;
@@ -0,0 +1,83 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { adjustBaseDirectory } from '../utils/adjust-basedir.js';
4
+ export function writeSidebar(navigation, outputDir, sidebar, siteDir, docsPresetPath, numberPrefixParser) {
5
+ if (sidebar?.autoConfiguration) {
6
+ const sidebarFileName = sidebar.typescript
7
+ ? 'typedoc-sidebar.ts'
8
+ : 'typedoc-sidebar.cjs';
9
+ const sidebarPath = path.resolve(outputDir, sidebarFileName);
10
+ let baseDir = path
11
+ .relative(siteDir, outputDir)
12
+ .split(path.sep)
13
+ .slice(1)
14
+ .join('/');
15
+ if (docsPresetPath) {
16
+ baseDir = adjustBaseDirectory(baseDir, docsPresetPath);
17
+ }
18
+ const sidebarJson = getSidebar(navigation, baseDir, sidebar, numberPrefixParser);
19
+ const sidebarContent = sidebar.typescript
20
+ ? getTypescriptSidebar(sidebarJson, sidebar)
21
+ : getJsSidebar(sidebarJson, sidebar);
22
+ fs.writeFileSync(sidebarPath, sidebarContent);
23
+ }
24
+ }
25
+ function getTypescriptSidebar(sidebarJson, sidebar) {
26
+ return `import { SidebarsConfig } from '@docusaurus/plugin-content-docs';
27
+ const typedocSidebar: SidebarsConfig = { items: ${JSON.stringify(sidebarJson, null, sidebar.pretty ? 2 : 0)}};
28
+ export default typedocSidebar;`;
29
+ }
30
+ function getJsSidebar(sidebarJson, sidebar) {
31
+ return `// @ts-check
32
+ /** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
33
+ const typedocSidebar = { items: ${JSON.stringify(sidebarJson, null, sidebar.pretty ? 2 : 0)}};
34
+ module.exports = typedocSidebar.items;`;
35
+ }
36
+ function getSidebar(navigation, basePath, options, numberPrefixParser) {
37
+ return navigation
38
+ .map((navigationItem) => getNavigationItem(navigationItem, basePath, options, numberPrefixParser))
39
+ .filter((navItem) => Boolean(navItem));
40
+ }
41
+ function getNavigationItem(navigationItem, basePath, options, numberPrefixParser) {
42
+ const navigationItemPath = navigationItem.path || navigationItem.url;
43
+ const parsedUrl = numberPrefixParser === false
44
+ ? navigationItemPath
45
+ : navigationItemPath?.replace(/\d+-/g, '');
46
+ const getId = () => {
47
+ const idParts = [];
48
+ if (basePath.length > 0) {
49
+ idParts.push(basePath);
50
+ }
51
+ if (parsedUrl) {
52
+ idParts.push(parsedUrl.replace(/\\/g, '/'));
53
+ }
54
+ if (navigationItemPath) {
55
+ return idParts.join('/').replace(/(.*)\.\w+$/, '$1');
56
+ }
57
+ return null;
58
+ };
59
+ const id = getId();
60
+ if (navigationItem.children?.length) {
61
+ return {
62
+ type: 'category',
63
+ label: navigationItem.title,
64
+ items: getSidebar(navigationItem.children, basePath, options, numberPrefixParser),
65
+ ...(id && {
66
+ link: {
67
+ type: 'doc',
68
+ id,
69
+ },
70
+ }),
71
+ };
72
+ }
73
+ return id
74
+ ? {
75
+ type: 'doc',
76
+ id,
77
+ label: navigationItem.title,
78
+ ...(navigationItem.isDeprecated && {
79
+ className: options.deprecatedItemClassName,
80
+ }),
81
+ }
82
+ : null;
83
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Export as cjs to be compatible with esm
3
+ */
4
+ module.exports = {
5
+ bootstrap: async (options, postRenderCallbackFn) => {
6
+ const typedoc = await import('typedoc');
7
+
8
+ const app = await typedoc.Application.bootstrapWithPlugins(options, [
9
+ new typedoc.TypeDocReader(),
10
+ new typedoc.PackageJsonReader(),
11
+ new typedoc.TSConfigReader(),
12
+ ]);
13
+
14
+ app.renderer.postRenderAsyncJobs.push(postRenderCallbackFn);
15
+
16
+ const project = await app.convert();
17
+
18
+ // if project is undefined typedoc has a problem - error logging will be supplied by typedoc.
19
+ if (!project) {
20
+ return;
21
+ }
22
+
23
+ if (options.watch) {
24
+ app.convertAndWatch(async (project) => {
25
+ await app.generateOutputs(project);
26
+ });
27
+ } else {
28
+ await app.generateOutputs(project);
29
+ }
30
+ },
31
+ };
@@ -2,17 +2,14 @@
2
2
  * Describes the options declared by the plugin.
3
3
  */
4
4
  export interface PluginOptions {
5
- /**
6
- * docusaurus.config options - should not be used if running as a docusaurus plugin.
7
- */
8
- docusaurusConfigOptions: string;
9
5
  /**
10
6
  * Configures the autogenerated Docusaurus sidebar.
11
7
  */
12
- sidebar: Sidebar;
8
+ sidebar?: Sidebar;
13
9
  }
14
10
  export interface Sidebar {
15
11
  autoConfiguration: boolean;
16
12
  pretty: boolean;
13
+ typescript: boolean;
17
14
  deprecatedItemClassName: string;
18
15
  }
@@ -1 +1,4 @@
1
+ /*
2
+ * THIS FILE IS AUTO GENERATED FROM THE OPTIONS CONFIG. DO NOT EDIT DIRECTLY
3
+ */
1
4
  export {};
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "docusaurus-plugin-typedoc",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "description": "A Docusaurus plugin to integrate TypeDoc ( + typedoc-plugin-markdown ) into a Docusaurus project.",
5
5
  "exports": {
6
- ".": "./dist/index.js",
7
- "./typedoc": "./dist/plugins/typedoc.js"
6
+ ".": "./dist/index.js"
8
7
  },
9
8
  "type": "module",
10
9
  "files": [
@@ -20,11 +19,11 @@
20
19
  },
21
20
  "homepage": "http://typedoc-plugin-markdown.org/plugins/docusaurus",
22
21
  "peerDependencies": {
23
- "typedoc-plugin-markdown": ">=4.4.0"
22
+ "typedoc-plugin-markdown": ">=4.6.0"
24
23
  },
25
24
  "scripts": {
26
25
  "lint": "eslint ./src",
27
- "prebuild": "rm -rf dist && prebuild-options",
26
+ "prebuild": "rm -rf dist && prebuild-options && copyfiles --up 1 ./src/**/*.cjs ./dist/",
28
27
  "prepublishOnly": "npm run lint && npm run build",
29
28
  "build": "tsc",
30
29
  "pretest": "rm -rf ./test/out && docusaurus generate-typedoc",
@@ -39,7 +38,7 @@
39
38
  "plugin"
40
39
  ],
41
40
  "devDependencies": {
42
- "@docusaurus/core": "^3.6.1",
43
- "@docusaurus/types": "^3.6.1"
41
+ "@docusaurus/core": "^3.7.0",
42
+ "@docusaurus/types": "^3.7.0"
44
43
  }
45
44
  }
@@ -1,2 +0,0 @@
1
- import { MarkdownApplication } from 'typedoc-plugin-markdown';
2
- export declare function load(app: MarkdownApplication): void;
@@ -1,56 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as path from 'path';
3
- import * as options from '../options/declarations.js';
4
- import { adjustBaseDirectory } from '../utils/adjust-basedir.js';
5
- import { getSidebar } from '../utils/get-sidebar.js';
6
- export function load(app) {
7
- Object.entries(options).forEach(([name, option]) => {
8
- app.options.addDeclaration({
9
- name,
10
- ...option,
11
- });
12
- });
13
- app.options.addReader(new (class {
14
- name = 'docusaurus-options';
15
- order = 100;
16
- supportsPackages = false;
17
- read(container) {
18
- const presets = JSON.parse(container.getValue('docusaurusConfigOptions'));
19
- const ignoreKeys = [
20
- 'id',
21
- 'siteDir',
22
- 'docsPresetPath',
23
- 'numberPrefixParser',
24
- ];
25
- Object.entries(presets)
26
- .filter(([key]) => !ignoreKeys.includes(key))
27
- .forEach(([key, value]) => {
28
- container.setValue(key, value);
29
- });
30
- }
31
- })());
32
- app.renderer.postRenderAsyncJobs.push(async (output) => {
33
- const outputDir = app.options.getValue('out');
34
- const docusaurusConfigOptions = JSON.parse(app.options.getValue('docusaurusConfigOptions'));
35
- const sidebar = app.options.getValue('sidebar');
36
- const siteDir = docusaurusConfigOptions.siteDir;
37
- const docsPresetPath = docusaurusConfigOptions.docsPresetPath;
38
- const numberPrefixParser = docusaurusConfigOptions.numberPrefixParser;
39
- if (sidebar?.autoConfiguration && output.navigation) {
40
- const sidebarPath = path.resolve(outputDir, 'typedoc-sidebar.cjs');
41
- let baseDir = path
42
- .relative(siteDir, outputDir)
43
- .split(path.sep)
44
- .slice(1)
45
- .join('/');
46
- if (docsPresetPath) {
47
- baseDir = adjustBaseDirectory(baseDir, docsPresetPath);
48
- }
49
- const sidebarJson = getSidebar(output.navigation, baseDir, sidebar, numberPrefixParser);
50
- fs.writeFileSync(sidebarPath, `// @ts-check
51
- /** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
52
- const typedocSidebar = { items: ${JSON.stringify(sidebarJson, null, sidebar.pretty ? 2 : 0)}};
53
- module.exports = typedocSidebar.items;`);
54
- }
55
- });
56
- }
@@ -1,3 +0,0 @@
1
- import { NavigationItem } from 'typedoc-plugin-markdown';
2
- import { Sidebar } from '../types/options.js';
3
- export declare function getSidebar(navigation: NavigationItem[], basePath: string, options: Sidebar, numberPrefixParser?: any): any;
@@ -1,48 +0,0 @@
1
- export function getSidebar(navigation, basePath, options, numberPrefixParser) {
2
- return navigation
3
- .map((navigationItem) => getNavigationItem(navigationItem, basePath, options, numberPrefixParser))
4
- .filter((navItem) => Boolean(navItem));
5
- }
6
- function getNavigationItem(navigationItem, basePath, options, numberPrefixParser) {
7
- const navigationItemPath = navigationItem.path || navigationItem.url;
8
- const parsedUrl = numberPrefixParser === false
9
- ? navigationItemPath
10
- : navigationItemPath?.replace(/\d+-/g, '');
11
- const getId = () => {
12
- const idParts = [];
13
- if (basePath.length > 0) {
14
- idParts.push(basePath);
15
- }
16
- if (parsedUrl) {
17
- idParts.push(parsedUrl.replace(/\\/g, '/'));
18
- }
19
- if (navigationItemPath) {
20
- return idParts.join('/').replace(/(.*)\.\w+$/, '$1');
21
- }
22
- return null;
23
- };
24
- const id = getId();
25
- if (navigationItem.children?.length) {
26
- return {
27
- type: 'category',
28
- label: navigationItem.title,
29
- items: getSidebar(navigationItem.children, basePath, options, numberPrefixParser),
30
- ...(id && {
31
- link: {
32
- type: 'doc',
33
- id,
34
- },
35
- }),
36
- };
37
- }
38
- return id
39
- ? {
40
- type: 'doc',
41
- id,
42
- label: navigationItem.title,
43
- ...(navigationItem.isDeprecated && {
44
- className: options.deprecatedItemClassName,
45
- }),
46
- }
47
- : null;
48
- }
File without changes