From 97d1cf0e92db1b9ab9e00de883d37b24870930c0 Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Sun, 27 Oct 2024 12:57:29 +0100 Subject: [PATCH] fix: handle dashes in wikilinks (#740) --- memium/source/document_source.py | 6 +++++- memium/source/test_document_source.py | 30 +++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/memium/source/document_source.py b/memium/source/document_source.py index d9a2a10d..3e1eecf3 100644 --- a/memium/source/document_source.py +++ b/memium/source/document_source.py @@ -33,7 +33,11 @@ def _replace_wikilinks_with_styling(self, input_str: str) -> str: @staticmethod def _replace_alias_wiki_links(text: str) -> str: - regex_pattern = r"\[\[[\w|\s|\d|\/\(|\)]+\|[\w|\s|\d|\/]+\]\]" + ob = r"\[\[" # Open bracket + cb = r"\]\]" # Close bracket + word_contents = r"\w|\s|\d|\-" # Word contents + + regex_pattern = rf"{ob}[{word_contents}|\/\(|\)]+\|[{word_contents}|\/]+{cb}" pattern_matches = re.findall(pattern=regex_pattern, string=text, flags=re.DOTALL) for match in pattern_matches: diff --git a/memium/source/test_document_source.py b/memium/source/test_document_source.py index be3d9a1c..11162d1a 100644 --- a/memium/source/test_document_source.py +++ b/memium/source/test_document_source.py @@ -1,4 +1,5 @@ import logging +from dataclasses import dataclass from pathlib import Path import pytest @@ -53,9 +54,26 @@ def test_sanitize_to_valid_markdown(self): ) -def test_replace_alias_wiki_links(): - text = "Linking to a valid [[Note/Note2|Note Alias]], and can handle [[Note2|Multiple Aliases]]" - expected_output = "Linking to a valid [[Note Alias]], and can handle [[Multiple Aliases]]" - assert ( - MarkdownDocumentSource(directory=Path())._replace_alias_wiki_links(text) == expected_output # type: ignore - ) +@dataclass(frozen=True) +class Ex: + given: str + # Required setup + + then: str + # What the expected result is + + +@pytest.mark.parametrize( + ("example"), + [ + Ex("[[N|Alias]]", "[[Alias]]"), # Base + Ex(" [[N|Alias]] ", " [[Alias]] "), # Spaces + Ex("[[N/N2|Alias]]", "[[Alias]]"), # Nesting + Ex("[[-|A]]", "[[A]]"), # Dash + ], + ids=lambda x: x.given, +) +def test_replace_alias_wiki_links(example: Ex): + given = MarkdownDocumentSource._replace_alias_wiki_links(example.given) # type: ignore + then = example.then + assert given == then