You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
< template >
< div class = "markdown-body" v-html ="compiledMarkdown" />
</template>
<script setup lang="ts" >
import { ref , watch } from ' vue '
import { marked } from ' marked '
import { markedHighlight } from ' marked -highlight '
import hljs from ' highlight.js '
import ' highlight.js / styles / github.css ' / / 可 替 换 为 其 他 主 题 , 例 如 ' atom -one -dark .css '
/ / props
const props = defineProps < {
content : string
} > ( )
const compiledMarkdown = ref ( '' )
// 配置 marked 的高亮插件
marked . use (
markedHighlight ( {
langPrefix : 'hljs language-' ,
highlight ( code : string , lang : string ) : string {
if ( lang && hljs . getLanguage ( lang ) ) {
return hljs . highlight ( code , { language : lang } ) . value
}
return hljs . highlightAuto ( code ) . value
}
} )
)
// 渲染 Markdown
async function renderMarkdown ( ) {
compiledMarkdown . value = await marked . parse ( props . content || '' )
}
// 监听 props.content
watch ( ( ) => props . content , renderMarkdown , { immediate : true } )
< / script >
< style >
. markdown - body {
font - size : 16 px ;
line - height : 1.8 ;
word - break : break - word ;
color : # 1 a1a1a ;
h1 , h2 , h3 , h4 {
font - weight : bold ;
margin : 1.2 em 0 0.6 em ;
}
h1 { font - size : 1.8 em ; }
h2 { font - size : 1.5 em ; }
h3 { font - size : 1.2 em ; }
a {
color : # 0366 d6 ;
text - decoration : none ;
}
a : hover {
text - decoration : underline ;
}
code {
background : # f6f8fa ;
padding : 0.2 em 0.4 em ;
border - radius : 4 px ;
font - family : 'Courier New' , Courier , monospace ;
}
pre {
background : # f6f8fa ;
padding : 1 em ;
border - radius : 6 px ;
overflow - x : auto ;
}
blockquote {
border - left : 4 px solid # dfe2e5 ;
padding - left : 1 em ;
color : # 6 a737d ;
margin : 1 em 0 ;
}
ul , ol {
padding - left : 2 em ;
margin : 0.5 em 0 ;
}
img {
max - width : 100 % ;
height : auto ;
border - radius : 6 px ;
margin : 0.5 em 0 ;
}
table {
border - collapse : collapse ;
width : 100 % ;
margin : 1 em 0 ;
}
th , td {
border : 1 px solid # dfe2e5 ;
padding : 0.6 em 1 em ;
}
th {
background - color : # f6f8fa ;
}
}
< / style >