Skip to content

Commit

Permalink
fix: handle dashes in wikilinks (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Oct 27, 2024
1 parent ed1be6c commit 97d1cf0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
6 changes: 5 additions & 1 deletion memium/source/document_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 24 additions & 6 deletions memium/source/test_document_source.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from dataclasses import dataclass
from pathlib import Path

import pytest
Expand Down Expand Up @@ -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

0 comments on commit 97d1cf0

Please sign in to comment.