From: Adam Dullage Date: Mon, 26 Sep 2022 11:40:52 +0000 (+0100) Subject: Ensure full title is always included in title highlights X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=3f06efebd50aa2dbe775d0a32a2d48399bce3ad2;p=flatnotes.git Ensure full title is always included in title highlights --- diff --git a/flatnotes/flatnotes.py b/flatnotes/flatnotes.py index 424b5e6..4cb90df 100644 --- a/flatnotes/flatnotes.py +++ b/flatnotes/flatnotes.py @@ -6,10 +6,10 @@ from datetime import datetime from typing import List, Literal, Set, Tuple import whoosh -from helpers import empty_dir, re_extract, strip_ext from whoosh import writing from whoosh.analysis import CharsetFilter, StemmingAnalyzer from whoosh.fields import DATETIME, ID, KEYWORD, TEXT, SchemaClass +from whoosh.highlight import ContextFragmenter, WholeFragmenter from whoosh.index import Index from whoosh.qparser import MultifieldParser from whoosh.qparser.dateparse import DateParserPlugin @@ -17,6 +17,8 @@ from whoosh.query import Every from whoosh.searching import Hit from whoosh.support.charset import accent_map +from helpers import empty_dir, re_extract, strip_ext + MARKDOWN_EXT = ".md" INDEX_SCHEMA_VERSION = "3" @@ -113,13 +115,14 @@ class SearchResult(Note): # is a float. self._score = hit.score if type(hit.score) is float else None - self._title_highlights = ( - hit.highlights("title", text=self.title) - if "title" in self._matched_fields - else None - ) + if "title" in self._matched_fields: + hit.results.fragmenter = WholeFragmenter() + self._title_highlights = hit.highlights("title", text=self.title) + else: + self._title_highlights = None if "content" in self._matched_fields: + hit.results.fragmenter = ContextFragmenter() content_ex_tags, _ = Flatnotes.extract_tags(self.content) self._content_highlights = hit.highlights( "content",