From 74200f7395002728d4599ff7b4e1712648574334 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 9 Mar 2025 09:10:50 -0700 Subject: [PATCH] Fix search not showing in sub-directories This fixes a problem where the search was not displaying in sub-directories. The problem was that `searcher.js` only exists in one place, and was loading `searchindex.json` with a relative path. However, when loading from a subdirectory, it needs the appropriate `..` to reach the root of the book. --- src/theme/searcher/searcher.js | 4 ++-- tests/gui/search.goml | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/gui/search.goml diff --git a/src/theme/searcher/searcher.js b/src/theme/searcher/searcher.js index a275f48e40..a2392f3d17 100644 --- a/src/theme/searcher/searcher.js +++ b/src/theme/searcher/searcher.js @@ -468,12 +468,12 @@ window.search = window.search || {}; showResults(true); } - fetch('{{ resource "searchindex.json" }}') + fetch(path_to_root + '{{ resource "searchindex.json" }}') .then(response => response.json()) .then(json => init(json)) .catch(error => { // Try to load searchindex.js if fetch failed var script = document.createElement('script'); - script.src = '{{ resource "searchindex.js" }}'; + script.src = path_to_root + '{{ resource "searchindex.js" }}'; script.onload = () => init(window.search); document.head.appendChild(script); }); diff --git a/tests/gui/search.goml b/tests/gui/search.goml new file mode 100644 index 0000000000..dd386d0d44 --- /dev/null +++ b/tests/gui/search.goml @@ -0,0 +1,33 @@ +// This tests basic search behavior. + +// We disable the requests checks because `searchindex.json` will always fail +// locally (due to CORS), but the searchindex.js will succeed. +fail-on-request-error: false +go-to: |DOC_PATH| + "index.html" + +define-function: ( + "open-search", + [], + block { + assert-css: ("#search-wrapper", {"display": "none"}) + press-key: 'S' + wait-for-css-false: ("#search-wrapper", {"display": "none"}) + } +) + +call-function: ("open-search", {}) +assert-text: ("#searchresults-header", "") +write: "strikethrough" +wait-for-text: ("#searchresults-header", "2 search results for 'strikethrough':") +// Close the search display +press-key: 'Escape' +wait-for-css: ("#search-wrapper", {"display": "none"}) +// Reopening the search should show the last value +call-function: ("open-search", {}) +assert-text: ("#searchresults-header", "2 search results for 'strikethrough':") +// Navigate to a sub-chapter +go-to: "./individual/strikethrough.html" +assert-text: ("#searchresults-header", "") +call-function: ("open-search", {}) +write: "strikethrough" +wait-for-text: ("#searchresults-header", "2 search results for 'strikethrough':")