Skip to content

Commit

Permalink
feat: TypeDoc 0.21 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Jun 18, 2021
1 parent fe06188 commit fa5e913
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 80 deletions.
10 changes: 5 additions & 5 deletions packages/typedoc-plugin-markdown/package.json
Expand Up @@ -8,20 +8,20 @@
],
"scripts": {
"lint": "eslint ./src --ext .ts",
"prepublishOnly": "npm run lint && npm run build && npm run test",
"prepublishOnly": "yarn run lint && yarn run build && yarn run test",
"build": "rm -rf dist && tsc && copyfiles --up 1 ./src/**/*.hbs ./dist/",
"pretest": "yarn run demo:md && markdownlint ./demo/md",
"test": "jest --colors",
"build-and-test": "npm run build && npm run test",
"build-and-test": "yarn run build && yarn run test",
"fixtures": "node ./tasks/fixtures.js",
"demos": "rm -rf ./demo && npm run build && npm-run-all demo:*",
"demos": "yarn run build && npm-run-all demo:*",
"demo:md": "typedoc --options ./demo.options.json --out ./demo/md",
"demo:html": "typedoc --options ./demo.options.json --plugin none --out ./demo/html"
},
"author": "Thomas Grey",
"license": "MIT",
"engines": {
"node": ">= 10.8.0"
"node": ">= 12.20.0"
},
"bugs": {
"url": "https://github.com/tgreyuk/typedoc-plugin-markdown/issues"
Expand All @@ -41,7 +41,7 @@
"typedoc"
],
"peerDependencies": {
"typedoc": ">=0.20.0"
"typedoc": ">=0.21.0"
},
"dependencies": {
"handlebars": "^4.7.7"
Expand Down
@@ -1,9 +1,12 @@
import * as path from 'path';

import { BindOption, Reflection } from 'typedoc';
import {
Component,
ContextAwareRendererComponent,
} from 'typedoc/dist/lib/output/components';
import { PageEvent } from 'typedoc/dist/lib/output/events';

import MarkdownTheme from '../theme';

@Component({ name: 'breadcrumbs' })
Expand All @@ -21,6 +24,7 @@ export class Breadcrumbs extends ContextAwareRendererComponent {
MarkdownTheme.HANDLEBARS.registerHelper(
'breadcrumbs',
(page: PageEvent) => {
const hasReadmeFile = this.readme !== path.join(process.cwd(), 'none');
const breadcrumbs: string[] = [];
const globalsName = this.entryPoints.length > 1 ? 'Modules' : 'Exports';
breadcrumbs.push(
Expand All @@ -32,7 +36,7 @@ export class Breadcrumbs extends ContextAwareRendererComponent {
this.entryDocument,
)})`,
);
if (this.readme !== 'none') {
if (hasReadmeFile) {
breadcrumbs.push(
page.url === page.project.url
? globalsName
Expand Down
45 changes: 38 additions & 7 deletions packages/typedoc-plugin-markdown/src/index.ts
@@ -1,11 +1,11 @@
import { Application } from 'typedoc/dist/lib/application';
import { ParameterType } from 'typedoc/dist/lib/utils/options/declaration';
import * as fs from 'fs';
import * as path from 'path';

import { MarkdownPlugin } from './plugin';
import { Application, Converter, ParameterType, Renderer } from 'typedoc';

export = (PluginHost: Application) => {
const app = PluginHost.owner;
import MarkdownTheme from './theme';

export function load(app: Application) {
app.options.addDeclaration({
help: '[Markdown Plugin] Do not render page title.',
name: 'hidePageTitle',
Expand Down Expand Up @@ -70,5 +70,36 @@ export = (PluginHost: Application) => {
type: ParameterType.String,
});

app.converter.addComponent('markdown', new MarkdownPlugin(app.converter));
};
app.converter.on(Converter.EVENT_BEGIN, () => {
Renderer.getDefaultTheme = () => path.join(__dirname, 'resources');
});

app.converter.on(Converter.EVENT_RESOLVE_BEGIN, () => {
const themeName = app.options.getValue('theme');

const themeDir = path.join(__dirname);

if (![themeDir, 'default', 'markdown'].includes(themeName)) {
// For custom themes check that the theme is a markdown theme
// If it is return and pass through to renderer
const themeFileName = path.resolve(path.join(themeName, 'theme.js'));
if (fs.existsSync(themeFileName) && isMarkdownTheme(themeFileName)) {
return;
}
app.logger.warn(
`[typedoc-plugin-markdown] '${themeName}' is not a recognised markdown theme. If an html theme is required, please disable this plugin.`,
);
}

app.options.setValue('theme', themeDir);
});
}

function isMarkdownTheme(themeFileName: string) {
try {
const ThemeClass = require(themeFileName).default;
return ThemeClass.prototype instanceof MarkdownTheme;
} catch (e) {
return false;
}
}
65 changes: 0 additions & 65 deletions packages/typedoc-plugin-markdown/src/plugin.ts

This file was deleted.

6 changes: 4 additions & 2 deletions packages/typedoc-plugin-markdown/src/theme.ts
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';

import * as Handlebars from 'handlebars';
import {
Expand Down Expand Up @@ -113,7 +114,8 @@ export default class MarkdownTheme extends Theme {
*/
getUrls(project: ProjectReflection): UrlMapping[] {
const urls: UrlMapping[] = [];
if (this.readme === 'none') {
const noReadmeFile = this.readme == path.join(process.cwd(), 'none');
if (noReadmeFile) {
project.url = this.entryDocument;
urls.push(new UrlMapping(this.entryDocument, project, 'reflection.hbs'));
} else {
Expand Down Expand Up @@ -244,7 +246,7 @@ export default class MarkdownTheme extends Theme {
return filteredNavigationItem as NavigationItem;
};
const navigation = createNavigationItem(project.name, undefined, false);
const hasReadme = this.readme !== 'none';
const hasReadme = this.readme !== path.join(process.cwd(), 'none');
if (hasReadme) {
navigation.children?.push(
createNavigationItem('Readme', this.entryDocument, false),
Expand Down

0 comments on commit fa5e913

Please sign in to comment.