mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 02:07:39 +08:00
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:
31
lib/core/processors/wiki_links_auto_add.dart
Normal file
31
lib/core/processors/wiki_links_auto_add.dart
Normal 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)}';
|
||||
}
|
21
test/processors/wiki_links_auto_add_test.dart
Normal file
21
test/processors/wiki_links_auto_add_test.dart
Normal 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?
|
||||
}
|
Reference in New Issue
Block a user