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

export async function stylesheetLinkElement(
  fileName: string,
  config: Config,
): Promise<Element> {
  const filePath = join(Deno.cwd(), config.locations.staticDir, fileName);
  const hash = await calculateFileHash(filePath);
  return el("link", [["rel", "stylesheet"], ["href", `${fileName}?h=${hash}`]]);
}

export async function staticScriptElement(
  fileName: string,
  attrs: [string, string][],
  config: Config,
): Promise<Element> {
  const filePath = join(Deno.cwd(), config.locations.staticDir, fileName);
  const hash = await calculateFileHash(filePath);
  return el("script", [["src", `${fileName}?h=${hash}`], ...attrs]);
}

async function calculateFileHash(
  filePath: string,
): Promise<string> {
  const content = (await Deno.readFile(filePath)).buffer;
  return toHashString(await crypto.subtle.digest("MD5", content), "hex");
}