summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/config.ts
blob: 5a07896f0bd971cabceddcb5dbee204df7edf09a (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
import { join } from "@std/path";
import { parse as parseToml } from "@std/toml";
import { z } from "zod/mod.ts";

const ConfigSchema = z.object({
  locations: z.object({
    contentDir: z.string(),
    destDir: z.string(),
    staticDir: z.string(),
  }),
  rendering: z.object({
    html: z.object({
      indentWidth: z.number(),
    }),
  }),
  blog: z.object({
    author: z.string(),
    fqdn: z.string(),
    siteName: z.string(),
    siteCopyrightYear: z.number(),
    tagLabels: z.record(z.string(), z.string()),
  }),
});

export type Config = z.infer<typeof ConfigSchema>;

export function getTagLabel(c: Config, slug: string): string {
  if (!(slug in c.blog.tagLabels)) {
    throw new Error(`Unknown tag: ${slug}`);
  }
  return c.blog.tagLabels[slug];
}

export function getDefaultConfigPath(): string {
  return join(Deno.cwd(), "nuldoc.toml");
}

export async function loadConfig(filePath: string): Promise<Config> {
  return ConfigSchema.parse(parseToml(await Deno.readTextFile(filePath)));
}