This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
275 lines (224 loc) · 6.66 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* © Copyright [2015] Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var HTMLtoJSX = require('htmltojsx');
var loaderUtils = require('loader-utils');
var jsdom = require('jsdom-no-contextify').jsdom;
var defaultView = jsdom().defaultView;
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
};
function createElement(tag) {
return defaultView.document.createElement(tag);
}
function isEmpty(string) {
return !/[^\s]/.test(string);
}
var ATTRIBUTE_MAPPING = {
'classname': 'className',
'activestyle': 'activeStyle',
'htmlfor': 'htmlFor'
};
var TagToReactRouter = function() {
return {
reset: function() {
this.output = '';
this.routerLink = false;
},
oneLevelOnly: function(element) {
//based on https://github.com/reactjs/react-magic/blob/master/src/htmltojsx.js#L253
// Only a single child element
if (
element.childNodes.length === 1
&& element.childNodes[0].nodeType === 1
) {
return true;
}
// Only one element, and all other children are whitespace
var foundElement = false;
for (var i = 0, count = element.childNodes.length; i < count; i++) {
var child = element.childNodes[i];
if (child.nodeType === 1) {
if (foundElement) {
// Encountered an element after already encountering another one
// Therefore, more than one element at root level
return false;
} else {
foundElement = true;
}
} else if (child.nodeType === 3 && !isEmpty(child.textContent)) {
// Contains text content
return false;
}
}
return true;
},
parse: function(element) {
this.reset();
var wrapper = createElement('div');
wrapper.innerHTML = element;
if (this.oneLevelOnly(wrapper)) {
this.traverse(wrapper);
} else {
this.visit(wrapper);
}
return this.output;
},
traverse: function(element) {
for (var i = 0, count = element.childNodes.length; i < count; i++) {
this.visit(element.childNodes[i]);
}
},
visit: function(element) {
this.begin(element);
this.traverse(element);
this.end(element);
},
handleText: function(element) {
var text = element.textContent;
if (this.routerLink) {
text = text.replace(/\[/g, '{').replace(/]/g, '}');
}
var tempEl = createElement('div');
tempEl.textContent = text;
this.output += tempEl.innerHTML;
},
beginNode: function(node) {
var tagName = this.routerLink ? 'Link' : node.tagName.toLowerCase();
var attributes = [];
for (var i = 0, count = node.attributes.length; i < count; i++) {
var value = node.attributes[i].value;
if (value.indexOf('[') === -1) {
value = '"' + value + '"';
} else {
value = '{' + value.replace(/\[/g, '{').replace(/\]/g, '}') + '}';
}
var name = node.attributes[i].name;
if (this.routerLink) {
name = name.replace(/data-/g, '');
}
for (var key in ATTRIBUTE_MAPPING) {
if (ATTRIBUTE_MAPPING.hasOwnProperty(key)) {
name = name.replace(key, ATTRIBUTE_MAPPING[key]);
}
}
attributes.push(name + '=' + value);
}
this.output += '<' + tagName;
if (attributes.length > 0) {
this.output += ' ' + attributes.join(' ');
}
if (node.firstChild) {
this.output += '>';
}
},
begin: function(node) {
switch (node.nodeType) {
case 1:
if (node.tagName === 'A' && node.getAttribute('data-to')) {
this.routerLink = true;
}
this.beginNode(node);
break;
case 3:
this.handleText(node);
break;
}
},
end: function(node) {
if (node.nodeType === 1) {
var tagName = this.routerLink ? 'Link' : node.tagName.toLowerCase();
this.routerLink = false;
if (node.firstChild) {
this.output += '</' + tagName + '>';
} else {
this.output += ' />';
}
}
}
};
};
function createReactComponent(content) {
var converter = new HTMLtoJSX({
createClass: false
});
var indent = ' ';
var output = [
'React.createClass({\n',
indent,
'render: function() {\n',
indent + indent,
'return (\n',
converter.convert(new TagToReactRouter().parse(content)),
');\n',
'}\n',
'})\n'
].join('');
return output;
}
function getGroupedElements(content) {
var wrapperEl = createElement('div');
wrapperEl.innerHTML = content;
var elements = wrapperEl.getElementsByTagName('*');
var groupedElements = {};
for (var i = 0; i < elements.length;) {
var wrapper = createElement('div');
wrapper.appendChild(elements[i]);
var tag = elements[i].tagName;
if (groupedElements[tag]) {
groupedElements[tag].push(wrapper.innerHTML);
} else {
groupedElements[tag] = [wrapper.innerHTML];
}
}
return groupedElements;
}
function replaceVariables(content, query) {
var newContent = content;
for (var key in query) {
if (query.hasOwnProperty(key)) {
if (key.indexOf('__') === 0) {
var re = new RegExp(key, 'g');
newContent = newContent.replace(re, query[key]);
}
}
}
return newContent;
}
module.exports = function(content) {
var query = loaderUtils.parseQuery(this.query);
content = replaceVariables(content, query);
var output;
if (query.group) {
var groupedElements = getGroupedElements(content);
var tmp = '{';
var index = 0;
for (var key in groupedElements) {
if (groupedElements.hasOwnProperty(key)) {
var elements = groupedElements[key];
tmp += [key.capitalize(), ':', createReactComponent(elements.join(''))].join('');
if (index < Object.keys(groupedElements).length - 1) {
tmp += ',';
}
index++;
}
}
tmp += '};';
output = tmp;
} else {
output = createReactComponent(content) + ';';
}
return 'module.exports = ' + output;
};