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 21, 2021
1 parent 0a55775 commit 9566a9c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-typedoc/package.json
Expand Up @@ -28,8 +28,8 @@
"build": "rm -rf ./dist && tsc",
"build-and-test": "yarn run build && yarn run test",
"test:init": "rm -rf test/site && npx @docusaurus/init@latest init test/site classic",
"test:demo:start": "rm -rf test/site/docs/api yarn run build && cd test/site && yarn run clear && yarn run start",
"test:demo:build": "rm -rf test/site/docs/api && yarn run build && cd test/site && yarn run clear && yarn run build && yarn run serve",
"test:demo:start": "yarn run build && cd test/site && yarn run start",
"test:demo:build": "yarn run build && cd test/site && yarn run build && yarn run serve",
"test": "jest --colors"
},
"author": "Thomas Grey",
Expand Down
14 changes: 2 additions & 12 deletions packages/docusaurus-plugin-typedoc/src/options.ts
@@ -1,5 +1,3 @@
import * as path from 'path';

import { PluginOptions } from './types';

const DEFAULT_PLUGIN_OPTIONS: PluginOptions = {
Expand All @@ -13,12 +11,12 @@ const DEFAULT_PLUGIN_OPTIONS: PluginOptions = {
readmeLabel: 'Readme',
position: null,
},
plugin: ['none'],
watch: false,
hideInPageTOC: true,
hideBreadcrumbs: true,
hidePageTitle: true,
entryDocument: 'index.md',
plugin: ['none'],
watch: false,
};

export const getPluginOptions = (
Expand All @@ -34,11 +32,3 @@ export const getPluginOptions = (
};
return options;
};

export const getOutputDirectory = (siteDir: string, options: PluginOptions) => {
return path.resolve(
siteDir,
options.docsRoot,
path.relative(process.cwd(), options.out),
);
};
20 changes: 12 additions & 8 deletions packages/docusaurus-plugin-typedoc/src/plugin.ts
@@ -1,11 +1,9 @@
import * as path from 'path';

import { LoadContext } from '@docusaurus/types';
import * as path from 'path';
import { Application } from 'typedoc';
import { load } from 'typedoc-plugin-markdown';

import { getOutputDirectory } from './options';
import { bootstrap } from './render';
import { getPluginOptions } from './options';
import { bootstrap, removeDir } from './render';
import { PluginOptions } from './types';

// store list of plugin ids when running multiple instances
Expand All @@ -20,13 +18,19 @@ export default async function pluginDocusaurus(

const { siteDir } = context;

const options = getPluginOptions(opts);

const outputDir = path.resolve(siteDir, options.docsRoot, options.out);

removeDir(outputDir);

const app = new Application();

app.options.setValue('theme', path.resolve(__dirname));

load(app);

const options = bootstrap(app, opts);
bootstrap(app, options);

const project = app.convert();

Expand All @@ -37,10 +41,10 @@ export default async function pluginDocusaurus(

if (options.watch) {
app.convertAndWatch(async (project) => {
await app.generateDocs(project, getOutputDirectory(siteDir, options));
await app.generateDocs(project, outputDir);
});
} else {
await app.generateDocs(project, getOutputDirectory(siteDir, options));
await app.generateDocs(project, outputDir);
}
}

Expand Down
33 changes: 21 additions & 12 deletions packages/docusaurus-plugin-typedoc/src/render.ts
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import {
Application,
MixedDeclarationOption,
Expand All @@ -9,26 +10,16 @@ import {
TypeDocReader,
UrlMapping,
} from 'typedoc';

import { getPluginOptions } from './options';

import { PluginOptions } from './types';

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

async function render(project: ProjectReflection, outputDirectory: string) {
if (
!this.prepareTheme() ||
!(await this.prepareOutputDirectory(outputDirectory))
) {
return;
}
const output = new RendererEvent(
RendererEvent.BEGIN,
outputDirectory,
Expand Down Expand Up @@ -77,3 +68,21 @@ const addTypedocDeclarations = (app: Application) => {
type: ParameterType.Mixed,
} as MixedDeclarationOption);
};

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);
}
}
}
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-typedoc/src/theme.ts
Expand Up @@ -49,7 +49,7 @@ export class DocusaurusTheme extends MarkdownTheme {
getRelativeUrl(url: string) {
const relativeUrl = super.getRelativeUrl(url).replace(/.md/g, '');
if (path.basename(relativeUrl).startsWith('index')) {
return relativeUrl.replace('index', '')
return relativeUrl.replace('index', '');
}
return relativeUrl;
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export class DocusaurusTheme extends MarkdownTheme {
if (page.url === this.entryDocument) {
items = {
...items,
slug: '/' + path.relative(process.cwd(), this.out).replace(/\\/g, '/') + '/',
slug: `/${path.relative(process.cwd(), this.out).replace(/\\/g, '/')}/`,
};
}
if (sidebarLabel && sidebarLabel !== pageTitle) {
Expand Down

0 comments on commit 9566a9c

Please sign in to comment.