mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 20:43:20 +08:00
YAML tags: Let it be a string seperated by spaces and have a #
In general we try to follow the same format as specified in the input note.
This commit is contained in:
@ -23,6 +23,9 @@ class NoteSerializationSettings {
|
||||
String typeKey = "type";
|
||||
String tagsKey = "tags";
|
||||
|
||||
bool tagsInString = false;
|
||||
bool tagsHaveHash = false;
|
||||
|
||||
bool saveTitleAsH1 = true;
|
||||
}
|
||||
|
||||
@ -83,6 +86,13 @@ class NoteSerializer implements NoteSerializerInterface {
|
||||
data.props.remove(settings.tagsKey);
|
||||
} else {
|
||||
data.props[settings.tagsKey] = note.tags.toList();
|
||||
if (settings.tagsInString) {
|
||||
var tags = note.tags;
|
||||
if (settings.tagsHaveHash) {
|
||||
tags = tags.map((e) => '#$e').toSet();
|
||||
}
|
||||
data.props[settings.tagsKey] = tags.join(' ');
|
||||
}
|
||||
}
|
||||
|
||||
note.extraProps.forEach((key, value) {
|
||||
@ -191,7 +201,15 @@ class NoteSerializer implements NoteSerializerInterface {
|
||||
} else if (tags is List) {
|
||||
note.tags = tags.map((t) => t.toString()).toSet();
|
||||
} else if (tags is String) {
|
||||
note.tags = {tags};
|
||||
settings.tagsInString = true;
|
||||
var allTags = tags.split(' ');
|
||||
settings.tagsHaveHash = allTags.every((t) => t.startsWith('#'));
|
||||
if (settings.tagsHaveHash) {
|
||||
allTags.removeWhere((e) => e.length <= 1);
|
||||
allTags = allTags.map((e) => e.substring(1)).toList();
|
||||
}
|
||||
|
||||
note.tags = allTags.toSet();
|
||||
} else {
|
||||
Log.e("Note Tags Decoding Failed: $tags");
|
||||
}
|
||||
|
@ -123,11 +123,11 @@ void main() {
|
||||
expect(doc.props['draft'], true);
|
||||
});
|
||||
|
||||
test('Test Non list Tag', () {
|
||||
test('Test string tag with #', () {
|
||||
var props = LinkedHashMap<String, dynamic>.from(<String, dynamic>{
|
||||
"title": "Why not?",
|
||||
"draft": true,
|
||||
"tags": "foo",
|
||||
"tags": "#foo #bar-do",
|
||||
});
|
||||
var doc = MdYamlDoc("body", props);
|
||||
|
||||
@ -140,13 +140,13 @@ void main() {
|
||||
expect(note.body, "body");
|
||||
expect(note.title, "Why not?");
|
||||
expect(note.extraProps, <String, dynamic>{"draft": true});
|
||||
expect(note.tags, <String>{"foo"});
|
||||
expect(note.tags, <String>{"foo", "bar-do"});
|
||||
|
||||
serializer.encode(note, doc);
|
||||
expect(doc.body, "body");
|
||||
expect(doc.props['title'], 'Why not?');
|
||||
expect(doc.props['draft'], true);
|
||||
expect(doc.props['tags'], ['foo']);
|
||||
expect(doc.props['tags'], "#foo #bar-do");
|
||||
expect(doc.props.length, 3);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user