利用Vue實現(xiàn)一個markdown編輯器實例代碼
前言
前段時間做項目的時候,需要一個Markdown編輯器,在網(wǎng)上找了一些開源的實現(xiàn),但是都不滿足需求
說實話,這些開源項目也很難滿足需求公司項目的需求,與其實現(xiàn)一個大而全的項目,倒不如實現(xiàn)一個簡單的,易于在源碼上修改的項目,核心功能都有的,以供修改使用
本文的源碼地址如下:https://github.com/jiulu313/HelloMarkDown(本地下載)
喜歡的朋友可以幫忙star一下,歡迎交流學(xué)習(xí)
先看一下本項目的效果圖(圖片經(jīng)過壓縮)
本文的目的就是實現(xiàn)一個有核心功能的,簡單,易于修改的項目
話不多說,來看思路
1 markdown內(nèi)容如何轉(zhuǎn)換成 html?
網(wǎng)上有一個開源的庫叫 marked,地址如下:https://github.com/markedjs/marked.git
我們可以安裝這個庫,使用很簡單,就一個函數(shù),傳進去markdown內(nèi)容,就返回了html內(nèi)容
2 markdown內(nèi)容轉(zhuǎn)換成了html,如何進行語法高亮?
網(wǎng)上也有一個開源的庫,地址如下 :https://highlightjs.org/
我們可以使用這兩個庫
先把markdown內(nèi)容解析成html內(nèi)容
把html內(nèi)容進行語法高亮
下面我們來一步一步實現(xiàn)代碼
3 代碼實現(xiàn)
默認你已經(jīng)創(chuàng)建好了vue的項目 , 創(chuàng)建vue項目 vue init webpack demo
這里面不多講。
3.1 安裝兩個庫,分別執(zhí)行下面兩條命令
npm install marked --save npm install highlight.js --save
3.2 首先創(chuàng)建一個 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部分
對應(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語法的庫 import hljs from 'highlight.js' //對代碼進行語法高亮的庫 import testData from '../testData' //測試數(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)聽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)聽htmlString并對其高亮 htmlString: function (value) { this.$nextTick(() => { const codes = document.querySelectorAll(".html_body pre code"); // elem 是一個 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)容,也就是通過marked函數(shù)轉(zhuǎn)換過來的
mounted(){ this.markString = testData },
顯示默認數(shù)據(jù)
//加粗 addBold() { this.changeSelectedText("**","**") }, //斜體 addItalic() { this.changeSelectedText("***","***") }, //加下劃線 addUnderline() { this.changeSelectedText("<u>","</u>") },
這三個函數(shù)都是調(diào)用了 changeSelectedText 函數(shù)
主要是對鼠標(biāo)選中的內(nèi)容進行改變,比如加粗效果,是在選中文本的兩邊分別添加 **
所以changeSelectedText函數(shù)的作用就是在選中的文本兩邊添加不同的md的符號
比如
this.changeSelectedText("","")
,就是在選中的文本左邊和右邊都添加**
然后再把最新的內(nèi)容賦值給 this.$refs.ref_md_edit.value
,同時也兩會給markString
這樣就可以做到選中文本加粗效果了
//監(jiān)聽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)聽markString的變化
然后調(diào)用marked函數(shù)進行轉(zhuǎn)換成html內(nèi)容,并賦值給htmlString
marked.setOptions
是設(shè)置一些配置,有興趣的可以查一下這些配置的作用
//監(jiān)聽htmlString并對其高亮 htmlString: function (value) { this.$nextTick(() => { const codes = document.querySelectorAll(".html_body pre code"); // elem 是一個 object codes.forEach(elem => { elem.innerHTML = "<ul><li>" + elem.innerHTML.replace(/\n/g, "\n</li><li>") + "\n</li></ul>" hljs.highlightBlock(elem); }); }); }
原本通過 highlight.js這個庫在顯示語法高亮的時候,是沒有行號的。這里我進行了擴展
通過 document.querySelectorAll(".html_body pre code")
找到nodeList
然后對其循環(huán),動態(tài)添加 ul , li, 這樣就可以顯示行號了
不過這需要對 highlight的css文件添加幾個樣式
源碼里面我把highlight中的css文件全部copy到項目中了,使用的是github.css
具體位置是在項目中的 assets/markdown/styles/github.css
如果想使用其它的主題,可以自己修改其它的對應(yīng)的css文件,這里使用了github的主題,所以只修改了github.css這一個文件
有興趣的可以查看一下
github.css文件的提交記錄
具體的思路就是這些,水平有限,難免有bug,如有發(fā)現(xiàn),歡迎提出
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Vue2.0結(jié)合webuploader實現(xiàn)文件分片上傳功能
這篇文章主要介紹了Vue2.0結(jié)合webuploader實現(xiàn)文件分片上傳功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-03-03詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳)
這篇文章主要介紹了詳解vue中使用vue-quill-editor富文本小結(jié)(圖片上傳),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04