profectus-docs/profectus-theme/theme.ts

103 lines
3.3 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');
const PARTIALS_PATH = path.join(__dirname, '..', 'profectus-theme', 'resources', 'partials');
2023-04-12 23:41:44 +00:00
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;
const partialFiles = fs.readdirSync(PARTIALS_PATH);
partialFiles.forEach((partialFile) => {
const partialName = path.basename(partialFile, '.hbs');
const partialContent = fs
.readFileSync(PARTIALS_PATH + '/' + partialFile)
.toString();
Handlebars.registerPartial(partialName, partialContent);
});
Handlebars.registerHelper("when", (context, operand_1, operator, operand_2, options) => {
let operators = { // {{#when <operand1> 'eq' <operand2>}}
'eq': (l,r) => l == r, // {{/when}}
'noteq': (l,r) => l != r,
'gt': (l,r) => (+l) > (+r), // {{#when var1 'eq' var2}}
'gteq': (l,r) => ((+l) > (+r)) || (l == r), // eq
'lt': (l,r) => (+l) < (+r), // {{else when var1 'gt' var2}}
'lteq': (l,r) => ((+l) < (+r)) || (l == r), // gt
'or': (l,r) => l || r, // {{else}}
'and': (l,r) => l && r, // lt
'%': (l,r) => (l % r) === 0 // {{/when}}
}
let result = operators[operator](operand_1,operand_2);
if(result) return options.fn(context);
return options.inverse(context);
});
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();
2023-04-20 03:11:04 +00:00
if (name.match(/features\/.*\/.*/) != null && !name.includes("/tabs/") && !name.includes("/decorators/")) {
2023-04-12 23:41:44 +00:00
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
}
}