aboutsummaryrefslogtreecommitdiffhomepage
path: root/nuldoc-src/templates/utils.ts
blob: 018c46036a1272367a3b3a4913fa11f5543d7bc6 (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
import { join } from "std/path/mod.ts";
import { crypto, toHashString } from "std/crypto/mod.ts";
import { Element, Node, Text } from "../dom.ts";
import { Config } from "../config.ts";

export function text(content: string): Text {
  return {
    kind: "text",
    content: content,
    raw: false,
  };
}

export function el(
  name: string,
  attrs: [string, string][],
  ...children: Node[]
): Element {
  return {
    kind: "element",
    name: name,
    attributes: new Map(attrs),
    children: children,
  };
}

export 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}`]]);
}

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

export 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);
}