Vue單頁(yè)面應(yīng)用中實(shí)現(xiàn)Markdown渲染
之前渲染 Markdown 的時(shí)候, 筆者使用的是 mavonEditor 的預(yù)覽模式, 使用起來(lái)比較爽, 只需要引入組件即可, 但是在最近的開(kāi)發(fā)中, 遇到了困難.
主要問(wèn)題在于作為單頁(yè)面應(yīng)用, 站內(nèi)鏈接必須是使用 router-link 跳轉(zhuǎn), 如果使用 mavonEditor 默認(rèn)渲染的 a 標(biāo)簽, 就會(huì)重新加載頁(yè)面, 用戶(hù)體驗(yàn)較差.
動(dòng)態(tài)渲染
想要實(shí)現(xiàn)在前端動(dòng)態(tài)地根據(jù)用戶(hù)內(nèi)容渲染router-link , 需要使用動(dòng)態(tài)渲染, 根據(jù) 官方文檔, 直接修改vue.config.js 即可:
// vue.config.js
module.exports = {
runtimeCompiler: true
}
渲染 Markdown
筆者使用的是 markdown-it, 配置過(guò)程如下:
安裝
npm install markdown-it --save # 本體 npm install markdown-it-highlightjs --save # 代碼高亮 npm install markdown-it-katex --save # latex 支持
這里還另外安裝了兩個(gè)語(yǔ)法插件, 如果有其他需要的話(huà), 可以在 npm 上搜索
靜態(tài)文件導(dǎo)入
highlight.js
通過(guò) cdn 導(dǎo)入, 在 index.html 中加入:
<link rel="stylesheet" rel="external nofollow" > <script src="http://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.5.0/build/highlight.min.js"></script>
github-markdown-css
markdown 的樣式
安裝
npm install github-markdown-css --save
導(dǎo)入
在 main.js 文件中添加
import 'github-markdown-css/github-markdown.css'
katex
通過(guò) cdn 導(dǎo)入, 在 index.html 中加入:
<link rel="stylesheet" rel="external nofollow" >
使用
首先在 components 目錄下創(chuàng)建 Markdown.vue 文件,
<template>
<components :is="html" class="markdown-body"></components>
</template>
<script>
import MarkdownIt from 'markdown-it'
import hljs from 'markdown-it-highlightjs'
import latex from 'markdown-it-katex'
export default {
name: 'Markdown',
props: {
content: String
},
data: () => ({
md: null
}),
computed: {
// 使用 computed 才能在動(dòng)態(tài)綁定時(shí)動(dòng)態(tài)更新
html: function () {
let res = this.md.render(this.content)
// 使用正則表達(dá)式將站內(nèi)鏈接替換為 router-link 標(biāo)簽
res = res.replace(/<a href="(?!http:\/\/|https:\/\/)(.*?)" rel="external nofollow" >(.*?)<\/a>/g, '<router-link to="$1">$2</router-link>')
// 使用正則表達(dá)式將站外鏈接在新窗口中打開(kāi)
res = res.replace(/<a href="(.*?)" rel="external nofollow" >(.*?)<\/a>/g, '<a href="$1" rel="external nofollow" target="_blank">$2</a>')
return {
template: '<div>' + res + '</div>'
}
}
},
created () {
this.md = new MarkdownIt()
this.md.use(hljs).use(latex)
}
}
</script>
然后在想使用的地方導(dǎo)入即可
<template>
<div>
<Markdown :content="content"/>
</div>
</template>
<script>
import Markdown from '@/components/Markdown.vue'
export default {
name: 'Home',
components: {
Markdown
},
data: () => ({
content: ''
}),
created () {
this.content = '# 測(cè)試'
}
}
</script>
以上就是Vue單頁(yè)面應(yīng)用中實(shí)現(xiàn)Markdown渲染的詳細(xì)內(nèi)容,更多關(guān)于vue Markdown渲染的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue使用echarts實(shí)現(xiàn)三維圖表繪制
這篇文章主要為大家詳細(xì)介紹了vue如何在項(xiàng)目中使用echarts實(shí)現(xiàn)三維圖表的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-08-08
vue設(shè)置導(dǎo)航欄、側(cè)邊欄為公共頁(yè)面的例子
今天小編就為大家分享一篇vue設(shè)置導(dǎo)航欄、側(cè)邊欄為公共頁(yè)面的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Vue express鑒權(quán)零基礎(chǔ)入門(mén)
這篇文章主要介紹了詳解express鑒權(quán),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Vue項(xiàng)目中安裝使用axios全過(guò)程
這篇文章主要介紹了Vue項(xiàng)目中安裝使用axios全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
如何解決前端上傳Formdata中的file為[object?Object]的問(wèn)題
文件上傳是Web開(kāi)發(fā)中常見(jiàn)的功能之一,下面這篇文章主要給大家介紹了關(guān)于如何解決前端上傳Formdata中的file為[object?Object]問(wèn)題的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07

