From 14050b29d3a91d9360f45ebe1317f1a71d184896 Mon Sep 17 00:00:00 2001 From: Hendrik Liebau Date: Fri, 15 Mar 2019 23:02:41 +0100 Subject: [PATCH] Add option to include modules in the top level toc Together with typedoc-plugin-monorepo this enables monorepos to generate combined API docs for their packages using "external modules". --- src/lib/output/plugins/TocPlugin.ts | 26 ++++++++++++++++++-------- src/lib/output/renderer.ts | 7 +++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/lib/output/plugins/TocPlugin.ts b/src/lib/output/plugins/TocPlugin.ts index becc4c84b..742dce00c 100644 --- a/src/lib/output/plugins/TocPlugin.ts +++ b/src/lib/output/plugins/TocPlugin.ts @@ -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'] || []; @@ -67,7 +77,7 @@ export class TocPlugin extends RendererComponent { return; } - if (child.kindOf(ReflectionKind.SomeModule)) { + if (child.kindOf(ReflectionKind.SomeModule) && !includeModules) { return; } diff --git a/src/lib/output/renderer.ts b/src/lib/output/renderer.ts index df6d5da29..1e1a88767 100644 --- a/src/lib/output/renderer.ts +++ b/src/lib/output/renderer.ts @@ -110,6 +110,13 @@ export class Renderer extends ChildableComponent }) 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. *