Files
tra-app/src/components/markdown-preview/markdown-preview.vue
T
2025-07-24 09:37:46 +08:00

70 lines
1.1 KiB
Vue

<template>
<view>
<rich-text :nodes="contentHtml"></rich-text>
</view>
</template>
<script>
import MarkdownIt from 'markdown-it';
export default {
name:"markdown-preview",
props: {
mdString: {
default: '',
type: String
}
},
data() {
return {
contentHtml:'',
mdObj:null
};
},
watch:{
mdString: {
handler(val){
this.contentHtml = this.parseContent(val)
},
deep: true,
immediate: true
}
},
methods:{
parseContent(text){
if(this.mdObj){
return this.mdObj.render(text)
}else{
setTimeout(()=>{
return this.parseContent(text)
},500)
}
}
},
mounted(){
this.mdObj = new MarkdownIt({
html: true,
linkify: true,
typographer: true
})
this.contentHtml = this.parseContent(this.mdString)
}
}
</script>
<style scoped lang="scss">
pre{
background-color: #f4f4f4;
padding: 10px;
border-radius: 5px;
overflow: auto;
}
code{
font-family: monospace;
background-color: #e8e8e8;
padding: 2px 4px;
border-radius: 3px;
}
p,span,div{
word-break: break-all;
}
</style>