sssg/src/plugins/compile-time-js.ts
yourfriendoss 91f39a80a7
Some checks failed
/ build (node-16) (push) Failing after 33s
/ build (windows-node-iron) (push) Failing after 2m26s
add all of the testing
2025-10-20 01:23:51 +03:00

32 lines
932 B
TypeScript

import { Plugin } from "..";
export default class CompileTimeJS extends Plugin {
name = "compile-time-js";
rewriteTriggers = ["html", "*"]
longLasting = false;
build = undefined;
async rewriteFile(file: string, filePath: string): Promise<string> {
let input = file;
const regex = /\{\&([\s\S]*?)\&\}/gms;
let m;
while ((m = regex.exec(input)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
const before = input.slice(0, m.index);
const expr = m[1];
const after = input.slice(m.index + m[0].length);
const result = eval(expr); // You can replace this with Function() for sandboxing
input = before + result + after;
// Reset regex lastIndex since we changed the string
regex.lastIndex = 0;
}
return input;
}
}