Skip to content

Commit f961e7e

Browse files
DocSearch (#26)
Co-authored-by: Isaac Virshup <[email protected]>
1 parent fca3aa2 commit f961e7e

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

scanpydoc/theme/__init__.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
Theme options
1010
=============
1111
12-
This theme only adds one configuration value:
12+
This theme adds the following configuration option,
13+
and the ones under `docsearch options`_:
1314
1415
.. confval:: accent_color
1516
@@ -28,6 +29,43 @@
2829
display_version=False,
2930
)
3031
32+
Docsearch options
33+
-----------------
34+
35+
These two configuration values are required to use docsearch_:
36+
37+
.. _docsearch: https://docsearch.algolia.com/
38+
39+
.. confval:: docsearch_key
40+
41+
:type: str
42+
43+
The API key provided by docsearch.
44+
45+
.. confval:: docsearch_index
46+
47+
:type: str
48+
49+
The index name used by docsearch.
50+
51+
The following configuration values are optional:
52+
53+
.. confval:: docsearch_doc_version
54+
55+
:type: str
56+
:default: ``'latest'`` or ``'stable'``
57+
58+
The documentation version searched.
59+
The default is ``'stable'`` if ``READTHEDOCS_VERSION=stable`` is set,
60+
and ``'latest'`` otherwise.
61+
62+
.. confval:: docsearch_js_version
63+
64+
:type: str
65+
:default: ``'2.6'``
66+
67+
The docsearch library version used.
68+
3169
"""
3270

3371
from pathlib import Path

scanpydoc/theme/layout.html

+28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
{% extends "sphinx_rtd_theme/layout.html" %}
2+
3+
<!-- Have to use htmltitle to include this earlier as our style -->
4+
{%- block htmltitle %}
5+
{{ super() }}
6+
{% if theme_docsearch_key and theme_docsearch_index %}
7+
<link rel=stylesheet href="https://cdn.jsdelivr.net/npm/docsearch.js@{{ theme_docsearch_js_version }}/dist/cdn/docsearch.min.css"/>
8+
{% endif %}
9+
{%- endblock -%}
10+
11+
<!-- Styles that come last -->
212
{%- block extrahead %}
313
{{ super() }}
414
{% if theme_accent_color %}
@@ -7,3 +17,21 @@
717
</style>
818
{% endif %}
919
{% endblock %}
20+
21+
{% set safe_version = version if version in ["latest", "stable"] else "latest" %}
22+
23+
{% block scripts %}
24+
{{ super() }}
25+
{% if theme_docsearch_key and theme_docsearch_index %}
26+
<script src="https://cdn.jsdelivr.net/npm/docsearch.js@{{ theme_docsearch_js_version }}/dist/cdn/docsearch.min.js"></script>
27+
<script>
28+
document.addEventListener('DOMContentLoaded', () => docsearch({
29+
apiKey: '{{ theme_docsearch_key }}',
30+
indexName: '{{ theme_docsearch_index }}',
31+
inputSelector: '#rtd-search-form > input[name="q"]',
32+
algoliaOptions: { facetFilters: ['version:{{ theme_docsearch_doc_version or safe_version }}'] },
33+
debug: false,
34+
}))
35+
</script>
36+
{% endif %}
37+
{% endblock %}

scanpydoc/theme/static/css/scanpy.css

+11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@
99
.wy-side-nav-search input[type="text"] { border-width: 0 }
1010
.wy-side-nav-search a.icon-home { font-size: 2em; color: var(--accent-color) }
1111
.wy-side-nav-search a.icon-home::before { content: none }
12+
.wy-menu li a { overflow: hidden; text-overflow: ellipsis; } /* display ellipsis in long items */
1213

14+
/* DocSearch adaptations */
15+
16+
.wy-nav-side { overflow: visible; }
17+
.wy-side-scroll { overflow: inherit; }
18+
19+
/* Making it a bit wider */
20+
.algolia-autocomplete .ds-dropdown-menu {
21+
max-width: 1000px;
22+
min-width: 80vw;
23+
}
1324

1425
/* Custom classes */
1526

scanpydoc/theme/theme.conf

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ style_external_links = False
1818
style_nav_header_background =
1919
# added by scanpydoc
2020
accent_color =
21+
docsearch_key =
22+
docsearch_index =
23+
docsearch_doc_version =
24+
docsearch_js_version = 2.6

0 commit comments

Comments
 (0)