2014-09-07 19:54:51 +03:00
|
|
|
// Handlebar helpers
|
|
|
|
Handlebars.registerHelper('well', function(options) {
|
|
|
|
return '<div class="well">' + options.fn(this) + '</div>';
|
|
|
|
});
|
|
|
|
|
2014-09-06 06:41:22 +03:00
|
|
|
/* © 2013 j201
|
|
|
|
* https://github.com/j201/meta-marked */
|
|
|
|
|
|
|
|
// Splits the given string into a meta section and a markdown section if a meta section is present, else returns null
|
|
|
|
function splitInput(str) {
|
|
|
|
if (str.slice(0, 3) !== '---') return;
|
|
|
|
|
|
|
|
var matcher = /\n(\.{3}|\-{3})/g;
|
|
|
|
var metaEnd = matcher.exec(str);
|
|
|
|
|
|
|
|
return metaEnd && [str.slice(0, metaEnd.index), str.slice(matcher.lastIndex)];
|
|
|
|
}
|
|
|
|
|
|
|
|
var metaMarked = function(src, opt, callback) {
|
|
|
|
if (Object.prototype.toString.call(src) !== '[object String]')
|
|
|
|
throw new TypeError('First parameter must be a string.');
|
|
|
|
|
|
|
|
var mySplitInput = splitInput(src);
|
|
|
|
if (mySplitInput) {
|
|
|
|
var meta;
|
|
|
|
try {
|
|
|
|
meta = jsyaml.safeLoad(mySplitInput[0]);
|
|
|
|
} catch(e) {
|
|
|
|
meta = null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
meta: meta,
|
|
|
|
md: mySplitInput[1]
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
meta: null,
|
|
|
|
md: src
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
marked.setOptions({
|
|
|
|
renderer: new marked.Renderer(),
|
|
|
|
gfm: true,
|
|
|
|
tables: true,
|
|
|
|
breaks: false,
|
|
|
|
pedantic: false,
|
|
|
|
sanitize: false,
|
|
|
|
smartLists: true,
|
|
|
|
smartypants: false
|
|
|
|
});
|
|
|
|
|
2013-10-10 18:38:30 +03:00
|
|
|
// Init highlight JS
|
|
|
|
hljs.initHighlightingOnLoad();
|
|
|
|
|
|
|
|
// Markdown Renderer
|
2014-09-06 06:41:22 +03:00
|
|
|
var MDR = {
|
|
|
|
meta: null,
|
|
|
|
md: null,
|
2014-02-07 05:40:58 +02:00
|
|
|
sanitize: true, // Override
|
2014-09-06 06:41:22 +03:00
|
|
|
parse: function(md){ return marked(md); },
|
2013-10-10 18:38:30 +03:00
|
|
|
convert: function(md, sanitize){
|
|
|
|
if (this.sanitize !== null) {
|
|
|
|
sanitize = this.sanitize;
|
|
|
|
}
|
2014-09-06 06:41:22 +03:00
|
|
|
this.md = md;
|
|
|
|
this.processMeta();
|
|
|
|
try {
|
|
|
|
var html = this.parse(this.md);
|
|
|
|
} catch(e) {
|
|
|
|
return this.md;
|
|
|
|
}
|
2014-09-10 18:53:22 +03:00
|
|
|
|
2013-10-10 18:38:30 +03:00
|
|
|
if (sanitize) {
|
|
|
|
// Causes some problems with inline styles
|
2014-02-07 05:40:58 +02:00
|
|
|
html = html_sanitize(html, function(url) {
|
2014-09-10 19:09:29 +03:00
|
|
|
if(/^(\/|https?:\/\/)/.test(url)) {
|
2014-02-07 05:40:58 +02:00
|
|
|
return url
|
|
|
|
}
|
|
|
|
}, function(id){
|
|
|
|
return id;
|
|
|
|
});
|
2013-10-10 18:38:30 +03:00
|
|
|
}
|
2014-09-08 20:49:37 +03:00
|
|
|
this.hook();
|
2013-10-10 18:38:30 +03:00
|
|
|
return html;
|
|
|
|
},
|
2014-09-06 06:41:22 +03:00
|
|
|
|
|
|
|
processMeta: function() {
|
|
|
|
var doc = metaMarked(this.md);
|
|
|
|
this.md = doc.md;
|
|
|
|
this.meta = doc.meta;
|
|
|
|
if (this.meta) {
|
|
|
|
try {
|
|
|
|
var template = Handlebars.compile(this.md);
|
|
|
|
this.md = template(this.meta);
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-09-08 20:49:37 +03:00
|
|
|
hook: function() {
|
2013-10-10 18:38:30 +03:00
|
|
|
}
|
|
|
|
};
|