36 lines
1 KiB
TypeScript
36 lines
1 KiB
TypeScript
import { Plugin } from "..";
|
|
import * as fs from "fs";
|
|
import * as path from "path";
|
|
|
|
export default class Variables extends Plugin {
|
|
name = "variables";
|
|
rewriteTriggers = ["html", "*"];
|
|
renameTo = undefined;
|
|
longLasting = false;
|
|
|
|
variables: Record<string, string> = {};
|
|
|
|
constructor() {
|
|
super();
|
|
this.variables["__BLOG_POSTS__"] = JSON.stringify(
|
|
fs.readdirSync("./website/blogs").map((z) => z.replace(".md", ""))
|
|
);
|
|
const templatePath = path.resolve(__dirname, "../../website/templates");
|
|
if (fs.existsSync(templatePath)) {
|
|
for (const file of fs.readdirSync(templatePath)) {
|
|
const id = file.toUpperCase().replace(".HTML", "");
|
|
this.variables["__TEMPLATE_" + id + "__"] = fs
|
|
.readFileSync(path.join(templatePath, file))
|
|
.toString("utf8");
|
|
}
|
|
}
|
|
}
|
|
|
|
async rewriteFile(file: string, filePath: string): Promise<string> {
|
|
let prevfile = file;
|
|
for (const a of Object.entries(this.variables)) {
|
|
prevfile = prevfile.replaceAll(a[0], a[1]);
|
|
}
|
|
return prevfile;
|
|
}
|
|
}
|