feat: support template in all code blocks

This commit is contained in:
Morten Olsen
2025-05-18 20:31:23 +02:00
parent 1cb885bb32
commit 1b2d345420
5 changed files with 132 additions and 88 deletions

View File

@@ -0,0 +1,34 @@
import Handlebars from "handlebars";
import { ExecutionHandler } from "../execution.js";
const codeHandler: ExecutionHandler = ({
node,
addStep,
}) => {
if (node.type !== 'code' || node.lang === 'http') {
return;
}
const optionParts = node.meta?.split(',') || [];
const options = Object.fromEntries(
optionParts.filter(Boolean).map((option) => {
const [key, value] = option.split('=');
return [key.trim(), value?.trim() || true];
})
);
addStep({
type: 'code',
node,
action: async ({ context }) => {
node.meta = undefined;
if (options['no-tmpl'] === true) {
return;
}
const template = Handlebars.compile(node.value);
const content = template(context);
node.value = content;
},
});
};
export { codeHandler };