markdown-it js 添加表格處理

var md = window.markdownit();
        var rst = md.render($('#new-post-content').val());
        ///////////////
        // 解析 table
        ///////////////
        var tcode = new RegExp(/(\|(?:[^\r\n\|]+\|)+)(?:\r?\n|\r)\|(?:[-—]+\|)+((?:(?:\r?\n|\r)(?:\|(?:[^\n\r\|]+\|)+))+)/,'gu'), curT = 1;
        while(curT !== null){
            curT=tcode.exec(rst);
            if(curT !== null){
                console.log(curT[2].split(/\r?\n|\r/));
                var rows = curT[2].split(/\r?\n|\r/).filter(function(a){return a.length === 0 ? false : true;}), rowtrs = '<table><thead><tr><td>'+curT[1].split('|').slice(1,-1).join('</td><td>')+'</td></tr></thead><tbody>';
                console.log(rows);
                for(var i in rows){
                    rowtrs += '<tr><td>'+rows[i].split('|').slice(1,-1).join("</td><td>")+'</td></tr>';
                }
                rowtrs += '</tbody></table>';
                rst = rst.replace(curT[0], rowtrs);
            }
        }
        //            document.getElementById("introduce-content").innerHTML = rst;

        $('#new-post-content').html(rst);
相關文章
相關標籤/搜索