mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 12:23:44 +08:00
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:
@ -51,6 +51,14 @@ class LinkExtractor implements md.NodeVisitor {
|
|||||||
var url = el.attributes['href'];
|
var url = el.attributes['href'];
|
||||||
var link = Link(term: title, filePath: url);
|
var link = Link(term: title, filePath: url);
|
||||||
links.add(link);
|
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;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -450,6 +450,7 @@ class Note with NotesNotifier {
|
|||||||
final doc = md.Document(
|
final doc = md.Document(
|
||||||
encodeHtml: false,
|
encodeHtml: false,
|
||||||
extensionSet: md.ExtensionSet.gitHubFlavored,
|
extensionSet: md.ExtensionSet.gitHubFlavored,
|
||||||
|
inlineSyntaxes: [MetaLinkSyntax()],
|
||||||
);
|
);
|
||||||
|
|
||||||
var lines = body.replaceAll('\r\n', '\n').split('\n');
|
var lines = body.replaceAll('\r\n', '\n').split('\n');
|
||||||
@ -459,6 +460,11 @@ class Note with NotesNotifier {
|
|||||||
var links = <Link>[];
|
var links = <Link>[];
|
||||||
for (var l in possibleLinks) {
|
for (var l in possibleLinks) {
|
||||||
var path = l.filePath;
|
var path = l.filePath;
|
||||||
|
if (path == null) {
|
||||||
|
links.add(l);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
var isLocal = (path.startsWith('/') || path.startsWith('.')) &&
|
var isLocal = (path.startsWith('/') || path.startsWith('.')) &&
|
||||||
!path.contains('://');
|
!path.contains('://');
|
||||||
if (isLocal) {
|
if (isLocal) {
|
||||||
|
@ -140,6 +140,26 @@ title: Foo
|
|||||||
expect(links.length, 2);
|
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 {
|
test('Should detect file format', () async {
|
||||||
var content = """---
|
var content = """---
|
||||||
title: Foo
|
title: Foo
|
||||||
|
Reference in New Issue
Block a user