Skip to content

web search done right

Fix autcomplete click bug. When clicking on autocomplete elements in the searchbar, the <b> tags were included in the new search query.

Mikkel Denkere00fc23c7ca9
diff --git a/src/frontend/autosuggest.rs b/src/frontend/autosuggest.rs
index 6f7de855..b72f0752 100644
--- a/src/frontend/autosuggest.rs
+++ b/src/frontend/autosuggest.rs
@@ -17,10 +17,11 @@
use std::{collections::HashMap, sync::Arc};
use axum::{extract, response::IntoResponse, Extension, Json};
+use serde::Serialize;
use super::State;
-fn highlight(query: &str, suggestion: String) -> String {
+fn highlight(query: &str, suggestion: &str) -> String {
let idx = suggestion
.chars()
.zip(query.chars())
@@ -34,6 +35,12 @@ fn highlight(query: &str, suggestion: String) -> String {
new_suggestion
}
+#[derive(Serialize)]
+struct Suggestion {
+ highlighted: String,
+ raw: String,
+}
+
pub async fn route(
extract::Query(params): extract::Query<HashMap<String, String>>,
Extension(state): Extension<Arc<State>>,
@@ -42,7 +49,11 @@ pub async fn route(
let mut suggestions = Vec::new();
for suggestion in state.autosuggest.suggestions(query).unwrap() {
- suggestions.push(highlight(query, suggestion));
+ let highlighted = highlight(query, &suggestion);
+ suggestions.push(Suggestion {
+ highlighted,
+ raw: suggestion,
+ });
}
Json(suggestions)
@@ -57,10 +68,10 @@ mod tests {
#[test]
fn suffix_highlight() {
- assert_eq!(&highlight("", "test".to_string()), "<b>test</b>");
- assert_eq!(&highlight("t", "test".to_string()), "t<b>est</b>");
- assert_eq!(&highlight("te", "test".to_string()), "te<b>st</b>");
- assert_eq!(&highlight("tes", "test".to_string()), "tes<b>t</b>");
- assert_eq!(&highlight("test", "test".to_string()), "test<b></b>");
+ assert_eq!(&highlight("", "test"), "<b>test</b>");
+ assert_eq!(&highlight("t", "test"), "t<b>est</b>");
+ assert_eq!(&highlight("te", "test"), "te<b>st</b>");
+ assert_eq!(&highlight("tes", "test"), "tes<b>t</b>");
+ assert_eq!(&highlight("test", "test"), "test<b></b>");
}
}
diff --git a/templates/searchbar.html b/templates/searchbar.html
index 49cb34d4..5a88ced1 100644
--- a/templates/searchbar.html
+++ b/templates/searchbar.html
@@ -107,7 +107,7 @@
let currentHighlighted = -1;
let currentSuggestions = [];
- let originalQuery = "{{ query|escape}}";
+ let originalQuery = "{{ query|escape }}";
function updateSuggestions(suggestionsData) {
@@ -127,17 +127,18 @@
suggestionsData.forEach(item => {
const node = document.createElement("div");
+ console.log(item);
node.classList.add("flex");
node.classList.add("suggestion-item");
- node.setAttribute("data-suggestion", item)
-
+ node.setAttribute("data-suggestion", item.raw)
+ console.log(node);
node.innerHTML = `
<div class="flex w-4 mr-3 items-center">
<img class="h-5" loading="lazy" src="/static/images/search.svg" />
</div>
<div>
- ${item}
+ ${item.highlighted}
</div>
`.trim();
@@ -147,10 +148,7 @@
suggestionItems = document.querySelectorAll('.suggestion-item')
suggestionItems.forEach(item => item.addEventListener("mousedown", (event) => {
- var suggestion = event.target.dataset.suggestion;
- var searchParams = new URLSearchParams(window.location.search);
- searchParams.set("q", suggestion);
- window.location = "/search?" + searchParams.toString();
+ handleSuggestionClick(event.target);
}));
suggestionItems.forEach((item, idx) => item.addEventListener("mouseenter", (event) => {
@@ -159,6 +157,24 @@
}));
}
+ function handleSuggestionClick(element) {
+ if (element == null) {
+ return;
+ }
+
+ var suggestion = element.dataset.suggestion;
+
+ if (suggestion == undefined) {
+ return handleSuggestionClick(element.parentElement);
+ }
+
+ var searchParams = new URLSearchParams(window.location.search);
+ searchParams.set("q", suggestion);
+ window.location = "/search?" + searchParams.toString();
+
+ return;
+ }
+
function fetchAndUpdateSuggestions() {
const q = searchbarInput.value;