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
+9 -2
View File
@@ -4,6 +4,10 @@ import gemtextVitePlugin from "./vitePlugin.js"
export const ext = "gmi" export const ext = "gmi"
export interface GemtextConfig {
layout?: string
}
type SetupHookParams = HookParameters<"astro:config:setup"> & { type SetupHookParams = HookParameters<"astro:config:setup"> & {
// `addPageExtension` and `contentEntryType` are not a public APIs // `addPageExtension` and `contentEntryType` are not a public APIs
// Add type defs here // Add type defs here
@@ -11,7 +15,10 @@ type SetupHookParams = HookParameters<"astro:config:setup"> & {
addContentEntryType: (contentEntryType: ContentEntryType) => void addContentEntryType: (contentEntryType: ContentEntryType) => void
} }
export default function gemtext(): AstroIntegration { export default function gemtext(config: GemtextConfig): AstroIntegration {
if (!config.layout) {
config.layout = "/src/layouts/Layout.astro"
}
return { return {
name: "astro-gemtext", name: "astro-gemtext",
hooks: { hooks: {
@@ -32,7 +39,7 @@ export default function gemtext(): AstroIntegration {
}) })
params.updateConfig({ params.updateConfig({
vite: { vite: {
plugins: [gemtextVitePlugin()], plugins: [gemtextVitePlugin(config)],
}, },
}) })
}, },
+13 -6
View File
@@ -1,9 +1,9 @@
import {HTMLRenderer, parse} from "gemtext" import {HTMLRenderer, parse} from "gemtext"
import {pathToFileURL} from "url" import {pathToFileURL} from "url"
import type {Plugin} from "vite" 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 result = parse(code)
const html = result.generate(HTMLRenderer) const html = result.generate(HTMLRenderer)
return ` return `
@@ -14,7 +14,11 @@ import {
unescapeHTML, unescapeHTML,
} from "astro/runtime/server/index.js"; } from "astro/runtime/server/index.js";
import { Fragment, jsx as h } from "astro/jsx-runtime"; 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 name = "TypstComponent";
export const html = ${JSON.stringify(html)}; export const html = ${JSON.stringify(html)};
export const frontmatter = undefined; export const frontmatter = undefined;
@@ -28,13 +32,16 @@ export function getHeadings() {
} }
export async function Content() { export async function Content() {
const content = h(Fragment, {"set:html": html}); 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 Content;
` `
} }
export default function gemtextVitePlugin(): Plugin { export default function gemtextVitePlugin(config: GemtextConfig): Plugin {
// let server: ViteDevServer // let server: ViteDevServer
return { return {
name: "astro-gemtext", name: "astro-gemtext",
@@ -42,7 +49,7 @@ export default function gemtextVitePlugin(): Plugin {
transform(code, id) { transform(code, id) {
if (!id.endsWith(`.${ext}`)) return if (!id.endsWith(`.${ext}`)) return
return { return {
code: transform(code, id), code: transform(code, id, config),
map: null, map: null,
} }
}, },