Comments (2)
'use strict';
const fs = require('hexo-fs');
function lazyProcess(htmlContent) {
const sourceImgKey = 'data-original';
const imgRegExp = /<img(\s*?)src="(.*?)"(.*?)>/gi;
let defaultImagePath = __dirname + '/default-image.json';
let loadingImage = this.config.lazyload.loadingImg;
if (!loadingImage) {
loadingImage = JSON.parse(fs.readFileSync(defaultImagePath)).default;
}
return htmlContent.replace(/<img(\s*?)src="(.*?)"(.*?)>/gi, function (str, p1, p2) {
// might be duplicate
if(/data-original/gi.test(str)){
return str;
}
return str.replace(p2, loadingImage + '" data-original="' + p2);
});
}
module.exports.processPost = function (data) {
data.content = lazyProcess.call(this, data.content);
return data;
};
module.exports.processSite = function (htmlContent) {
return lazyProcess.call(this, htmlContent);
};
在 hexo-lazyload-image/lib/process.js 的第20行添加如下代码
// 对于拥有 un-lazy属性的 img 跳过懒加载
if (/un-lazy/gi.test(str)) {
return str;
}
//对于base64地址的图片跳过懒加载
if (/src="data:image\/(png|jpg|jpeg|gif);base64,(.*?)/gi.test(str)) {
return str;
}
修改后的代码如下
'use strict';
const fs = require('hexo-fs');
function lazyProcess(htmlContent) {
const sourceImgKey = 'data-original';
const imgRegExp = /<img(\s*?)src="(.*?)"(.*?)>/gi;
let defaultImagePath = __dirname + '/default-image.json';
let loadingImage = this.config.lazyload.loadingImg;
if (!loadingImage) {
loadingImage = JSON.parse(fs.readFileSync(defaultImagePath)).default;
}
return htmlContent.replace(/<img(\s*?)src="(.*?)"(.*?)>/gi, function (str, p1, p2) {
// might be duplicate
if (/data-original/gi.test(str)) {
return str;
}
// 对于拥有 un-lazy属性的 img 跳过懒加载
if (/un-lazy/gi.test(str)) {
return str;
}
//对于base64地址的图片跳过懒加载
if (/src="data:image\/(png|jpg|jpeg|gif);base64,(.*?)/gi.test(str)) {
return str;
}
return str.replace(p2, loadingImage + '" data-original="' + p2);
});
}
module.exports.processPost = function (data) {
data.content = lazyProcess.call(this, data.content);
return data;
};
module.exports.processSite = function (htmlContent) {
return lazyProcess.call(this, htmlContent);
};
from hexo-lazyload-image.
@hvnobug 建议不错!两个部分都已经添加进去了
from hexo-lazyload-image.
Related Issues (20)
- 一个小小的建议 HOT 3
- I think the braces dependency needs to be updated. HOT 1
- 与Fancybox同时启用时产生冲突 HOT 1
- Bug:Picture in the sidebar is always loading HOT 5
- 安装插件后主页出现文字无法显示,只有博客框架和背景图片,请问怎么解决或者卸载 HOT 4
- 关于与 Pjax 的兼容问题 HOT 7
- 页面第一加载之后再刷新图片就不再加载了 HOT 2
- onlypost is not work HOT 2
- 如何固定懒加载图片占位符 HOT 5
- 懒加载不工作 HOT 1
- [FR] Load before it comes into the browser viewport HOT 4
- 几个功能增强建议 HOT 6
- 回调函数 HOT 1
- script.js文件中修改了97后前四张图还是loading占位 HOT 3
- onlypost 为true时 不工作 HOT 8
- 如何取消某页面懒加载 HOT 3
- next开启懒加载后报错 HOT 7
- 懒加载 小建议 HOT 1
- 与 fancybox 的兼容问题 HOT 3
- 与 hexo-blog-encrypt 插件的兼容问题 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hexo-lazyload-image.