Files
mes-ui-d2/src/components/d2-markdown/index.vue
liyang 3786b7ae04 lint
Former-commit-id: ecdadbbb5239575790787988dff81e89028e4b21 [formerly ecdadbbb5239575790787988dff81e89028e4b21 [formerly ecdadbbb5239575790787988dff81e89028e4b21 [formerly ecdadbbb5239575790787988dff81e89028e4b21 [formerly faa40145c1044f7d814aca7335e2958ee2af488f [formerly e7a26ae4b2b89f1095af40fabc80b6d7c28a88d7]]]]]
Former-commit-id: cdb18109e6f6405a153c0c77085cb26bfa2866f9
Former-commit-id: 9cd8dafaede8e06c29b9c1b28fbdc2a1ba3789e6
Former-commit-id: 8ee55ec8b9fa61ddc5c04797706db62c07e66fa9 [formerly cf46566d4316a4ac126dba8a16c9155133d6d6a1]
Former-commit-id: 7978ef3eb50dc580a497919fb7360ae382ebe71e
Former-commit-id: e0ae6ead53a9ea798a0746e72181f19c69786d2d
Former-commit-id: 784f3641d01fe1f6705725411e2abe95d0a3fd35
Former-commit-id: 13f3be9e685bd44b2c04b7821f1e737e4eefc7d9
Former-commit-id: 13aaa78b3f2f9808641261666f3c71aeb097dd9e
2018-09-11 16:46:24 +08:00

147 lines
3.2 KiB
Vue

<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'
import bandupan from './plugin/baidupan'
import 'github-markdown-css'
import { ComponentsMarkdownBase } from '@/api/components/markdown'
export default {
name: 'd2-markdown',
props: {
url: {
type: String,
required: false,
default: ''
},
source: {
type: String,
required: false,
default: ''
},
highlight: {
type: Boolean,
required: false,
default: false
},
// 百度网盘分享链接特殊样式
baidupan: {
type: Boolean,
required: false,
default: true
}
},
data () {
return {
getReadmePublicPath: '',
markedHTML: ''
}
},
mounted () {
if (this.url) {
this.initWithUrl()
} else if (this.source) {
this.initWithMd()
} else {
console.log('not mounted init')
}
},
methods: {
// 使用 md 初始化
initWithMd () {
this.markedHTML = this.marked(this.source)
},
// 使用 url 初始化
async initWithUrl () {
this.markedHTML = await this.getReadme(this.url)
},
// 从 url 加载原始数据
async getReadme (url) {
const data = await ComponentsMarkdownBase(url)
return this.marked(data)
},
marked (data) {
const renderer = new marked.Renderer()
renderer.blockquote = (quote) => {
// 百度网盘
return (this.baidupan && bandupan(quote, this.$baseUrl)) || `<blockquote>${quote}</blockquote>`
}
return marked(data, {
...this.highlight ? { highlight: (code) => highlight.highlightAuto(code).value } : {},
renderer
})
}
}
}
</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;
}
}
// 百度云
$baidupanHeight: 30px;
$baidupanPadding: 10px;
.baidupan {
overflow: hidden;
margin-bottom: 16px;
.container {
height: $baidupanHeight + 2 * $baidupanPadding;
border-radius: 4px;
border: 1px solid #dfe2e5;
padding: $baidupanPadding;
float: left;
cursor: pointer;
&:hover {
border: 1px solid $color-primary;
.line {
background-color: $color-primary;
}
}
.icon {
float: left;
height: $baidupanHeight;
text-align: center;
width: 40px;
margin-right: $baidupanPadding;
img {
width: 40px;
}
}
.url {
float: left;
height: $baidupanHeight;
line-height: $baidupanHeight;
color: $color-text-main;
}
.line {
float: left;
height: $baidupanHeight + 2 * $baidupanPadding;
width: 1px;
margin: -$baidupanPadding $baidupanPadding;
background-color: #dfe2e5;
}
.pwd {
float: left;
height: $baidupanHeight;
line-height: $baidupanHeight;
}
}
}
</style>