Skip to content

Build your personal knowledge base with Trilium Notes

refactor(commons): share one FNV-1a between link embeds and spreadsheets

Elian Doran3cc4e62e83f3
diff --git a/packages/commons/src/lib/link_embed.ts b/packages/commons/src/lib/link_embed.ts
index 163565a744..f782f873bf 100644
--- a/packages/commons/src/lib/link_embed.ts
+++ b/packages/commons/src/lib/link_embed.ts
@@ -1,3 +1,5 @@
+import { fnv1a } from "./utils.js";
+
export const YOUTUBE_REGEX = /(?:youtube\.com\/watch\?[^\s#]*v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/shorts\/)([a-zA-Z0-9_-]{11})/;
export function extractYouTubeVideoId(url: string): string | null {
@@ -135,7 +137,7 @@ export function linkPreviewImageName(url: string): string {
// URL. The digest identifies it regardless; only the readable part suffers.
}
- return `${capReadable(readable) || "image"}-${shortDigest(canonical)}`;
+ return `${capReadable(readable) || "image"}-${fnv1a(canonical).toString(16).padStart(8, "0")}`;
}
/** How much of the readable part to keep, so a long path does not crowd out the attachment list. */
@@ -157,24 +159,6 @@ function capReadable(value: string): string {
return value.slice(0, PREVIEW_NAME_MAX_READABLE).replace(/^[-.]+|[-.]+$/g, "");
}
-/**
- * A short digest of a string, used only to tell two addresses apart.
- *
- * FNV-1a, which is not a security primitive and is not asked to be one. Web Crypto is not an option
- * here: Trilium is served over plain HTTP as often as not, and `crypto.subtle` exists only in a
- * secure context.
- */
-function shortDigest(value: string): string {
- let hash = 0x811c9dc5;
-
- for (let i = 0; i < value.length; i++) {
- hash ^= value.charCodeAt(i);
- hash = Math.imul(hash, 0x01000193);
- }
-
- return (hash >>> 0).toString(16).padStart(8, "0");
-}
-
export interface LinkEmbedMetadata {
url: string;
title?: string;
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index 793be60ad4..e70ed3025b 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -12,6 +12,7 @@
import { format as formatNumfmt, formatColor as formatNumfmtColor } from "numfmt";
+import { fnv1a } from "../utils.js";
import {
BorderStyle,
type CellDocumentSegment,
@@ -125,13 +126,18 @@ const GRIDLINE_RULE = ":where(.spreadsheet-table.show-gridlines) td:not(.has-fil
class StyleClasses {
private readonly names = new Map<string, string>();
- /** Returns the class for `declarations`, or "" when there is nothing to style. */
+ /**
+ * Returns the class for `declarations`, or "" when there is nothing to style.
+ *
+ * A hash collision would show one style where another was meant, never a wrong document, and
+ * 32 bits is far more than the handful of distinct styles a workbook carries.
+ */
classFor(declarations: string): string {
if (!declarations) return "";
let name = this.names.get(declarations);
if (!name) {
- name = `sst-${hashDeclarations(declarations)}`;
+ name = `sst-${fnv1a(declarations).toString(36)}`;
this.names.set(declarations, name);
}
return name;
@@ -153,20 +159,6 @@ class StyleClasses {
}
}
-/**
- * Hashes a declaration string (FNV-1a, base36) for use in a class name. A collision would show
- * one style where another was meant, never a wrong document, and 32 bits is far more than the
- * handful of distinct styles a workbook carries.
- */
-function hashDeclarations(declarations: string): string {
- let hash = 0x811c9dc5;
- for (let i = 0; i < declarations.length; i++) {
- hash ^= declarations.charCodeAt(i);
- hash = Math.imul(hash, 0x01000193);
- }
- return (hash >>> 0).toString(36);
-}
-
// #region Images
/** A floating image resolved to its content-space box (header offsets removed), ready to emit. */
diff --git a/packages/commons/src/lib/utils.spec.ts b/packages/commons/src/lib/utils.spec.ts
index c61372ddc6..5ca0f58751 100644
--- a/packages/commons/src/lib/utils.spec.ts
+++ b/packages/commons/src/lib/utils.spec.ts
@@ -1,4 +1,4 @@
-import { deferred, formatLogMessage } from "./utils.js";
+import { deferred, fnv1a, formatLogMessage } from "./utils.js";
import { describe, expect, it } from "vitest";
describe("#deferred", () => {
@@ -83,3 +83,23 @@ describe("#deferred resolution", () => {
await expect(d2).rejects.toThrow("x");
});
});
+
+describe("#fnv1a", () => {
+ it("returns the FNV-1a reference digests as unsigned 32-bit numbers", () => {
+ expect(fnv1a("")).toBe(0x811c9dc5);
+ expect(fnv1a("a")).toBe(0xe40c292c);
+ expect(fnv1a("foobar")).toBe(0xbf9cf968);
+ });
+
+ it("is stable, distinguishes similar inputs and stays in range", () => {
+ expect(fnv1a("https://example.com/a")).toBe(fnv1a("https://example.com/a"));
+ expect(fnv1a("https://example.com/a")).not.toBe(fnv1a("https://example.com/b"));
+
+ for (const value of ["", "\u00ff", "\u4e2d\u6587", "x".repeat(1000)]) {
+ const hash = fnv1a(value);
+ expect(Number.isInteger(hash)).toBe(true);
+ expect(hash).toBeGreaterThanOrEqual(0);
+ expect(hash).toBeLessThanOrEqual(0xffffffff);
+ }
+ });
+});
diff --git a/packages/commons/src/lib/utils.ts b/packages/commons/src/lib/utils.ts
index a7698533e6..32dfcbb5b3 100644
--- a/packages/commons/src/lib/utils.ts
+++ b/packages/commons/src/lib/utils.ts
@@ -60,3 +60,21 @@ export function deferred<T>(): DeferredPromise<T> {
return promise as DeferredPromise<T>;
})();
}
+
+/**
+ * Hashes a string with FNV-1a, returning the digest as an unsigned 32-bit number.
+ *
+ * Not a security primitive and not asked to be one: callers use it to tell two values apart. Web
+ * Crypto is not an option here — Trilium is served over plain HTTP as often as not, and
+ * `crypto.subtle` exists only in a secure context.
+ */
+export function fnv1a(value: string): number {
+ let hash = 0x811c9dc5;
+
+ for (let i = 0; i < value.length; i++) {
+ hash ^= value.charCodeAt(i);
+ hash = Math.imul(hash, 0x01000193);
+ }
+
+ return hash >>> 0;
+}