From 6245c9a31d38f3a21adff3092c51132306005775 Mon Sep 17 00:00:00 2001 From: Nick Groenen Date: Tue, 22 Dec 2020 12:37:04 +0100 Subject: [PATCH] Fix: correct relative links within embedded notes Links within an embedded note would point to other local resources relative to the filesystem location of the note being embedded. When a note inside a different directory would embed such a note, these links would point to invalid locations. Now these links are calculated relative to the top note, which ensures these links will point to the right path. --- src/lib.rs | 15 ++++++++++++++- .../main-samples/embeds-note-in-subdir.md | 3 +++ .../input/main-samples/embeds-note-in-subdir.md | 3 +++ .../input/main-samples/subdir/note-in-subdir.md | 1 + 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/testdata/expected/main-samples/embeds-note-in-subdir.md create mode 100644 tests/testdata/input/main-samples/embeds-note-in-subdir.md create mode 100644 tests/testdata/input/main-samples/subdir/note-in-subdir.md diff --git a/src/lib.rs b/src/lib.rs index 9c996a1..8654a67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,6 +124,16 @@ impl Context { .expect("Context not initialized properly, file_tree is empty") } + /// Return the path of the root file. + /// + /// Typically this will yield the same element as `current_file`, but when a note is embedded + /// within another note, this will return the outer-most note. + fn root_file(&self) -> &PathBuf { + self.file_tree + .first() + .expect("Context not initialized properly, file_tree is empty") + } + /// Return the note depth (nesting level) for this context. fn note_depth(&self) -> usize { self.file_tree.len() @@ -440,10 +450,13 @@ impl<'a> Exporter<'a> { ]; } let target_file = target_file.unwrap(); + // We use root_file() rather than current_file() here to make sure links are always + // relative to the outer-most note, which is the note which this content is inserted into + // in case of embedded notes. let rel_link = diff_paths( target_file, &context - .current_file() + .root_file() .parent() .expect("obsidian content files should always have a parent"), ) diff --git a/tests/testdata/expected/main-samples/embeds-note-in-subdir.md b/tests/testdata/expected/main-samples/embeds-note-in-subdir.md new file mode 100644 index 0000000..360dd7e --- /dev/null +++ b/tests/testdata/expected/main-samples/embeds-note-in-subdir.md @@ -0,0 +1,3 @@ +This note embeds `subdir/note-in-subdir.md`: + +This note in `subdir/` links back to [pure-markdown-examples](pure-markdown-examples.md). diff --git a/tests/testdata/input/main-samples/embeds-note-in-subdir.md b/tests/testdata/input/main-samples/embeds-note-in-subdir.md new file mode 100644 index 0000000..f9e6c0f --- /dev/null +++ b/tests/testdata/input/main-samples/embeds-note-in-subdir.md @@ -0,0 +1,3 @@ +This note embeds `subdir/note-in-subdir.md`: + +![[note-in-subdir]] diff --git a/tests/testdata/input/main-samples/subdir/note-in-subdir.md b/tests/testdata/input/main-samples/subdir/note-in-subdir.md new file mode 100644 index 0000000..233e7fa --- /dev/null +++ b/tests/testdata/input/main-samples/subdir/note-in-subdir.md @@ -0,0 +1 @@ +This note in `subdir/` links back to [[pure-markdown-examples]].