diff --git a/layout/includes/head.pug b/layout/includes/head.pug index d8ff883..8d0674b 100644 --- a/layout/includes/head.pug +++ b/layout/includes/head.pug @@ -26,7 +26,7 @@ meta(name="theme-color" content=themeColor) include ./head/Open_Graph.pug !=favicon_tag(theme.favicon || config.favicon) -link(rel="canonical" href=urlNoIndex()) +link(rel="canonical" href=urlNoIndex(null,config.pretty_urls.trailing_index,config.pretty_urls.trailing_html)) //- 預解析 !=partial('includes/head/preconnect', {}, {cache: true}) diff --git a/layout/includes/head/Open_Graph.pug b/layout/includes/head/Open_Graph.pug index 1d04b47..8fe04e0 100644 --- a/layout/includes/head/Open_Graph.pug +++ b/layout/includes/head/Open_Graph.pug @@ -1,8 +1,9 @@ if theme.Open_Graph_meta.enable - + const coverVal = page.cover_type === 'img' ? page.cover : theme.avatar.img let ogOption = Object.assign({ type: is_post() ? 'article' : 'website', - image: (page.cover || theme.avatar.img) ? full_url_for(page.cover || theme.avatar.img) : '', + image: coverVal ? full_url_for(coverVal) : '', fb_admins: theme.facebook_comments.user_id || '', fb_app_id: theme.facebook_comments.app_id || '', }, theme.Open_Graph_meta.option) diff --git a/layout/includes/header/index.pug b/layout/includes/header/index.pug index d1aa7c0..df6f3da 100644 --- a/layout/includes/header/index.pug +++ b/layout/includes/header/index.pug @@ -1,6 +1,6 @@ if !theme.disable_top_img && page.top_img !== false if is_post() - - var top_img = page.top_img || page.cover || page.randomcover + - var top_img = page.top_img || page.cover || theme.default_top_img else if is_page() - var top_img = page.top_img || theme.default_top_img else if is_tag() @@ -17,7 +17,7 @@ if !theme.disable_top_img && page.top_img !== false - var top_img = page.top_img || theme.default_top_img if top_img !== false - - var imgSource = top_img && top_img.indexOf('/') !== -1 ? `background-image: url('${url_for(top_img)}')` : `background: ${top_img}` + - var imgSource = top_img && isImgOrUrl(top_img) ? `background-image: url('${url_for(top_img)}')` : `background: ${top_img}` - var bg_img = top_img ? imgSource : '' - var site_title = page.title || page.tag || page.category || config.title - var isHomeClass = is_home() ? 'full_page' : 'not-home-page' diff --git a/layout/includes/layout.pug b/layout/includes/layout.pug index eb1dfe1..dcd2c54 100644 --- a/layout/includes/layout.pug +++ b/layout/includes/layout.pug @@ -33,7 +33,7 @@ html(lang=config.language data-theme=theme.display_mode class=htmlClassHideAside if (footerBg === true) - var footer_bg = bg_img else - - var footer_bg = theme.footer_bg.indexOf('/') !== -1 ? `background-image: url('${url_for(footerBg)}')` : `background: ${footerBg}` + - var footer_bg = isImgOrUrl(theme.footer_bg) ? `background-image: url('${url_for(footerBg)}')` : `background: ${footerBg}` else - var footer_bg = '' diff --git a/layout/includes/mixins/article-sort.pug b/layout/includes/mixins/article-sort.pug index 01e9ef2..3eaf038 100644 --- a/layout/includes/mixins/article-sort.pug +++ b/layout/includes/mixins/article-sort.pug @@ -11,7 +11,10 @@ mixin articleSort(posts) .article-sort-item(class=no_cover) if article.cover && theme.cover.archives_enable a.article-sort-item-img(href=url_for(article.path) title=title) - img(src=url_for(article.cover) alt=title onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'`) + if article.cover_type === 'img' + img(src=url_for(article.cover) alt=title onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'`) + else + div(style=`background: ${article.cover}`) .article-sort-item-info .article-sort-item-time i.far.fa-calendar-alt diff --git a/layout/includes/mixins/post-ui.pug b/layout/includes/mixins/post-ui.pug index 3dd364d..4ed053b 100644 --- a/layout/includes/mixins/post-ui.pug +++ b/layout/includes/mixins/post-ui.pug @@ -14,7 +14,10 @@ mixin postUI(posts) if post_cover && theme.cover.index_enable .post_cover(class=leftOrRight) a(href=url_for(link) title=title) - img.post_bg(src=url_for(post_cover) onerror=`this.onerror=null;this.src='`+ url_for(theme.error_img.post_page) + `'` alt=title) + if article.cover_type === 'img' + img.post-bg(src=url_for(post_cover) onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'` alt=title) + else + div.post-bg(style=`background: ${post_cover}`) .recent-post-info(class=no_cover) a.article-title(href=url_for(link) title=title)= title .article-meta-wrap diff --git a/layout/includes/pagination.pug b/layout/includes/pagination.pug index c50b81a..2af0e00 100644 --- a/layout/includes/pagination.pug +++ b/layout/includes/pagination.pug @@ -13,19 +13,23 @@ if is_post() if(prev) - var hasPageNext = next ? 'pull-left' : 'pull-full' .prev-post(class=hasPageNext) - - var pagination_cover = prev.cover === false ? prev.randomcover : prev.cover - a(href=url_for(prev.path)) - img.prev-cover(src=url_for(pagination_cover) onerror=`onerror=null;src='${url_for(theme.error_img.post_page)}'` alt='cover of previous post') + a(href=url_for(prev.path) title=prev.title) + if prev.cover_type === 'img' + img.cover(src=url_for(prev.cover) onerror=`onerror=null;src='${url_for(theme.error_img.post_page)}'` alt='cover of previous post') + else + .cover(style=`background: ${prev.cover || 'var(--default-bg-color)'}`) .pagination-info .label=_p('pagination.prev') .prev_info=prev.title if(next) - var hasPagePrev = prev ? 'pull-right' : 'pull-full' - - var pagination_cover = next.cover == false ? next.randomcover : next.cover .next-post(class=hasPagePrev) - a(href=url_for(next.path)) - img.next-cover(src=url_for(pagination_cover) onerror=`onerror=null;src='${url_for(theme.error_img.post_page)}'` alt='cover of next post') + a(href=url_for(next.path) title=next.title) + if next.cover_type === 'img' + img.cover(src=url_for(next.cover) onerror=`onerror=null;src='${url_for(theme.error_img.post_page)}'` alt='cover of next post') + else + .cover(style=`background: ${next.cover || 'var(--default-bg-color)'}`) .pagination-info .label=_p('pagination.next') .next_info=next.title diff --git a/layout/includes/third-party/share/share-js.pug b/layout/includes/third-party/share/share-js.pug index 0a566fa..612332c 100644 --- a/layout/includes/third-party/share/share-js.pug +++ b/layout/includes/third-party/share/share-js.pug @@ -1,3 +1,4 @@ -.social-share(data-image=url_for(page.cover|| theme.avatar.img) data-sites= theme.sharejs.sites) +- const coverVal = page.cover_type === 'img' ? page.cover : theme.avatar.img +.social-share(data-image=url_for(coverVal) data-sites= theme.sharejs.sites) link(rel='stylesheet' href=url_for(theme.asset.sharejs_css) media="print" onload="this.media='all'") script(src=url_for(theme.asset.sharejs) defer) \ No newline at end of file diff --git a/layout/includes/widget/card_recent_post.pug b/layout/includes/widget/card_recent_post.pug index 60a0e07..dddf0fc 100644 --- a/layout/includes/widget/card_recent_post.pug +++ b/layout/includes/widget/card_recent_post.pug @@ -14,7 +14,10 @@ if theme.aside.card_recent_post.enable .aside-list-item(class=no_cover) if post_cover && theme.cover.aside_enable a.thumbnail(href=url_for(link) title=title) - img(src=url_for(post_cover) onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'` alt=title) + if article.cover_type === 'img' + img(src=url_for(post_cover) onerror=`this.onerror=null;this.src='${url_for(theme.error_img.post_page)}'` alt=title) + else + div(style=`background: ${post_cover}`) .content a.title(href=url_for(link) title=title)= title if theme.aside.card_recent_post.sort === 'updated' diff --git a/package.json b/package.json index 3dd25be..c2d5961 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexo-theme-butterfly", - "version": "4.6.0-b1", + "version": "4.6.0-b2", "description": "A Simple and Card UI Design theme for Hexo", "main": "package.json", "scripts": { diff --git a/scripts/filters/random_cover.js b/scripts/filters/random_cover.js index 47ed4fe..dacf563 100644 --- a/scripts/filters/random_cover.js +++ b/scripts/filters/random_cover.js @@ -6,40 +6,40 @@ 'use strict' hexo.extend.filter.register('before_post_render', function (data) { - const { config } = this - if (config.post_asset_folder) { - const imgTestReg = /\.(png|jpe?g|gif|svg|webp)(\?.*)?$/ + const imgTestReg = /\.(png|jpe?g|gif|svg|webp)(\?.*)?$/ + let randomCover + let coverVal = data.cover + + // Add path to top_img and cover if post_asset_folder is enabled + if (hexo.config.post_asset_folder) { const topImg = data.top_img - const cover = data.cover if (topImg && topImg.indexOf('/') === -1 && imgTestReg.test(topImg)) data.top_img = data.path + topImg - if (cover && cover.indexOf('/') === -1) data.cover = data.path + cover + if (coverVal && coverVal.indexOf('/') === -1 && imgTestReg.test(coverVal)) data.cover = data.path + coverVal } - if (data.cover === false) { - data.randomcover = randomCover() - return data + const randomCoverFn = () => { + const theme = hexo.theme.config + if (!(theme.cover && theme.cover.default_cover)) return false + if (!Array.isArray(theme.cover.default_cover)) return theme.cover.default_cover + const num = Math.floor(Math.random() * theme.cover.default_cover.length) + return theme.cover.default_cover[num] + } + + if (coverVal === false) return data + + // If cover is not set, use random cover + if (!coverVal) { + randomCover = randomCoverFn() + data.cover = randomCover + coverVal = randomCover // update coverVal } - data.cover = data.cover || randomCover() - return data -}) - -function randomCover () { - const theme = hexo.theme.config - let cover - let num - - if (theme.cover && theme.cover.default_cover) { - if (!Array.isArray(theme.cover.default_cover)) { - cover = theme.cover.default_cover - return cover - } else { - num = Math.floor(Math.random() * theme.cover.default_cover.length) - cover = theme.cover.default_cover[num] - return cover + if (coverVal) { + if (coverVal.indexOf('//') !== -1 || imgTestReg.test(coverVal)) { + data.cover_type = 'img' + return data } - } else { - cover = theme.default_top_img || 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' - return cover } -} + + return data +}) \ No newline at end of file diff --git a/scripts/helpers/page.js b/scripts/helpers/page.js index aa2b74b..8e3f892 100644 --- a/scripts/helpers/page.js +++ b/scripts/helpers/page.js @@ -54,8 +54,8 @@ hexo.extend.helper.register('cloudTags', function (options = {}) { return result }) -hexo.extend.helper.register('urlNoIndex', function (url = null) { - return prettyUrls(url || this.url, { trailing_index: false, trailing_html: false }) +hexo.extend.helper.register('urlNoIndex', function (url = null, trailingIndex = false, trailingHtml = false) { + return prettyUrls(url || this.url, { trailing_index: trailingIndex, trailing_html: trailingHtml }) }) hexo.extend.helper.register('md5', function (path) { @@ -95,3 +95,11 @@ hexo.extend.helper.register('findArchivesTitle', function (page, menu, date) { return loop(menu) || defaultTitle }) + +hexo.extend.helper.register('isImgOrUrl', function (path) { + const imgTestReg = /\.(png|jpe?g|gif|svg|webp)(\?.*)?$/ + if (path.indexOf('//') !== -1 || imgTestReg.test(path)) { + return true + } + return false +}) \ No newline at end of file diff --git a/scripts/helpers/related_post.js b/scripts/helpers/related_post.js index 6809ac7..9a46f68 100644 --- a/scripts/helpers/related_post.js +++ b/scripts/helpers/related_post.js @@ -15,7 +15,7 @@ hexo.extend.helper.register('related_posts', function (currentPost, allPosts) { title: post.title, path: post.path, cover: post.cover, - randomcover: post.randomcover, + cover_type: post.cover_type, weight: 1, updated: post.updated, created: post.date @@ -50,13 +50,14 @@ hexo.extend.helper.register('related_posts', function (currentPost, allPosts) { result += '