From 3348ab3501241532478972c964e02471ad21e178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loren=20=F0=9F=A4=93?= Date: Fri, 14 Oct 2022 16:58:21 -0400 Subject: [PATCH] feat: Add option `sidebar.collapsed` --- packages/docusaurus-plugin-typedoc/README.md | 4 +++- packages/docusaurus-plugin-typedoc/src/options.ts | 1 + packages/docusaurus-plugin-typedoc/src/theme.ts | 6 ++++++ packages/docusaurus-plugin-typedoc/src/types.ts | 1 + .../test/specs/__snapshots__/options.spec.ts.snap | 2 ++ .../test/specs/__snapshots__/plugin.spec.ts.snap | 5 +++++ .../docusaurus-plugin-typedoc/test/specs/plugin.spec.ts | 6 +++++- 7 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-plugin-typedoc/README.md b/packages/docusaurus-plugin-typedoc/README.md index 0f1ce8854..5cfc218f1 100644 --- a/packages/docusaurus-plugin-typedoc/README.md +++ b/packages/docusaurus-plugin-typedoc/README.md @@ -101,6 +101,7 @@ Options specific to the plugin should also be declared in the same object. | `includeExtension` | `true` | Determines whether to preserve the `.md` extension in relative links. `true` is recommended as per [Docusaurus documentation](https://docusaurus.io/docs/docs-markdown-features#referencing-other-documents) | | `frontmatter` | `null` | Additional frontmatter options object. See [Frontmatter](#frontmatter). | | `sidebar.categoryLabel` | `API` | The sidebar parent category label. | +| `sidebar.collapsed` | `true` | Whether the parent category is initially collapsed | | `sidebar.fullNames` | `false` | Display full names with module path. | | `sidebar.position` | `auto` | The position of the sidebar in the tree. | @@ -121,6 +122,7 @@ module.exports = { out: 'api-xyz', sidebar: { categoryLabel: 'API XYZ', + collapsed: false, position: 0, fullNames: true, }, @@ -153,7 +155,7 @@ module.exports = { 2. Alternatively, if you wish to manually control other parts of your sidebar you can use a slice for the TypeDoc sidebar. -> note: `sidebar.categoryLabel` and `sidebar.position` options are ignored with this implementation) +> note: `sidebar.categoryLabel`, `sidebar.collapsed`, and `sidebar.position` options are ignored with this implementation) ```js module.exports = { diff --git a/packages/docusaurus-plugin-typedoc/src/options.ts b/packages/docusaurus-plugin-typedoc/src/options.ts index 3d357cd33..cda272ab2 100644 --- a/packages/docusaurus-plugin-typedoc/src/options.ts +++ b/packages/docusaurus-plugin-typedoc/src/options.ts @@ -8,6 +8,7 @@ const DEFAULT_PLUGIN_OPTIONS: PluginOptions = { sidebar: { fullNames: false, categoryLabel: 'API', + collapsed: true, indexLabel: undefined, readmeLabel: 'Readme', position: null, diff --git a/packages/docusaurus-plugin-typedoc/src/theme.ts b/packages/docusaurus-plugin-typedoc/src/theme.ts index 419eef5a4..846430c43 100644 --- a/packages/docusaurus-plugin-typedoc/src/theme.ts +++ b/packages/docusaurus-plugin-typedoc/src/theme.ts @@ -80,6 +80,7 @@ export class DocusaurusTheme extends MarkdownTheme { renderer.outputDirectory, this.sidebar.categoryLabel, this.sidebar.position, + this.sidebar.collapsed, ); Object.keys(groupUrlsByKind(this.getUrls(renderer.project))).forEach( @@ -93,6 +94,7 @@ export class DocusaurusTheme extends MarkdownTheme { renderer.outputDirectory + '/' + mapping.directory, getKindPlural(kind), CATEGORY_POSITION[kind], + true, ); } }, @@ -198,11 +200,15 @@ const writeCategoryYaml = ( categoryPath: string, label: string, position: number | null, + collapsed: boolean, ) => { const yaml: string[] = [`label: "${label}"`]; if (position !== null) { yaml.push(`position: ${position}`); } + if (!collapsed) { + yaml.push(`collapsed: false`); + } if (fs.existsSync(categoryPath)) { fs.writeFileSync(categoryPath + '/_category_.yml', yaml.join('\n')); } diff --git a/packages/docusaurus-plugin-typedoc/src/types.ts b/packages/docusaurus-plugin-typedoc/src/types.ts index 734545a93..7406eed85 100644 --- a/packages/docusaurus-plugin-typedoc/src/types.ts +++ b/packages/docusaurus-plugin-typedoc/src/types.ts @@ -29,6 +29,7 @@ export type FrontMatter = export interface SidebarOptions { fullNames?: boolean; categoryLabel: string; + collapsed: boolean; indexLabel?: string; readmeLabel?: string; position: number | null; diff --git a/packages/docusaurus-plugin-typedoc/test/specs/__snapshots__/options.spec.ts.snap b/packages/docusaurus-plugin-typedoc/test/specs/__snapshots__/options.spec.ts.snap index c369b7a9c..e11436b41 100644 --- a/packages/docusaurus-plugin-typedoc/test/specs/__snapshots__/options.spec.ts.snap +++ b/packages/docusaurus-plugin-typedoc/test/specs/__snapshots__/options.spec.ts.snap @@ -4,6 +4,7 @@ exports[`Options: should merge custom sidebar options 1`] = ` Object { "autoConfiguration": true, "categoryLabel": "API", + "collapsed": true, "fullNames": true, "indexLabel": "Custom index", "position": null, @@ -40,6 +41,7 @@ Object { "sidebar": Object { "autoConfiguration": true, "categoryLabel": "API", + "collapsed": true, "fullNames": false, "indexLabel": undefined, "position": null, diff --git a/packages/docusaurus-plugin-typedoc/test/specs/__snapshots__/plugin.spec.ts.snap b/packages/docusaurus-plugin-typedoc/test/specs/__snapshots__/plugin.spec.ts.snap index 35886eba8..b6c70c365 100644 --- a/packages/docusaurus-plugin-typedoc/test/specs/__snapshots__/plugin.spec.ts.snap +++ b/packages/docusaurus-plugin-typedoc/test/specs/__snapshots__/plugin.spec.ts.snap @@ -17,6 +17,11 @@ exports[`Plugin: (docs) should write category yaml 1`] = ` position: 3" `; +exports[`Plugin: (docs) should write category yaml 2`] = ` +"label: \\"API\\" +collapsed: false" +`; + exports[`Plugin: (docs) should write doc 1`] = ` "--- id: \\"index\\" diff --git a/packages/docusaurus-plugin-typedoc/test/specs/plugin.spec.ts b/packages/docusaurus-plugin-typedoc/test/specs/plugin.spec.ts index b9fe68130..a1459cf13 100644 --- a/packages/docusaurus-plugin-typedoc/test/specs/plugin.spec.ts +++ b/packages/docusaurus-plugin-typedoc/test/specs/plugin.spec.ts @@ -31,7 +31,7 @@ describe(`Plugin:`, () => { let tmpobj; beforeAll(async () => { tmpobj = tmp.dirSync(); - await bootstrap(tmpobj); + await bootstrap(tmpobj, { sidebar: { collapsed: false } }); }); test(`should render`, () => { const files = fs.readdirSync(tmpobj.name + '/docs/api'); @@ -48,6 +48,10 @@ describe(`Plugin:`, () => { tmpobj.name + '/docs/api/classes/_category_.yml', ); expect(sidebar.toString()).toMatchSnapshot(); + const parentSidebar = fs.readFileSync( + tmpobj.name + '/docs/api/_category_.yml', + ); + expect(parentSidebar.toString()).toMatchSnapshot(); }); }); });