hexo/themes/blank/scripts/post_lazyload.js

22 lines
627 B
JavaScript
Raw Normal View History

2024-05-09 10:56:56 +08:00
/**
* lazyload.js
* transplant from hexo-theme-butterfly
*/
'use strict'
const urlFor = require('hexo-util').url_for.bind(hexo)
function lazyProcess (htmlContent) {
var bg = hexo.theme.config.lazyload.image ? urlFor(hexo.theme.config.lazyload.image) : 'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs='
return htmlContent.replace(/(<img .*?src=)/ig, `$1 "${bg}" data-src=`)
}
var processPost = function (data) {
if (!hexo.theme.config.lazyload.enable) return
data.content = lazyProcess.call(this, data.content)
return data
}
hexo.extend.filter.register('after_post_render', processPost)