2018-01-14 22:51:12 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="component-markdown">
|
|
|
|
|
<div class="spin-group" v-if="!markedHTML">
|
|
|
|
|
<div>正在加载</div>
|
|
|
|
|
</div>
|
2018-02-24 17:22:05 +08:00
|
|
|
<img src="/static/image/baidu-pan-logo.png">
|
2018-01-14 22:51:12 +08:00
|
|
|
<div class="markdown-body" v-html="markedHTML"></div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import marked from 'marked'
|
|
|
|
|
import highlight from 'highlight.js'
|
2018-02-24 16:43:48 +08:00
|
|
|
import bandupan from './plugin/baidupan'
|
2018-01-14 22:51:12 +08:00
|
|
|
export default {
|
|
|
|
|
props: {
|
|
|
|
|
url: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: false,
|
|
|
|
|
default: ''
|
|
|
|
|
},
|
|
|
|
|
md: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: false,
|
|
|
|
|
default: ''
|
2018-02-15 23:24:31 +08:00
|
|
|
},
|
|
|
|
|
highlight: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
required: false,
|
|
|
|
|
default: false
|
2018-02-24 16:28:13 +08:00
|
|
|
},
|
|
|
|
|
// 百度网盘分享链接特殊样式
|
|
|
|
|
baidupan: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
required: false,
|
|
|
|
|
default: true
|
2018-01-14 22:51:12 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
data () {
|
|
|
|
|
return {
|
|
|
|
|
getReadmePublicPath: '',
|
|
|
|
|
markedHTML: ''
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted () {
|
|
|
|
|
if (this.url) {
|
|
|
|
|
this.initWithUrl()
|
|
|
|
|
} else if (this.md) {
|
|
|
|
|
this.initWithMd()
|
|
|
|
|
} else {
|
|
|
|
|
console.log('not mounted init')
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
// 使用 md 初始化
|
|
|
|
|
initWithMd () {
|
|
|
|
|
this.markedHTML = this.marked(this.md)
|
|
|
|
|
},
|
|
|
|
|
// 使用 url 初始化
|
|
|
|
|
async initWithUrl () {
|
|
|
|
|
this.markedHTML = await this.getReadme(this.url)
|
|
|
|
|
},
|
|
|
|
|
// 从 url 加载原始数据
|
|
|
|
|
async getReadme (name) {
|
2018-01-28 23:22:25 +08:00
|
|
|
const data = await this.$axios.get(name)
|
2018-01-14 22:51:12 +08:00
|
|
|
return this.marked(data)
|
|
|
|
|
},
|
|
|
|
|
marked (data) {
|
2018-02-24 16:33:55 +08:00
|
|
|
const renderer = new marked.Renderer()
|
|
|
|
|
renderer.blockquote = (quote) => {
|
2018-02-24 17:22:05 +08:00
|
|
|
return bandupan(quote) || `<blockquote>${quote}</blockquote>`
|
2018-02-24 16:33:55 +08:00
|
|
|
}
|
2018-01-14 22:51:12 +08:00
|
|
|
return marked(data, {
|
2018-02-24 16:33:55 +08:00
|
|
|
...this.highlight ? {highlight: (code) => highlight.highlightAuto(code).value} : {},
|
|
|
|
|
renderer
|
2018-01-14 22:51:12 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
|
@import '~@/assets/style/public.scss';
|
|
|
|
|
.component-markdown {
|
|
|
|
|
.spin-group {
|
|
|
|
|
height: 100px;
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
align-items: center;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
color: $color-primary;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|