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:
43
src/execution/handlers/handlers.md.ts
Normal file
43
src/execution/handlers/handlers.md.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { dirname, resolve } from 'path';
|
||||
import { toString } from 'mdast-util-to-string'
|
||||
import { execute, type ExecutionHandler } from '../execution.js';
|
||||
|
||||
const fileHandler: ExecutionHandler = ({
|
||||
addStep,
|
||||
node,
|
||||
parent,
|
||||
index,
|
||||
file,
|
||||
}) => {
|
||||
if (node.type === 'leafDirective' && node.name === 'md') {
|
||||
addStep({
|
||||
type: 'file',
|
||||
node,
|
||||
action: async ({ context }) => {
|
||||
const filePath = resolve(
|
||||
dirname(file),
|
||||
toString(node)
|
||||
);
|
||||
if (!filePath) {
|
||||
throw new Error('File path is required');
|
||||
}
|
||||
const { root: newRoot } = await execute(filePath, {
|
||||
context,
|
||||
});
|
||||
if (!parent) {
|
||||
throw new Error('Parent node is required');
|
||||
}
|
||||
if (index === undefined) {
|
||||
throw new Error('Index is required');
|
||||
}
|
||||
if (node.attributes?.hidden === '') {
|
||||
parent.children?.splice(index, 1);
|
||||
return;
|
||||
}
|
||||
parent.children?.splice(index, 1, ...newRoot.children as any);
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export { fileHandler };
|
||||
Reference in New Issue
Block a user