mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-27 09:06:43 +08:00
Minor refactor
This commit is contained in:
@ -5,17 +5,19 @@ import 'package:collection/collection.dart';
|
|||||||
Function _deepEq = const DeepCollectionEquality().equals;
|
Function _deepEq = const DeepCollectionEquality().equals;
|
||||||
|
|
||||||
class MdYamlDoc {
|
class MdYamlDoc {
|
||||||
String body = "";
|
String body;
|
||||||
LinkedHashMap<String, dynamic> props = LinkedHashMap<String, dynamic>();
|
LinkedHashMap<String, dynamic> props;
|
||||||
|
|
||||||
MdYamlDoc([this.body, this.props]) {
|
MdYamlDoc({
|
||||||
body = body ?? "";
|
this.body = "",
|
||||||
|
this.props,
|
||||||
|
}) {
|
||||||
// ignore: prefer_collection_literals
|
// ignore: prefer_collection_literals
|
||||||
props = props ?? LinkedHashMap<String, dynamic>();
|
props = props ?? LinkedHashMap<String, dynamic>();
|
||||||
}
|
}
|
||||||
|
|
||||||
MdYamlDoc.from(MdYamlDoc other) {
|
MdYamlDoc.from(MdYamlDoc other) {
|
||||||
body = String.fromCharCodes(other.body.codeUnits);
|
body = other.body;
|
||||||
props = LinkedHashMap<String, dynamic>.from(other.props);
|
props = LinkedHashMap<String, dynamic>.from(other.props);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ class MarkdownYAMLCodec {
|
|||||||
bodyBeginingPos += 1;
|
bodyBeginingPos += 1;
|
||||||
}
|
}
|
||||||
var body = str.substring(bodyBeginingPos);
|
var body = str.substring(bodyBeginingPos);
|
||||||
return MdYamlDoc(body);
|
return MdYamlDoc(body: body);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str.startsWith(startYamlStr)) {
|
if (str.startsWith(startYamlStr)) {
|
||||||
@ -37,10 +37,10 @@ class MarkdownYAMLCodec {
|
|||||||
var yamlText =
|
var yamlText =
|
||||||
str.substring(4, str.length - endYamlStrWithoutLineEding.length);
|
str.substring(4, str.length - endYamlStrWithoutLineEding.length);
|
||||||
var map = parseYamlText(yamlText);
|
var map = parseYamlText(yamlText);
|
||||||
return MdYamlDoc("", map);
|
return MdYamlDoc(props: map);
|
||||||
}
|
}
|
||||||
|
|
||||||
return MdYamlDoc(str);
|
return MdYamlDoc(body: str);
|
||||||
}
|
}
|
||||||
|
|
||||||
var yamlText = str.substring(4, endYamlPos);
|
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)) {
|
if (str.endsWith(endYamlStr)) {
|
||||||
var endYamlPos = str.length - endYamlStr.length;
|
var endYamlPos = str.length - endYamlStr.length;
|
||||||
var startYamlPos = str.lastIndexOf(startYamlStr, endYamlPos);
|
var startYamlPos = str.lastIndexOf(startYamlStr, endYamlPos);
|
||||||
if (startYamlPos == -1) {
|
if (startYamlPos == -1) {
|
||||||
return MdYamlDoc(str);
|
return MdYamlDoc(body: str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: What if there is nothing afterwards?
|
// FIXME: What if there is nothing afterwards?
|
||||||
@ -74,10 +74,10 @@ class MarkdownYAMLCodec {
|
|||||||
var body = str.substring(0, startYamlPos);
|
var body = str.substring(0, startYamlPos);
|
||||||
|
|
||||||
reverse = true;
|
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) {
|
static LinkedHashMap<String, dynamic> parseYamlText(String yamlText) {
|
||||||
|
@ -17,7 +17,9 @@ void main() {
|
|||||||
test('Markdown Serializer', () {
|
test('Markdown Serializer', () {
|
||||||
var created = toIso8601WithTimezone(nowWithoutMicro());
|
var created = toIso8601WithTimezone(nowWithoutMicro());
|
||||||
var note = MdYamlDoc(
|
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 serializer = MarkdownYAMLCodec();
|
||||||
var str = serializer.encode(note);
|
var str = serializer.encode(note);
|
||||||
|
@ -18,8 +18,8 @@ void main() {
|
|||||||
bProps['title'] = "Foo";
|
bProps['title'] = "Foo";
|
||||||
bProps['list'] = ["Foo", "Bar", 1];
|
bProps['list'] = ["Foo", "Bar", 1];
|
||||||
|
|
||||||
var a = MdYamlDoc("a", aProps);
|
var a = MdYamlDoc(body: "a", props: aProps);
|
||||||
var b = MdYamlDoc("a", bProps);
|
var b = MdYamlDoc(body: "a", props: bProps);
|
||||||
expect(a, b);
|
expect(a, b);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ void main() {
|
|||||||
test('Test emojis', () {
|
test('Test emojis', () {
|
||||||
var props = LinkedHashMap<String, dynamic>.from(
|
var props = LinkedHashMap<String, dynamic>.from(
|
||||||
<String, dynamic>{"title": "Why not :coffee:?"});
|
<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();
|
var serializer = NoteSerializer.raw();
|
||||||
serializer.settings.saveTitleAsH1 = false;
|
serializer.settings.saveTitleAsH1 = false;
|
||||||
@ -36,7 +36,8 @@ void main() {
|
|||||||
|
|
||||||
test('Test Title Serialization', () {
|
test('Test Title Serialization', () {
|
||||||
var props = <String, dynamic>{};
|
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();
|
var serializer = NoteSerializer.raw();
|
||||||
serializer.settings.saveTitleAsH1 = true;
|
serializer.settings.saveTitleAsH1 = true;
|
||||||
@ -57,7 +58,8 @@ void main() {
|
|||||||
|
|
||||||
test('Test Title Reading with blank lines', () {
|
test('Test Title Reading with blank lines', () {
|
||||||
var props = <String, dynamic>{};
|
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();
|
var serializer = NoteSerializer.raw();
|
||||||
|
|
||||||
@ -70,7 +72,7 @@ void main() {
|
|||||||
|
|
||||||
test('Test Title Reading with blank lines and no body', () {
|
test('Test Title Reading with blank lines and no body', () {
|
||||||
var props = <String, dynamic>{};
|
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();
|
var serializer = NoteSerializer.raw();
|
||||||
|
|
||||||
@ -84,7 +86,7 @@ void main() {
|
|||||||
test('Test Old Title Serialization', () {
|
test('Test Old Title Serialization', () {
|
||||||
var props = LinkedHashMap<String, dynamic>.from(
|
var props = LinkedHashMap<String, dynamic>.from(
|
||||||
<String, dynamic>{"title": "Why not :coffee:?"});
|
<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();
|
var serializer = NoteSerializer.raw();
|
||||||
serializer.settings.saveTitleAsH1 = true;
|
serializer.settings.saveTitleAsH1 = true;
|
||||||
@ -106,7 +108,7 @@ void main() {
|
|||||||
"title": "Why not?",
|
"title": "Why not?",
|
||||||
"draft": true,
|
"draft": true,
|
||||||
});
|
});
|
||||||
var doc = MdYamlDoc("body", props);
|
var doc = MdYamlDoc(body: "body", props: props);
|
||||||
|
|
||||||
var serializer = NoteSerializer.raw();
|
var serializer = NoteSerializer.raw();
|
||||||
serializer.settings.saveTitleAsH1 = false;
|
serializer.settings.saveTitleAsH1 = false;
|
||||||
@ -131,7 +133,7 @@ void main() {
|
|||||||
"draft": true,
|
"draft": true,
|
||||||
"tags": "#foo #bar-do",
|
"tags": "#foo #bar-do",
|
||||||
});
|
});
|
||||||
var doc = MdYamlDoc("body", props);
|
var doc = MdYamlDoc(body: "body", props: props);
|
||||||
|
|
||||||
var serializer = NoteSerializer.raw();
|
var serializer = NoteSerializer.raw();
|
||||||
serializer.settings.saveTitleAsH1 = false;
|
serializer.settings.saveTitleAsH1 = false;
|
||||||
|
@ -34,7 +34,7 @@ void main() {
|
|||||||
n1.created = dt;
|
n1.created = dt;
|
||||||
|
|
||||||
var n2 = Note(parent, n2Path);
|
var n2 = Note(parent, n2Path);
|
||||||
n2.data = MdYamlDoc("test2", props);
|
n2.data = MdYamlDoc(body: "test2", props: props);
|
||||||
|
|
||||||
notes = [n1, n2];
|
notes = [n1, n2];
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user