[feat] fixes xaoxuu#442: img fancybox supports providing a different image for popup (#444)

This commit is contained in:
calfzhou 2024-04-25 00:15:19 +08:00 committed by GitHub
parent 35a6d250d3
commit 42db581a14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 7 deletions

View File

@ -24,9 +24,9 @@
autoStart: false, autoStart: false,
}, },
caption: (fancybox, slide) => { caption: (fancybox, slide) => {
return slide.triggerEl.alt || null return slide.triggerEl.alt || slide.triggerEl.dataset.caption || null
} }
}); });
}) })
} }
</script> </script>

View File

@ -2,7 +2,7 @@
* image.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/ * image.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
* 格式与官方标签插件一致使用空格分隔中括号内的是可选参数中括号不需要写出来 * 格式与官方标签插件一致使用空格分隔中括号内的是可选参数中括号不需要写出来
* *
* {% image src [alt] [width:400px] [bg:#eee] [download:true/false/url] %} * {% image src [alt] [width:400px] [bg:#eee] [download:true/false/url] [fancybox:true/false/url] %}
*/ */
'use strict' 'use strict'
@ -18,6 +18,7 @@ module.exports = ctx => function(args) {
} }
// fancybox // fancybox
var fancybox = false var fancybox = false
var fancyboxHref = null
if (ctx.theme.config.plugins.fancybox && ctx.theme.config.plugins.fancybox.enable) { if (ctx.theme.config.plugins.fancybox && ctx.theme.config.plugins.fancybox.enable) {
// 主题配置 // 主题配置
if (ctx.theme.config.tag_plugins.image && ctx.theme.config.tag_plugins.image.fancybox) { if (ctx.theme.config.tag_plugins.image && ctx.theme.config.tag_plugins.image.fancybox) {
@ -27,8 +28,11 @@ module.exports = ctx => function(args) {
if (args.fancybox && args.fancybox.length > 0) { if (args.fancybox && args.fancybox.length > 0) {
if (args.fancybox == 'false') { if (args.fancybox == 'false') {
fancybox = false fancybox = false
} else { } else if (args.fancybox === 'true') {
fancybox = args.fancybox fancybox = args.fancybox
} else {
fancybox = true
fancyboxHref = args.fancybox
} }
} }
} }
@ -36,16 +40,24 @@ module.exports = ctx => function(args) {
function img(src, alt, style) { function img(src, alt, style) {
let img = '' let img = ''
img += '<img src="' + src + '"' img += '<img src="' + src + '"'
let a = '<a data-fancybox'
if (alt) { if (alt) {
img += ' alt="' + alt + '"' img += ' alt="' + alt + '"'
a += ` data-caption="${alt}"`
} }
if (fancybox) { if (fancybox && !fancyboxHref) {
img += ` data-fancybox="${fancybox}"` img += ` data-fancybox="${fancybox}"`
} }
if (style.length > 0) { if (style.length > 0) {
img += ' style="' + style + '"' img += ' style="' + style + '"'
} }
img += '/>' img += '/>'
if (fancyboxHref) {
a += ` href="${fancyboxHref}">${img}</a>`
return a
}
return img return img
} }
@ -87,4 +99,4 @@ module.exports = ctx => function(args) {
el += '</div>' el += '</div>'
return el return el
} }

View File

@ -1,2 +1,2 @@
img[data-fancybox] img[data-fancybox], a[data-fancybox]
cursor: zoom-in cursor: zoom-in