mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 23:16:41 +07:00
added templaters
This commit is contained in:
parent
1754092f7c
commit
d5228ef146
13 changed files with 406 additions and 17 deletions
39
src/service/template/index.ts
Normal file
39
src/service/template/index.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import extract from "remark-extract-frontmatter";
|
||||
import frontmatter from "remark-frontmatter";
|
||||
import compiler from "remark-stringify";
|
||||
import parser from "remark-parse";
|
||||
import unified, { Processor } from "unified";
|
||||
import { parse } from "yaml";
|
||||
import toVFile from "to-vfile";
|
||||
import path from "path";
|
||||
|
||||
export class Template<T extends Record<string, any>> {
|
||||
private processor: Processor;
|
||||
public fields: T = {} as T;
|
||||
public template: string = "";
|
||||
|
||||
constructor(filename: string) {
|
||||
try {
|
||||
if (!filename) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.processor = unified()
|
||||
.use(parser)
|
||||
.use(compiler)
|
||||
.use(frontmatter)
|
||||
.use(extract, { yaml: parse });
|
||||
|
||||
const file = toVFile.readSync(path.join(__dirname, "../../", filename));
|
||||
const result = this.processor.processSync(file);
|
||||
|
||||
this.fields = result.data as T;
|
||||
this.template = result
|
||||
.toString()
|
||||
.replace(/^---\n(.*)---\n?$/gms, "")
|
||||
.trim();
|
||||
} catch (e) {
|
||||
throw new Error(`Template: ${e.toString()}`);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue