From 3a16879ddaf0f8f6ff3c2850caeedaa8267b1a86 Mon Sep 17 00:00:00 2001 From: Tony Wu Date: Fri, 1 May 2026 10:52:57 +0800 Subject: [PATCH 1/2] Fix: Remove `?highlight=` from URL when highlights are dismissed via clicking (#1841) --- .../front-end/searcher/searcher.js | 5 +++++ tests/gui/search.goml | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/crates/mdbook-html/front-end/searcher/searcher.js b/crates/mdbook-html/front-end/searcher/searcher.js index 5c4bab65..b5deebcc 100644 --- a/crates/mdbook-html/front-end/searcher/searcher.js +++ b/crates/mdbook-html/front-end/searcher/searcher.js @@ -341,6 +341,11 @@ window.search = window.search || {}; marker.unmark(); }, 300); } + // also removes the `?URL_MARK_PARAM=` search param so that + // in-page navigation doesn't make highlights unexpectedly appear again + const url = new URL(window.location.href); + url.searchParams.delete(URL_MARK_PARAM); + history.replaceState(null, '', url); }; for (let i = 0; i < markers.length; i++) { diff --git a/tests/gui/search.goml b/tests/gui/search.goml index 02efdf24..12ea7098 100644 --- a/tests/gui/search.goml +++ b/tests/gui/search.goml @@ -74,3 +74,24 @@ assert: "#mdbook-searchresults" // And now we press `Escape` to close everything. press-key: 'Escape' wait-for-css: ("#mdbook-search-wrapper", {"display": "none"}) + +// Search result links should have a `?highlight=` query param, which will highlight +// the search term when we navigate to the destination page. +go-to: |DOC_PATH| + "search/index.html?search=kale" +assert: '[href*="highlight=kale"]' + +// On navigation, text on the destination page that matches the search term should be highlighted. +go-to: |DOC_PATH| + "search/index.html?highlight=kale" +wait-for: '[data-markjs="true"]' +assert-text: ('[data-markjs="true"]', "kale", ALL) + +// Clicking on any of the highlights should dismiss them. +click: '[data-markjs="true"]' +assert-false: '[data-markjs="true"]' +// The `?highlight=` URL query param should also have been removed +// (so that highlights don't unexpectedly appear again.) +assert-window-property-false: ({"location": "highlight=kale"}, CONTAINS) +// Verify this by clicking on a heading to navigate to its anchor. This would otherwise preserve +// search params, and if the query param were not removed, highlights would appear again. +click: 'main h1 a' +assert-false: '[data-markjs="true"]' From b4e674877fbf45c86c0ff4065fd32103bde33061 Mon Sep 17 00:00:00 2001 From: tonywu6 Date: Wed, 6 May 2026 22:56:25 +0800 Subject: [PATCH 2/2] Update crates/mdbook-html/front-end/searcher/searcher.js Co-authored-by: Eric Huss --- crates/mdbook-html/front-end/searcher/searcher.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/mdbook-html/front-end/searcher/searcher.js b/crates/mdbook-html/front-end/searcher/searcher.js index b5deebcc..e58fa094 100644 --- a/crates/mdbook-html/front-end/searcher/searcher.js +++ b/crates/mdbook-html/front-end/searcher/searcher.js @@ -343,9 +343,7 @@ window.search = window.search || {}; } // also removes the `?URL_MARK_PARAM=` search param so that // in-page navigation doesn't make highlights unexpectedly appear again - const url = new URL(window.location.href); - url.searchParams.delete(URL_MARK_PARAM); - history.replaceState(null, '', url); + setSearchUrlParameters('', 'replace'); }; for (let i = 0; i < markers.length; i++) {