diff --git a/lib/storage/serializers.dart b/lib/storage/serializers.dart index c83d43e2..3313366f 100644 --- a/lib/storage/serializers.dart +++ b/lib/storage/serializers.dart @@ -46,37 +46,37 @@ class MarkdownYAMLSerializer implements NoteSerializer { NoteData decode(String str) { const startYamlStr = "---\n"; const endYamlStr = "\n---\n"; - const emptyYamlHeaderStr = "---\n---\n"; + const emptyYamlHeaderStr = "---\n---"; - if (str.startsWith(emptyYamlHeaderStr)) { - var bodyBeginingPos = emptyYamlHeaderStr.length; + if (str == emptyYamlHeaderStr) { + return NoteData(); + } + if (str.startsWith(emptyYamlHeaderStr + "\n")) { + var bodyBeginingPos = emptyYamlHeaderStr.length + 1; if (str[bodyBeginingPos] == '\n') { bodyBeginingPos += 1; } var body = str.substring(bodyBeginingPos); - return NoteData(body, LinkedHashMap()); + return NoteData(body); } if (str.startsWith(startYamlStr)) { var endYamlPos = str.indexOf(endYamlStr, startYamlStr.length); if (endYamlPos == -1) { - return NoteData(str, LinkedHashMap()); + // Try without the \n in the endYamlStr + const endYamlStrWithoutLineEding = "\n---"; + if (str.endsWith(endYamlStrWithoutLineEding)) { + var yamlText = + str.substring(4, str.length - endYamlStrWithoutLineEding.length); + var map = _parseYamlText(yamlText); + return NoteData("", map); + } + + return NoteData(str); } var yamlText = str.substring(4, endYamlPos); - var map = {}; - - try { - if (yamlText.isNotEmpty) { - var yamlMap = loadYaml(yamlText); - yamlMap.forEach((key, value) { - map[key] = value; - }); - } - } catch (err) { - Fimber.d( - 'MarkdownYAMLSerializer::decode("$yamlText") -> ${err.toString()}'); - } + var map = _parseYamlText(yamlText); var bodyBeginingPos = endYamlPos + endYamlStr.length; if (str[bodyBeginingPos] == '\n') { @@ -90,6 +90,25 @@ class MarkdownYAMLSerializer implements NoteSerializer { return NoteData(str, LinkedHashMap()); } + LinkedHashMap _parseYamlText(String yamlText) { + LinkedHashMap map = LinkedHashMap(); + if (yamlText.isEmpty) { + return map; + } + + try { + var yamlMap = loadYaml(yamlText); + yamlMap.forEach((key, value) { + map[key] = value; + }); + } catch (err) { + Fimber.d( + 'MarkdownYAMLSerializer::decode("$yamlText") -> ${err.toString()}'); + } + + return map; + } + @override String encode(NoteData note) { if (note.props.isEmpty) { diff --git a/test/serializers_test.dart b/test/serializers_test.dart index 896048ab..16efa178 100644 --- a/test/serializers_test.dart +++ b/test/serializers_test.dart @@ -50,7 +50,7 @@ Alright."""; expect(actualStr, note.body); }); - test('Markdown Serializer with empty YAML and no \\n', () { + test('Markdown Serializer with empty YAML and no \\n after body', () { var inputNoteStr = """--- --- Alright."""; @@ -62,6 +62,17 @@ Alright."""; expect(actualStr, note.body); }); + test('Markdown Serializer with empty YAML and doesn"t end with \\n', () { + var inputNoteStr = """--- +---"""; + + var serializer = MarkdownYAMLSerializer(); + var note = serializer.decode(inputNoteStr); + + expect("", note.body); + expect(0, note.props.length); + }); + test('Markdown Serializer YAML Order', () { var str = """--- type: Journal @@ -140,5 +151,20 @@ Alright."""; expect(actualStr, note.body); }); + + test('Only YAML Header without \\n', () { + var str = """--- +foo: bar +---"""; + + var serializer = MarkdownYAMLSerializer(); + var note = serializer.decode(str); + + expect("", note.body); + expect({"foo": "bar"}, note.props); + + var actualStr = serializer.encode(note); + expect(actualStr, str + '\n\n'); + }); }); }