Add ability to "skip" adding marked documentation to the search index (#11280)

Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
This commit is contained in:
Will Lachance 2025-02-12 11:47:14 -05:00 committed by GitHub
parent 94563a398b
commit bb68e72333
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 1 deletions

View File

@ -106,6 +106,8 @@ Features added
* #9169: Add the :confval:`intersphinx_resolve_self` option
to resolve an intersphinx reference to the current project.
Patch by Jakob Lykke Andersen and Adam Turner.
* #11280: Add ability to skip a particular section using the ``no-search`` class.
Patch by Will Lachance.
Bugs fixed
----------

View File

@ -226,6 +226,9 @@ class WordCollector(nodes.NodeVisitor):
def dispatch_visit(self, node: Node) -> None:
if isinstance(node, nodes.comment):
raise nodes.SkipNode
elif isinstance(node, nodes.Element) and 'no-search' in node['classes']:
# skip nodes marked with a 'no-search' class
raise nodes.SkipNode
elif isinstance(node, nodes.raw):
if 'html' in node.get('format', '').split():
# Some people might put content in raw HTML that should be searched,
@ -602,6 +605,9 @@ def _feed_visit_nodes(
) -> None:
if isinstance(node, nodes.comment):
return
elif isinstance(node, nodes.Element) and 'no-search' in node['classes']:
# skip nodes marked with a 'no-search' class
return
elif isinstance(node, nodes.raw):
if 'html' in node.get('format', '').split():
# Some people might put content in raw HTML that should be searched,

View File

@ -17,6 +17,10 @@ textinheading
International
.. tip::
:class: no-search
bat cat
.. toctree::
tocitem

View File

@ -398,10 +398,13 @@ def test_nosearch(app):
app.build()
index = load_searchindex(app.outdir / 'searchindex.js')
assert index['docnames'] == ['index', 'nosearch', 'tocitem']
# latex is in 'nosearch.rst', and nowhere else
assert 'latex' not in index['terms']
assert 'bat' in index['terms']
# cat is in 'index.rst' but is marked with the 'no-search' class
assert 'cat' not in index['terms']
# bat is indexed from 'index.rst' and 'tocitem.rst' (document IDs 0, 2), and
# not from 'nosearch.rst' (document ID 1)
assert 'bat' in index['terms']
assert index['terms']['bat'] == [0, 2]