31
31
class SearchIndex :
32
32
"""Class to get search index."""
33
33
34
- def __init__ (self , doc_name , app ):
34
+ def __init__ (self , doc_name , app , version_url_prefix ):
35
35
"""Initialize the class.
36
36
37
37
Parameters
@@ -40,12 +40,15 @@ def __init__(self, doc_name, app):
40
40
Document name.
41
41
app : Sphinx
42
42
Sphinx application.
43
+ version : str
44
+ Version of the document for prefixing the path.
43
45
"""
44
46
self ._doc_name = doc_name
45
47
self .doc_path = f"{ self ._doc_name } .html"
46
48
self .doc_title = app .env .titles [self ._doc_name ].astext ()
47
49
self ._doc_tree = app .env .get_doctree (self ._doc_name )
48
50
self .sections = []
51
+ self .url_prefix = version_url_prefix
49
52
50
53
def iterate_through_docs (self ):
51
54
"""Iterate through the document."""
@@ -71,7 +74,7 @@ def indices(self):
71
74
for sections in self .sections :
72
75
search_index = {
73
76
"objectID" : self ._doc_name ,
74
- "href" : f"{ self .doc_path } #{ sections ['section_anchor_id' ]} " ,
77
+ "href" : f"{ self .url_prefix } { self . doc_path } #{ sections ['section_anchor_id' ]} " ,
75
78
"title" : self .doc_title ,
76
79
"section" : sections ["section_title" ],
77
80
"text" : sections ["section_text" ],
@@ -100,11 +103,14 @@ def create_search_index(app, exception):
100
103
if not app .config .html_theme_options .get ("static_search" , {}):
101
104
return
102
105
106
+ switcher_version = app .config .html_theme_options .get ("switcher" , {}).get ("version_match" , "" )
107
+ version_url_prefix = f"version/{ switcher_version } /" if switcher_version else ""
108
+
103
109
all_docs = app .env .found_docs
104
110
search_index_list = []
105
111
106
112
for document in all_docs :
107
- search_index = SearchIndex (document , app )
113
+ search_index = SearchIndex (document , app , version_url_prefix )
108
114
search_index .iterate_through_docs ()
109
115
search_index_list .extend (search_index .indices )
110
116
0 commit comments