Skip to content

Commit b43b090

Browse files
fix: change file location for search.json file (#509)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent d99b7d9 commit b43b090

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

doc/changelog.d/509.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: change file location for `search.json` file

src/ansys_sphinx_theme/search/fuse_search.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
class SearchIndex:
3232
"""Class to get search index."""
3333

34-
def __init__(self, doc_name, app, version_url_prefix):
34+
def __init__(self, doc_name, app):
3535
"""Initialize the class.
3636
3737
Parameters
@@ -48,7 +48,6 @@ def __init__(self, doc_name, app, version_url_prefix):
4848
self.doc_title = app.env.titles[self._doc_name].astext()
4949
self._doc_tree = app.env.get_doctree(self._doc_name)
5050
self.sections = []
51-
self.url_prefix = version_url_prefix
5251

5352
def iterate_through_docs(self):
5453
"""Iterate through the document."""
@@ -74,7 +73,7 @@ def indices(self):
7473
for sections in self.sections:
7574
search_index = {
7675
"objectID": self._doc_name,
77-
"href": f"{self.url_prefix}{self.doc_path}#{sections['section_anchor_id']}",
76+
"href": f"{self.doc_path}#{sections['section_anchor_id']}",
7877
"title": self.doc_title,
7978
"section": sections["section_title"],
8079
"text": sections["section_text"],
@@ -100,17 +99,14 @@ def create_search_index(app, exception):
10099
if exception:
101100
return
102101

103-
switcher_version = app.config.html_theme_options.get("switcher", {}).get("version_match", "")
104-
version_url_prefix = f"version/{switcher_version}/" if switcher_version else ""
105-
106102
all_docs = app.env.found_docs
107103
search_index_list = []
108104

109105
for document in all_docs:
110-
search_index = SearchIndex(document, app, version_url_prefix)
106+
search_index = SearchIndex(document, app)
111107
search_index.iterate_through_docs()
112108
search_index_list.extend(search_index.indices)
113109

114-
search_index = app.builder.outdir / "search.json"
110+
search_index = app.builder.outdir / "_static" / "search.json"
115111
with search_index.open("w", encoding="utf-8") as index_file:
116112
json.dump(search_index_list, index_file, ensure_ascii=False, indent=4)

src/ansys_sphinx_theme/theme/ansys_sphinx_theme/components/ast-search-button.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var theme_static_search = JSON.parse('{{ theme_static_search | tojson | safe }}');
1313
var theme_limit = "{{ theme_limit }}";
1414
var min_chars_for_search = "{{ min_chars_for_search }}";
15+
var searchPath = "{{ pathto('_static/search.json', 1) }}";
1516

1617
</script>
1718

src/ansys_sphinx_theme/theme/ansys_sphinx_theme/static/js/fuse_search.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ require(["fuse"], function (Fuse) {
8080
return text.replace(regex, '<span class="highlight">$1</span>');
8181
}
8282

83+
function getDynamicPath(targetFile) {
84+
// Get the content root from the HTML element
85+
const contentRoot =
86+
document.documentElement.getAttribute("data-content_root");
87+
return `${contentRoot}${targetFile}`;
88+
}
89+
8390
function navigateToHref(href) {
84-
const baseUrl = window.location.origin;
85-
const relativeUrl = href.startsWith("/") ? href : `/${href}`;
86-
window.location.href = new URL(relativeUrl, baseUrl).href;
91+
const finalUrl = getDynamicPath(href);
92+
window.location.href = finalUrl;
8793
}
8894

8995
const searchBox = document
@@ -109,20 +115,21 @@ require(["fuse"], function (Fuse) {
109115
if (currentIndex >= 0 && currentIndex < resultItems.length) {
110116
const href = resultItems[currentIndex].getAttribute("data-href");
111117
navigateToHref(href);
118+
} else if (resultItems.length > 0) {
119+
const href = resultItems[0].getAttribute("data-href");
120+
navigateToHref(href);
112121
}
113122
break;
114123

115124
case "ArrowDown":
116125
if (resultItems.length > 0) {
117126
currentIndex = (currentIndex + 1) % resultItems.length; // Move down
118-
console.log("move down");
119127
focusSelected(resultItems);
120128
}
121129
break;
122130

123131
case "ArrowUp":
124132
if (resultItems.length > 0) {
125-
console.log("move up");
126133
currentIndex =
127134
(currentIndex - 1 + resultItems.length) % resultItems.length; // Move up
128135
focusSelected(resultItems);
@@ -160,20 +167,17 @@ require(["fuse"], function (Fuse) {
160167
if (currentIndex >= 0 && currentIndex < resultItems.length) {
161168
// Remove selected class from all items
162169
resultItems.forEach((item) => item.classList.remove("selected"));
163-
console.log(currentIndex);
164170
const currentItem = resultItems[currentIndex];
165171
currentItem.classList.add("selected"); // Add selected class
166172

167-
// Apply native focus
168-
console.log(currentItem);
173+
// Apply native focus to the current item
169174
currentItem.focus();
170175

171176
// Scroll the focused item into view
172177
currentItem.scrollIntoView({ block: "nearest" });
173178
}
174179
}
175-
176-
fetch("../../search.json")
180+
fetch(searchPath)
177181
.then((response) =>
178182
response.ok
179183
? response.json()

0 commit comments

Comments
 (0)