lxrmido
2016-08-04 09:12:07 +08:00
这就是 markdown 的前端格式化显示啊……有许多现成的轮子,思路大概如:
```
function parse(str){
var inContext = false, html = '';
str.split("\n").forEach(function(line){
switch(true){
case inContext && line[0] == '~' && line[1] == '~':
html += '</div>';
inContext = false;
break;
case inContext:
html += line;
break;
case line[0] == '~' && line[1] == '~':
inContext = true;
html += '<div class="img">';
break;
case line[0] == '#':
html += '<div class="mainTitle">' + line.slice(1) + '</div>';
break;
case line.length > 0:
html += '<p>' + line + '</p>';
default:
html += line;
break;
}
});
return html;
}
```