2013-09-19 19:48:21 +00:00
|
|
|
define([
|
|
|
|
"classes/Extension",
|
2013-10-07 00:17:21 +00:00
|
|
|
"yaml-js",
|
2013-11-07 23:10:38 +00:00
|
|
|
], function(Extension, YAML) {
|
2013-09-19 19:48:21 +00:00
|
|
|
|
2013-10-06 14:34:01 +00:00
|
|
|
var yamlFrontMatterParser = new Extension("yamlFrontMatterParser", "YAML front matter");
|
2013-09-19 19:48:21 +00:00
|
|
|
|
2013-11-07 23:10:38 +00:00
|
|
|
var eventMgr;
|
2013-09-19 19:48:21 +00:00
|
|
|
yamlFrontMatterParser.onEventMgrCreated = function(eventMgrParameter) {
|
|
|
|
eventMgr = eventMgrParameter;
|
|
|
|
};
|
|
|
|
|
2013-11-07 23:10:38 +00:00
|
|
|
var fileDesc;
|
2013-10-06 14:34:01 +00:00
|
|
|
yamlFrontMatterParser.onFileSelected = function(fileDescParam) {
|
|
|
|
fileDesc = fileDescParam;
|
|
|
|
};
|
|
|
|
|
2013-10-22 22:14:26 +00:00
|
|
|
var regex = /^(\s*-{3}\s*\n([\w\W]+?)\n\s*-{3}\s*\n)?([\w\W]*)$/;
|
2013-09-19 19:48:21 +00:00
|
|
|
yamlFrontMatterParser.onPagedownConfigure = function(editor) {
|
|
|
|
var converter = editor.getConverter();
|
|
|
|
converter.hooks.chain("preConversion", function(text) {
|
2013-10-22 22:14:26 +00:00
|
|
|
var results = regex.exec(text);
|
|
|
|
var yaml = results[2];
|
2013-09-19 19:48:21 +00:00
|
|
|
|
2013-10-22 22:14:26 +00:00
|
|
|
if (yaml && (!fileDesc.frontMatter || fileDesc.frontMatter._yaml != yaml)) {
|
2013-10-06 14:34:01 +00:00
|
|
|
fileDesc.frontMatter = undefined;
|
|
|
|
try {
|
2013-10-07 00:17:21 +00:00
|
|
|
fileDesc.frontMatter = YAML.parse(yaml);
|
2013-10-06 14:34:01 +00:00
|
|
|
fileDesc.frontMatter._yaml = yaml;
|
2013-10-22 22:14:26 +00:00
|
|
|
fileDesc.frontMatter._frontMatter = results[1];
|
2013-09-19 19:48:21 +00:00
|
|
|
}
|
2013-10-06 14:34:01 +00:00
|
|
|
catch (e) {}
|
2013-09-19 19:48:21 +00:00
|
|
|
}
|
2013-10-06 14:34:01 +00:00
|
|
|
eventMgr.onMarkdownTrim((results[1] || '').length);
|
|
|
|
return results[3];
|
2013-09-19 19:48:21 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return yamlFrontMatterParser;
|
|
|
|
});
|