From fa44220edf872ed6bb061057df9ebd0158303c7d Mon Sep 17 00:00:00 2001 From: Stavros Korokithakis Date: Tue, 18 Jan 2022 18:17:13 +0200 Subject: [PATCH] Fix search links --- move_html_to_dir | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/move_html_to_dir b/move_html_to_dir index 3e8298e..3ccd8b7 100755 --- a/move_html_to_dir +++ b/move_html_to_dir @@ -2,6 +2,7 @@ """ Rename files like `foo/bar.html` to `foo/bar/index.html` for prettier URLs. """ +import json import re import sys from pathlib import Path @@ -140,6 +141,23 @@ def replace_links(path: Path, replacements: Dict[str, str]): f.write(contents) +def fix_search_links(path: Path): + """Rename search links to remove the ".html" suffix.""" + with (path / "searchindex.json").open() as infile: + index = json.load(infile) + + doc_urls = [url.replace(".html", "/") for url in index["doc_urls"]] + index["doc_urls"] = doc_urls + + index_json = json.dumps(index).strip() + + with (path / "searchindex.json").open("w") as outfile: + outfile.write(index_json) + + with (path / "searchindex.js").open("w") as outfile: + outfile.write(f"Object.assign(window.search, {index_json});") + + def main(path: Path): path = path.resolve() convert_relative_to_absolute(path) @@ -166,6 +184,9 @@ def main(path: Path): replace_links(path, replacements) + print("Rewriting search links...") + fix_search_links(path) + if __name__ == "__main__": main(Path(sys.argv[1]))