Skip to content

Commit 6330d7e

Browse files
committed
BUG: Fix bug with prebuilt Lunr.js index by including the crucial part
Ref: ccc3658 ENH: Prebuild Lunr.js search index
1 parent aab629c commit 6330d7e

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

pdoc/build-index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ stdin.on('end', function () {
9090

9191
let out = idx.toJSON();
9292
compact(out);
93-
stdout.write(JSON.stringify(out));
93+
stdout.write(JSON.stringify([out, documents]));
9494
})

pdoc/cli.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,12 @@ def to_url_id(module):
417417
recursive_add_to_index(top_module)
418418
urls = sorted(url_cache.keys(), key=url_cache.__getitem__)
419419

420-
json_values = [dict(obj, url=urls[obj['url']]) for obj in index]
421420
cmd = ['node', str(Path(__file__).with_name('build-index.js'))]
422421
proc = None
423422
try:
424-
proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
425-
stdout, stderr = proc.communicate(json.dumps(json_values))
423+
proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
424+
stderr=subprocess.PIPE)
425+
stdout, stderr = proc.communicate(json.dumps(index))
426426
assert proc.poll() == 0, proc.poll()
427427
except Exception as ex:
428428
stderr = proc and proc.stderr and proc.stderr.read() # type: ignore
@@ -435,7 +435,8 @@ def to_url_id(module):
435435
stdout = ('URLS=' + json.dumps(urls, indent=0, separators=(',', ':')) +
436436
';\nINDEX=' + json.dumps(index, indent=0, separators=(',', ':')))
437437
else:
438-
stdout = 'INDEX=' + stdout
438+
stdout = (f'let [INDEX, DOCS] = {stdout}; '
439+
f'let URLS={json.dumps(urls, indent=0, separators=(",", ":"))}')
439440
main_path = args.output_dir
440441
index_path = Path(main_path).joinpath('index.js')
441442
index_path.write_text(stdout)

pdoc/templates/search.mako

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
try {
5656
return lunr.Index.load(_expand(INDEX)); // Prebuilt index
5757
} catch {
58+
window.DOCS = INDEX;
5859
return lunr(function () {
5960
this.ref('i');
6061
this.field('name', {boost: 10});
@@ -152,11 +153,11 @@
152153
}
153154
154155
set_status(
155-
'Search for "' + encodeURIComponent(query) + '" yielded ' + results.length + ' ' +
156+
'Search for "' + encodeURIComponent(query).replace('%20', ' ') + '" yielded ' + results.length + ' ' +
156157
(results.length === 1 ? 'result' : 'results') + ':');
157158
158159
results.forEach(function (result) {
159-
const dobj = INDEX[parseInt(result.ref)];
160+
const dobj = DOCS[parseInt(result.ref)];
160161
const docstring = dobj.doc;
161162
const url = URLS[dobj.url] + '#' + dobj.ref;
162163
const pretty_name = dobj.ref + (dobj.func ? '()' : '');

pdoc/test/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def test_lunr_search(self):
274274
self._basic_html_assertions(expected_files=files)
275275
self._check_files(exclude_patterns=['class="gcse-search"'])
276276
if shutil.which('node'):
277-
self._check_files(include_patterns=['INDEX={"version"'],
277+
self._check_files(include_patterns=['{"version"', 'pdoc.Doc'],
278278
file_pattern='index.js')
279279
else:
280280
self._check_files(

0 commit comments

Comments
 (0)