mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-06-06 05:11:45 +08:00
fix: 修復 lazyload 報 undefined 的 bug close #532
This commit is contained in:
@@ -8,20 +8,22 @@
|
||||
|
||||
const urlFor = require('hexo-util').url_for.bind(hexo)
|
||||
|
||||
if (!hexo.config.theme_config.lazyload.enable) return
|
||||
|
||||
function lazyload (htmlContent) {
|
||||
const bg = hexo.theme.config.lazyload.placeholder ? urlFor(hexo.theme.config.lazyload.placeholder) : 'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs='
|
||||
return htmlContent.replace(/(<img.*? src=)/ig, `$1 "${bg}" data-lazy-src=`)
|
||||
}
|
||||
|
||||
if (hexo.config.theme_config.lazyload.field === 'site') {
|
||||
hexo.extend.filter.register('after_render:html', function (data) {
|
||||
return lazyload.call(this, data)
|
||||
})
|
||||
} else {
|
||||
hexo.extend.filter.register('after_post_render', data => {
|
||||
data.content = lazyload.call(this, data.content)
|
||||
return data
|
||||
})
|
||||
}
|
||||
hexo.extend.filter.register('after_render:html', function (data) {
|
||||
const config = hexo.theme.config.lazyload
|
||||
if (!config.enable) return
|
||||
if (config.field !== 'site') return
|
||||
return lazyload.call(this, data)
|
||||
})
|
||||
|
||||
hexo.extend.filter.register('after_post_render', data => {
|
||||
const config = hexo.theme.config.lazyload
|
||||
if (!config.enable) return
|
||||
if (config.field !== 'post') return
|
||||
data.content = lazyload.call(this, data.content)
|
||||
return data
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user