From: ChristianAvila Date: Fri, 6 Dec 2024 21:43:39 +0000 (-0500) Subject: feat: Support hyphen in the tags X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=749fcf0c86fcdc59f10a310a6675fb275bf92ddf;p=flatnotes.git feat: Support hyphen in the tags This requires an index rebuild that can be set as an environment variable during startup and is run during the first call. --- diff --git a/server/notes/file_system/file_system.py b/server/notes/file_system/file_system.py index a463b41..6393f92 100644 --- a/server/notes/file_system/file_system.py +++ b/server/notes/file_system/file_system.py @@ -41,9 +41,9 @@ class IndexSchema(SchemaClass): class FileSystemNotes(BaseNotes): - TAGS_RE = re.compile(r"(?:(?<=^#)|(?<=\s#))\w+(?=\s|$)") + TAGS_RE = re.compile(r"(?:(?<=^#)|(?<=\s#))[a-zA-Z0-9_-]+(?=\s|$)") CODEBLOCK_RE = re.compile(r"`{1,3}.*?`{1,3}", re.DOTALL) - TAGS_WITH_HASH_RE = re.compile(r"(?:(?<=^)|(?<=\s))#\w+(?=\s|$)") + TAGS_WITH_HASH_RE = re.compile(r"(?:(?<=^)|(?<=\s))#[a-zA-Z0-9_-]+(?=\s|$)") def __init__(self): self.storage_path = get_env("FLATNOTES_PATH", mandatory=True) @@ -52,7 +52,8 @@ class FileSystemNotes(BaseNotes): f"'{self.storage_path}' is not a valid directory." ) self.index = self._load_index() - self._sync_index_with_retry(optimize=True) + clean = get_env("FLATNOTES_CLEAN_INDEX", default=False, cast_bool=True) + self._sync_index_with_retry(optimize=True, clean=clean) def create(self, data: NoteCreate) -> Note: """Create a new note.""" @@ -196,8 +197,11 @@ class FileSystemNotes(BaseNotes): - The content without the tags. - A set of tags converted to lowercase.""" content_ex_codeblock = re.sub(cls.CODEBLOCK_RE, "", content) + logger.debug(f"Content without codeblocks: {content_ex_codeblock}") _, tags = cls._re_extract(cls.TAGS_RE, content_ex_codeblock) + logger.debug(f"Tags extracted: {tags}") content_ex_tags, _ = cls._re_extract(cls.TAGS_RE, content) + logger.debug(f"Content without tags: {content_ex_tags}") try: tags = [tag.lower() for tag in tags] return (content_ex_tags, set(tags)) @@ -235,6 +239,7 @@ class FileSystemNotes(BaseNotes): indexed = set() writer = self.index.writer() if clean: + logger.info(f"Cleaning index {clean}") writer.mergetype = writing.CLEAR # Clear the index with self.index.searcher() as searcher: for idx_note in searcher.all_stored_fields(): @@ -264,6 +269,7 @@ class FileSystemNotes(BaseNotes): writer, self._get_by_filename(filename) ) logger.info(f"'{filename}' added to index") + writer.commit(optimize=optimize) logger.info("Index synchronized")