Skip to content

Commit ddc20a4

Browse files
committed
feat: add pagefind search
Instead of using sphinx's built-in search which has some challenges in its implementation, as well as the integration with the customized builder and theme, use a different approach. Pagefind will generate fragments that can be loaded client-side efficiently. Refs: https://pagefind.app/ Refs: https://pypi.org/project/pagefind/ Signed-off-by: Mike Fiedler <[email protected]>
1 parent 1fb889b commit ddc20a4

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ALLSPHINXOPTS = --builder $(BUILDER) \
2222
.PHONY: html
2323
html: venv
2424
$(SPHINXBUILD) $(ALLSPHINXOPTS)
25+
$(VENVDIR)/bin/python3 -m pagefind --site build --verbose
2526

2627
## htmlview to open the index page built by the html target in your browser
2728
.PHONY: htmlview

pep_sphinx_extensions/pep_processor/transforms/pep_title.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ def apply(self) -> None:
4444

4545
# Generate the title section node and its properties
4646
title_nodes = _line_to_nodes(pep_title_string)
47-
pep_title_node = nodes.section("", nodes.title("", "", *title_nodes, classes=["page-title"]), names=["pep-content"])
47+
# TODO: Why doesn't the new attribute show up in the HTML output?
48+
# This is needed to change the way the index entry titles are generated,
49+
# but is currently getting ignored/removed somewhere in the stack.
50+
pep_title_attributes = {"classes": ["page-title"], "data-pagefind-meta": "title"}
51+
pep_title_node = nodes.section("", nodes.title("", "", *title_nodes, **pep_title_attributes), names=["pep-content"])
4852

4953
# Insert the title node as the root element, move children down
5054
document_children = self.document.children

pep_sphinx_extensions/pep_theme/templates/page.html

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<link rel="stylesheet" href="{{ pathto('_static/mq.css', resource=True) }}" type="text/css">
1313
<link rel="stylesheet" href="{{ pathto('_static/pygments.css', resource=True) }}" type="text/css" media="(prefers-color-scheme: light)" id="pyg-light">
1414
<link rel="stylesheet" href="{{ pathto('_static/pygments_dark.css', resource=True) }}" type="text/css" media="(prefers-color-scheme: dark)" id="pyg-dark">
15+
<link rel="stylesheet" href="/pagefind/pagefind-ui.css" type="text/css">
1516
<link rel="alternate" type="application/rss+xml" title="Latest PEPs" href="https://peps.python.org/peps.rss">
1617
<meta property="og:title" content='{{ title + " | peps.python.org"|safe }}'>
1718
<meta property="og:description" content="{{ description }}">
@@ -46,11 +47,12 @@ <h1>Python Enhancement Proposals</h1>
4647
<span class="visually-hidden">Toggle light / dark / auto colour theme</span>
4748
</button>
4849
</header>
49-
<article>
50+
<article data-pagefind-body>
5051
{{ body }}
5152
</article>
5253
{%- if not pagename.startswith(("404", "numerical")) %}
5354
<nav id="pep-sidebar">
55+
<div id="search"></div>
5456
<h2>Contents</h2>
5557
{{ toc }}
5658
<br>
@@ -63,5 +65,11 @@ <h2>Contents</h2>
6365
<script src="{{ pathto('_static/colour_scheme.js', resource=True) }}"></script>
6466
<script src="{{ pathto('_static/wrap_tables.js', resource=True) }}"></script>
6567
<script src="{{ pathto('_static/sticky_banner.js', resource=True) }}"></script>
68+
<script src="/pagefind/pagefind-ui.js"></script>
69+
<script>
70+
window.addEventListener('DOMContentLoaded', (event) => {
71+
new PagefindUI({ element: "#search", showSubResults: true });
72+
});
73+
</script>
6674
</body>
6775
</html>

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Pygments >= 2.9.0
55
Sphinx >= 5.1.1, != 6.1.0, != 6.1.1, < 8.1.0
66
docutils >= 0.19.0
77
sphinx-notfound-page >= 1.0.2
8+
# For search
9+
pagefind[bin] >= 1.3.0
810

911
# For tests
1012
pytest

0 commit comments

Comments
 (0)