Skip to content

Commit

Permalink
fix: cleanup pre-render steps
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Sep 23, 2021
1 parent d4e5b1d commit 6f2e5de
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/vuepress-plugin-typedoc/src/plugin.ts
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path';

import { Application, ProjectReflection } from 'typedoc';
import { load } from 'typedoc-plugin-markdown';

import { removeDir, render } from './render';
import { addOptions, getOptions } from './options';

import { getSidebarJson } from './sidebar';
Expand All @@ -14,8 +14,14 @@ let project: ProjectReflection | undefined;
export const typedocPlugin = (opts: PluginOptions, ctx: any) => {
const options = getOptions(opts);

const outputDirectory = ctx.sourceDir + '/' + options.out;

removeDir(outputDirectory);

app = new Application();

app.renderer.render = render;

load(app);

addOptions(app);
Expand All @@ -29,8 +35,6 @@ export const typedocPlugin = (opts: PluginOptions, ctx: any) => {
return;
}

const outputDirectory = ctx.sourceDir + '/' + options.out;

if (options.watch) {
app.convertAndWatch(async (project) => {
app.generateDocs(project, outputDirectory);
Expand Down
40 changes: 40 additions & 0 deletions packages/vuepress-plugin-typedoc/src/render.ts
@@ -0,0 +1,40 @@
import * as fs from 'fs';
import { ProjectReflection, RendererEvent, UrlMapping } from 'typedoc';

export async function render(
project: ProjectReflection,
outputDirectory: string,
) {
const output = new RendererEvent(
RendererEvent.BEGIN,
outputDirectory,
project,
);
output.urls = this.theme!.getUrls(project);
this.trigger(output);
if (!output.isDefaultPrevented) {
output?.urls?.forEach((mapping: UrlMapping) => {
this.renderDocument(output.createPageEvent(mapping));
});

this.trigger(RendererEvent.END, output);
}
}

export function removeDir(path: string) {
if (fs.existsSync(path)) {
const files = fs.readdirSync(path);
if (files.length > 0) {
files.forEach(function (filename) {
if (fs.statSync(path + '/' + filename).isDirectory()) {
removeDir(path + '/' + filename);
} else {
fs.unlinkSync(path + '/' + filename);
}
});
fs.rmdirSync(path);
} else {
fs.rmdirSync(path);
}
}
}

0 comments on commit 6f2e5de

Please sign in to comment.