From 23f122ed1cd759f54c2da3fdcd2d756f111e18df Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Wed, 11 May 2022 09:27:28 +0800 Subject: [PATCH] Add handlebars helper --- src/generator/TemplateFilter.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/generator/TemplateFilter.ts b/src/generator/TemplateFilter.ts index 40fbcde..7a7324c 100644 --- a/src/generator/TemplateFilter.ts +++ b/src/generator/TemplateFilter.ts @@ -3,9 +3,7 @@ import Handlebars from "handlebars"; import App from "../App"; import { MultipleMessage } from "../base/provider/BaseProvider"; import { ConfigCheckError } from "../error/ConfigCheckError"; - -// 请勿删除这些没有使用的导入,模板中会用到 -const { Utils } = require('../Utils'); +import { Utils } from "../Utils"; export type TemplateFilterConfig = { [key: string]: string }; @@ -23,6 +21,7 @@ export class TemplateFilter { } async initialize() { + this.initHandlebars(); for (let key in this.config) { let template = this.config[key]; 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() { if (!('base' in this.config) && !('default' in this.config)) { throw new ConfigCheckError('Unset template.base or template.default');