Skip to content

Commit

Permalink
feat: TypeDoc 0.22 compatibility fixes (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Sep 14, 2021
1 parent 65ca426 commit 963250c
Show file tree
Hide file tree
Showing 68 changed files with 3,413 additions and 3,572 deletions.
20 changes: 10 additions & 10 deletions package.json
Expand Up @@ -21,24 +21,24 @@
"devDependencies": {
"@types/fs-extra": "^9.0.12",
"@types/jest": "^26.0.24",
"@types/node": "^16.3.1",
"@types/progress": "^2.0.4",
"@types/node": "^16.9.1",
"@types/progress": "^2.0.5",
"@types/tmp": "^0.2.1",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"@typescript-eslint/eslint-plugin": "^4.31.0",
"@typescript-eslint/parser": "^4.31.0",
"copyfiles": "^2.4.1",
"cuid": "^2.1.8",
"eslint": "^7.30.0",
"eslint": "^7.32.0",
"fs-extra": "^10.0.0",
"jest": "^26.6.3",
"lerna": "^4.0.0",
"markdownlint": "^0.23.1",
"markdownlint-cli": "^0.27.1",
"markdownlint": "^0.24.0",
"markdownlint-cli": "^0.28.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.2",
"prettier": "^2.4.0",
"tmp": "^0.2.1",
"ts-jest": "^26.5.5",
"typedoc": "^0.21.4",
"typescript": "^4.3.5"
"typedoc": "^0.22.3",
"typescript": "^4.4.3"
}
}
116 changes: 0 additions & 116 deletions packages/docusaurus-plugin-typedoc/src/front-matter.ts

This file was deleted.

123 changes: 1 addition & 122 deletions packages/docusaurus-plugin-typedoc/src/render.ts
@@ -1,144 +1,23 @@
import * as fs from 'fs';

import ProgressBar from 'progress';
import {
Application,
MixedDeclarationOption,
ParameterType,
ProjectReflection,
ReflectionKind,
StringDeclarationOption,
TSConfigReader,
TypeDocReader,
UrlMapping,
} from 'typedoc';
import { GroupPlugin } from 'typedoc/dist/lib/converter/plugins';
import { RendererEvent } from 'typedoc/dist/lib/output/events';
import { TemplateMapping } from 'typedoc/dist/lib/output/themes/DefaultTheme';

import { getPluginOptions } from './options';
import { PluginOptions } from './types';

const CATEGORY_POSITION = {
[ReflectionKind.Module]: 1,
[ReflectionKind.Namespace]: 1,
[ReflectionKind.Enum]: 2,
[ReflectionKind.Class]: 3,
[ReflectionKind.Interface]: 4,
[ReflectionKind.TypeAlias]: 5,
[ReflectionKind.Variable]: 6,
[ReflectionKind.Function]: 7,
[ReflectionKind.ObjectLiteral]: 8,
};
import { PluginOptions } from './types';

export const bootstrap = (app: Application, opts: Partial<PluginOptions>) => {
addTypedocReaders(app);
addTypedocDeclarations(app);
app.renderer.render = render;
app.bootstrap({ ...getPluginOptions(opts) });
return app.options.getRawValues() as PluginOptions;
};

async function render(project: ProjectReflection, outputDirectory: string) {
if (
!this.prepareTheme() ||
!isOutputDirectory(this.application, outputDirectory)
) {
return;
}

const output = new RendererEvent(
RendererEvent.BEGIN,
outputDirectory,
project,
);

const options: PluginOptions = this.application.options.getRawValues();

output.settings = options;
output.urls = this.theme!.getUrls(project);

if (output.urls) {
const bar = new ProgressBar('Rendering [:bar] :percent', {
total: output.urls.length,
width: 40,
});
this.trigger(output);
if (!output.isDefaultPrevented) {
output.urls?.forEach((mapping: UrlMapping, i) => {
this.renderDocument(output.createPageEvent(mapping));
bar.tick();
});
this.trigger(RendererEvent.END, output);
}

writeCategoryYaml(
outputDirectory,
options.sidebar.categoryLabel,
options.sidebar.position,
);

Object.keys(groupUrlsByKind(output.urls)).forEach((group) => {
const kind = parseInt(group);
const mapping = this.theme.mappings.find((mapping: TemplateMapping) =>
mapping.kind.includes(kind),
);
if (mapping) {
writeCategoryYaml(
outputDirectory + '/' + mapping.directory,
GroupPlugin.getKindPlural(kind),
CATEGORY_POSITION[kind],
);
}
});
}
}

const isOutputDirectory = (app: Application, outputDirectory: string) => {
const options = app.options.getRawValues() as PluginOptions;
if (options.disableOutputCheck) {
return true;
}

if (!fs.existsSync(outputDirectory)) {
return true;
}

if (app.renderer.theme!.isOutputDirectory(outputDirectory)) {
if (fs.existsSync(outputDirectory)) {
fs.rmdirSync(outputDirectory, { recursive: true });
}
return true;
} else {
app.logger.error(
`The output directory "${outputDirectory}" exists but does not seem to be a documentation generated by TypeDoc.\n` +
'Make sure this is the right target directory, delete the folder and rerun TypeDoc.',
);
return false;
}
};

const writeCategoryYaml = (
categoryPath: string,
label: string,
position: number | null,
) => {
const yaml: string[] = [`label: "${label}"`];
if (position !== null) {
yaml.push(`position: ${position}`);
}
if (fs.existsSync(categoryPath)) {
fs.writeFileSync(categoryPath + '/_category_.yml', yaml.join('\n'));
}
};

const groupUrlsByKind = (urls: UrlMapping[]) => {
return urls.reduce(
(r, v, i, a, k = v.model.kind) => ((r[k] || (r[k] = [])).push(v), r),
{},
);
};

const addTypedocReaders = (app: Application) => {
app.options.addReader(new TypeDocReader());
app.options.addReader(new TSConfigReader());
Expand Down

0 comments on commit 963250c

Please sign in to comment.