-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibrary.js
More file actions
63 lines (40 loc) · 1.46 KB
/
library.js
File metadata and controls
63 lines (40 loc) · 1.46 KB
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
function getSetString(match, src, width, height) {
if (width === "" && height === "") {
return match;
}
return "<img src=\"" + src + "\" width=\"" + width + "\" height=\"" + height + "\"" + " style=\"height:" + height + "px;\"";
}
function getPercentString(match, src, percent) {
return "<img src=\"" + src + "\" width=\"" + percent + "%\"" + " height=\"" + 'auto' + "\"";
}
function getWidthString(match, src, width) {
if (width === "") {
return match;
}
return "<img src=\"" + src + "\" width=\"" + width + "\" height=\"" + 'auto' + "\"";
}
function replaceContent(data) {
percentRegex = /<img src="([^@]*)@([0-9]+)%(25)?"/g;
absoluteRegex = /<img src="([^@]*)@([0-9]*)x([0-9]*)"/g;
multiplyRegex = /<img src="([^@]*)@([0-9]*\.?[0-9]*)"/g;
var newData = data;
newData = newData.replace(percentRegex, getPercentString);
newData = newData.replace(multiplyRegex, getWidthString);
newData = newData.replace(absoluteRegex, getSetString);
return newData;
}
var ImageSizer = {
sizeImages: function(data, callback) {
if (data && data.postData && data.postData.content) {
data.postData.content = replaceContent(data.postData.content);
}
callback(null, data);
},
sizeImagesInComposerPreview: function(data, callback) {
if (data) {
data = replaceContent(data);
}
callback(null, data);
}
};
module.exports = ImageSizer;