generated from Deuscx/starter-vue-component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdown.tsx
151 lines (131 loc) · 4.06 KB
/
Markdown.tsx
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { toJsxRuntime } from 'hast-util-to-jsx-runtime'
import remarkParse from 'remark-parse'
import type { Options as RehypeOptions } from 'remark-rehype'
import remarkRehype from 'remark-rehype'
import type { PluggableList } from 'unified'
import { unified } from 'unified'
import type { Visitor } from 'unist-util-visit'
import { visit } from 'unist-util-visit'
import { Fragment, defineComponent, h } from 'vue'
const emptyRemarkRehypeOptions = { allowDangerousHtml: true }
const MarkdownProps = {
content: {
type: String,
required: true,
},
class: {
type: String,
default: '',
},
rehypePlugins: {
type: Object as PropType<PluggableList>,
default: () => [],
},
remarkPlugins: {
type: Object as PropType<PluggableList>,
default: () => [],
},
remarkRehypeOptions: {
type: Object as PropType<RehypeOptions>,
default: () => {},
},
skipHtml: {
type: Boolean,
default: false,
},
}
export default defineComponent({
props: MarkdownProps,
setup(props, { slots }) {
const _components = Object.keys(slots)
.reduce<Record<string, any>>((acc, key) => {
const originSlot = slots[key]
const originSlotRender = typeof originSlot === 'function' ? originSlot : null
switch (key) {
case 'pre': {
acc[key] = (props: any) => {
const code = props.node?.children[0]?.children[0]?.value
if (originSlotRender)
return originSlotRender({ code })
}
break
}
default:
if (originSlotRender) {
acc[key] = (props: any) => {
return originSlotRender(props)
}
}
}
return acc
}, {})
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
function jsx(type, props, key) {
const { children } = props
delete props.children
if (arguments.length > 2)
props.key = key
let child = children
// if children is string wrap in array
if (typeof children === 'string')
child = [children]
// if type is component wrap children in a function
// https://stackoverflow.com/questions/69875273/non-function-value-encountered-for-default-slot-in-vue-3-composition-api-comp
if (typeof type === 'function' && children)
child = () => children
return h(type, props, child)
}
const remarkRehypeOptions = props.remarkRehypeOptions
? { ...props.remarkRehypeOptions, ...emptyRemarkRehypeOptions }
: emptyRemarkRehypeOptions
const processor = computed(() =>
unified()
.use(remarkParse)
.use(props.remarkPlugins)
.use(remarkRehype, remarkRehypeOptions)
.use(props.rehypePlugins), {
})
return () => {
const content = props.content || ''
const className = props.class
const skipHtml = props.skipHtml
const mdastTree = processor.value.parse(content)
/** @type {Nodes} */
let hastTree = processor.value.runSync(mdastTree)
// Wrap in `div` if there’s a class name.
if (className) {
hastTree = {
type: 'element',
tagName: 'div',
properties: { className },
// Assume no doctypes.
children: /** @type {Array<ElementContent>} */ (
hastTree.type === 'root' ? hastTree.children : [hastTree]
),
} as any
}
const transform: Visitor = (node, index, parent) => {
if (node.type === 'raw' && parent && typeof index === 'number') {
if (skipHtml)
parent.children.splice(index, 1)
else
parent.children[index] = { type: 'text', value: (node as any).value } as any
return index
}
}
visit(hastTree, transform)
const vnode = toJsxRuntime(hastTree, {
Fragment,
components: _components,
ignoreInvalidStyle: true,
jsx,
jsxs: jsx,
passKeys: true,
passNode: true,
elementAttributeNameCase: 'html',
})
return vnode
}
},
})