Skip to content

Commit

Permalink
only use searchindex.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nick1udwig committed Aug 9, 2024
1 parent ec996d3 commit ed8d33d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
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')
.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

0 comments on commit ed8d33d

Please sign in to comment.