Skip to content

Commit

Permalink
Add support for automatically loading themes with "typedoc-theme"
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Nov 14, 2021
1 parent 3b5174e commit 4fadd91
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- TypeDoc will now detect and warn if multiple instances of the package are loaded. This usually means that a plugin has its own version of TypeDoc installed, which will lead to things breaking in unexpected ways.
It will only work if both loaded TypeDocs are v0.22.9 or later.
- TypeDoc will now automatically load packages with `typedoctheme` or `typedoc-theme` in their keywords.
Plugins which define a custom theme should include this keyword so that they can be automatically collected and displayed at https://typedoc.org/guides/themes/.

### Bug Fixes

Expand Down
6 changes: 2 additions & 4 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
CallbackLogger,
loadPlugins,
writeFile,
discoverNpmPlugins,
discoverPlugins,
NeverIfInternal,
TSConfigReader,
} from "./utils/index";
Expand Down Expand Up @@ -135,9 +135,7 @@ export class Application extends ChildableComponent<
}
this.logger.level = this.options.getValue("logLevel");

const plugins = this.options.isSet("plugin")
? this.options.getValue("plugin")
: discoverNpmPlugins(this);
const plugins = discoverPlugins(this);
loadPlugins(this, plugins);

this.options.reset();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type NeverIfInternal<T> = IfInternal<never, T>;
const loadSymbol = Symbol.for("typedoc_loads");
const getLoads = () => globalThis[loadSymbol as never] || 0;

// @ts-expect-error
// @ts-expect-error there's no way to add symbols to globalThis, sadly.
globalThis[loadSymbol] = getLoads() + 1;

export function hasBeenLoadedMultipleTimes() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type {
TypeDocOptionValues,
ParameterTypeToOptionTypeMap,
} from "./options";
export { discoverNpmPlugins, loadPlugins } from "./plugins";
export { discoverPlugins, loadPlugins } from "./plugins";
export { sortReflections } from "./sort";
export type { SortStrategy } from "./sort";

Expand Down
17 changes: 15 additions & 2 deletions src/lib/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ export function loadPlugins(app: Application, plugins: readonly string[]) {
}
}

export function discoverNpmPlugins(app: Application): string[] {
export function discoverPlugins(app: Application): string[] {
// If the plugin option is set, then automatic discovery is disabled, and we should just
// return the plugins that the user has asked for.
if (app.options.isSet("plugin")) {
return app.options.getValue("plugin");
}

const result: string[] = [];
discover();
return result;
Expand Down Expand Up @@ -100,6 +106,13 @@ function loadPackageInfo(logger: Logger, fileName: string): any {
}
}

const PLUGIN_KEYWORDS = [
"typedocplugin",
"typedoc-plugin",
"typedoctheme",
"typedoc-theme",
];

/**
* Test whether the given package info describes a TypeDoc plugin.
*/
Expand All @@ -116,7 +129,7 @@ function isPlugin(info: any): boolean {
return keywords.some(
(keyword) =>
typeof keyword === "string" &&
keyword.toLocaleLowerCase() === "typedocplugin"
PLUGIN_KEYWORDS.includes(keyword.toLocaleLowerCase())
);
}

Expand Down

0 comments on commit 4fadd91

Please sign in to comment.