From 5f453c84cbd70714e96144ba923045ff81f1955d Mon Sep 17 00:00:00 2001 From: tgreyuk Date: Sun, 17 Dec 2023 23:29:20 +0000 Subject: [PATCH] fix: Packages output filename fixes --- .changeset/nasty-swans-occur.md | 5 ++ package-lock.json | 2 +- .../src/theme/resources/partials/header.ts | 2 +- .../src/theme/urls-context.ts | 77 +++++++++++-------- .../test/__utils__/fixture-config.ts | 15 ++++ .../__snapshots__/navigation.spec.ts.snap | 40 +++++++++- .../specs/__snapshots__/urls.spec.ts.snap | 27 +++++++ .../test/specs/urls.spec.ts | 7 ++ stubs/src/packages/package-3/index.ts | 1 - stubs/src/packages/package-3/src/index.ts | 1 + stubs/src/packages/package-3/src/module-1.ts | 1 + stubs/src/packages/package-3/src/module-2.ts | 1 + stubs/src/packages/package-3/typedoc.json | 2 +- 13 files changed, 141 insertions(+), 40 deletions(-) create mode 100644 .changeset/nasty-swans-occur.md create mode 100644 stubs/src/packages/package-3/src/index.ts create mode 100644 stubs/src/packages/package-3/src/module-1.ts create mode 100644 stubs/src/packages/package-3/src/module-2.ts diff --git a/.changeset/nasty-swans-occur.md b/.changeset/nasty-swans-occur.md new file mode 100644 index 000000000..b52ec4594 --- /dev/null +++ b/.changeset/nasty-swans-occur.md @@ -0,0 +1,5 @@ +--- +'typedoc-plugin-markdown': patch +--- + +- Packages output file fixes diff --git a/package-lock.json b/package-lock.json index a2fe2d432..e4eab2814 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12587,7 +12587,7 @@ } }, "packages/typedoc-plugin-markdown": { - "version": "4.0.0-next.36", + "version": "4.0.0-next.37", "license": "MIT", "peerDependencies": { "typedoc": "0.25.x" diff --git a/packages/typedoc-plugin-markdown/src/theme/resources/partials/header.ts b/packages/typedoc-plugin-markdown/src/theme/resources/partials/header.ts index 2c2b1011e..ba5561798 100644 --- a/packages/typedoc-plugin-markdown/src/theme/resources/partials/header.ts +++ b/packages/typedoc-plugin-markdown/src/theme/resources/partials/header.ts @@ -18,7 +18,7 @@ export function header( ): string { const isPackages = context.options.getValue('entryPointStrategy') === - EntryPointStrategy.Packages && !Boolean(page.project.groups); + EntryPointStrategy.Packages; if (isPackages) { const packageItem = findPackage(page.model); if (packageItem) { diff --git a/packages/typedoc-plugin-markdown/src/theme/urls-context.ts b/packages/typedoc-plugin-markdown/src/theme/urls-context.ts index d88ca061c..17f44c4db 100644 --- a/packages/typedoc-plugin-markdown/src/theme/urls-context.ts +++ b/packages/typedoc-plugin-markdown/src/theme/urls-context.ts @@ -82,45 +82,54 @@ export class UrlsContext { } if (isPackages) { - this.project.children?.forEach((projectChild) => { - const preservePackageReadme = - Boolean(projectChild.readme) && !this.options.getValue('mergeReadme'); - - const packagesIndex = getIndexFileName(projectChild); - const packageMeta = (this.renderer as any).packages[projectChild.name]; - - const outputFileStrategy = - packageMeta.outputFileStrategy || - this.options.getValue('outputFileStrategy'); - - const url = `${projectChild.name}/${ - preservePackageReadme ? packagesIndex : entryFileName - }`; - - if (preservePackageReadme) { - this.urls.push( - new UrlMapping( - `${path.dirname(url)}/${entryFileName}`, - projectChild as any, - this.theme.readmeTemplate, - ), - ); - } - - this.urls.push( - new UrlMapping(url, projectChild as any, this.theme.projectTemplate), - ); - - projectChild.url = url; - - this.buildUrlsFromProject(projectChild, url, outputFileStrategy); - }); + if (Object.keys((this.renderer as any).packages)?.length === 1) { + this.buildUrlsFromProject(this.project); + } else { + this.project.children?.forEach((projectChild) => { + this.buildUrlsFromPackage(projectChild); + }); + } } else { this.buildUrlsFromProject(this.project); } return this.urls; } + private buildUrlsFromPackage(projectChild: DeclarationReflection) { + const entryFileName = this.options.getValue('entryFileName'); + const preservePackageReadme = + Boolean(projectChild.readme) && !this.options.getValue('mergeReadme'); + + const packagesIndex = getIndexFileName(projectChild); + const packageMeta = (this.renderer as any).packages[projectChild.name]; + + const outputFileStrategy = + packageMeta?.outputFileStrategy || + this.options.getValue('outputFileStrategy'); + + const url = `${projectChild.name}/${ + preservePackageReadme ? packagesIndex : entryFileName + }`; + + if (preservePackageReadme) { + this.urls.push( + new UrlMapping( + `${path.dirname(url)}/${entryFileName}`, + projectChild as any, + this.theme.readmeTemplate, + ), + ); + } + + this.urls.push( + new UrlMapping(url, projectChild as any, this.theme.projectTemplate), + ); + + projectChild.url = url; + + this.buildUrlsFromProject(projectChild, url, outputFileStrategy); + } + /** * * @param project @@ -190,7 +199,7 @@ export class UrlsContext { OutputFileStrategy.Modules && reflection.name === 'index' ) { - return `module.index.md`; + return urlPath.replace('index.md', 'module.index.md'); } return urlPath; } diff --git a/packages/typedoc-plugin-markdown/test/__utils__/fixture-config.ts b/packages/typedoc-plugin-markdown/test/__utils__/fixture-config.ts index 1a67b9b3a..e3acd5fdd 100644 --- a/packages/typedoc-plugin-markdown/test/__utils__/fixture-config.ts +++ b/packages/typedoc-plugin-markdown/test/__utils__/fixture-config.ts @@ -9,6 +9,7 @@ export enum FixtureOutputFileStrategy { export enum FixtureEntryPoints { Reflections = '/src/reflections/index.ts', Packages = '/src/packages/*', + Package = '/src/packages/package-1', Groups = '/src/groups/**/*.ts', Comments = '/src/comments/index.ts', EntryFiles = '/src/entryfiles/*', @@ -18,6 +19,7 @@ export enum FixtureEntryPoints { export enum FixtureOutputDir { Reflections = 'reflections', Packages = 'packages', + Package = 'package', Groups = 'groups', Comments = 'comments', EntryFiles = 'entryfiles', @@ -105,12 +107,25 @@ export const FIXTURES: Fixture[] = [ entryPointStrategy: 'packages', hideGenerator: true, name: 'packages-example', + hideInPageTOC: true, }, options: [ {}, { mergeReadme: true, includeVersion: true, indexPageTitle: 'API' }, ], }, + { + outDir: FixtureOutputDir.Package, + entryPoints: FixtureEntryPoints.Package, + commonOptions: { + entryPointStrategy: 'packages', + hideGenerator: true, + name: 'packages-example', + includeVersion: true, + hideInPageTOC: true, + }, + options: [{}], + }, { outDir: FixtureOutputDir.EntryFiles, entryPoints: FixtureEntryPoints.EntryFiles, diff --git a/packages/typedoc-plugin-markdown/test/specs/__snapshots__/navigation.spec.ts.snap b/packages/typedoc-plugin-markdown/test/specs/__snapshots__/navigation.spec.ts.snap index b1114727e..fdb0414da 100644 --- a/packages/typedoc-plugin-markdown/test/specs/__snapshots__/navigation.spec.ts.snap +++ b/packages/typedoc-plugin-markdown/test/specs/__snapshots__/navigation.spec.ts.snap @@ -60,7 +60,25 @@ exports[`Navigation should get Navigation Json for packages: (Output File Strate }, { "title": "package-3", - "children": [], + "children": [ + { + "title": "Modules", + "children": [ + { + "title": "index", + "url": "package-3/index/index.md" + }, + { + "title": "module-1", + "url": "package-3/module-1/index.md" + }, + { + "title": "module-2", + "url": "package-3/module-2/index.md" + } + ] + } + ], "url": "package-3/index.md" }, { @@ -109,7 +127,25 @@ exports[`Navigation should get Navigation Json for packages: (Output File Strate }, { "title": "package-3", - "children": [], + "children": [ + { + "title": "Modules", + "children": [ + { + "title": "index", + "url": "package-3/module.index.md" + }, + { + "title": "module-1", + "url": "package-3/module-1.md" + }, + { + "title": "module-2", + "url": "package-3/module-2.md" + } + ] + } + ], "url": "package-3/index.md" }, { diff --git a/packages/typedoc-plugin-markdown/test/specs/__snapshots__/urls.spec.ts.snap b/packages/typedoc-plugin-markdown/test/specs/__snapshots__/urls.spec.ts.snap index f1f0f41ad..f17475949 100644 --- a/packages/typedoc-plugin-markdown/test/specs/__snapshots__/urls.spec.ts.snap +++ b/packages/typedoc-plugin-markdown/test/specs/__snapshots__/urls.spec.ts.snap @@ -180,7 +180,10 @@ exports[`Urls should gets Urls for packages entry points: outputFileStrategy: me "package-2/module-1/interfaces/PackageInterface1.md", "package-2/module-2/index.md", "package-2/module-2/interfaces/PackageInterface2.md", + "package-3/index/index.md", "package-3/index.md", + "package-3/module-1/index.md", + "package-3/module-2/index.md", "package-4/index.md", "package-4/interfaces/PackageInterface.md", "packages.md", @@ -197,7 +200,10 @@ exports[`Urls should gets Urls for packages entry points: outputFileStrategy: me "package-2/module-1/interfaces/PackageInterface1.md", "package-2/module-2/index.md", "package-2/module-2/interfaces/PackageInterface2.md", + "package-3/index/index.md", "package-3/index.md", + "package-3/module-1/index.md", + "package-3/module-2/index.md", "package-4/index.md", "package-4/interfaces/PackageInterface.md", ] @@ -212,6 +218,9 @@ exports[`Urls should gets Urls for packages entry points: outputFileStrategy: mo "package-2/module-1.md", "package-2/module-2.md", "package-3/index.md", + "package-3/module-1.md", + "package-3/module-2.md", + "package-3/module.index.md", "package-4/index.md", "package-4/interfaces/PackageInterface.md", "packages.md", @@ -226,6 +235,9 @@ exports[`Urls should gets Urls for packages entry points: outputFileStrategy: mo "package-2/module-1.md", "package-2/module-2.md", "package-3/index.md", + "package-3/module-1.md", + "package-3/module-2.md", + "package-3/module.index.md", "package-4/index.md", "package-4/interfaces/PackageInterface.md", ] @@ -363,3 +375,18 @@ exports[`Urls should gets Urls for single entry points: outputFileStrategy: modu "index.md", ] `; + +exports[`Urls should gets Urls for single package entry points: outputFileStrategy: members 1`] = ` +[ + "index.md", + "interfaces/PackageInterface.md", + "packages.md", +] +`; + +exports[`Urls should gets Urls for single package entry points: outputFileStrategy: modules 1`] = ` +[ + "index.md", + "packages.md", +] +`; diff --git a/packages/typedoc-plugin-markdown/test/specs/urls.spec.ts b/packages/typedoc-plugin-markdown/test/specs/urls.spec.ts index 67e257a84..d56768bd7 100644 --- a/packages/typedoc-plugin-markdown/test/specs/urls.spec.ts +++ b/packages/typedoc-plugin-markdown/test/specs/urls.spec.ts @@ -26,6 +26,13 @@ describe(`Urls`, () => { ]); }); + test(`should gets Urls for single package entry points`, () => { + expectUrlsToEqual(FixtureOutputDir.Package, [ + FixtureOutputFileStrategy.Members, + FixtureOutputFileStrategy.Modules, + ]); + }); + test(`should gets Urls for entry module`, () => { expectUrlsToEqual(FixtureOutputDir.EntryFiles, [ FixtureOutputFileStrategy.Members, diff --git a/stubs/src/packages/package-3/index.ts b/stubs/src/packages/package-3/index.ts index dba5883fc..e69de29bb 100644 --- a/stubs/src/packages/package-3/index.ts +++ b/stubs/src/packages/package-3/index.ts @@ -1 +0,0 @@ -export interface PackageInterface {} diff --git a/stubs/src/packages/package-3/src/index.ts b/stubs/src/packages/package-3/src/index.ts new file mode 100644 index 000000000..e3d9e4e60 --- /dev/null +++ b/stubs/src/packages/package-3/src/index.ts @@ -0,0 +1 @@ +export interface PackageInterface1 {} diff --git a/stubs/src/packages/package-3/src/module-1.ts b/stubs/src/packages/package-3/src/module-1.ts new file mode 100644 index 000000000..e3d9e4e60 --- /dev/null +++ b/stubs/src/packages/package-3/src/module-1.ts @@ -0,0 +1 @@ +export interface PackageInterface1 {} diff --git a/stubs/src/packages/package-3/src/module-2.ts b/stubs/src/packages/package-3/src/module-2.ts new file mode 100644 index 000000000..0189d8180 --- /dev/null +++ b/stubs/src/packages/package-3/src/module-2.ts @@ -0,0 +1 @@ +export interface PackageInterface2 {} diff --git a/stubs/src/packages/package-3/typedoc.json b/stubs/src/packages/package-3/typedoc.json index c4e59f3c9..2f9212b79 100644 --- a/stubs/src/packages/package-3/typedoc.json +++ b/stubs/src/packages/package-3/typedoc.json @@ -1,5 +1,5 @@ { - "entryPoints": ["./index.ts"], + "entryPoints": ["./src/index.ts", "./src/module-1.ts", "./src/module-2.ts"], "readme": "none", "outputFileStrategy": "modules" }