summaryrefslogtreecommitdiff
path: root/src/index.ts
blob: a9fd873b8de8906e6ea880ced09d066aaa32858a (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type {AstroIntegration, ContentEntryType, HookParameters} from "astro"
import {fileURLToPath} from "url"
import gemtextVitePlugin from "./vitePlugin.js"
import { parse as parseYaml } from "yaml";

export const ext = "gmi"

/** Gemtext configuration options */
export interface GemtextConfig {
    /** Absolute import path to the layout to use for all gemtext pages.
     * If not provided, HMR and the Astro toolbar will not work. (default: none) */
    layout?: string
    /** The format to use for the page title. Can be one of:
     * - `first-heading`: The first heading in the document will be used as the title. (default)
     * - `filename`: The filename of the document will be used as the title.
     */
    titleFormat?: "first-heading" | "filename"
}

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.titleFormat) {
        config.titleFormat = "first-heading"
    }
    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 ({ fileUrl, contents }) => {
                      const lines = contents.split('\n');
                      let data: Record<string, unknown> = {};
                      let frontmatter = '';

                      if (lines.at(0) === '---' && lines.lastIndexOf('---')) {
                        frontmatter = lines.slice(1, lines.indexOf('---', 1)).join('\n');
                        try {
                          data = { ...data, ...parseYaml(frontmatter, { schema: 'yaml-1.1' }) };
                        } catch { }
                      }

                      return {
                        data,
                        rawData: frontmatter,
                        body: contents,
                        slug: typeof data['slug'] === 'string' ? data['slug'] : fileUrl.pathname,
                      };
                    },
                })
                params.updateConfig({
                    vite: {
                        plugins: [gemtextVitePlugin(config)],
                    },
                })
            },
        },
    }
}