Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub fn create_files(search_config: &Search, destination: &Path, book: &Book) ->
}

if search_config.copy_js {
utils::fs::write_file(destination, "searchindex.json", index.as_bytes())?;
utils::fs::write_file(
destination,
"searchindex.js",
Expand Down
14 changes: 11 additions & 3 deletions src/theme/searcher/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,17 @@ window.search = window.search || {};
showResults(true);
}

fetch(path_to_root + 'searchindex.json')
.then(response => response.json())
.then(json => init(json))
fetch(path_to_root + 'searchindex.js')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why keeping this part of the code instead of just using the content from the .catch() call? There is no point in parsing JS.

.then(response => response.text())
.then(text => {
const jsonMatch = text.match(/Object\.assign\(window\.search,\s*(\{[\s\S]*\})\s*\)/);
if (jsonMatch && jsonMatch[1]) {
return JSON.parse(jsonMatch[1]);
} else {
throw new Error('Unable to extract JSON from the script');
}
})
.then(json => init(json))
.catch(error => { // Try to load searchindex.js if fetch failed
var script = document.createElement('script');
script.src = path_to_root + 'searchindex.js';
Expand Down