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

步驟3,引入到html,并使用
使用showdown.js的基本方式:
function compile(){
//獲取要轉(zhuǎn)換的文字
var text = document.getElementById("content").value;
//創(chuàng)建實(shí)例
var converter = new showdown.Converter();
//進(jìn)行轉(zhuǎn)換
var html = converter.makeHtml(text);
//展示到對(duì)應(yīng)的地方 result便是id名稱
document.getElementById("result").innerHTML = html;
}
仿簡(jiǎn)書的markdown實(shí)例代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>測(cè)試使用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>
<!--<--設(shè)置id為oriContent,如果想實(shí)現(xiàn)實(shí)時(shí)更新,使用onkeyup方法--!>-->
<textarea id="oriContent" style="height:400px;width:600px;" onkeyup="convert()"></textarea>
<!--<---設(shè)置展示的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>
運(yùn)行結(jié)果:

之后又發(fā)現(xiàn)一個(gè)開源庫(kù):
https://github.com/chjj/marked
使用方式相對(duì)簡(jiǎn)單一些,參照上面的實(shí)例,完成相同的功能,如下:
實(shí)例代碼:
<!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>

暫時(shí)先看這兩個(gè)開源庫(kù),目前都不能像簡(jiǎn)書一樣對(duì)照片進(jìn)行操作,待了解~