summaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 215b0331f50d1a77ea70f75bdbafe5a84ab464a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type {AstroIntegration, ContentEntryType, HookParameters} from "astro"
import {fileURLToPath} from "url"
import gemtextVitePlugin from "./vitePlugin.js"

export const ext = "gmi"

export interface GemtextConfig {
    layout?: string
}

type SetupHookParams = HookParameters<"astro:config:setup"> & {
    // `addPageExtension` and `contentEntryType` are not a public APIs
    // Add type defs here
    addPageExtension: (extension: string) => void
    addContentEntryType: (contentEntryType: ContentEntryType) => void
}

export default function gemtext(config: GemtextConfig = {}): AstroIntegration {
    if (!config.layout) {
        config.layout = "/src/layouts/Layout.astro"
    }
    return {
        name: "astro-gemtext",
        hooks: {
            "astro:config:setup": async (_params) => {
                const params = _params as SetupHookParams
                params.addRenderer({
                    name: "astro-gemtext",
                    serverEntrypoint: fileURLToPath(
                        new URL("./renderer.js", import.meta.url),
                    ),
                })
                params.addPageExtension(ext)
                params.addContentEntryType({
                    extensions: [`.${ext}`],
                    getEntryInfo: async (params) => {
                        throw new Error("Not implemented")
                    },
                })
                params.updateConfig({
                    vite: {
                        plugins: [gemtextVitePlugin(config)],
                    },
                })
            },
        },
    }
}