From 7506e3990d515ad97953f867b5e1af236951b430 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 7 Jul 2020 23:27:44 +0200 Subject: [PATCH] Detect all kinds of local links Not only links which start with ./ We also try to guess with the .md or .txt extension if the file is present. Fixes #173 --- lib/widgets/note_viewer.dart | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/widgets/note_viewer.dart b/lib/widgets/note_viewer.dart index dd903eb3..20b7559e 100644 --- a/lib/widgets/note_viewer.dart +++ b/lib/widgets/note_viewer.dart @@ -61,18 +61,38 @@ class NoteViewer extends StatelessWidget { data: note.body, // selectable: false, -> making this true breaks link navigation styleSheet: markdownStyleSheet, - onTapLink: (String link) { + onTapLink: (String link) async { + var spec = link; if (link.startsWith('./')) { - var spec = link.substring(2); - var linkedNote = note.parent.getNoteWithSpec(spec); + spec = link.substring(2); + } + + var linkedNote = note.parent.getNoteWithSpec(spec); + if (linkedNote != null) { + openNoteEditor(context, linkedNote); + return; + } + + if (!spec.endsWith('.md')) { + linkedNote = note.parent.getNoteWithSpec(spec + '.md'); if (linkedNote != null) { openNoteEditor(context, linkedNote); - } else { - showSnackbar(context, "Link '$link' not found"); + return; } - } else { - Log.i("Launching $link"); - launch(link); + } + if (!spec.endsWith('.txt')) { + linkedNote = note.parent.getNoteWithSpec(spec + '.txt'); + if (linkedNote != null) { + openNoteEditor(context, linkedNote); + return; + } + } + + try { + await launch(link); + } catch (e, stackTrace) { + Log.e("Opening Link", ex: e, stacktrace: stackTrace); + showSnackbar(context, "Link '$link' not found"); } }, imageBuilder: (url, title, alt) => kDefaultImageBuilder(