aboutsummaryrefslogtreecommitdiffhomepage
path: root/nuldoc-src/components/page_layout.ts
blob: d76e3b2944b9dd30e2c1909ac5de618ef520bfba (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { crypto, toHashString } from "std/crypto/mod.ts";
import { join } from "std/path/mod.ts";
import { Config } from "../config.ts";
import { el, Element, text } from "../dom.ts";

type Params = {
  metaCopyrightYear: number;
  metaDescription: string;
  metaKeywords: string[];
  metaTitle: string;
  requiresSyntaxHighlight: boolean;
};

export async function pageLayout(
  {
    metaCopyrightYear,
    metaDescription,
    metaKeywords,
    metaTitle,
    requiresSyntaxHighlight,
  }: Params,
  body: Element,
  config: Config,
): Promise<Element> {
  const head = el(
    "head",
    [],
    metaElement([["charset", "UTF-8"]]),
    metaElement([["name", "viewport"], [
      "content",
      "width=device-width, initial-scale=1.0",
    ]]),
    metaElement([["name", "author"], ["content", config.blog.author]]),
    metaElement([["name", "copyright"], [
      "content",
      `&copy; ${metaCopyrightYear} ${config.blog.author}`,
    ]]),
    metaElement([["name", "description"], [
      "content",
      metaDescription,
    ]]),
    ...(metaKeywords.length === 0 ? [] : [
      metaElement([["name", "keywords"], [
        "content",
        metaKeywords.join(","),
      ]]),
    ]),
    linkElement("icon", "/favicon.svg", "image/svg+xml"),
    el("title", [], text(`${metaTitle} | ${config.blog.siteName}`)),
    await stylesheetLinkElement("/style.css", config),
    ...(
      requiresSyntaxHighlight
        ? [await stylesheetLinkElement("/hl.css", config)]
        : []
    ),
  );
  return el(
    "html",
    [["lang", "ja-JP"]],
    head,
    body,
  );
}

async function stylesheetLinkElement(
  fileName: string,
  config: Config,
): Promise<Element> {
  const filePath = join(Deno.cwd(), config.locations.staticDir, fileName);
  const content = (await Deno.readFile(filePath)).buffer;
  const hash = toHashString(await crypto.subtle.digest("MD5", content), "hex");
  return el("link", [["rel", "stylesheet"], ["href", `${fileName}?h=${hash}`]]);
}

function metaElement(attrs: [string, string][]): Element {
  return el("meta", attrs);
}

function linkElement(
  rel: string,
  href: string,
  type: string | null,
): Element {
  const attrs: [string, string][] = [["rel", rel], ["href", href]];
  if (type !== null) {
    attrs.push(["type", type]);
  }
  return el("link", attrs);
}