您现在的位置是:首页 > 文章详情

推荐一个markdown格式转html格式的开源JavaScript库

日期:2018-09-28点击:544

这个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代码的结果。

原文链接:https://yq.aliyun.com/articles/646680
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章