-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
95 lines (83 loc) · 3.99 KB
/
helpers.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
/*
* Custom theme helpers for Handlebars.js
*/
let themeHelpers = {
lazyLoadForContentImages: function(postText, lazyLoadEffect = '') {
let modifiedPostText = postText;
// Select all images from the content
modifiedPostText = modifiedPostText.replace(/<img[a-zA-Z0-9\s\"\'\=\-]*?src="(.*?)".*?>/gmi, function(match, url) {
if (match.indexOf('data-is-external-image="true"') > -1) {
return match;
}
if (lazyLoadEffect === 'lqip') {
match = match.replace('src="', 'data-src="');
match = match.replace('srcset="', 'data-srcset="');
if (match.indexOf('class="') > -1) {
match = match.replace('class="', 'class="lazyload ');
} else {
match = match.replace('<img', '<img class="lazyload"');
}
return match;
}
// Create *-xs image path
let image = url.split('.');
if(
url.indexOf('/gallery/') === -1 && (
image[image.length - 1] === 'jpeg' ||
image[image.length - 1] === 'jpg' ||
image[image.length - 1] === 'png' ||
image[image.length - 1] === 'JPEG' ||
image[image.length - 1] === 'JPG' ||
image[image.length - 1] === 'PNG'
)
) {
let xsImage = image.slice(0, image.length - 1).join('.') + '-xs.' + image[image.length - 1];
xsImage = xsImage.split('/');
xsImage[xsImage.length - 2] = xsImage[xsImage.length - 2] + '/responsive';
xsImage = xsImage.join('/');
if (lazyLoadEffect === 'blur') {
// Replace src attribute with *-xs image path
match = match.replace(/src=".*?"/gi, 'src="' + xsImage + '"');
// change srcset to data-srcset
match = match.replace('srcset="', 'data-srcset="');
}
if (lazyLoadEffect === 'fadein') {
// Remove src attribute
match = match.replace(/src=".*?"/gi, 'src="' + xsImage + '"');
// change srcset to data-srcset
match = match.replace('srcset="', 'data-srcset="');
}
// replace sizes with data-sizes
match = match.replace(/sizes=".*?"/i, 'data-sizes="auto"');
// add necessary CSS classes
if(match.indexOf('class="') > -1) {
match = match.replace('class="', 'class="lazyload ');
} else {
match = match.replace('<img', '<img class="lazyload"');
}
}
// return modified <img> tag
return match;
});
// Select all iframes from the content
modifiedPostText = modifiedPostText.replace(/<iframe[a-zA-Z0-9\s\"\'\=\-]*?src="(.*?)".*?>/gmi, function(match, url) {
// Replace src attribute with data-src
match = match.replace('src="', 'data-src="');
// Add class attribute
match = match.replace('<iframe', '<iframe class="lazyload"');
// return modified <iframe> tag
return match;
});
// Select all videos from the content
modifiedPostText = modifiedPostText.replace(/<video[a-zA-Z0-9\s\"\'\=\-]*?src="(.*?)".*?>/gmi, function(match, url) {
// Replace src attribute with data-src
match = match.replace('src="', 'data-src="');
// Add class attribute
match = match.replace('<video', '<video class="lazyload"');
// return modified <video> tag
return match;
});
return modifiedPostText;
}
};
module.exports = themeHelpers;