adjusted highlight filter, to highlight a perfect match

This commit is contained in:
Kamil
2024-11-27 17:15:35 +00:00
parent c9363104ec
commit c6eb95112e

View File

@@ -14,10 +14,17 @@ def template_filter(name):
return decorator return decorator
@template_filter('highlight') @template_filter('highlight')
def highlight_search(text, search_query): def highlight_search(text: str, search_query: str) -> Markup:
if not search_query: if not search_query:
return text return text
search_query_escaped = re.escape(search_query) search_query_escaped = re.escape(search_query)
# If the text matches the search query exactly, apply a different highlight
if text.strip().lower() == search_query.strip().lower():
return Markup(f'<mark style="background-color: lightgreen; color: black;">{text}</mark>')
# Highlight partial matches in the text
highlighted_text = re.sub( highlighted_text = re.sub(
f"({search_query_escaped})", f"({search_query_escaped})",
r'<mark>\1</mark>', r'<mark>\1</mark>',