This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
/
importer.js
131 lines (123 loc) 路 3.16 KB
/
importer.js
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
import { convertFromHTML } from 'draft-convert';
import { Inline, Block, Entity as EntityType } from './util/constants';
export const htmlToStyle = (nodeName, node, currentStyle) => {
switch (nodeName) {
case 'em':
return currentStyle.add(Inline.ITALIC);
case 'strong':
return currentStyle.add(Inline.BOLD);
case 'strike':
return currentStyle.add(Inline.STRIKETHROUGH);
case 'u':
return currentStyle.add(Inline.UNDERLINE);
case 'span':
if (node.className === `md-inline-${Inline.HIGHLIGHT.toLowerCase()}`) {
return currentStyle.add(Inline.HIGHLIGHT);
}
break;
case 'code':
return currentStyle.add(Inline.CODE);
default:
break;
}
return currentStyle;
};
export const htmlToEntity = (nodeName, node, createEntity) => {
if (nodeName === 'a') {
return createEntity(EntityType.LINK, 'MUTABLE', { url: node.href });
}
return undefined;
};
export const htmlToBlock = (nodeName, node) => {
if (nodeName === 'h1') {
return {
type: Block.H1,
data: {},
};
} else if (nodeName === 'h2') {
return {
type: Block.H2,
data: {},
};
} else if (nodeName === 'h3') {
return {
type: Block.H3,
data: {},
};
} else if (nodeName === 'h4') {
return {
type: Block.H4,
data: {},
};
} else if (nodeName === 'h5') {
return {
type: Block.H5,
data: {},
};
} else if (nodeName === 'h6') {
return {
type: Block.H6,
data: {},
};
} else if (nodeName === 'p' && (
node.className === `md-block-${Block.CAPTION.toLowerCase()}` ||
node.className === `md-block-${Block.BLOCKQUOTE_CAPTION.toLowerCase()}`
)) {
return {
type: Block.BLOCKQUOTE_CAPTION,
data: {},
};
} else if (nodeName === 'figure') {
if (node.className.match(/^md-block-image/)) {
const imageNode = node.querySelector('img');
return {
type: Block.IMAGE,
data: {
src: imageNode && imageNode.src,
},
};
} else if (node.className === `md-block-${Block.ATOMIC.toLowerCase()}`) {
return {
type: Block.ATOMIC,
data: {},
};
}
return undefined;
} else if (nodeName === 'div' && node.className && node.className.match(/^md-block-todo/)) {
const inputNode = node.querySelector('input');
return {
type: Block.TODO,
data: {
checked: inputNode && inputNode.checked,
},
};
} else if (nodeName === 'hr') {
return {
type: Block.BREAK,
data: {},
};
} else if (nodeName === 'blockquote') {
return {
type: Block.BLOCKQUOTE,
data: {},
};
} else if (nodeName === 'p') {
return {
type: Block.UNSTYLED,
data: {},
};
} else if (['div', 'pre'].includes(nodeName) && node.className === 'md-block-code-container') {
return {
type: Block.CODE,
data: {},
};
}
return undefined;
};
export const options = {
htmlToStyle,
htmlToEntity,
htmlToBlock,
};
export const setImportOptions = (htmlOptions = options) => convertFromHTML(htmlOptions);
export default (rawHTML, htmlOptions = options) => convertFromHTML(htmlOptions)(rawHTML);