mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 10:17:16 +08:00
MdYamlDoc: Add a very basic reverse implementation
A user requested that the YAML header be present at the bottom. I thought this would be quite simple, but there are lots of edge cases which will need to be solved. Related to #223
This commit is contained in:
@ -7,6 +7,10 @@ import 'package:gitjournal/utils/logger.dart';
|
|||||||
import 'md_yaml_doc.dart';
|
import 'md_yaml_doc.dart';
|
||||||
|
|
||||||
class MarkdownYAMLCodec {
|
class MarkdownYAMLCodec {
|
||||||
|
bool reverse;
|
||||||
|
|
||||||
|
MarkdownYAMLCodec({this.reverse = false});
|
||||||
|
|
||||||
MdYamlDoc decode(String str) {
|
MdYamlDoc decode(String str) {
|
||||||
const startYamlStr = "---\n";
|
const startYamlStr = "---\n";
|
||||||
const endYamlStr = "\n---\n";
|
const endYamlStr = "\n---\n";
|
||||||
@ -56,6 +60,23 @@ class MarkdownYAMLCodec {
|
|||||||
return MdYamlDoc(body, map);
|
return MdYamlDoc(body, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (str.endsWith(endYamlStr)) {
|
||||||
|
var endYamlPos = str.length - endYamlStr.length;
|
||||||
|
var startYamlPos = str.lastIndexOf(startYamlStr, endYamlPos);
|
||||||
|
if (startYamlPos == -1) {
|
||||||
|
return MdYamlDoc(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: What if there is nothing afterwards?
|
||||||
|
var yamlText =
|
||||||
|
str.substring(startYamlPos + startYamlStr.length, endYamlPos);
|
||||||
|
var map = parseYamlText(yamlText);
|
||||||
|
var body = str.substring(0, startYamlPos);
|
||||||
|
|
||||||
|
reverse = true;
|
||||||
|
return MdYamlDoc(body, map);
|
||||||
|
}
|
||||||
|
|
||||||
return MdYamlDoc(str, LinkedHashMap<String, dynamic>());
|
return MdYamlDoc(str, LinkedHashMap<String, dynamic>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,9 +106,16 @@ class MarkdownYAMLCodec {
|
|||||||
return note.body;
|
return note.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
var str = toYamlHeader(note.props);
|
var str = "";
|
||||||
|
if (reverse) {
|
||||||
|
str += note.body.trimRight();
|
||||||
|
str += '\n\n';
|
||||||
|
str += toYamlHeader(note.props);
|
||||||
|
} else {
|
||||||
|
str += toYamlHeader(note.props);
|
||||||
str += '\n';
|
str += '\n';
|
||||||
str += note.body;
|
str += note.body;
|
||||||
|
}
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
@ -184,5 +184,29 @@ foo: bar
|
|||||||
var actualStr = serializer.encode(note);
|
var actualStr = serializer.encode(note);
|
||||||
expect(actualStr, str + '\n');
|
expect(actualStr, str + '\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('YAML at the end of the doc', () {
|
||||||
|
var str = """Alright.
|
||||||
|
|
||||||
|
---
|
||||||
|
type: Journal
|
||||||
|
created: 2017-02-15T22:41:19+01:00
|
||||||
|
foo: bar
|
||||||
|
---
|
||||||
|
""";
|
||||||
|
|
||||||
|
var serializer = MarkdownYAMLCodec(reverse: true);
|
||||||
|
var doc = serializer.decode(str);
|
||||||
|
expect(doc.body, "Alright.\n\n");
|
||||||
|
expect(doc.props.length, 3);
|
||||||
|
|
||||||
|
var actualStr = serializer.encode(doc);
|
||||||
|
|
||||||
|
expect(actualStr, str);
|
||||||
|
});
|
||||||
|
|
||||||
|
// FIXME: Add another test for yaml header at the bottom without a newline
|
||||||
|
// FIXME: Add another test for yaml header at the bottom with lots of new lines after
|
||||||
|
// FIXME: Add another test for yaml header at the bottom with lots of new lines with spaces after
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user