Parse links in wiki syntax [[term]]

This way we can use this faster syntax which is also supported by
Obsidian.
This commit is contained in:
Vishesh Handa
2020-06-08 00:51:13 +02:00
parent 702366671a
commit 67c2777d9b
3 changed files with 48 additions and 0 deletions

View File

@ -51,6 +51,14 @@ class LinkExtractor implements md.NodeVisitor {
var url = el.attributes['href'];
var link = Link(term: title, filePath: url);
links.add(link);
return;
}
if (tag == 'wikiLink') {
var value = el.attributes['value'];
var link = Link(term: value, filePath: null);
links.add(link);
return;
}
}
@ -61,3 +69,17 @@ class LinkExtractor implements md.NodeVisitor {
return links;
}
}
class MetaLinkSyntax extends md.InlineSyntax {
static final String _pattern = '\\[\\[([^\\[\\]]+)\\]\\]';
MetaLinkSyntax() : super(_pattern);
@override
bool onMatch(md.InlineParser parser, Match match) {
md.Element el = md.Element.withTag('wikiLink');
el.attributes['value'] = '${match[1].trim()}';
parser.addNode(el);
return true;
}
}

View File

@ -450,6 +450,7 @@ class Note with NotesNotifier {
final doc = md.Document(
encodeHtml: false,
extensionSet: md.ExtensionSet.gitHubFlavored,
inlineSyntaxes: [MetaLinkSyntax()],
);
var lines = body.replaceAll('\r\n', '\n').split('\n');
@ -459,6 +460,11 @@ class Note with NotesNotifier {
var links = <Link>[];
for (var l in possibleLinks) {
var path = l.filePath;
if (path == null) {
links.add(l);
continue;
}
var isLocal = (path.startsWith('/') || path.startsWith('.')) &&
!path.contains('://');
if (isLocal) {

View File

@ -140,6 +140,26 @@ title: Foo
expect(links.length, 2);
});
test('Should parse wiki style links', () async {
var content = "[[GitJournal]] needs some [[Wild Fire]]";
var notePath = p.join(tempDir.path, "note63.md");
await File(notePath).writeAsString(content);
var parentFolder = NotesFolderFS(null, tempDir.path);
var note = Note(parentFolder, notePath);
await note.load();
var links = await note.fetchLinks();
expect(links[0].filePath, null);
expect(links[0].term, "GitJournal");
expect(links[1].filePath, null);
expect(links[1].term, "Wild Fire");
expect(links.length, 2);
});
test('Should detect file format', () async {
var content = """---
title: Foo