Skip to content

Commit a0b27d6

Browse files
authored
activate sphinx search (#121)
1 parent bf6eef2 commit a0b27d6

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

.prettierrc.cjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/** @type {import("prettier").Config} */
55
module.exports = {
66
plugins: [require.resolve("prettier-plugin-jinja-template")],
7+
semi: false,
78
overrides: [
89
{
910
files: [".vscode/*.json"],
@@ -21,4 +22,4 @@ module.exports = {
2122
},
2223
},
2324
],
24-
};
25+
}

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"python.analysis.typeCheckingMode": "strict",
23
"python.testing.pytestArgs": ["-vv", "--color=yes"],
34
"python.testing.unittestEnabled": false,
45
"python.testing.pytestEnabled": true,

docs/conf.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
import os
6+
import re
57
from typing import TYPE_CHECKING
68
from pathlib import PurePosixPath
79
from datetime import datetime
@@ -23,6 +25,7 @@
2325
"sphinx.ext.autosummary",
2426
"scanpydoc",
2527
"sphinx.ext.linkcode", # needs to be after scanpydoc
28+
"sphinx_search.extension",
2629
]
2730

2831
intersphinx_mapping = dict(
@@ -59,8 +62,6 @@
5962

6063
def test_search(value: str, pattern: str) -> bool:
6164
"""Tests if `pattern` can be found in `value`."""
62-
import re
63-
6465
return bool(re.search(pattern, value))
6566

6667

@@ -76,9 +77,17 @@ def test_search(value: str, pattern: str) -> bool:
7677

7778
rtd_links_prefix = PurePosixPath("src")
7879

80+
rtd_ver = os.environ.get("READTHEDOCS_VERSION", "")
81+
if re.fullmatch(r"\d+", rtd_ver): # PR versions don’t have a own search index
82+
rtd_sphinx_search_default_filter = (
83+
f"subprojects:{os.getenv('READTHEDOCS_PROJECT')}/latest"
84+
)
85+
7986

8087
def setup(app: Sphinx) -> None:
8188
"""Set up custom Sphinx extension."""
89+
if rtd_ver: # if we’re on ReadTheDocs, hide the pydata-sphinx-theme search popup
90+
app.add_js_file("scripts/rtd-sphinx-search.js", loading_method="defer")
8291
app.add_object_type(
8392
"confval",
8493
"confval",

pyproject.toml

+3-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ dependencies = [
2424
]
2525

2626
[project.optional-dependencies]
27-
dev = [
28-
'pre-commit',
29-
]
27+
dev = ['pre-commit']
3028
test = [
3129
'pytest',
3230
'coverage',
@@ -37,11 +35,10 @@ test = [
3735
doc = [
3836
'scanpydoc[typehints,theme]',
3937
'sphinx',
38+
'readthedocs-sphinx-search',
4039
]
4140
typehints = ['sphinx-autodoc-typehints>=1.15.2']
42-
theme = [
43-
'sphinx-book-theme>=1.1.0rc1',
44-
]
41+
theme = ['sphinx-book-theme>=1.1.0']
4542

4643
[project.entry-points.'sphinx.html_themes']
4744
scanpydoc = 'scanpydoc.theme'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* This JS is only included when the RTD Sphinx search is active.
3+
*/
4+
5+
setTimeout(overrideSearchButton, 0)
6+
7+
function overrideSearchButton() {
8+
/** @type {HTMLDivElement} */
9+
const search_backdrop = document.querySelector(".search__backdrop")
10+
if (!search_backdrop) {
11+
setTimeout(overrideSearchButton, 500)
12+
return
13+
}
14+
search_backdrop.style.zIndex = "1020"
15+
16+
/** @type {HTMLButtonElement} */
17+
const search_button = document.querySelector("button[aria-label='Search']")
18+
search_button.addEventListener("click", () => {
19+
showSearchModal()
20+
21+
// hide the theme’s search popup
22+
/** @type {HTMLDivElement} */
23+
const theme_popup = document.querySelector(".search-button__wrapper")
24+
theme_popup.style.display = "none"
25+
})
26+
}

src/scanpydoc/theme/theme.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[theme]
22
inherit = sphinx_book_theme
33
pygments_style = tango
4-
sidebars = navbar-logo.html, icon-links.html, sbt-sidebar-nav.html
4+
sidebars = navbar-logo.html, icon-links.html, search-button-field.html, sbt-sidebar-nav.html
55
stylesheet = styles/scanpy.css
66

77
[options]

0 commit comments

Comments
 (0)