aboutsummaryrefslogtreecommitdiffhomepage
path: root/nuldoc-src/components/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'nuldoc-src/components/utils.ts')
-rw-r--r--nuldoc-src/components/utils.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/nuldoc-src/components/utils.ts b/nuldoc-src/components/utils.ts
new file mode 100644
index 0000000..f0de71f
--- /dev/null
+++ b/nuldoc-src/components/utils.ts
@@ -0,0 +1,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");
+}