Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option sidebar.collapsed #359

Merged
merged 1 commit into from Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/docusaurus-plugin-typedoc/README.md
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
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
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
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
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
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
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();
});
});
});