Support for YAML front matter

This commit is contained in:
benweet 2013-10-06 15:34:01 +01:00
parent 4c327ad586
commit a9b9c3eb5e
3 changed files with 19 additions and 18 deletions

View File

@ -6,7 +6,7 @@ define([
"classes/Extension", "classes/Extension",
"settings", "settings",
"text!html/settingsExtensionsAccordion.html", "text!html/settingsExtensionsAccordion.html",
// "extensions/yamlFrontMatterParser", "extensions/yamlFrontMatterParser",
"extensions/markdownSectionParser", "extensions/markdownSectionParser",
"extensions/partialRendering", "extensions/partialRendering",
"extensions/buttonMarkdownSyntax", "extensions/buttonMarkdownSyntax",

View File

@ -1,33 +1,37 @@
define([ define([
"classes/Extension", "classes/Extension",
"text!html/yamlFrontMatterParserSettingsBlock.html",
"js-yaml", "js-yaml",
], function(Extension, yamlFrontMatterParserSettingsBlock) { ], function(Extension, yamlFrontMatterParserSettingsBlock) {
var yamlFrontMatterParser = new Extension("yamlFrontMatterParser", "YAML front matter", true); var yamlFrontMatterParser = new Extension("yamlFrontMatterParser", "YAML front matter");
yamlFrontMatterParser.settingsBlock = yamlFrontMatterParserSettingsBlock;
var eventMgr = undefined; var eventMgr = undefined;
yamlFrontMatterParser.onEventMgrCreated = function(eventMgrParameter) { yamlFrontMatterParser.onEventMgrCreated = function(eventMgrParameter) {
eventMgr = eventMgrParameter; eventMgr = eventMgrParameter;
}; };
var fileDesc = undefined;
yamlFrontMatterParser.onFileSelected = function(fileDescParam) {
fileDesc = fileDescParam;
};
var regex = /^(\s*-{3}\s*\n([\w\W]+?)\n\s*-{3}\s*\n)?([\w\W]*)*/;
yamlFrontMatterParser.onPagedownConfigure = function(editor) { yamlFrontMatterParser.onPagedownConfigure = function(editor) {
var converter = editor.getConverter(); var converter = editor.getConverter();
converter.hooks.chain("preConversion", function(text) { converter.hooks.chain("preConversion", function(text) {
try { var results = regex.exec(text),
var re = /^(\s*-{3}\s*\n([\w\W]+?)\n\s*-{3}\s*\n)?([\w\W]*)*/, results = re.exec(text), conf = {}, yaml; yaml;
if((yaml = results[2])) { if ((yaml = results[2]) && (!fileDesc.frontMatter || fileDesc.frontMatter._yaml != yaml)) {
conf = jsyaml.load(yaml); fileDesc.frontMatter = undefined;
console.log(conf); try {
fileDesc.frontMatter = jsyaml.load(yaml);
fileDesc.frontMatter._yaml = yaml;
} }
eventMgr.onMarkdownTrim(results[1].length); catch (e) {}
return results[3];
}
catch(e) {
return text;
} }
eventMgr.onMarkdownTrim((results[1] || '').length);
return results[3];
}); });
}; };

View File

@ -1,3 +0,0 @@
<p>Parses YAML front matter block at the begining of the document.</p>
<blockquote><b>Interpreted variables:</b> <i>title, published, category/categories/tags</i></blockquote>
<span class="help-block pull-right"><a target="_blank" href="http://jekyllrb.com/docs/frontmatter/">More info</a></span>