2022-10-04 23:52:46 +08:00
|
|
|
/* global hexo */
|
|
|
|
|
2021-01-07 15:15:39 +08:00
|
|
|
'use strict'
|
2022-10-04 23:52:46 +08:00
|
|
|
|
2021-01-07 15:15:39 +08:00
|
|
|
hexo.extend.filter.register(
|
|
|
|
'after_post_render',
|
|
|
|
function (data) {
|
2022-09-30 11:40:30 +08:00
|
|
|
const theme = hexo.theme.config
|
|
|
|
if (!theme.lazyload || !theme.lazyload.enable) return
|
2021-01-07 15:15:39 +08:00
|
|
|
data.content = data.content.replace(
|
2022-10-04 23:52:46 +08:00
|
|
|
// Match 'img' tags 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) {
|
2022-10-05 00:21:13 +08:00
|
|
|
let hasAlt = false
|
2022-09-30 11:40:30 +08:00
|
|
|
if (!src) return match
|
2022-10-08 20:56:08 +08:00
|
|
|
if (attrBegin.includes('alt="')) {
|
|
|
|
hasAlt = true
|
|
|
|
} else if (attrBegin.includes('alt="')) {
|
|
|
|
hasAlt = true
|
|
|
|
}
|
2021-01-07 15:15:39 +08:00
|
|
|
return `<img ${attrBegin}
|
|
|
|
lazyload
|
2022-10-05 00:21:13 +08:00
|
|
|
${hasAlt ? '' : 'alt="image"'}
|
2021-01-07 15:15:39 +08:00
|
|
|
data-src="${src}"
|
|
|
|
${attrEnd}
|
|
|
|
>`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
1
|
2022-09-30 11:40:30 +08:00
|
|
|
)
|