summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/components/utils.ts
blob: ce69310056f1c6df250851f66ca29b65be9c48c0 (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 { Hash } from "checksum/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: Record<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);
  return new Hash("md5").digest(content).hex();
}