Skip to content

Commit

Permalink
feat: add pagefind search
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
miketheman committed Jan 31, 2025
1 parent 1fb889b commit 3d3d32c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ALLSPHINXOPTS = --builder $(BUILDER) \
.PHONY: html
html: venv
$(SPHINXBUILD) $(ALLSPHINXOPTS)
$(VENVDIR)/bin/python3 -m pagefind --site $(BUILDDIR) --verbose

## htmlview to open the index page built by the html target in your browser
.PHONY: htmlview
Expand Down
6 changes: 5 additions & 1 deletion pep_sphinx_extensions/pep_processor/transforms/pep_title.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def apply(self) -> None:

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

Check warning on line 51 in pep_sphinx_extensions/pep_processor/transforms/pep_title.py

View check run for this annotation

Codecov / codecov/patch

pep_sphinx_extensions/pep_processor/transforms/pep_title.py#L50-L51

Added lines #L50 - L51 were not covered by tests

# Insert the title node as the root element, move children down
document_children = self.document.children
Expand Down
10 changes: 9 additions & 1 deletion pep_sphinx_extensions/pep_theme/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<link rel="stylesheet" href="{{ pathto('_static/mq.css', resource=True) }}" type="text/css">
<link rel="stylesheet" href="{{ pathto('_static/pygments.css', resource=True) }}" type="text/css" media="(prefers-color-scheme: light)" id="pyg-light">
<link rel="stylesheet" href="{{ pathto('_static/pygments_dark.css', resource=True) }}" type="text/css" media="(prefers-color-scheme: dark)" id="pyg-dark">
<link rel="stylesheet" href="/pagefind/pagefind-ui.css" type="text/css">
<link rel="alternate" type="application/rss+xml" title="Latest PEPs" href="https://peps.python.org/peps.rss">
<meta property="og:title" content='{{ title + " | peps.python.org"|safe }}'>
<meta property="og:description" content="{{ description }}">
Expand Down Expand Up @@ -46,11 +47,12 @@ <h1>Python Enhancement Proposals</h1>
<span class="visually-hidden">Toggle light / dark / auto colour theme</span>
</button>
</header>
<article>
<article data-pagefind-body>
{{ body }}
</article>
{%- if not pagename.startswith(("404", "numerical")) %}
<nav id="pep-sidebar">
<div id="search"></div>
<h2>Contents</h2>
{{ toc }}
<br>
Expand All @@ -63,5 +65,11 @@ <h2>Contents</h2>
<script src="{{ pathto('_static/colour_scheme.js', resource=True) }}"></script>
<script src="{{ pathto('_static/wrap_tables.js', resource=True) }}"></script>
<script src="{{ pathto('_static/sticky_banner.js', resource=True) }}"></script>
<script src="/pagefind/pagefind-ui.js"></script>
<script>
window.addEventListener('DOMContentLoaded', (event) => {
new PagefindUI({ element: "#search", showSubResults: true });
});
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Pygments >= 2.9.0
Sphinx >= 5.1.1, != 6.1.0, != 6.1.1, < 8.1.0
docutils >= 0.19.0
sphinx-notfound-page >= 1.0.2
# For search
pagefind[bin] >= 1.3.0

# For tests
pytest
Expand Down

0 comments on commit 3d3d32c

Please sign in to comment.