Minor refactor

This commit is contained in:
Vishesh Handa
2021-02-09 12:00:52 +01:00
parent 74f7a17f25
commit 5280917e7a
6 changed files with 29 additions and 23 deletions

View File

@ -5,17 +5,19 @@ import 'package:collection/collection.dart';
Function _deepEq = const DeepCollectionEquality().equals;
class MdYamlDoc {
String body = "";
LinkedHashMap<String, dynamic> props = LinkedHashMap<String, dynamic>();
String body;
LinkedHashMap<String, dynamic> props;
MdYamlDoc([this.body, this.props]) {
body = body ?? "";
MdYamlDoc({
this.body = "",
this.props,
}) {
// ignore: prefer_collection_literals
props = props ?? LinkedHashMap<String, dynamic>();
}
MdYamlDoc.from(MdYamlDoc other) {
body = String.fromCharCodes(other.body.codeUnits);
body = other.body;
props = LinkedHashMap<String, dynamic>.from(other.props);
}

View File

@ -25,7 +25,7 @@ class MarkdownYAMLCodec {
bodyBeginingPos += 1;
}
var body = str.substring(bodyBeginingPos);
return MdYamlDoc(body);
return MdYamlDoc(body: body);
}
if (str.startsWith(startYamlStr)) {
@ -37,10 +37,10 @@ class MarkdownYAMLCodec {
var yamlText =
str.substring(4, str.length - endYamlStrWithoutLineEding.length);
var map = parseYamlText(yamlText);
return MdYamlDoc("", map);
return MdYamlDoc(props: map);
}
return MdYamlDoc(str);
return MdYamlDoc(body: str);
}
var yamlText = str.substring(4, endYamlPos);
@ -57,14 +57,14 @@ class MarkdownYAMLCodec {
}
}
return MdYamlDoc(body, map);
return MdYamlDoc(body: body, props: map);
}
if (str.endsWith(endYamlStr)) {
var endYamlPos = str.length - endYamlStr.length;
var startYamlPos = str.lastIndexOf(startYamlStr, endYamlPos);
if (startYamlPos == -1) {
return MdYamlDoc(str);
return MdYamlDoc(body: str);
}
// FIXME: What if there is nothing afterwards?
@ -74,10 +74,10 @@ class MarkdownYAMLCodec {
var body = str.substring(0, startYamlPos);
reverse = true;
return MdYamlDoc(body, map);
return MdYamlDoc(body: body, props: map);
}
return MdYamlDoc(str, LinkedHashMap<String, dynamic>());
return MdYamlDoc(body: str);
}
static LinkedHashMap<String, dynamic> parseYamlText(String yamlText) {

View File

@ -17,7 +17,9 @@ void main() {
test('Markdown Serializer', () {
var created = toIso8601WithTimezone(nowWithoutMicro());
var note = MdYamlDoc(
"This is the body", LinkedHashMap.from({"created": created}));
body: "This is the body",
props: LinkedHashMap.from({"created": created}),
);
var serializer = MarkdownYAMLCodec();
var str = serializer.encode(note);

View File

@ -18,8 +18,8 @@ void main() {
bProps['title'] = "Foo";
bProps['list'] = ["Foo", "Bar", 1];
var a = MdYamlDoc("a", aProps);
var b = MdYamlDoc("a", bProps);
var a = MdYamlDoc(body: "a", props: aProps);
var b = MdYamlDoc(body: "a", props: bProps);
expect(a, b);
});
}

View File

@ -15,7 +15,7 @@ void main() {
test('Test emojis', () {
var props = LinkedHashMap<String, dynamic>.from(
<String, dynamic>{"title": "Why not :coffee:?"});
var doc = MdYamlDoc("I :heart: you", props);
var doc = MdYamlDoc(body: "I :heart: you", props: props);
var serializer = NoteSerializer.raw();
serializer.settings.saveTitleAsH1 = false;
@ -36,7 +36,8 @@ void main() {
test('Test Title Serialization', () {
var props = <String, dynamic>{};
var doc = MdYamlDoc("# Why not :coffee:?\n\nI :heart: you", props);
var doc =
MdYamlDoc(body: "# Why not :coffee:?\n\nI :heart: you", props: props);
var serializer = NoteSerializer.raw();
serializer.settings.saveTitleAsH1 = true;
@ -57,7 +58,8 @@ void main() {
test('Test Title Reading with blank lines', () {
var props = <String, dynamic>{};
var doc = MdYamlDoc("\n# Why not :coffee:?\n\nI :heart: you", props);
var doc = MdYamlDoc(
body: "\n# Why not :coffee:?\n\nI :heart: you", props: props);
var serializer = NoteSerializer.raw();
@ -70,7 +72,7 @@ void main() {
test('Test Title Reading with blank lines and no body', () {
var props = <String, dynamic>{};
var doc = MdYamlDoc("\n# Why not :coffee:?", props);
var doc = MdYamlDoc(body: "\n# Why not :coffee:?", props: props);
var serializer = NoteSerializer.raw();
@ -84,7 +86,7 @@ void main() {
test('Test Old Title Serialization', () {
var props = LinkedHashMap<String, dynamic>.from(
<String, dynamic>{"title": "Why not :coffee:?"});
var doc = MdYamlDoc("I :heart: you", props);
var doc = MdYamlDoc(body: "I :heart: you", props: props);
var serializer = NoteSerializer.raw();
serializer.settings.saveTitleAsH1 = true;
@ -106,7 +108,7 @@ void main() {
"title": "Why not?",
"draft": true,
});
var doc = MdYamlDoc("body", props);
var doc = MdYamlDoc(body: "body", props: props);
var serializer = NoteSerializer.raw();
serializer.settings.saveTitleAsH1 = false;
@ -131,7 +133,7 @@ void main() {
"draft": true,
"tags": "#foo #bar-do",
});
var doc = MdYamlDoc("body", props);
var doc = MdYamlDoc(body: "body", props: props);
var serializer = NoteSerializer.raw();
serializer.settings.saveTitleAsH1 = false;

View File

@ -34,7 +34,7 @@ void main() {
n1.created = dt;
var n2 = Note(parent, n2Path);
n2.data = MdYamlDoc("test2", props);
n2.data = MdYamlDoc(body: "test2", props: props);
notes = [n1, n2];
});