-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
205 lines (188 loc) · 7.06 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const htmlParser = require('htmlparser');
const R = require('ramda');
const { paragraph, styles } = require('./helpers');
const htmlAttrs = {
tag: {
ul: 'unordered-list',
ol: 'ordered-list',
li: 'list-item',
blockquote: 'blockquote',
p: 'paragraph',
h1: 'heading-1',
h2: 'heading-2',
h3: 'heading-3',
h4: 'heading-4',
h5: 'heading-5',
h6: 'heading-6',
hr: 'hr',
br: 'br',
a: 'hyperlink',
b: 'bold',
strong: 'bold',
code: 'text',
i: 'italic',
em: 'italic',
u: 'underline',
img: 'embedded-asset-block',
span: 'text',
table: 'table',
th: 'table-header-cell',
tr: 'table-row',
td: 'table-cell'
},
text: 'text',
};
let transformed = []; //What should come out in the end
const transformDom = (dom) => {
let results = [];
R.forEach((elm) => {
//RTF Tables in Contentful. Contentful currently pass a 'tbody' tag which isn't supported when importing. This removes it from the hierarchy.
if (elm?.children?.[0].name == 'tbody') {
elm.children.splice(0, 1, ...elm.children[0].children);
}
const { type, name, data, attribs, children } = elm;
//console.log(elm);
let content = [];
let newData = {};
if (children) {
content = transformDom(children);
}
if (type === 'text') {
newData = {
data: {},
marks: [],
value: data,
nodeType: type,
};
} else if (type === 'tag') {
switch(name) {
case 'span':
//Spans seem to just be passed through
newData = content;
break;
case 'code':
const Entities = require('html-entities').XmlEntities;
const entities = new Entities();
newData = R.map((node) => {
node = R.assoc('value', entities.decode(node.value), node);
node = R.assoc('marks', R.append({type: 'code'}, node.marks), node);
return node;
}, content);
break;
case 'img':
const fileName = R.last(R.split('/', attribs.src));
newData = {
data: {
target: {
sys: {
space: {},
type: 'Asset',
createdAt: '',
updatedAt: '',
environment: {},
revision: null,
locale: 'en-US',
},
fields: {
title: R.head(R.split('.', fileName)),
description: attribs.alt,
file: {
url: attribs.src,
details: {
size: 46234, //@TODO - don't hardcode
image: {
width: parseInt(attribs.width, 10),
height: parseInt(attribs.height, 10),
},
},
fileName,
contentType: 'image/' + R.last(R.split('.', fileName)),
},
},
},
},
content: [],
nodeType: htmlAttrs[type][name],
};
break
case 'i':
case 'b':
case 'strong':
case 'u':
newData = styles(content, htmlAttrs[type][name]);
break;
case 'a':
newData = {
data: { uri: R.propOr('', 'href', attribs) },
content,
nodeType: htmlAttrs[type][name],
};
break;
case 'li':
//@TODO shouldn't need to cast to an array...
content = R.type(content) === 'Array' ? content : [content];
let newContent = [];
//Seems to want text wrapped in some type of content tag (p, h*, etc)
content = R.forEach((node)=> {
if (node.nodeType === 'text' || node.nodeType === 'hyperlink') {
//if the last of new content isn't a `paragraph`
if (R.propOr(false, 'nodeType', R.last(newContent)) !== 'paragraph') {
newContent = R.concat(newContent, paragraph([], 'paragraph'));
}
//put node in R.last(newContent).content
newContent[newContent.length - 1].content.push(node);
} else {
newContent = R.append(node, newContent);
}
}, content);
newData = {
data: {},
content: newContent,
nodeType: htmlAttrs[type][name],
};
break;
case 'p':
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
newData = paragraph(content, htmlAttrs[type][name])
break
default:
if (!htmlAttrs[type][name]) {
console.log('*** new data needed under -', type, name);
}
newData = {
data: {},
content,
nodeType: htmlAttrs[type][name],
};
break;
}
} else {
console.log('***new type needed -', type, data);
}
results = R.type(newData) === 'Array' ? R.concat(results, newData) : R.append(newData, results);
}, dom);
return results;
};
const handleFn = (error, dom) => {
if (error) {
throw error;
}
transformed = {
data: {},
content: transformDom(dom),
nodeType: 'document',
};
};
const parser = new htmlParser.Parser(new htmlParser.DefaultHandler(handleFn));
const parseHtml = (html) => {
parser.parseComplete(html); //returns undefined...
return transformed;
};
module.exports = {
parseHtml,
};