feature: support more than html for SSI

This commit is contained in:
Andy Burke 2025-07-14 19:22:59 -07:00
parent 02c10aaa72
commit b3e5a4e1b4
4 changed files with 190 additions and 4 deletions

View file

@ -4,6 +4,7 @@
*/
import * as path from '@std/path';
import { md_to_html } from '@andyburke/serverus/handlers/markdown';
// https://stackoverflow.com/a/75205316
const replaceAsync = async (str: string, regex: RegExp, asyncFn: (match: any, ...args: any) => Promise<any>) => {
@ -34,7 +35,27 @@ async function load_html_with_ssi(html_file: string): Promise<string> {
break;
}
return await load_html_with_ssi(resolved);
const HANDLERS: Record<string, (include_path: string) => Promise<string> | string> = {
default: async (include_path: string) => {
try {
return await Deno.readTextFile(include_path);
} catch (error) {
console.dir({ error });
throw error;
}
},
'.md': async (include_path: string) => {
const markdown_content = await Deno.readTextFile(include_path);
return md_to_html(markdown_content);
},
'.html': load_html_with_ssi
};
const extension = path.extname(resolved);
const handler = extension ? (HANDLERS[extension.toLowerCase()] ?? HANDLERS.default) : HANDLERS.default;
return await handler(resolved);
}
default: {