diff --git a/lib/core/processors/wiki_links_auto_add.dart b/lib/core/processors/wiki_links_auto_add.dart new file mode 100644 index 00000000..89305d69 --- /dev/null +++ b/lib/core/processors/wiki_links_auto_add.dart @@ -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 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)}'; +} diff --git a/test/processors/wiki_links_auto_add_test.dart b/test/processors/wiki_links_auto_add_test.dart new file mode 100644 index 00000000..559f86cc --- /dev/null +++ b/test/processors/wiki_links_auto_add_test.dart @@ -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? +}