-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markdown.vue
51 lines (50 loc) · 1.65 KB
/
Markdown.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<script>
import Slider from './Slider'
import { markdownToAst } from './markdown.js'
import katex from 'katex'
import 'katex/dist/katex.min.css'
export default {
props: {
markdown: {
type: String
}
},
methods: {
astNodeToVueNode(createElement, astNode) {
switch (astNode.type) {
case 'paragraph':
return createElement(
'p',
astNode.children.map(
this.astNodeToVueNode.bind(
this, createElement)))
case 'text':
return astNode.content
case 'link':
return createElement(
'a',
{ domProps: { href: astNode.href } },
astNode.children.map(
this.astNodeToVueNode.bind(
this, createElement)))
case 'slider':
return createElement(
Slider,
{ props: { images: astNode.images } }
)
case 'katex':
return createElement('span', {
class: 'katex',
domProps: { innerHTML: katex.renderToString(astNode.content) } })
}
},
},
render(createElement) {
const ast = markdownToAst(this.markdown)
return createElement(
'div',
{ class: 'markdown' },
ast.map(this.astNodeToVueNode.bind(this, createElement)))
}
}
</script>