work in progress

by epinapala

HTML

<textarea id="wikitext" cols="20" rows="10">* bullet1
bullet1cont 

kkk

    
# iii

lll

* bullet2
bullet2 cont</textarea>
<br>
<input id="btnsave" type="button" value="Preview HTML" />
<br>
<hr>
<div id="html"></div>

JavaScript

document.getElementById("btnsave").addEventListener("click", parseWiki, false);

function parseLists(str) {
    str = str.replace(/(^|[^\n])\n(?!\*|#|\n)/g, " ");
    return str.replace(/(?:(?:(?:^|\n)[\*#].*)+)/g, function (match) {
        var listType = match.match(/(^|\n)#/) ? 'ol' : 'ul';
        match = match.replace(/(^|\n)[\*#][ ]{0,1}/g, "$1");
        match = parseLists(match);
        return '<' + listType + '><li>' + match.replace(/^\n/, '').split(/\n/).join('</li><li>') + '</li></' + listType + '>';
    });
}

function parseLists2(str)
{
  return str.replace(/(?:(?:(?:^|\n)[\*#](?:.+\n)+.*)+)/g, function(match){ 
        match = match.replace(/\n/g," ");
        console.log(match);
        var listType = match.match(/(^|\s)#/) ? 'ol' : 'ul';
        match = match.replace(/(^|\s)[\*#][ ]{0,1}/g, "$1");
        match = parseLists2(match);
        return '<'
                + listType + '><li>'
                + match.replace(/^\n/, '')
                .split(/\n/).join('</li><li>')
                + '</li></' + listType
                + '>';
    });
}

function parseWiki() {
    var textArea = document.getElementById('wikitext');
    var div = document.getElementById('html');
    div.innerHTML = parseLists(textArea.value);
}