Files
tra-app/src/components/markdown-preview/markdown-preview.vue
T
2025-12-05 12:11:52 +08:00

79 lines
1.3 KiB
Vue

<template>
<view>
<rich-text :nodes="mdText"></rich-text>
</view>
</template>
<script>
import MarkdownIt from 'markdown-it';
// import DOMPurify from 'dompurify'
export default {
name:"markdown-preview",
props: {
mdString: {
default: '',
type: String
}
},
data() {
return {
contentHtml:'',
mdObj:null
};
},
watch:{
mdString: {
handler(val){
let _text = val.replace(/</g,'&lt;').replace(/>/g,'&gt;')
this.contentHtml = this.parseContent(_text)
},
deep: true,
immediate: true
}
},
computed:{
mdText(){
// let _endtext = DOMPurify.sanitize(this.contentHtml)
return this.contentHtml
}
},
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,li{
word-break: break-all;
line-height: 50rpx;
}
</style>