Skip to content

Build your personal knowledge base with Trilium Notes

server/share pages: fix heading links

Adorian Doranc8b32411252e
diff --git a/packages/share-theme/src/page/toc.spec.ts b/packages/share-theme/src/page/toc.spec.ts
index 58bef70daf..2c07a9c7cf 100644
--- a/packages/share-theme/src/page/toc.spec.ts
+++ b/packages/share-theme/src/page/toc.spec.ts
@@ -9,6 +9,7 @@ describe("setupToC", () => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
vi.useRealTimers();
+ history.replaceState(null, "", location.pathname);
});
it("marks the entry of the last heading scrolled past, whatever the heading levels", () => {
@@ -128,6 +129,19 @@ describe("setupToC", () => {
expect(scrollIntoView).not.toHaveBeenCalled();
});
+ it("leaves the block of a block link out of the section's address", async () => {
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ vi.stubGlobal("navigator", { clipboard: { writeText } });
+ renderPage([ [ 2, "intro", 100 ] ]);
+ const expected = new URL(`${location.pathname}?lang=en#intro`, location.href).href;
+ history.replaceState(null, "", "?block=a:b&lang=en");
+
+ headingLink("intro").click();
+
+ expect(location.href).toBe(expected);
+ await vi.waitFor(() => expect(writeText).toHaveBeenCalledExactlyOnceWith(expected));
+ });
+
it("leaves a click with a modifier key or another button to the browser", () => {
const writeText = vi.fn().mockResolvedValue(undefined);
vi.stubGlobal("navigator", { clipboard: { writeText } });
diff --git a/packages/share-theme/src/page/toc.ts b/packages/share-theme/src/page/toc.ts
index 1072ea7e70..590e8200ff 100644
--- a/packages/share-theme/src/page/toc.ts
+++ b/packages/share-theme/src/page/toc.ts
@@ -62,6 +62,7 @@ const COPIED_DURATION = 1500;
/**
* Makes a click on the link of a heading put the address of its section in the location bar, in
* place of the current one and without scrolling, and copy it, showing a check mark for a moment.
+ * The address leaves out the `block` parameter of a block link, which would scroll to the block.
* A click with a modifier key or another button than the main one is left to the browser.
*/
function setupHeadingLinks() {
@@ -80,8 +81,10 @@ function setupHeadingLinks() {
}
e.preventDefault();
- history.replaceState(history.state, "", link.href);
- navigator.clipboard?.writeText(link.href).then(() => {
+ const url = new URL(link.href);
+ url.searchParams.delete("block");
+ history.replaceState(history.state, "", url);
+ navigator.clipboard?.writeText(url.href).then(() => {
setCopied(true);
clearTimeout(resetTimer);
resetTimer = setTimeout(() => setCopied(false), COPIED_DURATION);