summaryrefslogtreecommitdiff
path: root/src/vitePlugin.ts
diff options
context:
space:
mode:
authoraspizu <aspizu@protonmail.com>2025-03-10 02:58:09 +0530
committeraspizu <aspizu@protonmail.com>2025-03-10 02:58:09 +0530
commit8137b02b1ea25836f80e6d61fae7592ddb6837f2 (patch)
tree41c14a3a75bec0dc2a83c6de36b56f88aa1b4404 /src/vitePlugin.ts
initial commit
Diffstat (limited to 'src/vitePlugin.ts')
-rw-r--r--src/vitePlugin.ts50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/vitePlugin.ts b/src/vitePlugin.ts
new file mode 100644
index 0000000..df4433d
--- /dev/null
+++ b/src/vitePlugin.ts
@@ -0,0 +1,50 @@
+import {HTMLRenderer, parse} from "gemtext"
+import {pathToFileURL} from "url"
+import type {Plugin} from "vite"
+import {ext} from "./index.js"
+
+function transform(code: string, id: string) {
+ const result = parse(code)
+ const html = result.generate(HTMLRenderer)
+ return `
+import {
+ createComponent,
+ render,
+ renderComponent,
+ unescapeHTML,
+} from "astro/runtime/server/index.js";
+import { Fragment, jsx as h } from "astro/jsx-runtime";
+import Layout from "../layouts/Layout.astro";
+export const name = "TypstComponent";
+export const html = ${JSON.stringify(html)};
+export const frontmatter = undefined;
+export const file = ${JSON.stringify(id)};
+export const url = ${JSON.stringify(pathToFileURL(id))};
+export function compiledContent() {
+ return html;
+}
+export function getHeadings() {
+ return undefined;
+}
+export async function Content() {
+ const content = h(Fragment, {"set:html": html});
+ return h(Layout, {title: url, children: content});
+}
+export default Content;
+`
+}
+
+export default function gemtextVitePlugin(): Plugin {
+ // let server: ViteDevServer
+ return {
+ name: "astro-gemtext",
+ enforce: "pre",
+ transform(code, id) {
+ if (!id.endsWith(`.${ext}`)) return
+ return {
+ code: transform(code, id),
+ map: null,
+ }
+ },
+ }
+}