diff --git a/src/renderer/html_handlebars/search.rs b/src/renderer/html_handlebars/search.rs index 1144c9f588..2acbd7525b 100644 --- a/src/renderer/html_handlebars/search.rs +++ b/src/renderer/html_handlebars/search.rs @@ -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", diff --git a/src/theme/searcher/searcher.js b/src/theme/searcher/searcher.js index dc03e0a02d..a58df45a7d 100644 --- a/src/theme/searcher/searcher.js +++ b/src/theme/searcher/searcher.js @@ -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';