From 151679788ab9f737e6feb02419489a74dcf7d98d Mon Sep 17 00:00:00 2001 From: Nick Groenen Date: Sat, 2 Dec 2023 12:19:16 +0100 Subject: [PATCH] Fix: trim filenames while resolving wikilinks Obsidian trims the filename part in a [[WikiLink|label]], so each of these are equivalent: [[wikilink]] [[ wikilink ]] [[ wikilink |wikilink]] Obsidian-export now behaves similarly. Fixes #188 --- src/references.rs | 4 ++-- .../issue-188-wikilinks-need-trimming.md | 17 +++++++++++++++++ .../issue-188-wikilinks-need-trimming.md | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/testdata/expected/main-samples/issue-188-wikilinks-need-trimming.md create mode 100644 tests/testdata/input/main-samples/issue-188-wikilinks-need-trimming.md diff --git a/src/references.rs b/src/references.rs index b354038..c57feda 100644 --- a/src/references.rs +++ b/src/references.rs @@ -73,9 +73,9 @@ impl<'a> ObsidianNoteReference<'a> { let captures = OBSIDIAN_NOTE_LINK_RE .captures(text) .expect("note link regex didn't match - bad input?"); - let file = captures.name("file").map(|v| v.as_str()); + let file = captures.name("file").map(|v| v.as_str().trim()); let label = captures.name("label").map(|v| v.as_str()); - let section = captures.name("section").map(|v| v.as_str()); + let section = captures.name("section").map(|v| v.as_str().trim()); ObsidianNoteReference { file, diff --git a/tests/testdata/expected/main-samples/issue-188-wikilinks-need-trimming.md b/tests/testdata/expected/main-samples/issue-188-wikilinks-need-trimming.md new file mode 100644 index 0000000..69fa125 --- /dev/null +++ b/tests/testdata/expected/main-samples/issue-188-wikilinks-need-trimming.md @@ -0,0 +1,17 @@ +Obsidian trims space before and after the filename in a wikilink target. +These should all be the same: + +[foo](foo.md) +[foo](foo.md) +[foo](foo.md) +[foo](foo.md) + +[foo](foo.md) +[foo](foo.md) +[foo](foo.md) +[foo](foo.md) + +[foo > ^abc](foo.md#abc) +[foo > ^abc](foo.md#abc) +[foo > ^abc](foo.md#abc) +[foo > ^abc](foo.md#abc) diff --git a/tests/testdata/input/main-samples/issue-188-wikilinks-need-trimming.md b/tests/testdata/input/main-samples/issue-188-wikilinks-need-trimming.md new file mode 100644 index 0000000..26f4df9 --- /dev/null +++ b/tests/testdata/input/main-samples/issue-188-wikilinks-need-trimming.md @@ -0,0 +1,17 @@ +Obsidian trims space before and after the filename in a wikilink target. +These should all be the same: + +[[foo]] +[[ foo]] +[[foo ]] +[[ foo ]] + +[[foo|foo]] +[[ foo|foo]] +[[foo |foo]] +[[ foo |foo]] + +[[foo#^abc]] +[[foo#^abc ]] +[[foo#^abc |foo > ^abc]] +[[ foo#^abc ]]