學習地址:http://www.itdecent.cn/p/747d6f8dddb0
練習代碼地址:https://github.com/956159241/DemoOfWebFront
步驟1.下載showdown.js
步驟2引入到自己的項目中,結(jié)構(gòu)如下:

步驟3,引入到html,并使用
使用showdown.js的基本方式:
function compile(){
//獲取要轉(zhuǎn)換的文字
var text = document.getElementById("content").value;
//創(chuàng)建實例
var converter = new showdown.Converter();
//進行轉(zhuǎn)換
var html = converter.makeHtml(text);
//展示到對應的地方 result便是id名稱
document.getElementById("result").innerHTML = html;
}
仿簡書的markdown實例代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>測試使用markdown</title>
<script type="text/javascript" src="showdown-master/showdown-master/dist/showdown.min.js"></script>
<style>
blockquote {
border-left:#eee solid 5px;
padding-left:20px;
}
ul li {
line-height: 20px;
}
code {
color:#D34B62;
background: #F6F6F6;
}
</style>
</head>
<body>
<div>
<!--<--設置id為oriContent,如果想實現(xiàn)實時更新,使用onkeyup方法--!>-->
<textarea id="oriContent" style="height:400px;width:600px;" onkeyup="convert()"></textarea>
<!--<---設置展示的div添加id-!>-->
<div id="result"></div>
</div>
<!--<--寫轉(zhuǎn)化函數(shù)--!>-->
<script type="text/javascript">
function convert(){
var text = document.getElementById("oriContent").value;
var converter = new showdown.Converter();
var html = converter.makeHtml(text);
document.getElementById("result").innerHTML = html;
}
</script>
</body>
</html>
運行結(jié)果:

之后又發(fā)現(xiàn)一個開源庫:
https://github.com/chjj/marked
使用方式相對簡單一些,參照上面的實例,完成相同的功能,如下:
實例代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<textarea id="oriContent" style="height:400px;width:600px;" onkeyup="convert()"></textarea>
<div id="result"></div>
<script type="text/javascript">
function convert(){
var text = document.getElementById("oriContent").value;
var html = marked(text);
document.getElementById("result").innerHTML = html;
}
</script>
</body>
</html>

暫時先看這兩個開源庫,目前都不能像簡書一樣對照片進行操作,待了解~