WikiLinks: Always resolve from the base path

Fixes #212

With this [[Fire]] will always resolve the rootFolder/Fire.md
irrespective of which note it appears in. This is analogous to the way
Obsidian works.
This commit is contained in:
Vishesh Handa
2020-08-11 00:11:45 +02:00
parent 3d23d2b343
commit 7e6a74dbba
2 changed files with 12 additions and 4 deletions

View File

@ -7,7 +7,7 @@ class LinkResolver {
Note resolve(String link) {
var spec = link;
var parent = inputNote.parent;
var rootFolder = inputNote.parent.rootFolder;
if (link.startsWith('[[') && link.endsWith(']]') && link.length > 4) {
// FIXME: What if the case is different?
@ -18,20 +18,20 @@ class LinkResolver {
spec = link.substring(2);
}
var linkedNote = parent.getNoteWithSpec(spec);
var linkedNote = rootFolder.getNoteWithSpec(spec);
if (linkedNote != null) {
return linkedNote;
}
if (!spec.endsWith('.md')) {
linkedNote = parent.getNoteWithSpec(spec + '.md');
linkedNote = rootFolder.getNoteWithSpec(spec + '.md');
if (linkedNote != null) {
return linkedNote;
}
}
if (!spec.endsWith('.txt')) {
linkedNote = parent.getNoteWithSpec(spec + '.txt');
linkedNote = rootFolder.getNoteWithSpec(spec + '.txt');
if (linkedNote != null) {
return linkedNote;
}

View File

@ -75,6 +75,14 @@ void main() {
var resolvedNote = linkResolver.resolve('[[zeplin]]');
expect(resolvedNote.filePath, p.join(tempDir.path, 'zeplin.txt'));
});
test('Non base path [[Fire]] should resolve to [[Fire.md]]', () {
var note = rootFolder.getNoteWithSpec('Folder/Water.md');
var linkResolver = LinkResolver(note);
var resolvedNote = linkResolver.resolve('[[Fire]]');
expect(resolvedNote.filePath, p.join(tempDir.path, 'Fire.md'));
});
}
Future<void> generateNote(String basePath, String path) async {