推薦一個markdown格式轉html格式的開源JavaScript庫
阿新 • • 發佈:2018-10-20
ron 輸入框 com world 字符 scrip ogr gem load !
這個markdown格式轉html格式的開源JavaScript庫在github上的地址:
https://github.com/millerblack/markdown-js
從markdown 格式轉成html源代碼格式
新建一個以js結尾的文件,將下列內容粘貼進去:
var markdown = require( "markdown" ).markdown;
console.log( markdown.toHTML( "Hello *World*!" ) );
用nodejs執行,可以看到markdown格式的字符串:
Hello World!
被自動轉換成了html格式的字符串:
Hello World
除了nodejs以外,我們還可以在瀏覽器裏使用這個開源庫。
新建一個html,將下列源碼粘貼進去:
<!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的原創文章,請關註公眾號"汪子熙":
推薦一個markdown格式轉html格式的開源JavaScript庫