From 75158fcdf73f254770af75709278b87f44bcbf62 Mon Sep 17 00:00:00 2001 From: Stephan Koelker Date: Tue, 15 Apr 2025 10:11:53 +0800 Subject: [PATCH 1/2] Implement wiki link escaping --- src/obsidian_to_hugo/wiki_links_processor.py | 4 +- tests/test_wiki_links_processor.py | 53 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/obsidian_to_hugo/wiki_links_processor.py b/src/obsidian_to_hugo/wiki_links_processor.py index 387e468..13f8470 100644 --- a/src/obsidian_to_hugo/wiki_links_processor.py +++ b/src/obsidian_to_hugo/wiki_links_processor.py @@ -7,6 +7,7 @@ WikiLink = TypedDict("WikiLink", {"wiki_link": str, "link": str, "text": str}) +WIKI_LINK_PATTERN = re.compile(r"(?:^|(?<=[^\\])|(?<=\\\\))\[\[(.*?)\]\]") def get_wiki_links(text: str) -> List[WikiLink]: @@ -18,8 +19,7 @@ def get_wiki_links(text: str) -> List[WikiLink]: - text: the possible extracted text """ wiki_links = [] - wiki_link_regex = r"\[\[(.*?)\]\]" - for match in re.finditer(wiki_link_regex, text): + for match in re.finditer(WIKI_LINK_PATTERN, text): out = { "wiki_link": match.group(), } diff --git a/tests/test_wiki_links_processor.py b/tests/test_wiki_links_processor.py index 9cb0114..e19a648 100644 --- a/tests/test_wiki_links_processor.py +++ b/tests/test_wiki_links_processor.py @@ -24,6 +24,59 @@ def test_get_wiki_links(self): }, ) + def test_get_wiki_links_no_space(self): + text = "hello[[world]] [[bar|baz]]" + links = wiki_links_processor.get_wiki_links(text) + self.assertEqual(len(links), 2) + self.assertEqual( + links[0], + { + "wiki_link": "[[world]]", + "link": "world", + "text": "world", + }, + ) + self.assertEqual( + links[1], + { + "wiki_link": "[[bar|baz]]", + "link": "bar", + "text": "baz", + }, + ) + + def test_get_wiki_link_with_escape(self): + wiki_links = wiki_links_processor.get_wiki_links("Hello \\[[world]], [[foo]]") + self.assertEqual(len(wiki_links), 1) + self.assertEqual( + wiki_links[0], + { + "wiki_link": "[[foo]]", + "link": "foo", + "text": "foo", + }, + ) + + def test_get_wiki_link_with_escaped_backslash(self): + wiki_links = wiki_links_processor.get_wiki_links("Hello \\\\[[world]], [[foo]]") + self.assertEqual(len(wiki_links), 2) + self.assertEqual( + wiki_links[0], + { + "wiki_link": "[[world]]", + "link": "world", + "text": "world", + }, + ) + self.assertEqual( + wiki_links[1], + { + "wiki_link": "[[foo]]", + "link": "foo", + "text": "foo", + }, + ) + def test_convert_wiki_link(self): wiki_link = wiki_links_processor.get_wiki_links("[[foo]]")[0] hugo_link = wiki_links_processor.wiki_link_to_hugo_link(wiki_link) From bea58eace4b4df2ec5d8773516c62c3d4e049069 Mon Sep 17 00:00:00 2001 From: Stephan Koelker Date: Wed, 16 Apr 2025 09:40:26 +0800 Subject: [PATCH 2/2] Update README: remove note about wiki link escapes --- README.md | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/README.md b/README.md index 0d21052..02bf34f 100644 --- a/README.md +++ b/README.md @@ -41,16 +41,6 @@ It takes care of the following steps: | `[[/some/wiki/link#Some Heading\|Some Heading Link]]` | `[Some Heading Link]({{< ref "/some/wiki/link#some-heading" >}})` | `==foo bar===` | `foo bar` -> **Note** -> For now, there is *no way to escape* obsidian wiki links. Every link -> will be replaced with a hugo link. The only way to get around this is changing -> the wiki link to don't match the exact sytax, for example by adding an -> [invisible space](https://en.wikipedia.org/wiki/Zero-width_space) (Obsidian will highlight the invisible character as a red dot). -> ![](https://raw.githubusercontent.com/devidw/obsidian-to-hugo/master/img/do-not-do-that.png) -> However, this still is really really *not* best -> practice, so if anyone wants to implement real escaping, [please do -> so](https://github.com/devidw/obsidian-to-hugo/pulls). - ## Installation @@ -148,4 +138,4 @@ obsidian_to_hugo = ObsidianToHugo( ) obsidian_to_hugo.run() -``` \ No newline at end of file +```