diff --git a/lib/core/note_serializer.dart b/lib/core/note_serializer.dart index 106bb83e..d7e8f779 100644 --- a/lib/core/note_serializer.dart +++ b/lib/core/note_serializer.dart @@ -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"); } diff --git a/test/note_serializer_test.dart b/test/note_serializer_test.dart index c38122df..3ba3671c 100644 --- a/test/note_serializer_test.dart +++ b/test/note_serializer_test.dart @@ -123,11 +123,11 @@ void main() { expect(doc.props['draft'], true); }); - test('Test Non list Tag', () { + test('Test string tag with #', () { var props = LinkedHashMap.from({ "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, {"draft": true}); - expect(note.tags, {"foo"}); + expect(note.tags, {"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); }); });