1
0
mirror of https://github.com/GitJournal/GitJournal.git synced 2025-07-21 07:03:25 +08:00

Improved note parsing

We now handle markdown files without any YAML Headers, and different
variations of the yaml header.

This should also fix a crash.
This commit is contained in:
Vishesh Handa
2019-11-01 23:00:54 +01:00
parent 8de58c138a
commit 3cf951f8dd
2 changed files with 97 additions and 10 deletions

@ -32,7 +32,7 @@ class NoteData {
@override
String toString() {
return 'NoteData{bodt: $body, props: $props}';
return 'NoteData{body: $body, props: $props}';
}
}
@ -44,14 +44,31 @@ abstract class NoteSerializer {
class MarkdownYAMLSerializer implements NoteSerializer {
@override
NoteData decode(String str) {
if (str.startsWith("---\n")) {
var parts = str.split("---\n");
const startYamlStr = "---\n";
const endYamlStr = "\n---\n";
const emptyYamlHeaderStr = "---\n---\n";
if (str.startsWith(emptyYamlHeaderStr)) {
var bodyBeginingPos = emptyYamlHeaderStr.length;
if (str[bodyBeginingPos] == '\n') {
bodyBeginingPos += 1;
}
var body = str.substring(bodyBeginingPos);
return NoteData(body, LinkedHashMap<String, dynamic>());
}
if (str.startsWith(startYamlStr)) {
var endYamlPos = str.indexOf(endYamlStr, startYamlStr.length);
if (endYamlPos == -1) {
return NoteData(str, LinkedHashMap<String, dynamic>());
}
var yamlText = str.substring(4, endYamlPos);
var map = <String, dynamic>{};
var yamlText = parts[1].trim();
try {
if (yamlText.isNotEmpty) {
var yamlMap = loadYaml(parts[1]);
var yamlMap = loadYaml(yamlText);
yamlMap.forEach((key, value) {
map[key] = value;
});
@ -60,7 +77,12 @@ class MarkdownYAMLSerializer implements NoteSerializer {
Fimber.d(
'MarkdownYAMLSerializer::decode("$yamlText") -> ${err.toString()}');
}
var body = parts[2].trimLeft();
var bodyBeginingPos = endYamlPos + endYamlStr.length;
if (str[bodyBeginingPos] == '\n') {
bodyBeginingPos += 1;
}
var body = str.substring(bodyBeginingPos);
return NoteData(body, map);
}
@ -70,6 +92,10 @@ class MarkdownYAMLSerializer implements NoteSerializer {
@override
String encode(NoteData note) {
if (note.props.isEmpty) {
return note.body;
}
const serparator = '---\n';
var str = "";
str += serparator;

@ -11,11 +11,11 @@ DateTime nowWithoutMicro() {
void main() {
group('Serializers', () {
var created = toIso8601WithTimezone(nowWithoutMicro());
var note =
NoteData("This is the body", LinkedHashMap.from({"created": created}));
test('Markdown Serializer', () {
var created = toIso8601WithTimezone(nowWithoutMicro());
var note = NoteData(
"This is the body", LinkedHashMap.from({"created": created}));
var serializer = MarkdownYAMLSerializer();
var str = serializer.encode(note);
var note2 = serializer.decode(str);
@ -41,6 +41,18 @@ Alright.""";
var inputNoteStr = """---
---
Alright.""";
var serializer = MarkdownYAMLSerializer();
var note = serializer.decode(inputNoteStr);
var actualStr = "Alright.";
expect(actualStr, note.body);
});
test('Markdown Serializer with empty YAML and no \\n', () {
var inputNoteStr = """---
---
Alright.""";
var serializer = MarkdownYAMLSerializer();
@ -79,5 +91,54 @@ Alright.""";
expect(actualStr, str);
});
test('Note Starting with ---', () {
var str = """---
Alright.""";
var serializer = MarkdownYAMLSerializer();
var note = serializer.decode(str);
var actualStr = serializer.encode(note);
expect(actualStr, str);
});
test('Plain Markdown', () {
var str = """Alright.""";
var serializer = MarkdownYAMLSerializer();
var note = serializer.decode(str);
var actualStr = serializer.encode(note);
expect(actualStr, str);
});
test('Markdown with --- in body', () {
var str = """---
foo: [bar, gar]
---
Alright. ---\n Good boy --- Howdy""";
var serializer = MarkdownYAMLSerializer();
var note = serializer.decode(str);
var actualStr = serializer.encode(note);
expect(actualStr, str);
});
test('Markdown without \\n after yaml header', () {
var str = """---
foo: [bar, gar]
---
Alright.""";
var serializer = MarkdownYAMLSerializer();
var note = serializer.decode(str);
var actualStr = "Alright.";
expect(actualStr, note.body);
});
});
}