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]].