fix: 修復 lazyload 報 undefined 的 bug close #532

This commit is contained in:
Jerry 2021-03-30 22:35:40 +08:00
parent 531269c60e
commit c48ce9af8c
3 changed files with 16 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "hexo-theme-butterfly", "name": "hexo-theme-butterfly",
"version": "3.7.5-b2", "version": "3.7.5-b3",
"description": "A Simple and Card UI Design theme for Hexo", "description": "A Simple and Card UI Design theme for Hexo",
"main": "package.json", "main": "package.json",
"scripts": { "scripts": {

View File

@ -2,7 +2,7 @@ const logger = require('hexo-log')()
hexo.extend.filter.register('before_generate', () => { hexo.extend.filter.register('before_generate', () => {
// Get first two digits of the Hexo version number // Get first two digits of the Hexo version number
var hexoVer = hexo.version.replace(/(^.*\..*)\..*/, '$1') const hexoVer = hexo.version.replace(/(^.*\..*)\..*/, '$1')
if (hexoVer < 5) { if (hexoVer < 5) {
logger.error('Please update Hexo to V5.0.0 or higher!') logger.error('Please update Hexo to V5.0.0 or higher!')

View File

@ -8,20 +8,22 @@
const urlFor = require('hexo-util').url_for.bind(hexo) const urlFor = require('hexo-util').url_for.bind(hexo)
if (!hexo.config.theme_config.lazyload.enable) return
function lazyload (htmlContent) { function lazyload (htmlContent) {
const bg = hexo.theme.config.lazyload.placeholder ? urlFor(hexo.theme.config.lazyload.placeholder) : 'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=' 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=`) 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) {
hexo.extend.filter.register('after_render:html', function (data) { const config = hexo.theme.config.lazyload
return lazyload.call(this, data) if (!config.enable) return
}) if (config.field !== 'site') return
} else { return lazyload.call(this, data)
hexo.extend.filter.register('after_post_render', data => { })
data.content = lazyload.call(this, data.content)
return 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
})