Skip to content

Build your personal knowledge base with Trilium Notes

feat(clipper): drop the globe icon from existing clippings

Elian Doran7873720d68b9
diff --git a/packages/trilium-core/src/migrations/0241__drop_clipper_globe_icon.spec.ts b/packages/trilium-core/src/migrations/0241__drop_clipper_globe_icon.spec.ts
new file mode 100644
index 0000000000..6a7b4cfe64
--- /dev/null
+++ b/packages/trilium-core/src/migrations/0241__drop_clipper_globe_icon.spec.ts
@@ -0,0 +1,79 @@
+import { readFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+import { beforeEach, describe, expect, it } from "vitest";
+
+import becca from "../becca/becca.js";
+import becca_loader from "../becca/becca_loader.js";
+import BBranch from "../becca/entities/bbranch.js";
+import BNote from "../becca/entities/bnote.js";
+import * as cls from "../services/context.js";
+import { getSql } from "../services/sql/index.js";
+import migration from "./0241__drop_clipper_globe_icon.js";
+
+const __dirname = dirname(fileURLToPath(import.meta.url));
+
+describe("Migration 0241: clippings drop the globe icon the clipper wrote", () => {
+ beforeEach(async () => {
+ // Resolved here rather than at describe-collection time, after initializeCore() has run.
+ getSql().rebuildFromBuffer(readFileSync(join(__dirname, "../test/fixtures/document.db")));
+ await inContext(() => becca_loader.load());
+ });
+
+ it("removes the clipper's globe from clippings and leaves every other icon", async () => {
+ await inContext(() => {
+ buildNote("clipped", { clipType: "page", iconClass: "bx bx-globe" });
+ buildNote("customized", { clipType: "note", iconClass: "bx bx-star" });
+ buildNote("notClipped", { iconClass: "bx bx-globe" });
+ const parent = buildNote("inheritsGlobe", { clipType: "tabs" });
+ parent.addLabel("iconClass", "bx bx-globe", true);
+ buildNote("child", {}, "inheritsGlobe");
+ });
+
+ await inContext(() => migration());
+ await inContext(() => becca_loader.load());
+
+ await inContext(() => {
+ const iconOf = (noteId: string) => becca.getNoteOrThrow(noteId).getOwnedLabelValue("iconClass");
+
+ expect(iconOf("clipped")).toBeNull();
+ expect(becca.getNoteOrThrow("clipped").getOwnedLabelValue("clipType")).toBe("page");
+ expect(iconOf("customized")).toBe("bx bx-star");
+ expect(iconOf("notClipped")).toBe("bx bx-globe");
+ // An inheritable globe was written by the user, not by the clipper.
+ expect(iconOf("inheritsGlobe")).toBe("bx bx-globe");
+ });
+ });
+});
+
+/** Runs `fn` inside a CLS context, which entity writes and becca loads both require. */
+function inContext<T>(fn: () => T): Promise<T> {
+ return new Promise<T>((resolve, reject) => {
+ cls.getContext().init(() => {
+ try {
+ resolve(fn());
+ } catch (e) {
+ reject(e);
+ }
+ });
+ });
+}
+
+function buildNote(noteId: string, labels: Record<string, string>, parentNoteId = "root") {
+ const note = new BNote({
+ noteId,
+ title: noteId,
+ type: "text",
+ mime: "text/html",
+ isProtected: false,
+ blobId: ""
+ }).save();
+ new BBranch({ noteId, parentNoteId }).save();
+
+ for (const [ name, value ] of Object.entries(labels)) {
+ note.addLabel(name, value);
+ }
+
+ return note;
+}
diff --git a/packages/trilium-core/src/migrations/0241__drop_clipper_globe_icon.ts b/packages/trilium-core/src/migrations/0241__drop_clipper_globe_icon.ts
new file mode 100644
index 0000000000..05def97b38
--- /dev/null
+++ b/packages/trilium-core/src/migrations/0241__drop_clipper_globe_icon.ts
@@ -0,0 +1,29 @@
+import becca from "../becca/becca";
+import becca_loader from "../becca/becca_loader";
+import { getContext } from "../services/context";
+
+/** The icon the web clipper wrote onto every clipping that had a page URL. */
+const CLIPPER_GLOBE_ICON = "bx bx-globe";
+
+/**
+ * Deletes the `#iconClass` the web clipper wrote onto its clippings, so they take the icon
+ * `getNoteIcon()` derives from their `#clipType`. Only an owned, non-inheritable label holding
+ * exactly the clipper's value is deleted; any other icon was set by the user.
+ */
+export default () => {
+ getContext().init(() => {
+ becca_loader.load();
+
+ for (const note of Object.values(becca.notes)) {
+ if (!note.hasOwnedLabel("clipType")) {
+ continue;
+ }
+
+ for (const label of note.getOwnedLabels("iconClass")) {
+ if (label.value === CLIPPER_GLOBE_ICON && !label.isInheritable) {
+ label.markAsDeleted();
+ }
+ }
+ }
+ });
+};
diff --git a/packages/trilium-core/src/migrations/migrations.ts b/packages/trilium-core/src/migrations/migrations.ts
index 5e06935d9c..304e162ec9 100644
--- a/packages/trilium-core/src/migrations/migrations.ts
+++ b/packages/trilium-core/src/migrations/migrations.ts
@@ -9,6 +9,11 @@ export function getMaxMigrationVersion() {
// Migrations should be kept in descending order, so the latest migration is first.
export const MIGRATIONS: (SqlMigration | JsMigration)[] = [
+ // Delete the globe icon the web clipper wrote onto clippings, so they take the icon of their clipType
+ {
+ version: 241,
+ module: () => import("./0241__drop_clipper_globe_icon.js")
+ },
// Give every board its own select definition for the label it groups by, so the columns stop
// living only in the board.json attachment
{
diff --git a/packages/trilium-core/src/test/fixtures/document.db b/packages/trilium-core/src/test/fixtures/document.db
index f6f1272683..c17975a086 100644
Binary files a/packages/trilium-core/src/test/fixtures/document.db and b/packages/trilium-core/src/test/fixtures/document.db differ