Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(share): generate browser-openable share links on the desktop (#10589)
Elian Doranbe1eeb6dea40
diff --git a/apps/client/src/services/share_link.spec.ts b/apps/client/src/services/share_link.spec.tsnew file mode 100644index 0000000000..a14664de37--- /dev/null+++ b/apps/client/src/services/share_link.spec.ts@@ -0,0 +1,38 @@+import { afterEach, describe, expect, it } from "vitest";++import { buildShareLink } from "./share_link";++describe("buildShareLink", () => {+ const originalGlob = window.glob;++ afterEach(() => {+ window.glob = originalGlob;+ });++ function setGlob(patch: Record<string, unknown>) {+ window.glob = { ...patch } as typeof window.glob;+ }++ it("uses the configured sync server host when set (regardless of the local origin)", () => {+ setGlob({ httpBaseUrl: "http://127.0.0.1:37742" });+ expect(buildShareLink("abc123", "https://notes.example.com")).toBe("https://notes.example.com/share/abc123");+ });++ it("uses the injected loopback origin on the desktop renderer (not the trilium-app:// location)", () => {+ // The renderer loads from trilium-app://app/, so without httpBaseUrl the link would be+ // trilium-app://app/share/... — the regression this guards against.+ setGlob({ httpBaseUrl: "http://127.0.0.1:37742" });+ expect(buildShareLink("abc123", undefined)).toBe("http://127.0.0.1:37742/share/abc123");+ });++ it("derives from the page origin when there is no sync server and no injected origin (server / browser)", () => {+ setGlob({});+ // happy-dom serves the page from http://localhost:3000/ by default.+ expect(buildShareLink("abc123", null)).toBe("http://localhost:3000/share/abc123");+ });++ it("resolves an empty shareId (share root) to the /share/ base", () => {+ setGlob({ httpBaseUrl: "http://127.0.0.1:37742" });+ expect(buildShareLink("", undefined)).toBe("http://127.0.0.1:37742/share/");+ });+});diff --git a/apps/client/src/services/share_link.ts b/apps/client/src/services/share_link.tsnew file mode 100644index 0000000000..64cdd02e55--- /dev/null+++ b/apps/client/src/services/share_link.ts@@ -0,0 +1,27 @@+/**+ * Builds the browser-openable URL for a shared note.+ *+ * Precedence: an explicitly configured sync server wins; otherwise the desktop+ * renderer uses the server-injected loopback origin (`glob.httpBaseUrl`) because it+ * loads from `trilium-app://`, where location-based derivation would yield an unusable+ * `trilium-app://app/share/...` link (#10589); everywhere else we derive from the page+ * origin (which is correct for the server build and for a browser hitting the desktop).+ */+export function buildShareLink(shareId: string, syncServerHost: string | null | undefined): string {+ if (syncServerHost) {+ return new URL(`/share/${shareId}`, syncServerHost).href;+ }++ if (window.glob.httpBaseUrl) {+ return new URL(`/share/${shareId}`, window.glob.httpBaseUrl).href;+ }++ let host = location.host;+ if (host.endsWith("/")) {+ // seems like IE has trailing slash+ // https://github.com/zadam/trilium/issues/3782+ host = host.substring(0, host.length - 1);+ }++ return `${location.protocol}//${host}${location.pathname}share/${shareId}`;+}diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.tsindex 9dbbaaaeab..a7a96ab347 100644--- a/apps/client/src/widgets/note_tree.ts+++ b/apps/client/src/widgets/note_tree.ts@@ -22,6 +22,7 @@ import type { AttributeRow, BranchRow } from "../services/load_results.js";import noteCreateService from "../services/note_create.js";import options from "../services/options.js";import server from "../services/server.js";+import { buildShareLink } from "../services/share_link.js";import shortcutService from "../services/shortcuts.js";import toastService from "../services/toast.js";import treeService from "../services/tree.js";@@ -1956,7 +1957,9 @@ function buildEnhanceTitle() {// Add shared indicator with tooltip if note is sharedif (note.isShared()) {const shareId = note.getOwnedLabelValue("shareAlias") || note.noteId;- const shareUrl = `${location.origin}${location.pathname}share/${shareId}`;+ // Pass no sync host to preserve this tooltip's prior local-origin behavior; the helper+ // still substitutes the loopback origin for the trilium-app:// desktop renderer (#10589).+ const shareUrl = buildShareLink(shareId, undefined);const tooltipText = t("note_tree.shared-indicator-tooltip-with-url", { url: shareUrl });const $sharedIndicator = $(`<span class="note-indicator-icon shared-indicator"></span>`);diff --git a/apps/client/src/widgets/shared_info.tsx b/apps/client/src/widgets/shared_info.tsxindex bbcdbe9828..fba2369c8a 100644--- a/apps/client/src/widgets/shared_info.tsx+++ b/apps/client/src/widgets/shared_info.tsx@@ -5,6 +5,7 @@ import { useEffect, useState } from "preact/hooks";import FNote from "../entities/fnote";import attributes from "../services/attributes";import { t } from "../services/i18n";+import { buildShareLink } from "../services/share_link";import { isElectron } from "../services/utils";import HelpButton from "./react/HelpButton";import { useNoteContext, useTriliumEvent, useTriliumOption } from "./react/hooks";@@ -40,21 +41,7 @@ export function useShareInfo(note: FNote | null | undefined) {return;}- let link;- const shareId = getShareId(note);-- if (syncServerHost) {- link = new URL(`/share/${shareId}`, syncServerHost).href;- } else {- let host = location.host;- if (host.endsWith("/")) {- // seems like IE has trailing slash- // https://github.com/zadam/trilium/issues/3782- host = host.substring(0, host.length - 1);- }-- link = `${location.protocol}//${host}${location.pathname}share/${shareId}`;- }+ const link = buildShareLink(getShareId(note), syncServerHost);setLink(`<a href="${link}" class="external tn-link">${link}</a>`);setLinkHref(link);