no message
Former-commit-id: 0a96bb54e9270e451e59b72e00e35053a7c0e152 Former-commit-id: c250ba90da6546626b9831847630aae786c88344 Former-commit-id: c1182b1bdc6ea9b3934275286a93e2760a643bfe
This commit is contained in:
78
src/components/core/Markdown/index.vue
Normal file
78
src/components/core/Markdown/index.vue
Normal file
@@ -0,0 +1,78 @@
|
||||
<template>
|
||||
<div class="component-markdown">
|
||||
<div class="spin-group" v-if="!markedHTML">
|
||||
<div>正在加载</div>
|
||||
</div>
|
||||
<div class="markdown-body" v-html="markedHTML"></div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import marked from 'marked'
|
||||
import highlight from 'highlight.js'
|
||||
export default {
|
||||
props: {
|
||||
url: {
|
||||
type: String,
|
||||
required: false,
|
||||
default: ''
|
||||
},
|
||||
md: {
|
||||
type: String,
|
||||
required: false,
|
||||
default: ''
|
||||
}
|
||||
},
|
||||
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)
|
||||
console.log(this.markedHTML)
|
||||
},
|
||||
// 使用 url 初始化
|
||||
async initWithUrl () {
|
||||
this.markedHTML = await this.getReadme(this.url)
|
||||
},
|
||||
// 从 url 加载原始数据
|
||||
async getReadme (name) {
|
||||
const { data } = await this.$axios.get(name)
|
||||
return this.marked(data)
|
||||
},
|
||||
marked (data) {
|
||||
return marked(data, {
|
||||
highlight: (code) => highlight.highlightAuto(code).value
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</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>
|
||||
|
||||
Reference in New Issue
Block a user