mirror of
https://github.com/morten-olsen/http.md.git
synced 2026-02-08 00:46:28 +01:00
feat: support template in all code blocks
This commit is contained in:
34
src/execution/handlers/handlers.code.ts
Normal file
34
src/execution/handlers/handlers.code.ts
Normal 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 };
|
||||
Reference in New Issue
Block a user