mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-06-06 05:11:45 +08:00
🏷️ 更新2.3.5 更新內容查看 https://github.com/jerryc127/hexo-theme-butterfly/releases/tag/2.3.5
This commit is contained in:
@@ -2,36 +2,19 @@
|
||||
* Butterfly
|
||||
* lazyload
|
||||
* replace src to data-src
|
||||
* add class 'lazyload'
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const url_for = require('hexo-util').url_for.bind(hexo)
|
||||
const urlFor = require('hexo-util').url_for.bind(hexo)
|
||||
|
||||
function lazyProcess (htmlContent) {
|
||||
var bg = url_for(hexo.theme.config.lodding_bg.post)
|
||||
return htmlContent.replace(
|
||||
/<img(.*?)src="(.*?)"(.*?)>/gi,
|
||||
(str, p1, p2, p3) => {
|
||||
if (/data-src/gi.test(str)) {
|
||||
return str
|
||||
}
|
||||
if (/class="(.*?)"/gi.test(str)) {
|
||||
str = str.replace(/class="(.*?)"/gi, (classStr, p1) => {
|
||||
return classStr.replace(p1, `${p1} lazyload`)
|
||||
})
|
||||
str = str.replace(p2, `${bg}`)
|
||||
return str.replace('>', ` data-src="${p2}">`)
|
||||
}
|
||||
str = str.replace(p2, `${bg}`)
|
||||
return str.replace(p3, ` class="lazyload" data-src="${p2}" ${p3}`)
|
||||
}
|
||||
)
|
||||
var bg = hexo.theme.config.lodding_bg.post ? urlFor(hexo.theme.config.lodding_bg.post) : '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
|
||||
if (!hexo.theme.config.lazyload) return
|
||||
data.content = lazyProcess.call(this, data.content)
|
||||
return data
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user