fix: fixed lazyload not working when `alt` attribute is empty

This commit is contained in:
XPoet 2022-10-05 00:21:13 +08:00
parent 41e59293cb
commit 002374b304
1 changed files with 3 additions and 0 deletions

View File

@ -11,9 +11,12 @@ hexo.extend.filter.register(
// Match 'img' tags the src attribute. // Match 'img' tags the src attribute.
/<img([^>]*)src="([^"]*)"([^>\/]*)\/?\s*>/gim, /<img([^>]*)src="([^"]*)"([^>\/]*)\/?\s*>/gim,
function (match, attrBegin, src, attrEnd) { function (match, attrBegin, src, attrEnd) {
let hasAlt = false
if (!src) return match if (!src) return match
;[attrBegin, attrEnd].forEach(x => { if (x.includes('alt="')) { hasAlt = true } })
return `<img ${attrBegin} return `<img ${attrBegin}
lazyload lazyload
${hasAlt ? '' : 'alt="image"'}
data-src="${src}" data-src="${src}"
${attrEnd} ${attrEnd}
>` >`