ant-design-vue/site/components/md.vue

55 lines
1.0 KiB
Vue
Raw Normal View History

2018-04-04 10:39:21 +00:00
<template>
<div v-html="marked(text)" class="markdown" />
</template>
<script>
import marked from 'marked'
import { isZhCN } from '../util'
2018-05-08 03:20:07 +00:00
const renderer = new marked.Renderer()
renderer.heading = function (text, level) {
return '<h' +
level +
' id="' +
text.replace(/[^\w]+/g, '-') +
'">' +
text +
'</h' +
level +
'>\n'
}
2018-04-04 10:39:21 +00:00
marked.setOptions({
2018-04-07 09:38:39 +00:00
renderer,
2018-04-04 10:39:21 +00:00
gfm: true,
tables: true,
breaks: true,
pedantic: true,
sanitize: true,
smartLists: true,
smartypants: true,
})
export default {
name: 'md',
props: {
cn: String,
us: String,
},
2018-07-13 13:55:29 +00:00
inject: {
demoContext: { default: {}},
},
2018-04-04 10:39:21 +00:00
data () {
let text = ''
const { cn, us } = this
if (this.$slots.default && this.$slots.default[0] && this.$slots.default[0].text) {
text = this.$slots.default[0].text
} else {
2018-07-13 13:55:29 +00:00
text = isZhCN(this.demoContext.name) ? cn : us
2018-04-04 10:39:21 +00:00
}
text = text || ''
text = text.split('\n').map(t => t.trim()).join('\n')
return {
marked,
text,
}
},
}
</script>