Add handlebars helper

main
落雨楓 3 years ago
parent f2258cca35
commit 23f122ed1c

@ -3,9 +3,7 @@ import Handlebars from "handlebars";
import App from "../App"; import App from "../App";
import { MultipleMessage } from "../base/provider/BaseProvider"; import { MultipleMessage } from "../base/provider/BaseProvider";
import { ConfigCheckError } from "../error/ConfigCheckError"; import { ConfigCheckError } from "../error/ConfigCheckError";
import { Utils } from "../Utils";
// 请勿删除这些没有使用的导入,模板中会用到
const { Utils } = require('../Utils');
export type TemplateFilterConfig = { [key: string]: string }; export type TemplateFilterConfig = { [key: string]: string };
@ -23,6 +21,7 @@ export class TemplateFilter {
} }
async initialize() { async initialize() {
this.initHandlebars();
for (let key in this.config) { for (let key in this.config) {
let template = this.config[key]; let template = this.config[key];
if (key === "default") { if (key === "default") {
@ -40,6 +39,22 @@ export class TemplateFilter {
} }
} }
initHandlebars() {
Handlebars.registerHelper('excerpt', (...args) => {
if (args.length > 2) {
let text: any = args[0];
let maxLength: any = parseInt(args[1]);
let ellipsis: any = undefined;
if (args.length > 3) {
return Utils.excerpt(text, parseInt(maxLength), ellipsis);
}
}
return args[0];
});
Handlebars.registerHelper('currentDate', Utils.getCurrentDate);
}
checkConfig() { checkConfig() {
if (!('base' in this.config) && !('default' in this.config)) { if (!('base' in this.config) && !('default' in this.config)) {
throw new ConfigCheckError('Unset template.base or template.default'); throw new ConfigCheckError('Unset template.base or template.default');

Loading…
Cancel
Save