Skip to content

Commit

Permalink
Merge pull request #359 from lorensr/collapsed
Browse files Browse the repository at this point in the history
feat: Add option `sidebar.collapsed`
  • Loading branch information
tgreyuk committed Dec 1, 2022
2 parents 670df31 + 3348ab3 commit 2748562
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/docusaurus-plugin-typedoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand All @@ -121,6 +122,7 @@ module.exports = {
out: 'api-xyz',
sidebar: {
categoryLabel: 'API XYZ',
collapsed: false,
position: 0,
fullNames: true,
},
Expand Down Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-typedoc/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const DEFAULT_PLUGIN_OPTIONS: PluginOptions = {
sidebar: {
fullNames: false,
categoryLabel: 'API',
collapsed: true,
indexLabel: undefined,
readmeLabel: 'Readme',
position: null,
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus-plugin-typedoc/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -93,6 +94,7 @@ export class DocusaurusTheme extends MarkdownTheme {
renderer.outputDirectory + '/' + mapping.directory,
getKindPlural(kind),
CATEGORY_POSITION[kind],
true,
);
}
},
Expand Down Expand Up @@ -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'));
}
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-typedoc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type FrontMatter =
export interface SidebarOptions {
fullNames?: boolean;
categoryLabel: string;
collapsed: boolean;
indexLabel?: string;
readmeLabel?: string;
position: number | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -40,6 +41,7 @@ Object {
"sidebar": Object {
"autoConfiguration": true,
"categoryLabel": "API",
"collapsed": true,
"fullNames": false,
"indexLabel": undefined,
"position": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\\"
Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-plugin-typedoc/test/specs/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
});
});
});

0 comments on commit 2748562

Please sign in to comment.