1
- import { h , PropType , VNode , Component , defineComponent } from 'vue'
2
- import MarkdownIt , { Options as MarkdownItOptions , PluginSimple } from 'markdown-it'
3
- export type { Options } from 'markdown-it'
1
+ import MarkdownIt , {
2
+ Options as MarkdownItOptions ,
3
+ PluginSimple ,
4
+ } from "markdown-it" ;
5
+ import { Component , PropType , computed , defineComponent , h , ref } from "vue" ;
6
+ export type { Options } from "markdown-it" ;
4
7
5
8
const VueMarkdown : Component = defineComponent ( {
6
- name : ' VueMarkdown' ,
9
+ name : " VueMarkdown" ,
7
10
props : {
8
11
source : {
9
12
type : String ,
@@ -16,28 +19,19 @@ const VueMarkdown: Component = defineComponent({
16
19
plugins : {
17
20
type : Array as PropType < PluginSimple [ ] > ,
18
21
required : false ,
19
- }
20
- } ,
21
- data ( ) {
22
- return {
23
- md : null as MarkdownIt | null ,
24
- }
25
- } ,
26
- computed : {
27
- content ( ) : string | undefined {
28
- const src = this . source
29
- return this . md ?. render ( src )
30
22
} ,
31
23
} ,
32
- created ( ) {
33
- this . md = new MarkdownIt ( this . options ?? { } )
34
- for ( const plugin of this . plugins ?? [ ] ) {
35
- this . md . use ( plugin )
24
+ setup ( props ) {
25
+ const md = ref < MarkdownIt > ( new MarkdownIt ( props . options ?? { } ) ) ;
26
+
27
+ for ( const plugin of props . plugins ?? [ ] ) {
28
+ md . value . use ( plugin ) ;
36
29
}
30
+
31
+ const content = computed ( ( ) => md . value . render ( props . source ) ) ;
32
+
33
+ return ( ) => h ( "div" , { innerHTML : content . value } ) ;
37
34
} ,
38
- render ( ) : VNode {
39
- return h ( 'div' , { innerHTML : this . content } )
40
- } ,
41
- } )
35
+ } ) ;
42
36
43
- export default VueMarkdown
37
+ export default VueMarkdown ;
0 commit comments