这个markdown格式转html格式的开源JavaScript库在github上的地址:javascript
https://github.com/millerblac...html
从markdown 格式转成html源代码格式java
新建一个以js结尾的文件,将下列内容粘贴进去:node
var markdown = require( "markdown" ).markdown; console.log( markdown.toHTML( "Hello *World*!" ) );
用nodejs执行,能够看到markdown格式的字符串:git
Hello World!github
被自动转换成了html格式的字符串:浏览器
<p>Hello World!</p>markdown
除了nodejs之外,咱们还能够在浏览器里使用这个开源库。ui
新建一个html,将下列源码粘贴进去:this
<!DOCTYPE html> <html> <body> <textarea id="text-input" oninput="this.editor.update()" rows="6" cols="60">Type **Markdown** here.</textarea> <div id="preview"> </div> <script src="../node_modules/markdown/lib/markdown.js"></script> <script> function Editor(input, preview) { this.update = function () { preview.innerHTML = markdown.toHTML(input.value); }; input.editor = this; this.update(); } var $ = function (id) { return document.getElementById(id); }; new Editor($("text-input"), $("preview")); </script> </body> </html>
用浏览器打开这个html,在顶部输入框里输入markdown代码后,能自动调用这个开源库,转换成html源代码,而后赋给innerHTML, 这样咱们在UI上能看到实时的markdown代码转html代码的结果。
要获取更多Jerry的原创文章,请关注公众号"汪子熙":