Render search suggestions on the server
The typeahead requests /_search via htmx (debounced, newer requests replace in-flight ones) and gets rendered rows with the match highlighting done in Go. Rows are links, so picking in the header is plain navigation; the editor's link dialog reads data-name instead. The editor's [[ autocomplete keeps the JSON response. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHighlightMatches(t *testing.T) {
|
||||
cases := []struct{ name, query, want string }{
|
||||
{"Project Alpha", "alpha", "Project <strong>Alpha</strong>"},
|
||||
// Overlapping and adjacent spans merge.
|
||||
{"Project Alpha", "pro ject", "<strong>Project</strong> Alpha"},
|
||||
{"Project Alpha", "roj proj", "<strong>Proj</strong>ect Alpha"},
|
||||
{"a<b>&c", "b", "a<<strong>b</strong>>&c"},
|
||||
{"Notes", "zzz", "Notes"},
|
||||
// Lowercasing changes the byte length: no highlight, still escaped.
|
||||
{"İstanbul <x>", "stan", "İstanbul <x>"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := string(highlightMatches(c.name, suggestTokens(c.query))); got != c.want {
|
||||
t.Errorf("highlightMatches(%q, %q) = %q, want %q", c.name, c.query, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user