php解析markdown前端渲染,Vuejs使用 vue-markdown 来渲染评论方法

如果你想使用一个编辑器或者是在评论系统中支持 markdown。这个 package 的有点还是挺多了,比如默认就支持 emoji,这个就很完美啦!laravist 的新版就使用了 vue-markdown 来渲染评论。本文主要介绍了Vuejs 中使用 markdown的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望能帮助到大家。

安装

直接使用 npm 来安装:npm install --save vue-markdown

使用

也是很简单的,可以直接这样:import VueMarkdown from 'vue-markdown'

new Vue({

components: {

VueMarkdown

}

})

或者是这样,举一个具象化的例子是:比如我们有一个 Comment.vue 组件用来渲染评论,可以在这个组件中直接指明:import VueMarkdown from 'vue-markdown';

export default { // ... other codes

props:['comment'],

data(){

return {

comment : this.comment

}

},

components: {

VueMarkdown

},

// ... other codes

}

然后在渲染的时候这个:

这里我们首先通过 comment props 传入整个 comment(这个comment其实就是一个对象) ,然后在 Comment.vue 通过 :source 来给 veu-markdown 组件传入每个评论的 body 字段内容,注意这里的 comment.body 在数据库中保存的就是评论的 markdown 格式的内容。

Vuejs服务器端渲染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: `

});

// 其他变量设置

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);

{{htmlTitle}}

总结

本文介绍的 vue-markdown 在某些应用场景中其实超级好用,特别是对于评论系统想支持 markdown 这个需求来说,容易集成,优点多多。希望对大家的学习有所帮。

相关推荐:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值