From 702da72a518629932f00152c371beefca0871add Mon Sep 17 00:00:00 2001
From: Vishesh Handa <me@vhanda.in>
Date: Sun, 1 Dec 2019 16:43:13 +0100
Subject: [PATCH] Note serializer: Add yet another edge case

---
 lib/storage/serializers.dart | 11 ++++++++---
 test/serializers_test.dart   | 18 +++++++++++++++++-
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/lib/storage/serializers.dart b/lib/storage/serializers.dart
index 3313366f..7d8aee4f 100644
--- a/lib/storage/serializers.dart
+++ b/lib/storage/serializers.dart
@@ -78,11 +78,16 @@ class MarkdownYAMLSerializer implements NoteSerializer {
       var yamlText = str.substring(4, endYamlPos);
       var map = _parseYamlText(yamlText);
 
+      var body = "";
       var bodyBeginingPos = endYamlPos + endYamlStr.length;
-      if (str[bodyBeginingPos] == '\n') {
-        bodyBeginingPos += 1;
+      if (bodyBeginingPos < str.length) {
+        if (str[bodyBeginingPos] == '\n') {
+          bodyBeginingPos += 1;
+        }
+        if (bodyBeginingPos < str.length) {
+          body = str.substring(bodyBeginingPos);
+        }
       }
-      var body = str.substring(bodyBeginingPos);
 
       return NoteData(body, map);
     }
diff --git a/test/serializers_test.dart b/test/serializers_test.dart
index 16efa178..55735314 100644
--- a/test/serializers_test.dart
+++ b/test/serializers_test.dart
@@ -152,7 +152,7 @@ Alright.""";
       expect(actualStr, note.body);
     });
 
-    test('Only YAML Header without \\n', () {
+    test('Only YAML Header without \\n at end', () {
       var str = """---
 foo: bar
 ---""";
@@ -166,5 +166,21 @@ foo: bar
       var actualStr = serializer.encode(note);
       expect(actualStr, str + '\n\n');
     });
+
+    test('Only YAML Header with \\n at end', () {
+      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');
+    });
   });
 }