2021-01-07 15:15:39 +08:00
|
|
|
'use strict'
|
|
|
|
hexo.extend.filter.register(
|
|
|
|
'after_post_render',
|
|
|
|
function (data) {
|
|
|
|
const theme = hexo.theme.config;
|
|
|
|
if (!theme.lazyload || !theme.lazyload.enable) return;
|
|
|
|
data.content = data.content.replace(
|
|
|
|
// Match 'img' tags width the src attribute.
|
2021-04-27 15:11:41 +08:00
|
|
|
/<img([^>]*)src="([^"]*)"([^>\/]*)\/?\s*>/gim,
|
2021-01-07 15:15:39 +08:00
|
|
|
function (match, attrBegin, src, attrEnd) {
|
|
|
|
// Exit if the src doesn't exists.
|
|
|
|
if (!src) return match;
|
|
|
|
|
|
|
|
return `<img ${attrBegin}
|
|
|
|
lazyload
|
|
|
|
src="/images/loading.svg"
|
|
|
|
data-src="${src}"
|
|
|
|
${attrEnd}
|
|
|
|
>`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
1
|
|
|
|
);
|