Skip to content

Commit 1d2f8b0

Browse files
committed
Fixed baseUrl error for search
1 parent 6f75e0c commit 1d2f8b0

File tree

4 files changed

+13
-42
lines changed

4 files changed

+13
-42
lines changed

redirect.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h1>Demo listing pages</h1>
2424
<ul>
2525
<script>
2626
for (theme of ["mkdocs", "readthedocs", "material"]) {
27-
document.write(`<li>${theme}: <a href="${theme}/plugin/listing-search.html">search page</a>, <a href="${theme}-no-dir/plugin/all-listings.html">all listings page</a></li>`)
27+
document.write(`<li>${theme}: <a href="${theme}-no-dir/plugin/listing-search.html">search page</a>, <a href="${theme}-no-dir/plugin/all-listings.html">all listings page</a></li>`)
2828
}
2929
</script>
3030
</ul>

src/mkdocs_extract_listings_plugin/listing-search.js

+3-29
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,11 @@ STYLE=``;
1313
OFFLINE_JSON_DATA=null;
1414
// END
1515

16-
let parentDirectoryUrl;
17-
if (document.currentScript && document.currentScript.src) {
18-
// This is run when the script is included via an <script src=...> tag.
19-
// The script is always at the same location, no matter on which page it is inluded.
20-
parentDirectoryUrl = new URL(document.currentScript.src);
21-
parentDirectoryUrl.pathname = parentDirectoryUrl.pathname.substring(0, parentDirectoryUrl.pathname.lastIndexOf("/") + 1);
22-
} else {
23-
// This is run when the script is inlined to a page.
24-
// In this case we can not be certain about where we are, but the python code fixed all relative paths, so that they should work from here.
25-
parentDirectoryUrl = new URL(window.location.href);
26-
const directoryUrlsUsed = parentDirectoryUrl.pathname.endsWith("/") || parentDirectoryUrl.pathname.endsWith("/index.html")
27-
if (directoryUrlsUsed) {
28-
// Directory URLs are in use -> go up one directory and remove file name
29-
parentDirectoryUrl.pathname = parentDirectoryUrl.pathname.substring(0, parentDirectoryUrl.pathname.lastIndexOf("/"));
30-
parentDirectoryUrl.pathname = parentDirectoryUrl.pathname.substring(0, parentDirectoryUrl.pathname.lastIndexOf("/") + 1);
31-
} else {
32-
// Directory URLs are disabled -> stay in directory but remove file name
33-
parentDirectoryUrl.pathname = parentDirectoryUrl.pathname.substring(0, parentDirectoryUrl.pathname.lastIndexOf("/") + 1);
34-
}
35-
}
36-
37-
parentDirectoryUrl.query = "";
38-
parentDirectoryUrl.hash = "";
39-
const base_url = parentDirectoryUrl.href.endsWith("/") ? parentDirectoryUrl.href : parentDirectoryUrl.href + "/";
40-
console.debug("The base URL for the search result links is", base_url);
41-
16+
// Convert relative to absolute URL
4217
const normalizeUrl = (url) => {
43-
return new URL(url, location.href);
18+
return new URL(url, location.href).pathname;
4419
};
4520

46-
4721
const parent = document.getElementById("listing-extract-search");
4822
if (parent) {
4923
const search_mode = parent.getAttribute("data-searchmode") || DEFAULT_SEARCH_MODE;
@@ -224,7 +198,7 @@ if (parent) {
224198
const on_json_loaded = (json) => {
225199
// Publicly accessible for easier debugging
226200
// Remap the URLs based on the location of this script (which is in the same directory as the JSON file)
227-
window.extract_listings_case_sensitive = json.map(x => ({...x, page_url: normalizeUrl(base_url + x.page_url)}));
201+
window.extract_listings_case_sensitive = json.map(x => ({...x, page_url: normalizeUrl(x.page_url)}));
228202
// @TODO: maybe only cache this if an cae-insensitive mode is selected?
229203
window.extract_listings_lowercase = window.extract_listings_case_sensitive.map(x => ({...x, text: x.text.toLowerCase()}));
230204

src/mkdocs_extract_listings_plugin/search_page.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,14 @@ def get_javascript_file_source_code(page_data_list: list[PageData], plugin_confi
2020

2121
# We traverse from the JSON file up to the root directory
2222
path_to_root = "../" * script_or_page_path.count("/")
23+
if config.use_directory_urls:
24+
path_to_root += "../"
2325
if offline:
2426
json_data = get_json_data(page_data_list, path_to_root)
2527
js = js.replace("OFFLINE_JSON_DATA=null;", f"OFFLINE_JSON_DATA={json.dumps(json_data)};")
2628
else:
2729
write_json_file(page_data_list, plugin_config, config, path_to_root)
2830

29-
if config.site_url:
30-
path = urlparse(config.site_url).path
31-
# Remove trailing trashes
32-
while path.endswith("/"):
33-
path = path[:-1]
34-
35-
if path:
36-
# Path contains something else than just slashes
37-
js = js.replace('BASE_URL=""', f'BASE_URL="{path}"')
38-
3931
return js
4032

4133

test-offline-build.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ python3 -m pip install -r requirements.txt
1616
python3 -m pip install .
1717

1818
# Do not use directory urls, since the browser does not map from /path/ to /path/index.html for file:// urls
19-
sed -e '/^use_directory_urls:/s|true|false|' -e '/offline:/s|false|true|' -e '/plugins:/a- offline' mkdocs.yml > "mkdocs-offline.yml"
19+
# use AWK instead of sed '/plugins:/a- offline' to make it work on macOS
20+
sed -e '/^use_directory_urls:/s|true|false|' -e '/offline:/s|false|true|' mkdocs.yml | awk '{print} /plugins:/ {print "- offline"}' > "mkdocs-offline.yml"
2021

2122
echo "[*] Building site"
2223
python3 -m mkdocs build -f "mkdocs-offline.yml" || exit 1
2324

2425
echo "[*] Opening path in firefox"
25-
firefox site/plugin/index.html
26+
if [[ $(uname) == Darwin ]]; then
27+
open -a Firefox.app site/plugin/index.html
28+
else
29+
firefox site/plugin/index.html
30+
fi

0 commit comments

Comments
 (0)