autocomplete: fix incorrect highlighting when tag contains repeated words.

Fix tags like `short_shorts` or `hunter_x_hunter` being highlighted
incorrectly. Typing `short_sh` would highlight it as SHort_SHorts
instead of as SHORT_SHorts.
This commit is contained in:
evazion
2022-09-05 21:07:54 -05:00
parent d2147eca80
commit a80ee22746
2 changed files with 47 additions and 15 deletions

View File

@@ -56,7 +56,7 @@ class AutocompleteComponent < ApplicationComponent
# highlight_matching_words("very_long_hair", "long_ha*") => "<span>very_</span><b>long</b><span>_</span><b>hair</b>"
def highlight_matching_words(target, pattern)
pattern_words = Tag.parse_query(pattern)
pattern_words.sort_by! { |word| [word.include?("*") ? 0 : 1, -word.size] }
pattern_words.sort_by! { |word| [word.include?("*") ? 1 : 0, -word.size] }
target_words = Tag.split_words(target)
target_words.map do |word|