-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathLexicalTextVisitor.ts
95 lines (84 loc) · 2.92 KB
/
LexicalTextVisitor.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
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
import { $isTextNode, TextNode } from 'lexical'
import * as Mdast from 'mdast'
import { IS_BOLD, IS_CODE, IS_ITALIC, IS_UNDERLINE } from '../../FormatConstants'
import { LexicalExportVisitor } from '../../exportMarkdownFromLexical'
export function isMdastText(mdastNode: Mdast.Content): mdastNode is Mdast.Text {
return mdastNode.type === 'text'
}
export const LexicalTextVisitor: LexicalExportVisitor<TextNode, Mdast.Text> = {
shouldJoin: (prevNode, currentNode) => {
return ['text', 'emphasis', 'strong', 'mdxJsxTextElement'].includes(prevNode.type) && prevNode.type === currentNode.type
},
join<T extends Mdast.Content>(prevNode: T, currentNode: T) {
if (isMdastText(prevNode) && isMdastText(currentNode)) {
return {
type: 'text',
value: prevNode.value + currentNode.value
} as unknown as T
} else {
return {
...prevNode,
children: [...(prevNode as unknown as Mdast.Parent).children, ...(currentNode as unknown as Mdast.Parent).children]
}
}
},
testLexicalNode: $isTextNode,
visitLexicalNode: ({ lexicalNode, mdastParent, actions }) => {
const previousSibling = lexicalNode.getPreviousSibling()
const prevFormat = $isTextNode(previousSibling) ? previousSibling.getFormat() : 0
const textContent = lexicalNode.getTextContent()
// if the node is only whitespace, ignore the format.
const format = lexicalNode.getFormat() ?? 0
if (format & IS_CODE) {
actions.addAndStepInto('inlineCode', {
value: textContent
})
return
}
let localParentNode = mdastParent
if (prevFormat & format & IS_ITALIC) {
localParentNode = actions.appendToParent(localParentNode, {
type: 'emphasis',
children: []
}) as Mdast.Parent
}
if (prevFormat & format & IS_BOLD) {
localParentNode = actions.appendToParent(localParentNode, {
type: 'strong',
children: []
}) as Mdast.Parent
}
if (prevFormat & format & IS_UNDERLINE) {
localParentNode = actions.appendToParent(localParentNode, {
type: 'mdxJsxTextElement',
name: 'u',
children: [],
attributes: []
}) as Mdast.Parent
}
if (format & IS_ITALIC && !(prevFormat & IS_ITALIC)) {
localParentNode = actions.appendToParent(localParentNode, {
type: 'emphasis',
children: []
}) as Mdast.Parent
}
if (format & IS_BOLD && !(prevFormat & IS_BOLD)) {
localParentNode = actions.appendToParent(localParentNode, {
type: 'strong',
children: []
}) as Mdast.Parent
}
if (format & IS_UNDERLINE && !(prevFormat & IS_UNDERLINE)) {
localParentNode = actions.appendToParent(localParentNode, {
type: 'mdxJsxTextElement',
name: 'u',
children: [],
attributes: []
}) as Mdast.Parent
}
actions.appendToParent(localParentNode, {
type: 'text',
value: textContent
})
}
}