From ed8d33d774aae1f175ac12a821d3aee54f9d4652 Mon Sep 17 00:00:00 2001 From: hosted-fornet Date: Thu, 8 Aug 2024 17:14:01 -0700 Subject: [PATCH] only use searchindex.js --- src/renderer/html_handlebars/search.rs | 1 - src/theme/searcher/searcher.js | 14 +++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) 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';