Skip to content

Commit

Permalink
BUG: Fix bug with prebuilt Lunr.js index by including the crucial part
Browse files Browse the repository at this point in the history
Ref: ccc3658 ENH: Prebuild Lunr.js search index
  • Loading branch information
kernc committed Dec 13, 2024
1 parent aab629c commit 6330d7e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pdoc/build-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ stdin.on('end', function () {

let out = idx.toJSON();
compact(out);
stdout.write(JSON.stringify(out));
stdout.write(JSON.stringify([out, documents]));
})
9 changes: 5 additions & 4 deletions pdoc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,12 @@ def to_url_id(module):
recursive_add_to_index(top_module)
urls = sorted(url_cache.keys(), key=url_cache.__getitem__)

json_values = [dict(obj, url=urls[obj['url']]) for obj in index]
cmd = ['node', str(Path(__file__).with_name('build-index.js'))]
proc = None
try:
proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = proc.communicate(json.dumps(json_values))
proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = proc.communicate(json.dumps(index))
assert proc.poll() == 0, proc.poll()
except Exception as ex:
stderr = proc and proc.stderr and proc.stderr.read() # type: ignore
Expand All @@ -435,7 +435,8 @@ def to_url_id(module):
stdout = ('URLS=' + json.dumps(urls, indent=0, separators=(',', ':')) +
';\nINDEX=' + json.dumps(index, indent=0, separators=(',', ':')))
else:
stdout = 'INDEX=' + stdout
stdout = (f'let [INDEX, DOCS] = {stdout}; '
f'let URLS={json.dumps(urls, indent=0, separators=(",", ":"))}')
main_path = args.output_dir
index_path = Path(main_path).joinpath('index.js')
index_path.write_text(stdout)
Expand Down
5 changes: 3 additions & 2 deletions pdoc/templates/search.mako
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
try {
return lunr.Index.load(_expand(INDEX)); // Prebuilt index
} catch {
window.DOCS = INDEX;
return lunr(function () {
this.ref('i');
this.field('name', {boost: 10});
Expand Down Expand Up @@ -152,11 +153,11 @@
}
set_status(
'Search for "' + encodeURIComponent(query) + '" yielded ' + results.length + ' ' +
'Search for "' + encodeURIComponent(query).replace('%20', ' ') + '" yielded ' + results.length + ' ' +
(results.length === 1 ? 'result' : 'results') + ':');
results.forEach(function (result) {
const dobj = INDEX[parseInt(result.ref)];
const dobj = DOCS[parseInt(result.ref)];
const docstring = dobj.doc;
const url = URLS[dobj.url] + '#' + dobj.ref;
const pretty_name = dobj.ref + (dobj.func ? '()' : '');
Expand Down
2 changes: 1 addition & 1 deletion pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def test_lunr_search(self):
self._basic_html_assertions(expected_files=files)
self._check_files(exclude_patterns=['class="gcse-search"'])
if shutil.which('node'):
self._check_files(include_patterns=['INDEX={"version"'],
self._check_files(include_patterns=['{"version"', 'pdoc.Doc'],
file_pattern='index.js')
else:
self._check_files(
Expand Down

0 comments on commit 6330d7e

Please sign in to comment.