aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/nuldoc/nuldoc-src/config.ts
blob: 95d79415f55eb7de437fb4d60032c5e0efddafc1 (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
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(),
    }),
  }),
  site: z.object({
    author: z.string(),
    copyrightYear: z.number(),
  }),
  sites: z.object({
    default: z.object({
      fqdn: z.string(),
      siteName: z.string(),
    }),
    about: z.object({
      fqdn: z.string(),
      siteName: z.string(),
    }),
    blog: z.object({
      fqdn: z.string(),
      siteName: z.string(),
    }),
    slides: z.object({
      fqdn: z.string(),
      siteName: z.string(),
    }),
  }),
  blog: z.object({
    postsPerPage: z.number().default(10),
    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)));
}