Skip to content

Commit

Permalink
fix: change file location for search.json file (#509)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
Revathyvenugopal162 and pyansys-ci-bot authored Sep 24, 2024
1 parent d99b7d9 commit b43b090
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/509.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: change file location for `search.json` file
12 changes: 4 additions & 8 deletions src/ansys_sphinx_theme/search/fuse_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class SearchIndex:
"""Class to get search index."""

def __init__(self, doc_name, app, version_url_prefix):
def __init__(self, doc_name, app):
"""Initialize the class.
Parameters
Expand All @@ -48,7 +48,6 @@ def __init__(self, doc_name, app, version_url_prefix):
self.doc_title = app.env.titles[self._doc_name].astext()
self._doc_tree = app.env.get_doctree(self._doc_name)
self.sections = []
self.url_prefix = version_url_prefix

def iterate_through_docs(self):
"""Iterate through the document."""
Expand All @@ -74,7 +73,7 @@ def indices(self):
for sections in self.sections:
search_index = {
"objectID": self._doc_name,
"href": f"{self.url_prefix}{self.doc_path}#{sections['section_anchor_id']}",
"href": f"{self.doc_path}#{sections['section_anchor_id']}",
"title": self.doc_title,
"section": sections["section_title"],
"text": sections["section_text"],
Expand All @@ -100,17 +99,14 @@ def create_search_index(app, exception):
if exception:
return

switcher_version = app.config.html_theme_options.get("switcher", {}).get("version_match", "")
version_url_prefix = f"version/{switcher_version}/" if switcher_version else ""

all_docs = app.env.found_docs
search_index_list = []

for document in all_docs:
search_index = SearchIndex(document, app, version_url_prefix)
search_index = SearchIndex(document, app)
search_index.iterate_through_docs()
search_index_list.extend(search_index.indices)

search_index = app.builder.outdir / "search.json"
search_index = app.builder.outdir / "_static" / "search.json"
with search_index.open("w", encoding="utf-8") as index_file:
json.dump(search_index_list, index_file, ensure_ascii=False, indent=4)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var theme_static_search = JSON.parse('{{ theme_static_search | tojson | safe }}');
var theme_limit = "{{ theme_limit }}";
var min_chars_for_search = "{{ min_chars_for_search }}";
var searchPath = "{{ pathto('_static/search.json', 1) }}";

</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ require(["fuse"], function (Fuse) {
return text.replace(regex, '<span class="highlight">$1</span>');
}

function getDynamicPath(targetFile) {
// Get the content root from the HTML element
const contentRoot =
document.documentElement.getAttribute("data-content_root");
return `${contentRoot}${targetFile}`;
}

function navigateToHref(href) {
const baseUrl = window.location.origin;
const relativeUrl = href.startsWith("/") ? href : `/${href}`;
window.location.href = new URL(relativeUrl, baseUrl).href;
const finalUrl = getDynamicPath(href);
window.location.href = finalUrl;
}

const searchBox = document
Expand All @@ -109,20 +115,21 @@ require(["fuse"], function (Fuse) {
if (currentIndex >= 0 && currentIndex < resultItems.length) {
const href = resultItems[currentIndex].getAttribute("data-href");
navigateToHref(href);
} else if (resultItems.length > 0) {
const href = resultItems[0].getAttribute("data-href");
navigateToHref(href);
}
break;

case "ArrowDown":
if (resultItems.length > 0) {
currentIndex = (currentIndex + 1) % resultItems.length; // Move down
console.log("move down");
focusSelected(resultItems);
}
break;

case "ArrowUp":
if (resultItems.length > 0) {
console.log("move up");
currentIndex =
(currentIndex - 1 + resultItems.length) % resultItems.length; // Move up
focusSelected(resultItems);
Expand Down Expand Up @@ -160,20 +167,17 @@ require(["fuse"], function (Fuse) {
if (currentIndex >= 0 && currentIndex < resultItems.length) {
// Remove selected class from all items
resultItems.forEach((item) => item.classList.remove("selected"));
console.log(currentIndex);
const currentItem = resultItems[currentIndex];
currentItem.classList.add("selected"); // Add selected class

// Apply native focus
console.log(currentItem);
// Apply native focus to the current item
currentItem.focus();

// Scroll the focused item into view
currentItem.scrollIntoView({ block: "nearest" });
}
}

fetch("../../search.json")
fetch(searchPath)
.then((response) =>
response.ok
? response.json()
Expand Down

0 comments on commit b43b090

Please sign in to comment.