Skip to content

Commit

Permalink
Only show search results from the current version of the docs (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff authored Jan 22, 2025
1 parent e6689e8 commit ad0dbf5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
34 changes: 28 additions & 6 deletions src/components/nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,44 @@ fn SearchModal() -> Element {
let (bytes, _) =
dioxus_search::yazi::decompress(&data, dioxus_search::yazi::Format::Zlib).ok()?;

let index = dioxus_search::SearchIndex::from_bytes("search", bytes);
let index: dioxus_search::SearchIndex<Route> =
dioxus_search::SearchIndex::from_bytes("search", bytes);

Some(index)
});

let search = move || {
let query = &search_text.read();
search_index
let mut results = search_index
.value()
.as_ref()
.map(|search| search.as_ref().map(|s| s.search(query)))
.flatten()
.unwrap_or_else(|| Ok(vec![]))
.and_then(|search| search.as_ref().map(|s| s.search(query)))
.unwrap_or_else(|| Ok(vec![]));
let current_route: Route = router().current();

// Only show search results from the version of the docs the user is currently on (or the latest if they
// are not on a doc page)
if let Ok(results) = &mut results {
results.retain(|result| {
// If the user is not on a doc page, show only the latest docs
if !current_route.is_docs() {
return result.route.is_latest_docs();
}
// Otherwise, show the results from the current version of the docs
matches!(
(&current_route, &result.route),
(Route::Docs06 { .. }, Route::Docs06 { .. })
| (Route::Docs05 { .. }, Route::Docs05 { .. })
| (Route::Docs04 { .. }, Route::Docs04 { .. })
| (Route::Docs03 { .. }, Route::Docs03 { .. })
)
});
}

results
};

let mut results = use_signal(|| search());
let mut results = use_signal(search);

let mut last_key_press = use_signal(|| {
if cfg!(target_arch = "wasm32") {
Expand Down
16 changes: 10 additions & 6 deletions src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ impl CurrentDocsVersion {
}
}

pub fn use_current_docs_version() -> CurrentDocsVersion {
pub fn use_try_current_docs_version() -> Option<CurrentDocsVersion> {
let route = use_route();
match route {
Route::Docs06 { child } => CurrentDocsVersion::V06(child),
Route::Docs05 { child } => CurrentDocsVersion::V05(child),
Route::Docs04 { child } => CurrentDocsVersion::V04(child),
Route::Docs03 { child } => CurrentDocsVersion::V03(child),
_ => panic!("current docs version should be set"),
Route::Docs06 { child } => Some(CurrentDocsVersion::V06(child)),
Route::Docs05 { child } => Some(CurrentDocsVersion::V05(child)),
Route::Docs04 { child } => Some(CurrentDocsVersion::V04(child)),
Route::Docs03 { child } => Some(CurrentDocsVersion::V03(child)),
_ => None,
}
}

pub fn use_current_docs_version() -> CurrentDocsVersion {
use_try_current_docs_version().expect("current docs version should be set")
}

pub trait AnyBookRoute: Routable + PartialEq + Hash + Eq + Clone + Copy {
fn sections(&self) -> &[use_mdbook::mdbook_shared::Section];
fn page(&self) -> &use_mdbook::mdbook_shared::Page<Self>;
Expand Down

0 comments on commit ad0dbf5

Please sign in to comment.