Skip to content

Commit

Permalink
support search-specific meatadata
Browse files Browse the repository at this point in the history
add `search-exclude` and `search-boost` metadata fields
  • Loading branch information
2bndy5 committed Jul 4, 2024
1 parent 002acbe commit 2ddb63f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
33 changes: 33 additions & 0 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ Each metadata is evaluated as a :rst:`:key: value` pair.
.. seealso::
Using comments requires extra steps. See `Adding a comment system`_ instructions.


.. themeconf:: search-boost

If specified, boosts the current page's search index entries by a factor given.
Without specifying this, all search entries are ranked at a boost factor of 1.

.. md-tab-set::

.. md-tab-item:: :si-icon:`material/arrow-up-circle` Rank up

.. code-block:: rst
:search-boost: 1
.. md-tab-item:: :si-icon:`material/arrow-down-circle` Rank down

.. code-block:: rst
:search-boost: 0.5
.. seealso::
Review advice from `mkdocs-material theme's search boost metadata
<https://squidfunk.github.io/mkdocs-material/plugins/search/#meta.search.boost>`_.

.. themeconf:: search-exclude

If specified, excludes the current page from the search index.

.. code-block:: rst
:caption: Exclude current page from search index
:search-exclude:
Configuration Options
=====================

Expand Down
16 changes: 13 additions & 3 deletions sphinx_immaterial/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,25 @@ def _html_page_context(
if content is None:
# Special page like `genindex`, exclude from search index.
return

page_meta: Dict[str, str] = context.get("meta") or {}
meta: Dict[str, Any] = {"tags": []}
# TODO: When tags are supported, they need to be appended to `meta["tags"]`
if "search-exclude" in page_meta:
meta["exclude"] = True
if "search-boost" in page_meta:
meta["boost"] = page_meta["search-boost"]

indexer = _make_indexer(app)
url = app.builder.get_target_uri(pagename)
page_ctx = context.get("page")
indexer.add_entry_from_context(
_Page(
content=content,
title=context["page"]["title"],
meta={},
title=page_ctx["title"],
meta={"search": meta},
url=url,
toc=context["page"]["toc"],
toc=page_ctx["toc"],
)
)
queue = getattr(app, _SEARCH_QUEUE_KEY)
Expand Down

0 comments on commit 2ddb63f

Please sign in to comment.