profectus-docs/profectus-theme/theme.ts

78 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-03-11 06:36:06 +00:00
import * as fs from 'fs';
2023-04-12 23:41:44 +00:00
import * as Handlebars from 'handlebars';
import * as path from "path";
2022-03-11 06:36:06 +00:00
import {
ContainerReflection,
PageEvent,
Renderer,
DeclarationReflection,
2023-04-12 23:41:44 +00:00
ReflectionKind,
2022-03-11 06:36:06 +00:00
RendererEvent
} from 'typedoc';
import { MarkdownTheme } from 'typedoc-plugin-markdown';
import registerTypeHelper from './type';
2023-04-12 23:41:44 +00:00
const TEMPLATE_PATH = path.join(__dirname, '..', 'profectus-theme', 'resources', 'templates');
2022-03-11 06:36:06 +00:00
export class ProfectusTheme extends MarkdownTheme {
constructor(renderer: Renderer) {
super(renderer);
this.entryDocument = 'index.md';
this.hideBreadcrumbs = true;
this.hideInPageTOC = true;
2023-04-05 12:44:37 +00:00
// registerTypeHelper();
2022-03-11 06:36:06 +00:00
}
2023-04-12 23:41:44 +00:00
getReflectionMemberTemplate() {
const templ = super.getReflectionMemberTemplate();
return (pageEvent) => {
return templ(pageEvent);
}
}
2022-03-11 06:36:06 +00:00
getReflectionTemplate() {
return (pageEvent) => {
if (pageEvent.url === "index.md") {
return "# Profectus API";
}
2023-04-12 23:41:44 +00:00
return Handlebars.compile(
fs.readFileSync(path.join(TEMPLATE_PATH, 'reflection.hbs')).toString(),
)(pageEvent, {
allowProtoMethodsByDefault: true,
allowProtoPropertiesByDefault: true,
data: { theme: this }
})
2022-03-11 06:36:06 +00:00
}
}
getRelativeUrl(url: string) {
const relativeUrl = super
.getRelativeUrl(url)
.replace(/(.*).md/, '$1')
.replace(/ /g, '-');
return relativeUrl.startsWith('..') ? relativeUrl : './' + relativeUrl;
}
toUrl(mapping: any, reflection: DeclarationReflection) {
2023-04-12 23:41:44 +00:00
let name = reflection.getFullName();
if (name.match(/features\/.*\/.*/) != null && !name.includes("/tabs/")) {
name = name.replace(/features\/.*\/(.*)/, "features/$1");
}
return `${mapping.directory}/${name}.md`;
}
get mappings() {
return [
{
kind: [ReflectionKind.Module],
isLeaf: false,
directory: 'modules',
template: this.getReflectionTemplate(),
}
];
2022-03-11 06:36:06 +00:00
}
}