Vuejs中使用markdown服務(wù)器端渲染的示例
啊哈,又是來推薦一個(gè) vuejs 的 package,miaolz123/vue-markdown。 對應(yīng)的應(yīng)用場景是:你想使用一個(gè)編輯器或者是在評論系統(tǒng)中支持 markdown。這個(gè) package 的有點(diǎn)還是挺多了,比如默認(rèn)就支持 emoji,這個(gè)就很完美啦!laravist 的新版就使用了 vue-markdown 來渲染評論。
安裝
直接使用 npm 來安裝:
npm install --save vue-markdown
使用
也是很簡單的,可以直接這樣:
import VueMarkdown from 'vue-markdown'
new Vue({
components: {
VueMarkdown
}
})
或者是這樣,舉一個(gè)具象化的例子是:比如我們有一個(gè) Comment.vue 組件用來渲染評論,可以在這個(gè)組件中直接指明:
import VueMarkdown from 'vue-markdown';
<template>
<div>
<vue-markdown :source="comment.body"></vue-markdown>
</div>
</template>
export default { // ... other codes
props:['comment'],
data(){
return {
comment : this.comment
}
},
components: {
VueMarkdown
},
// ... other codes
}
然后在渲染的時(shí)候這個(gè):
<div class="comments"> <div class="comments" v-for="comment in comments"> <comment :comment="comment"> </comment> </div> </div>
這里我們首先通過 comment props 傳入整個(gè) comment(這個(gè)comment其實(shí)就是一個(gè)對象) ,然后在 Comment.vue 通過 :source 來給 veu-markdown 組件傳入每個(gè)評論的 body 字段內(nèi)容,注意這里的 comment.body 在數(shù)據(jù)庫中保存的就是評論的 markdown 格式的內(nèi)容。
Vuejs服務(wù)器端渲染markdown示例
const Koa = require('koa');
const _ = require('koa-route');
const vsr = require('vue-server-renderer');
const fs = require('fs');
const indexjs = require('./component/index.js');
const Vue = require('vue');
const MD = require('markdown-it')
const server = new Koa();
const route = {
index: (ctx, id) => {
// 解析markdown
const md = new MD().render(fs.readFileSync('./markdown/' + id + '.md', 'utf-8'));
// vue插入html代碼
const app = new Vue({
data: {
main: md
},
template: `
<div>
<div class="main" v-html="main"></div>
</div>`
});
// 其他變量設(shè)置
const context = {
htmlTitle: id
};
// 加載模板html文件
const renderer = vsr.createRenderer({
template: fs.readFileSync('./template/index.template.html', 'utf-8')
});
// 渲染
renderer.renderToString(app, context, (err, html) => {
if (err) {
ctx.response.status = 500;
} else {
ctx.response.body = html;
}
})
}
};
server.use(_.get('/post/:id', route.index));
server.listen(8080);
<!DOCTYPE html>
<html lang="CH-ZN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{htmlTitle}}</title>
</head>
<body>
<!--vue-ssr-outlet-->
</body>
</html>
總結(jié)
本文介紹的 vue-markdown 在某些應(yīng)用場景中其實(shí)超級好用,特別是對于評論系統(tǒng)想支持 markdown 這個(gè)需求來說,容易集成,優(yōu)點(diǎn)多多。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- nodejs+axios爬取html出現(xiàn)中文亂碼并解決示例
- js+html+css實(shí)現(xiàn)簡單電子時(shí)鐘
- 使用Javascript在HTML中顯示實(shí)時(shí)時(shí)間
- JS+HTML實(shí)現(xiàn)經(jīng)典游戲吃豆人
- JavaScript markdown 編輯器實(shí)現(xiàn)雙屏同步滾動(dòng)
- JavaScript+Node.js寫一款markdown解析器
- js正則匹配markdown里的圖片標(biāo)簽的實(shí)現(xiàn)
- JS加載解析Markdown文檔過程詳解
- 一文詳解JavaScript?如何將?HTML?轉(zhuǎn)成?Markdown
相關(guān)文章
詳解Vue.js在頁面加載時(shí)執(zhí)行某個(gè)方法
這篇文章主要介紹了詳解Vue.js在頁面加載時(shí)執(zhí)行某個(gè)方法的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
vue3.x 使用jsplumb實(shí)現(xiàn)拖拽連線
這篇文章主要為大家詳細(xì)介紹了vue3.x 使用jsplumb實(shí)現(xiàn)拖拽連線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue3源碼分析偵聽器watch的實(shí)現(xiàn)原理
watch?的本質(zhì)就是觀測一個(gè)響應(yīng)式數(shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生變化時(shí)通知并執(zhí)行相應(yīng)的回調(diào)函數(shù)。watch的實(shí)現(xiàn)利用了effect?和?options.scheduler?選項(xiàng),這篇文章主要介紹了Vue3源碼分析偵聽器watch的實(shí)現(xiàn)原理,需要的朋友可以參考下2022-08-08
vue.js路由mode配置之去掉url上默認(rèn)的#方法
今天小編就為大家分享一篇vue.js路由mode配置之去掉url上默認(rèn)的#方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue監(jiān)聽頁面變化的實(shí)現(xiàn)方法小結(jié)
在Vue.js應(yīng)用開發(fā)過程中,監(jiān)聽頁面變化是一個(gè)非常常見的需求,無論是為了響應(yīng)用戶交互、優(yōu)化性能,還是實(shí)現(xiàn)復(fù)雜的業(yè)務(wù)邏輯,監(jiān)聽頁面變化的能力都是不可或缺的,本文將詳細(xì)介紹如何在Vue項(xiàng)目中實(shí)現(xiàn)頁面變化監(jiān)聽,需要的朋友可以參考下2024-10-10
利用vue3仿蘋果系統(tǒng)側(cè)邊消息提示效果實(shí)例
這篇文章主要給大家介紹了關(guān)于如何利用vue3仿蘋果系統(tǒng)側(cè)邊消息提示效果的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-12-12
Vue3動(dòng)態(tài)使用KeepAlive組件的實(shí)現(xiàn)步驟
在 Vue 3 項(xiàng)目中,我們有時(shí)需要根據(jù)路由的 meta 信息來動(dòng)態(tài)決定是否使用 KeepAlive 組件,以控制組件的緩存行為,所以本文給大家介紹了Vue3動(dòng)態(tài)使用KeepAlive組件的實(shí)現(xiàn)步驟,通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-11-11
vue項(xiàng)目打包發(fā)布后接口報(bào)405錯(cuò)誤的解決
這篇文章主要介紹了vue項(xiàng)目打包發(fā)布后接口報(bào)405錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

