feat: allow customizing layout import source, or fallback to using no layout without throwing an error

This commit is contained in:
aspizu
2025-03-10 03:26:07 +05:30
parent 595673bae6
commit 0359223d46
2 changed files with 22 additions and 8 deletions
+13 -6
View File
@@ -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,
}
},