-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathvHtml.ts
47 lines (45 loc) · 1019 Bytes
/
vHtml.ts
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
import {
createBindDirectiveNode,
isElementNode,
} from '@dcloudio/uni-cli-shared'
import {
type DirectiveNode,
type ElementNode,
ElementTypes,
NodeTypes,
type PlainElementNode,
findDir,
} from '@vue/compiler-core'
import type { NodeTransform } from '../transform'
export const transformHtml: NodeTransform = (node, _) => {
if (!isElementNode(node)) {
return
}
const dir = findDir(node, 'html')
if (!dir) {
return
}
// remove v-html
node.props.splice(node.props.indexOf(dir), 1)
if (node.tagType !== ElementTypes.ELEMENT) {
return
}
node.isSelfClosing = false
node.children = [createRichText(node, dir)]
}
function createRichText(
node: PlainElementNode,
dir: DirectiveNode
): ElementNode {
return {
tag: 'rich-text',
type: NodeTypes.ELEMENT,
tagType: ElementTypes.ELEMENT,
props: [createBindDirectiveNode('nodes', dir.exp || '')],
isSelfClosing: true,
children: [],
codegenNode: undefined,
ns: node.ns,
loc: node.loc,
}
}