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(client): navigate from the app's own URL when a query precedes the hash
matt wilkie3475c9c1f1f2
diff --git a/apps/client/src/services/link.spec.ts b/apps/client/src/services/link.spec.tsindex 3a0effad99..126f77b5f2 100644--- a/apps/client/src/services/link.spec.ts+++ b/apps/client/src/services/link.spec.ts@@ -156,6 +156,42 @@ describe("parseNavigationStateFromUrl", () => {// hash present at index 0, but the path is too short to be a valid note idexpect(parseNavigationStateFromUrl("#ab")).toStrictEqual({});});++ it("parses the app's own address with a query or a slash-less sub-path before the hash", () => {+ const note = { notePath: "root/WWaBNf3SSA1b", noteId: "WWaBNf3SSA1b" };+ const desktop = new URL("https://host/?desktop");+ const mobile = new URL("https://host/?mobile");+ const subPath = new URL("https://host/trilium");++ expect(parseNavigationStateFromUrl("https://host/?desktop#root/WWaBNf3SSA1b", desktop))+ .toMatchObject(note);+ expect(parseNavigationStateFromUrl("https://host/?mobile#root/WWaBNf3SSA1b", mobile))+ .toMatchObject(note);+ expect(parseNavigationStateFromUrl("https://host/trilium#root/WWaBNf3SSA1b", subPath))+ .toMatchObject(note);+ expect(parseNavigationStateFromUrl("https://host/?desktop#?searchString=hello", desktop))+ .toMatchObject({ searchString: "hello" });++ // Another origin or another pathname is an external document, whatever its hash holds.+ expect(parseNavigationStateFromUrl("https://other/?desktop#root/WWaBNf3SSA1b", desktop))+ .toStrictEqual({});+ expect(parseNavigationStateFromUrl("https://host/wiki/Page?x=1#root/WWaBNf3SSA1b", desktop))+ .toStrictEqual({});+ });++ it("compares against window.location by default", () => {+ const { happyDOM } = window as unknown as { happyDOM: { setURL(url: string): void } };+ const previousUrl = window.location.href;+ happyDOM.setURL("https://host/?desktop");++ try {+ const url = `${window.location.href.replace(/#.*/, "")}#root/WWaBNf3SSA1b`;+ expect(parseNavigationStateFromUrl(url))+ .toMatchObject({ notePath: "root/WWaBNf3SSA1b" });+ } finally {+ happyDOM.setURL(previousUrl);+ }+ });});describe("calculateHash", () => {diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.tsindex 57951769ad..832ed508d4 100644--- a/apps/client/src/services/link.ts+++ b/apps/client/src/services/link.ts@@ -332,7 +332,10 @@ function isExtraWindowUrl(url: string, hashIdx: number) {return /[?&]extraWindow(?:[=&]|$)/.test(url.slice(0, hashIdx));}-export function parseNavigationStateFromUrl(url: string | undefined) {+export function parseNavigationStateFromUrl(+ url: string | undefined,+ location: UrlParts = window.location+) {if (!url) {return {};}@@ -346,7 +349,13 @@ export function parseNavigationStateFromUrl(url: string | undefined) {const isExtraWindow = isExtraWindowUrl(url, hashIdx);// Exclude external links that contain #- if (hashIdx !== 0 && !url.includes("/#root") && !url.includes("/#?searchString") && !isExtraWindow) {+ if (+ hashIdx !== 0+ && !url.includes("/#root")+ && !url.includes("/#?searchString")+ && !isExtraWindow+ && !isSameDocumentUrl(url, hashIdx, location)+ ) {return {};}@@ -426,6 +435,13 @@ export function parseNavigationStateFromUrl(url: string | undefined) {};}+/** Whether `url` addresses the document at `location`, ignoring the query string and the hash. */+function isSameDocumentUrl(url: string, hashIdx: number, location: UrlParts) {+ const [ documentUrl ] = url.slice(0, hashIdx).split("?");++ return documentUrl === `${location.protocol}//${location.host}${location.pathname}`;+}+/** Iterates the `name=value` pairs of a hash's parameter string, decoding both sides. */function parseHashParams(paramString: string | undefined) {if (!paramString) {