Add the scaffolding for automatically adding Wiki Links

The idea is that on save a Note, terms which have already been used
before as Wiki Links should automatically be converted into Wiki Links.
This commit is contained in:
Vishesh Handa
2020-09-02 00:40:45 +02:00
parent 9ec33842bc
commit 25f516c15c
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,31 @@
import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/core/notes_folder.dart';
class WikiLinksAutoAddProcessor {
final NotesFolder rootFolder;
WikiLinksAutoAddProcessor(this.rootFolder);
void process(Note note) {}
String processBody(String body, List<String> tags) {
for (var tag in tags) {
var regexp = RegExp('\\b$tag\\b');
int start = 0;
while (true) {
var i = body.indexOf(regexp, start);
if (i == -1) {
break;
}
body = _replace(body, i, i + tag.length, '[[$tag]]');
start = i + tag.length + 4;
}
}
return body;
}
}
String _replace(String body, int startPos, int endPos, String replacement) {
return '${body.substring(0, startPos)}$replacement${body.substring(endPos)}';
}

View File

@ -0,0 +1,21 @@
import 'package:test/test.dart';
import 'package:gitjournal/core/processors/wiki_links_auto_add.dart';
void main() {
test('Should process body', () {
var body =
"GitJournal is the best? And it works quite well with Foam, Foam and Obsidian.";
var p = WikiLinksAutoAddProcessor(null);
var newBody = p.processBody(body, ['GitJournal', 'Foam', 'Obsidian']);
var expectedBody =
"[[GitJournal]] is the best? And it works quite well with [[Foam]], [[Foam]] and [[Obsidian]].";
expect(newBody, expectedBody);
});
// Add a test to see if processing a Note works
// FIXME: Make sure the wiki link terms do not have special characters
// FIXME: WHat about piped links?
}