mirror of
https://github.com/icecoder/ICEcoder.git
synced 2026-03-02 23:03:59 +01:00
44 lines
1017 B
JavaScript
44 lines
1017 B
JavaScript
;function mmd(src)
|
|
{
|
|
var h='';
|
|
|
|
function escape(t)
|
|
{
|
|
return new Option(t).innerHTML;
|
|
}
|
|
function inlineEscape(s)
|
|
{
|
|
return escape(s)
|
|
.replace(/!\[([^\]]*)]\(([^(]+)\)/g, '<img alt="$1" src="$2">')
|
|
.replace(/\[([^\]]+)]\(([^(]+)\)/g, '$1'.link('$2'))
|
|
.replace(/`([^`]+)`/g, '<code>$1</code>')
|
|
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
|
.replace(/\*([^*]+)\*/g, '<em>$1</em>');
|
|
}
|
|
|
|
src
|
|
.replace(/^\s+|\r|\s+$/g, '')
|
|
.replace(/\t/g, ' ')
|
|
.split(/\n\n+/)
|
|
.forEach(function(b, f, R)
|
|
{
|
|
f=b[0];
|
|
R=
|
|
{
|
|
'*':[/\n\* /,'<ul><li>','</li></ul>'],
|
|
'1':[/\n[1-9]\d*\.? /,'<ol><li>','</li></ol>'],
|
|
' ':[/\n /,'<pre><code>','</pre></code>','\n'],
|
|
'>':[/\n> /,'<blockquote>','</blockquote>','\n']
|
|
}[f];
|
|
h+=
|
|
R?R[1]+('\n'+b)
|
|
.split(R[0])
|
|
.slice(1)
|
|
.map(R[3]?escape:inlineEscape)
|
|
.join(R[3]||'</li>\n<li>')+R[2]:
|
|
f=='#'?'<h'+(f=b.indexOf(' '))+'>'+inlineEscape(b.slice(f+1))+'</h'+f+'>':
|
|
f=='<'?b:
|
|
'<p>'+inlineEscape(b)+'</p>';
|
|
});
|
|
return h;
|
|
}; |