What is Sphinx built-in search, and how to use it in themes?#
Sphinx is a powerful documentation generator with features that enhance content usability. Its built-in search offers a simple way to find information within your documentation. This guide will help you implement and customize Sphinx’s search in your themes for a better user experience for documentation readers.
Note
Applies to Sphinx 7.2.5 and above.
Built-in search capabilities#
Sphinx’s built-in search comes out of the box. It lacks advanced features but is still worth supporting in Sphinx themes. Among the main built-in search capabilities are:
Basic full-text search. It doesn’t support wildcards (? or *) and operators (OR, NOT, …).
Multiple words are ANDed. Searching multiple words shows matches that contain all words.
Works from a single letter. Even single-letter words can be searched.
Browser-only. It works entirely in the browser with a few JavaScript and doesn’t require any backend.
The search result page can show the surroundings of the occurrences found.
Highlight found occurrences on the result page and the page itself when open from the result page.
Search across all pages, but individual pages can be hidden with the meta field
:nosearch:
at the beginning of the document.:nosearch: Sphinx is cool ==============
--- nosearch: --- # Sphinx is cool
Adding built-in search to Sphinx theme#
The process of bringing built-in search capability to your custom Sphinx theme involves a few easy steps, from customizing your HTML templates to styling the search results effectively.
1. Add content root data-content_root
to <html>
#
The main template layout.html
must set data attribute content_root
to the <html>
element.
<html
{% if language is not none %} lang="{{ language }}"{% endif %}
data-content_root="{{ content_root }}"
>
(Please note the underscore instead of hyphen as usual for data attributes. It’s proof backend Python developers chose the name :-))
The searchtools.js
used the path when requesting a preview on the search page.
2. Add role="main"
to body#
To work, the built-in search script assumes that the document or body
block will be marked with role="main"
in HTML. E.g.:
<article role="main">{% block body %}{% endblock %}</article>
3. Style search results#
Matching words are wrapped to <span class="highlighted">
by the sphinx_highlight.js
script.
Search results are rendered as <ul>
within #search-results
by the searchtools.js
script.
Example styles:
.highlighted {
@apply rounded border border-dotted border-gray-300 bg-yellow-200;
}
/* Search page results */
#search-results ul {
/* Display as vertical grid */
@apply grid list-none grid-cols-1 gap-4 p-0;
li a {
@apply text-xl;
}
}
4. Add a search box#
The search term must be passed to the search page in the q
query parameter (thus name="q"
in <input>
element). E.g., http://docs.mysite.com/search.html?q=extension
.
With proper accessibility and some styles:
<search>
<form action="{{ pathto('search') }}" method="get">
<input
type="text"
name="q"
placeholder="{{ _('Hledat') }}"
aria-label="{{ _('Vyhledávání v dokumentaci') }}"
spellcheck="false"
autocomplete="off"
autocorrect="off"
autocapitalize="off"
class="h-4/5 w-60 rounded bg-slate-50 p-1 text-xs ring-1 ring-slate-200 transition duration-200 placeholder:text-xs hover:ring-2 hover:ring-slate-400 focus:outline-none focus:ring-4 focus:ring-slate-400"
/>
</form>
</search>
5 Customize search.html
template#
The search.html
is a search result page template. The Basic theme contains a search form and results styled as an unordered list.
When you want to customize a search result page or author a Sphinx theme not based on Basic, don’t forget to
Include the JavaScript files listed below (odkaz).
Have a container for search results to be injected by scripts with
id="search-results"
.
{# Template for the search page. #}
{%- extends "layout.html" %}
{% set title = _('Search') %}
{%- block before_body %}
{{ super() }}
<script src="{{ pathto('_static/searchtools.js', 1) }}"></script>
<script src="{{ pathto('_static/language_data.js', 1) }}"></script>
<script src="{{ pathto('searchindex.js', 1) }}" defer="defer"></script>
<meta name="robots" content="noindex" />
{% endblock %}
{% block body %}
<h1 id="search-documentation">{{ _('Search') }}</h1>
<noscript>
<div class="admonition warning">
<p>
{% trans %}
Please activate JavaScript to enable the search functionality.
{% endtrans %}
</p>
</div>
</noscript>
<p>
{% trans %}
Searching for multiple words only shows matches that contain all words.
{% endtrans %}
</p>
<div id="search-results"></div>
{% endblock %}
Files and templates of built-in search#
Sphinx’s built-in search is comprised of a few static files and templates. When your theme doesn’t inherit from the built-in Basic theme, you must ensure all templates and scripts are present.
search.html
template presents search results. It loadsstatic/searchtools.js
,static/language_data.js
andsearchindex.js
.The Basic theme and Basic derived themes have almost identical
searchbox.html
andsearchfield.html
sidebar templates. They contain the search box. The former has a heading with “Quick search” text, and the latter only has a “Search” search box placeholder.static/searchtools.js
implements full-text search.static/language_data.js
lists the project language’s stopwords, stemmers, etc.searchindex.js
(not instatic
!) is a giant data file for a search containing doc names, file names, doc titles, etc. This file will be generated during the build in the output folder root.
Search-as-you-type example#
Clever mind Kayce Basques took Sphinx’s built-in search customization to the next level. He created and shared a cool search-as-you-type right into the Sphinx theme.
Pssst, Kayce also writes https://technicalwriting.dev blog.
Comments
comments powered by Disqus