Handle invalid markdown

In these cases we just try to fetch the body the best we can and ignore
all the keys.

This should give us more info about #4
This commit is contained in:
Vishesh Handa
2019-05-27 10:10:17 +02:00
parent 041108fd96
commit f7aeb3527d
2 changed files with 25 additions and 5 deletions

View File

@ -26,12 +26,18 @@ class MarkdownYAMLSerializer implements NoteSerializer {
Note decode(String str) { Note decode(String str) {
if (str.startsWith("---\n")) { if (str.startsWith("---\n")) {
var parts = str.split("---\n"); var parts = str.split("---\n");
var yamlMap = loadYaml(parts[1]);
var map = <String, dynamic>{}; var map = <String, dynamic>{};
yamlMap.forEach((key, value) {
map[key] = value; try {
}); var yamlMap = loadYaml(parts[1]);
yamlMap.forEach((key, value) {
map[key] = value;
});
} catch (err, stack) {
print(
'MarkdownYAMLSerializer::decode (${parts[1]}) -> ${err.toString()}');
print(stack.toString());
}
map['body'] = parts[2].trimLeft(); map['body'] = parts[2].trimLeft();
return Note.fromJson(map); return Note.fromJson(map);

View File

@ -31,6 +31,20 @@ void main() {
expect(note2, note); expect(note2, note);
}); });
test('Markdown Serializer with invalid Markdown', () {
var inputNoteStr = """---
type
---
Alright.""";
var serializer = MarkdownYAMLSerializer();
var note = serializer.decode(inputNoteStr);
var actualStr = "Alright.";
expect(actualStr, note.body);
});
/* /*
test('Markdown Serializer YAML Order', () { test('Markdown Serializer YAML Order', () {
var str = """--- var str = """---