'use strict';
var Markdown = {
render: function(md) {
if (!md) return '';
var html = md;
// Code blocks
html = html.replace(/```(\w*)\n([\s\S]*?)```/g, function(_, lang, code) {
return '
' + Markdown.esc(code.trim()) + '
';
});
// Inline code
html = html.replace(/`([^`]+)`/g, '$1');
// Bold
html = html.replace(/\*\*(.+?)\*\*/g, '$1');
// Italic
html = html.replace(/\*(.+?)\*/g, '$1');
// Headings
html = html.replace(/^### (.+)$/gm, '$1
');
html = html.replace(/^## (.+)$/gm, '$1
');
html = html.replace(/^# (.+)$/gm, '$1
');
// HR
html = html.replace(/^---$/gm, '
');
// Tables
html = html.replace(/^\|(.+)\|\s*\n\|[-| :]+\|\s*\n((?:\|.+\|\s*\n?)*)/gm, function(_, header, rows) {
var ths = header.split('|').map(function(h) { return '' + h.trim() + ' | '; }).join('');
var trs = rows.trim().split('\n').map(function(row) {
var tds = row.replace(/^\||\|$/g, '').split('|').map(function(c) { return '' + c.trim() + ' | '; }).join('');
return '' + tds + '
';
}).join('');
return '';
});
// Lists
html = html.replace(/^- (.+)$/gm, '$1');
html = html.replace(/([\s\S]*?<\/li>)/g, function(match) {
if (match.indexOf('') === -1) return '';
return match;
});
// Paragraphs
html = html.replace(/^(?!<[huptlo]|<\/|
$1');
return html;
},
esc: function(str) {
return str.replace(/&/g, '&').replace(//g, '>');
}
};