Skip to content

Commit

Permalink
Add option to include modules in the top level toc
Browse files Browse the repository at this point in the history
Together with typedoc-plugin-monorepo this enables monorepos to generate
combined API docs for their packages using "external modules".
  • Loading branch information
unstubbable committed Mar 15, 2019
1 parent cdf3acd commit 14050b2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/lib/output/plugins/TocPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,35 @@ export class TocPlugin extends RendererComponent {
}

const trail: Reflection[] = [];
while (!(model instanceof ProjectReflection) && !model.kindOf(ReflectionKind.SomeModule)) {
while (this.shouldBeIncludedInTrail(model)) {
trail.unshift(model);
model = model.parent;
}

const tocRestriction = this.owner.toc;
const tocIncludeModules = this.owner.tocIncludeModules;
page.toc = new NavigationItem();
TocPlugin.buildToc(model, trail, page.toc, tocRestriction);
TocPlugin.buildToc(model, trail, page.toc, tocRestriction, tocIncludeModules);
}

private shouldBeIncludedInTrail(model: any): boolean {
if (model instanceof ProjectReflection) {
return false;
}

return !model.kindOf(ReflectionKind.SomeModule) || this.owner.tocIncludeModules;
}

/**
* Create a toc navigation item structure.
*
* @param model The models whose children should be written to the toc.
* @param trail Defines the active trail of expanded toc entries.
* @param parent The parent [[NavigationItem]] the toc should be appended to.
* @param restriction The restricted table of contents.
* @param model The models whose children should be written to the toc.
* @param trail Defines the active trail of expanded toc entries.
* @param parent The parent [[NavigationItem]] the toc should be appended to.
* @param restriction The restricted top level toc entries.
* @param includeModules Include modules in the top level toc.
*/
static buildToc(model: Reflection, trail: Reflection[], parent: NavigationItem, restriction?: string[]) {
static buildToc(model: Reflection, trail: Reflection[], parent: NavigationItem, restriction?: string[], includeModules?: boolean) {
const index = trail.indexOf(model);
const children = model['children'] || [];

Expand All @@ -67,7 +77,7 @@ export class TocPlugin extends RendererComponent {
return;
}

if (child.kindOf(ReflectionKind.SomeModule)) {
if (child.kindOf(ReflectionKind.SomeModule) && !includeModules) {
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib/output/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ export class Renderer extends ChildableComponent<Application, RendererComponent>
})
toc!: string[];

@Option({
name: 'tocIncludeModules',
help: 'Include modules in the top level table of contents.',
type: ParameterType.Boolean
})
tocIncludeModules!: boolean;

/**
* Create a new Renderer instance.
*
Expand Down

0 comments on commit 14050b2

Please sign in to comment.