forked from MilWolf/nodebb-plugin-image-md-size
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.js
More file actions
60 lines (40 loc) · 1.43 KB
/
library.js
File metadata and controls
60 lines (40 loc) · 1.43 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
function getSetString(match, src, width, height) {
if (width === "" && height === "") {
return match;
}
return `<img src="${src}" style="width:${width}px!important; height:${height}px!important;"`;
}
function getPercentString(match, src, percent) {
return `<img src="${src}" style="width:${percent}%!important; height: auto!important;"`;
}
function getWidthString(match, src, width) {
if (width === "") {
return match;
}
return `<img src="${src}" style="width: ${width}px!important; height: auto!important;"`;
}
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;