Skip to content

Commit 74200f7

Browse files
committed
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.
1 parent 1a5286b commit 74200f7

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/theme/searcher/searcher.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,12 @@ window.search = window.search || {};
468468
showResults(true);
469469
}
470470

471-
fetch('{{ resource "searchindex.json" }}')
471+
fetch(path_to_root + '{{ resource "searchindex.json" }}')
472472
.then(response => response.json())
473473
.then(json => init(json))
474474
.catch(error => { // Try to load searchindex.js if fetch failed
475475
var script = document.createElement('script');
476-
script.src = '{{ resource "searchindex.js" }}';
476+
script.src = path_to_root + '{{ resource "searchindex.js" }}';
477477
script.onload = () => init(window.search);
478478
document.head.appendChild(script);
479479
});

tests/gui/search.goml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This tests basic search behavior.
2+
3+
// We disable the requests checks because `searchindex.json` will always fail
4+
// locally (due to CORS), but the searchindex.js will succeed.
5+
fail-on-request-error: false
6+
go-to: |DOC_PATH| + "index.html"
7+
8+
define-function: (
9+
"open-search",
10+
[],
11+
block {
12+
assert-css: ("#search-wrapper", {"display": "none"})
13+
press-key: 'S'
14+
wait-for-css-false: ("#search-wrapper", {"display": "none"})
15+
}
16+
)
17+
18+
call-function: ("open-search", {})
19+
assert-text: ("#searchresults-header", "")
20+
write: "strikethrough"
21+
wait-for-text: ("#searchresults-header", "2 search results for 'strikethrough':")
22+
// Close the search display
23+
press-key: 'Escape'
24+
wait-for-css: ("#search-wrapper", {"display": "none"})
25+
// Reopening the search should show the last value
26+
call-function: ("open-search", {})
27+
assert-text: ("#searchresults-header", "2 search results for 'strikethrough':")
28+
// Navigate to a sub-chapter
29+
go-to: "./individual/strikethrough.html"
30+
assert-text: ("#searchresults-header", "")
31+
call-function: ("open-search", {})
32+
write: "strikethrough"
33+
wait-for-text: ("#searchresults-header", "2 search results for 'strikethrough':")

0 commit comments

Comments
 (0)