summaryrefslogtreecommitdiff
path: root/src/vitePlugin.ts
diff options
context:
space:
mode:
authoraspizu <aspizu@protonmail.com>2025-03-10 03:26:07 +0530
committeraspizu <aspizu@protonmail.com>2025-03-10 03:26:07 +0530
commit0359223d4634738644dd352a3cea39c27e2bf227 (patch)
treee3fe667a4354af8f0c879d0548232142138852da /src/vitePlugin.ts
parent595673bae6dcdcf2f5c13ce15efaa535e011b894 (diff)
feat: allow customizing layout import source, or fallback to using no layout without throwing an error
Diffstat (limited to 'src/vitePlugin.ts')
-rw-r--r--src/vitePlugin.ts19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/vitePlugin.ts b/src/vitePlugin.ts
index df4433d..48673a8 100644
--- a/src/vitePlugin.ts
+++ b/src/vitePlugin.ts
@@ -1,9 +1,9 @@
import {HTMLRenderer, parse} from "gemtext"
import {pathToFileURL} from "url"
import type {Plugin} from "vite"
-import {ext} from "./index.js"
+import {ext, type GemtextConfig} from "./index.js"
-function transform(code: string, id: string) {
+function transform(code: string, id: string, config: GemtextConfig) {
const result = parse(code)
const html = result.generate(HTMLRenderer)
return `
@@ -14,7 +14,11 @@ import {
unescapeHTML,
} from "astro/runtime/server/index.js";
import { Fragment, jsx as h } from "astro/jsx-runtime";
-import Layout from "../layouts/Layout.astro";
+let Layout = undefined;
+try {
+ Layout = import(${JSON.stringify(config.layout)});
+} catch (e) {
+}
export const name = "TypstComponent";
export const html = ${JSON.stringify(html)};
export const frontmatter = undefined;
@@ -28,13 +32,16 @@ export function getHeadings() {
}
export async function Content() {
const content = h(Fragment, {"set:html": html});
- return h(Layout, {title: url, children: content});
+ if (Layout)
+ return h(Layout, {title: url, children: content});
+ else
+ return content;
}
export default Content;
`
}
-export default function gemtextVitePlugin(): Plugin {
+export default function gemtextVitePlugin(config: GemtextConfig): Plugin {
// let server: ViteDevServer
return {
name: "astro-gemtext",
@@ -42,7 +49,7 @@ export default function gemtextVitePlugin(): Plugin {
transform(code, id) {
if (!id.endsWith(`.${ext}`)) return
return {
- code: transform(code, id),
+ code: transform(code, id, config),
map: null,
}
},