> This is unfortunate. Is there something less invasive available?
I did a quick search, and came across this pull request in the mkdocs
repository:
https://github.com/mkdocs/mkdocs/pull/1805
I quickly tested this:
* Create the file `docs/markdown/javascript/fetch_shim.js`, adding the
contents of the attached file.
* Run this command in `docs/` directory:
`echo "shim_localSearchIndex = $(cat
reference/search/search_index.json)" >
reference/search/search_index.js`
* Add the two lines below to the `extra_javascript` field in `docs/mkdocs.yml`:
- search/search_index.js
- javascripts/fetch_shim.js
* Run `mkdocs build` in the `docs/` directory.
Search works locally after following these steps.
Unfortunately, this breaks the site (freezes completely because the
script fetch_shim.js goes into some kind of infinite loop) when I try
to serve it locally with `mkdocs serve`.
To implement these changes to allow them to work with docwriter will
require fixing the attached script to prevent the page from freezing,
modifying docwriter to add the required entries to `mkdocs.yml`, and
possibly the build system to create the `search_index.js` file after
the site is generated.
Another option seems to be using the `mkdocs-localsearch` plugin
(https://github.com/wilhelmer/mkdocs-localsearch#installation-material-v4),
but I am getting errors while trying to build the site (with `mkdocs
build`) after following the instructions given in the link.
Unfortunately, it is unlikely that I will be able to find time this
week to work on this, although I can help with finding the files/build
targets to make the required changes once the problems I stated above
are fixed.
Nikhil
// Simple decorator shim for fetch, which makes the search_index.json file
fetchable (ONLY if you enable search:local_search_shim in mkdocs.yml)
fetch_native = fetch
fetch = function(url, options){
// Simple helper to resolve relative url (./search/search_index.json) to
absolute (file://C:/Users...)
var absolutePath = function(href) {
var link = document.createElement("a");
link.href = href;
absolute = link.href;
link.remove();
return absolute;
}
// Check if this fetch call is one we need to "intercept"
if (absolutePath(url).startsWith("file:") &&
absolutePath(url).endsWith("search_index.json")) {
// If we detect that this IS a call trying to fetch the search index,
then...
console.log("LOCAL SEARCH SHIM: Detected search_index fetch attempt!
Using search index shim for " + url)
// Return a "forged" object that mimics a normal fetch call's output
// This looks messy, but it essentially just slips in the search index
wrapped in
// all the formatting that normally results from the fetch() call
return new Promise(
function(resolve, reject){
var shimResponse = {
json: function(){
// This should return the search index
return shim_localSearchIndex;
}
}
resolve( shimResponse )
}
)
}
// In all other cases, behave normally
else {
console.log("LOCAL SEARCH SHIM: Using native fetch code for " + url)
return fetch(url, options);
}
}