利用Vue實(shí)現(xiàn)一個(gè)markdown編輯器實(shí)例代碼
前言
前段時(shí)間做項(xiàng)目的時(shí)候,需要一個(gè)Markdown編輯器,在網(wǎng)上找了一些開(kāi)源的實(shí)現(xiàn),但是都不滿足需求
說(shuō)實(shí)話,這些開(kāi)源項(xiàng)目也很難滿足需求公司項(xiàng)目的需求,與其實(shí)現(xiàn)一個(gè)大而全的項(xiàng)目,倒不如實(shí)現(xiàn)一個(gè)簡(jiǎn)單的,易于在源碼上修改的項(xiàng)目,核心功能都有的,以供修改使用
本文的源碼地址如下:https://github.com/jiulu313/HelloMarkDown(本地下載)
喜歡的朋友可以幫忙star一下,歡迎交流學(xué)習(xí)
先看一下本項(xiàng)目的效果圖(圖片經(jīng)過(guò)壓縮)

本文的目的就是實(shí)現(xiàn)一個(gè)有核心功能的,簡(jiǎn)單,易于修改的項(xiàng)目
話不多說(shuō),來(lái)看思路
1 markdown內(nèi)容如何轉(zhuǎn)換成 html?
網(wǎng)上有一個(gè)開(kāi)源的庫(kù)叫 marked,地址如下:https://github.com/markedjs/marked.git
我們可以安裝這個(gè)庫(kù),使用很簡(jiǎn)單,就一個(gè)函數(shù),傳進(jìn)去markdown內(nèi)容,就返回了html內(nèi)容
2 markdown內(nèi)容轉(zhuǎn)換成了html,如何進(jìn)行語(yǔ)法高亮?
網(wǎng)上也有一個(gè)開(kāi)源的庫(kù),地址如下 :https://highlightjs.org/
我們可以使用這兩個(gè)庫(kù)
先把markdown內(nèi)容解析成html內(nèi)容
把html內(nèi)容進(jìn)行語(yǔ)法高亮
下面我們來(lái)一步一步實(shí)現(xiàn)代碼
3 代碼實(shí)現(xiàn)
默認(rèn)你已經(jīng)創(chuàng)建好了vue的項(xiàng)目 , 創(chuàng)建vue項(xiàng)目 vue init webpack demo
這里面不多講。
3.1 安裝兩個(gè)庫(kù),分別執(zhí)行下面兩條命令
npm install marked --save npm install highlight.js --save
3.2 首先創(chuàng)建一個(gè) HelloMarkDown 的 Vue組件
布局文件的代碼如下:
<template>
<div class="md_root_content" v-bind:style="{width:this.width,height: this.height}">
<!--功能按鈕區(qū)-->
<div class="button_bar">
<span v-on:click="addBold"><B>B</B></span>
<span v-on:click="addUnderline"><B>U</B></span>
<span v-on:click="addItalic"><B>I</B></span>
</div>
<!--主要內(nèi)容區(qū)-->
<div class="content_bar">
<!--markdown編輯器區(qū)-->
<div class="markdown_body">
<textarea ref="ref_md_edit" class="md_textarea_content" v-model="markString">
</textarea>
</div>
<!--解析成html區(qū)-->
<div class="html_body">
<p v-html="htmlString"></p>
</div>
</div>
</div>
</template>
主要分為上下兩塊,上面是功能區(qū)的布局
下面一塊,分左右兩部分,左邊是markdown,右邊是顯示html部分
對(duì)應(yīng)的樣式代碼如下:
<style scoped>
.md_root_content {
display: flex;
display: -webkit-flex;
flex-direction: column;
}
.button_bar {
width: 100%;
height: 40px;
background-color: #d4d4d4;
display: flex;
display: -webkit-flex;
align-items: center;
}
div.button_bar span {
width: 30px;
line-height: 40px;
text-align: center;
color: orange;
cursor: pointer;
}
.content_bar {
display: flex;
display: -webkit-flex;
width: 100%;
height: 100%;
}
.markdown_body {
width: 50%;
height: 100%;
display: flex;
display: -webkit-flex;
}
.html_body {
width: 50%;
height: 100%;
display: flex;
display: -webkit-flex;
background-color: #dfe9f1;
}
.md_textarea_content {
flex: 1;
height: 100%;
padding: 12px;
overflow: auto;
box-sizing: border-box;
resize: none;
outline: none;
border: none;
background-color: #f4f4f4;
font-size: 14px;
color: #232323;
line-height: 24px;
}
</style>
業(yè)務(wù)邏輯部分的代碼如下:
<script>
import marked from 'marked' //解析mardown語(yǔ)法的庫(kù)
import hljs from 'highlight.js' //對(duì)代碼進(jìn)行語(yǔ)法高亮的庫(kù)
import testData from '../testData' //測(cè)試數(shù)據(jù)
export default {
name: "HelloMarkDown",
props: {
width: {
type: String,
default: '1000px'
},
height: {
type: String,
default: '600px'
}
},
data() {
return {
markString: '',
htmlString: '',
}
},
mounted(){
this.markString = testData
},
methods: {
//加粗
addBold() {
this.changeSelectedText("**","**")
},
//斜體
addItalic() {
this.changeSelectedText("***","***")
},
addUnderline() {
this.changeSelectedText("<u>","</u>")
},
changeSelectedText(startString,endString){
let t = this.$refs.ref_md_edit
if (window.getSelection) {
if (t.selectionStart != undefined && t.selectionEnd != undefined) {
let str1 = t.value.substring(0, t.selectionStart)
let str2 = t.value.substring(t.selectionStart, t.selectionEnd)
let str3 = t.value.substring(t.selectionEnd)
let result = str1 + startString + str2 + endString + str3
t.value = result
this.markString = t.value
}
}
}
},
watch: {
//監(jiān)聽(tīng)markString變化
markString: function (value) {
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: true,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false
})
this.htmlString = marked(value)
},
//監(jiān)聽(tīng)htmlString并對(duì)其高亮
htmlString: function (value) {
this.$nextTick(() => {
const codes = document.querySelectorAll(".html_body pre code");
// elem 是一個(gè) object
codes.forEach(elem => {
elem.innerHTML = "<ul><li>" + elem.innerHTML.replace(/\n/g, "\n</li><li>") + "\n</li></ul>"
hljs.highlightBlock(elem);
});
});
}
}
}
</script>
script中的代碼解釋
props: {
width: {
type: String,
default: '1000px'
},
height: {
type: String,
default: '600px'
}
},
width: 組件的寬度
height:組件的高度
data() {
return {
markString: '',
htmlString: '',
}
},
markString:保存我們輸入的markdown內(nèi)容
htmlString:保存markdown內(nèi)容轉(zhuǎn)換成的html內(nèi)容,也就是通過(guò)marked函數(shù)轉(zhuǎn)換過(guò)來(lái)的
mounted(){
this.markString = testData
},
顯示默認(rèn)數(shù)據(jù)
//加粗
addBold() {
this.changeSelectedText("**","**")
},
//斜體
addItalic() {
this.changeSelectedText("***","***")
},
//加下劃線
addUnderline() {
this.changeSelectedText("<u>","</u>")
},
這三個(gè)函數(shù)都是調(diào)用了 changeSelectedText 函數(shù)
主要是對(duì)鼠標(biāo)選中的內(nèi)容進(jìn)行改變,比如加粗效果,是在選中文本的兩邊分別添加 **
所以changeSelectedText函數(shù)的作用就是在選中的文本兩邊添加不同的md的符號(hào)
比如
this.changeSelectedText("","") ,就是在選中的文本左邊和右邊都添加**
然后再把最新的內(nèi)容賦值給 this.$refs.ref_md_edit.value,同時(shí)也兩會(huì)給markString
這樣就可以做到選中文本加粗效果了
//監(jiān)聽(tīng)markString變化
markString: function (value) {
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: true,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false
})
this.htmlString = marked(value)
},
此時(shí)是監(jiān)聽(tīng)markString的變化
然后調(diào)用marked函數(shù)進(jìn)行轉(zhuǎn)換成html內(nèi)容,并賦值給htmlString
marked.setOptions 是設(shè)置一些配置,有興趣的可以查一下這些配置的作用
//監(jiān)聽(tīng)htmlString并對(duì)其高亮
htmlString: function (value) {
this.$nextTick(() => {
const codes = document.querySelectorAll(".html_body pre code");
// elem 是一個(gè) object
codes.forEach(elem => {
elem.innerHTML = "<ul><li>" + elem.innerHTML.replace(/\n/g, "\n</li><li>") + "\n</li></ul>"
hljs.highlightBlock(elem);
});
});
}
原本通過(guò) highlight.js這個(gè)庫(kù)在顯示語(yǔ)法高亮的時(shí)候,是沒(méi)有行號(hào)的。這里我進(jìn)行了擴(kuò)展
通過(guò) document.querySelectorAll(".html_body pre code") 找到nodeList
然后對(duì)其循環(huán),動(dòng)態(tài)添加 ul , li, 這樣就可以顯示行號(hào)了
不過(guò)這需要對(duì) highlight的css文件添加幾個(gè)樣式
源碼里面我把highlight中的css文件全部copy到項(xiàng)目中了,使用的是github.css
具體位置是在項(xiàng)目中的 assets/markdown/styles/github.css
如果想使用其它的主題,可以自己修改其它的對(duì)應(yīng)的css文件,這里使用了github的主題,所以只修改了github.css這一個(gè)文件
有興趣的可以查看一下
github.css文件的提交記錄
具體的思路就是這些,水平有限,難免有bug,如有發(fā)現(xiàn),歡迎提出
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Vue2.0結(jié)合webuploader實(shí)現(xiàn)文件分片上傳功能
這篇文章主要介紹了Vue2.0結(jié)合webuploader實(shí)現(xiàn)文件分片上傳功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03
詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳)
這篇文章主要介紹了詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue實(shí)現(xiàn)消息列表向上無(wú)縫滾動(dòng)效果
本文主要實(shí)現(xiàn)vue項(xiàng)目中,消息列表逐條向上無(wú)縫滾動(dòng),每條消息展示10秒后再滾動(dòng),為了保證用戶能看清消息主題,未使用第三方插件,本文實(shí)現(xiàn)方法比較簡(jiǎn)約,需要的朋友可以參考下2024-06-06

