Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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===` | `<mark>foo bar</mark>`

> **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

Expand Down Expand Up @@ -148,4 +138,4 @@ obsidian_to_hugo = ObsidianToHugo(
)

obsidian_to_hugo.run()
```
```
4 changes: 2 additions & 2 deletions src/obsidian_to_hugo/wiki_links_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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(),
}
Expand Down
53 changes: 53 additions & 0 deletions tests/test_wiki_links_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down