2014-10-03 21:49:18 +03:00
|
|
|
// Init highlight JS
|
|
|
|
hljs.initHighlightingOnLoad();
|
2014-09-06 06:41:22 +03:00
|
|
|
|
|
|
|
function splitInput(str) {
|
2014-10-02 01:14:54 +03:00
|
|
|
if (str.slice(0, 3) !== '---') return;
|
2014-09-06 06:41:22 +03:00
|
|
|
|
2014-10-02 01:14:54 +03:00
|
|
|
var matcher = /\n(\.{3}|\-{3})/g;
|
|
|
|
var metaEnd = matcher.exec(str);
|
2014-09-06 06:41:22 +03:00
|
|
|
|
2014-10-02 01:14:54 +03:00
|
|
|
return metaEnd && [str.slice(0, metaEnd.index), str.slice(matcher.lastIndex)];
|
2014-09-06 06:41:22 +03:00
|
|
|
}
|
|
|
|
|
2014-10-03 21:49:18 +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
|
2014-09-06 06:41:22 +03:00
|
|
|
var metaMarked = function(src, opt, callback) {
|
2014-10-02 01:14:54 +03:00
|
|
|
if (Object.prototype.toString.call(src) !== '[object String]')
|
|
|
|
throw new TypeError('First parameter must be a string.');
|
2014-09-06 06:41:22 +03:00
|
|
|
|
2014-10-02 01:14:54 +03:00
|
|
|
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
|
2014-09-06 06:41:22 +03:00
|
|
|
}
|
2014-10-02 01:14:54 +03:00
|
|
|
}
|
2014-09-06 06:41:22 +03:00
|
|
|
};
|
|
|
|
|
2016-07-26 03:20:45 +03:00
|
|
|
var markdownit = window.markdownit({
|
|
|
|
html: true,
|
|
|
|
linkify: true,
|
2016-07-26 03:59:28 +03:00
|
|
|
typographer: true,
|
2016-07-26 03:20:45 +03:00
|
|
|
highlight: function (str, lang) {
|
|
|
|
if (lang && hljs.getLanguage(lang)) {
|
|
|
|
try {
|
|
|
|
return hljs.highlight(lang, str).value;
|
|
|
|
} catch (__) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ''; // use external default escaping
|
|
|
|
}
|
2016-09-04 23:03:16 +03:00
|
|
|
}).use(markdownItAnchor, {
|
|
|
|
level: 1,
|
|
|
|
// slugify: string => string,
|
|
|
|
permalink: false,
|
|
|
|
// renderPermalink: (slug, opts, state, permalink) => {},
|
|
|
|
permalinkClass: 'header-anchor',
|
|
|
|
permalinkSymbol: '¶',
|
|
|
|
permalinkBefore: false
|
2014-09-06 06:41:22 +03:00
|
|
|
});
|
|
|
|
|
2013-10-10 18:38:30 +03:00
|
|
|
// Markdown Renderer
|
2014-09-06 06:41:22 +03:00
|
|
|
var MDR = {
|
2014-10-02 01:14:54 +03:00
|
|
|
meta: null,
|
|
|
|
md: null,
|
|
|
|
sanitize: true, // Override
|
2015-08-31 03:17:51 +03:00
|
|
|
parse: function(md){
|
2016-07-26 03:20:45 +03:00
|
|
|
return markdownit.render(md);
|
2015-08-31 03:17:51 +03:00
|
|
|
},
|
2014-10-02 01:14:54 +03:00
|
|
|
convert: function(md, sanitize) {
|
|
|
|
if (this.sanitize !== null) {
|
|
|
|
sanitize = this.sanitize;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2014-10-02 01:14:54 +03:00
|
|
|
if (sanitize) {
|
|
|
|
// Causes some problems with inline styles
|
|
|
|
html = html_sanitize(html, function(url) {
|
|
|
|
try {
|
|
|
|
var prot = decodeURIComponent(url.toString());
|
|
|
|
} catch (e) {
|
|
|
|
return '';
|
2013-10-10 18:38:30 +03:00
|
|
|
}
|
2014-10-02 01:14:54 +03:00
|
|
|
if (prot.indexOf('javascript:') === 0) {
|
|
|
|
return '';
|
2014-09-06 06:41:22 +03:00
|
|
|
}
|
2014-10-02 01:14:54 +03:00
|
|
|
return prot;
|
|
|
|
}, function(id){
|
|
|
|
return id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.hook();
|
|
|
|
return html;
|
|
|
|
},
|
2014-09-06 06:41:22 +03:00
|
|
|
|
2014-10-02 01:14:54 +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);
|
2014-10-03 21:49:18 +03:00
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2013-10-10 18:38:30 +03:00
|
|
|
}
|
2014-10-02 01:14:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
hook: function() {
|
|
|
|
}
|
2015-08-31 03:17:51 +03:00
|
|
|
};
|
|
|
|
|
2016-07-26 03:20:45 +03:00
|
|
|
// Add some custom classes to table tags
|
|
|
|
markdownit.renderer.rules.table_open = function (tokens, idx, options, env, self) {
|
|
|
|
tokens[idx].attrPush(['class', 'table table-bordered']);
|
|
|
|
return self.renderToken(tokens, idx, options);
|
|
|
|
};
|