Skip to content

Commit

Permalink
fix: Enable outputFileStrategy in 'packages' mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Dec 15, 2023
1 parent 086dc53 commit d8c170d
Show file tree
Hide file tree
Showing 25 changed files with 226 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-jeans-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typedoc-plugin-markdown': patch
---

- Enable outputFileStrategy in "packages" mode
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"lint": "npm run lint --workspaces",
"build": "npm run build --workspaces",
"test": "npm run test --workspaces",
"test": "npm run test --workspace=typedoc-plugin-markdown",
"prerelease": "npm run build",
"release": "npx changeset publish"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/typedoc-plugin-markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "npm-run-all test:*",
"test:lint-md": "node test/__scripts__/lint.md.mjs",
"test:lint-mdx": "node test/__scripts__/lint.mdx.mjs",
"test:jest": "jest --updateSnapshot",
"test:jest": "jest",
"build-and-run": "npm run build && npm run pretest",
"build-and-test": "npm run build && npm run test",
"api-docs": "npm run build && typedoc --options ./typedoc.api.js --out ./docs/api",
Expand Down
11 changes: 10 additions & 1 deletion packages/typedoc-plugin-markdown/src/plugin/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module
*/

import { Application, Renderer, Theme } from 'typedoc';
import { Application, Context, Converter, Renderer, Theme } from 'typedoc';
import { MarkdownTheme } from '../theme';
import * as options from './options/config';
import { generateMarkdown, renderMarkdown } from './renderer';
Expand Down Expand Up @@ -39,4 +39,13 @@ export function load(app: Application) {
['default', MarkdownTheme],
]),
});

app.converter.on(Converter.EVENT_RESOLVE_END, (context: Context) => {
if (app.options.packageDir) {
(app.renderer as any).packages = {
...((app.renderer as any).packages || {}),
[context.project.name]: { dir: app.options.packageDir },
};
}
});
}
28 changes: 27 additions & 1 deletion packages/typedoc-plugin-markdown/src/plugin/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

import * as fs from 'fs';
import * as path from 'path';
import { DeclarationReflection, ProjectReflection, Reflection } from 'typedoc';
import {
DeclarationReflection,
Options,
ProjectReflection,
Reflection,
} from 'typedoc';
import { formatContents } from '../support/utils';
import { MarkdownPageEvent, MarkdownRendererEvent } from './events';

Expand Down Expand Up @@ -83,6 +88,27 @@ export async function renderMarkdown(
project,
);

if (this.packages) {
const getOptionsForPackage = new Promise((resolve, reject) => {
const packages = {};
Object.entries(this.packages).forEach(async ([k, v]) => {
packages[k] = {};
const origOptions = this.application.options;
const packageOptions: Options = origOptions.copyForPackage(
(v as any).dir,
);
await packageOptions.read(this.application.logger, (v as any).dir);
const isSet = packageOptions.isSet('outputFileStrategy');
packages[k].outputFileStrategy = isSet
? packageOptions.getValue('outputFileStrategy')
: null;
resolve(packages);
});
});

this.packages = await getOptionsForPackage;
}

output.urls = this.theme!.getUrls(project);
output.navigation = this.theme!.getNavigation(project);

Expand Down
2 changes: 2 additions & 0 deletions packages/typedoc-plugin-markdown/src/theme/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
*/

import { ReflectionKind } from 'typedoc';
import { OutputFileStrategy } from '../plugin/options/custom-maps';

export interface UrlOption {
parentUrl?: string;
directory?: string | null;
forceDirectory?: boolean;
outputFileStrategy?: OutputFileStrategy;
}

export interface TemplateMapping {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ function packageHeader(
const entryFileName = context.options.getValue('entryFileName');

md.push(getProjectName(page.project, context, page.project.url));
md.push('\\>');
md.push('•');

const packageItemName = packageItem.packageVersion
? `${packageItem.name} - v${packageItem.packageVersion}`
: packageItem.name;

md.push(bold(packageItem.name));
md.push(bold(packageItemName));

md.push('•');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ export function pageTitle(
): string {
const memberPageTitle = context.options.getValue('memberPageTitle') as string;
const name = getMemberTitle(page.model as DeclarationReflection);
const projectName = getProjectDisplayName(
page.project,
context.options.getValue('includeVersion'),
);

if (page.model?.url === page.project.url) {
const projectName = getProjectDisplayName(
page.project,
context.options.getValue('includeVersion'),
);
return context.options
.getValue('indexPageTitle')
.replace('{projectName}', projectName);
}

if (page.model.kindOf(ReflectionKind.Module)) {
return name;
return page.model.packageVersion
? `${name} - v${page.model.packageVersion}`
: name;
}

return memberPageTitle
Expand Down
19 changes: 11 additions & 8 deletions packages/typedoc-plugin-markdown/src/theme/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export class MarkdownTheme extends Theme {
return new UrlsContext(
this,
project,
this.application.options.getRawValues(),
this.application.renderer,
this.application.options,
);
}

Expand Down Expand Up @@ -155,19 +156,21 @@ export class MarkdownTheme extends Theme {
* Returns the template mapping for a given reflection kind
* @param kind
*/
getTemplateMapping(kind: ReflectionKind): TemplateMapping {
const outputFileStrategy = this.application.options.getValue(
'outputFileStrategy',
) as OutputFileStrategy;
getTemplateMapping(
kind: ReflectionKind,
outputFileStrategyOverride?: OutputFileStrategy,
): TemplateMapping {
const outputFileStrategy =
outputFileStrategyOverride ||
this.application.options.getValue('outputFileStrategy');

const getDirectoryName = (reflectionKind: ReflectionKind) => {
const pluralString = ReflectionKind.pluralString(reflectionKind);
return slugify(pluralString).toLowerCase();
};

const membersWithOwnFile = this.application.options.getValue(
'membersWithOwnFile',
) as string[];
const membersWithOwnFile =
this.application.options.getValue('membersWithOwnFile');

const mappings = {
[ReflectionKind.Module]: {
Expand Down
Loading

0 comments on commit d8c170d

Please sign in to comment.