From eb11e4afeb1d350618d862859421e6f901ca03e5 Mon Sep 17 00:00:00 2001 From: Jerry Date: Thu, 2 Apr 2020 22:16:01 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=BE=A9POST-META=E9=97=9C?= =?UTF-8?q?=E9=96=89=E9=96=B2=E8=AE=80=E5=88=86=E9=90=98=E5=92=8C=E8=A8=AA?= =?UTF-8?q?=E5=95=8F=E9=87=8F=E5=BE=8C,=E8=A9=95=E8=AB=96=E9=87=8F?= =?UTF-8?q?=E5=89=8D=E6=9C=89=E5=88=86=E5=89=B2=E7=B7=9A=E7=9A=84bug=20:bu?= =?UTF-8?q?g:=20=E4=BF=AE=E5=BE=A9canvas=5Fribbon=E9=80=8F=E6=98=8E?= =?UTF-8?q?=E5=BA=A6=E8=AE=8A=E7=82=BA1=E5=86=8D=E8=AE=8A=E5=9B=9E?= =?UTF-8?q?=E9=A0=90=E8=A8=AD=E5=80=BCbug=20:bug:=20=E4=BF=AE=E5=BE=A9?= =?UTF-8?q?=E7=95=B6default=5Fcover=E6=B2=92=E8=A8=AD=E7=BD=AE=E6=99=82?= =?UTF-8?q?=EF=BC=8C=E6=9C=83=E5=87=BA=E7=8F=BE=E7=84=A1=E5=9C=96=E7=89=87?= =?UTF-8?q?=E7=9A=84bug=20:art:=20=E9=83=A8=E5=88=86js=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- layout/404.pug | 5 +- layout/includes/header/post-info.pug | 6 +- scripts/events/404.js | 11 +- scripts/events/replace-config.js | 4 +- scripts/filters/hide-post.js | 75 ++++++------- scripts/filters/post-lazyload.js | 54 ++++++---- scripts/filters/random-cover.js | 53 +++++---- scripts/helpers/list-archives.js | 125 +++++++++++----------- scripts/helpers/my-html.js | 31 +++--- scripts/helpers/related-post.js | 142 ++++++++++++++++--------- scripts/tags/gallery.js | 21 ++-- scripts/tags/hide.js | 48 +++++---- scripts/tags/mermaid.js | 14 ++- scripts/tags/note.js | 12 +-- source/css/_global/index.styl | 14 ++- source/css/_layout/head.styl | 7 +- source/js/third-party/canvas-ribbon.js | 116 +++++++++++--------- source/js/utils.js | 10 +- 18 files changed, 433 insertions(+), 315 deletions(-) diff --git a/layout/404.pug b/layout/404.pug index 4dc3c92..73d6f37 100644 --- a/layout/404.pug +++ b/layout/404.pug @@ -17,7 +17,6 @@ html(lang=config.language data-theme=theme.display_mode) h1#error_title= '404' #error_subtitle= theme.error_404.subtitle include includes/rightside.pug - include includes/search/index.pug - each item in theme.CDN_USE.js - script(src=url_for(item)) + !=partial('includes/search/index', {}, {cache:theme.fragment_cache}) + !=fragment_cache('my_js', function(){return my_html('js',theme.CDN_USE.js)}) include includes/additional-js.pug diff --git a/layout/includes/header/post-info.pug b/layout/includes/header/post-info.pug index 9f7a011..3964283 100644 --- a/layout/includes/header/post-info.pug +++ b/layout/includes/header/post-info.pug @@ -62,21 +62,21 @@ span.post-meta-commentcount - var commentCount = theme.fontawesome_v5 && theme.fontawesome_v5.enable ? 'far fa-comments' : 'fa fa-comment-o' if ((theme.disqus && theme.disqus.enable && theme.disqus.count && page.comments !== false) || (theme.disqusjs && theme.disqusjs.enable && theme.disqusjs.count && page.comments !== false)) - if (theme.busuanzi && theme.busuanzi.page_pv) + if (postWordcount || theme.busuanzi.page_pv) span.post-meta__separator | i.post-meta__icon(class=commentCount aria-hidden="true") span= _p('post.comments') + ':' span.disqus-comment-count.comment-count a(href=full_url_for(page.path) + '#disqus_thread') else if (theme.valine && theme.valine.enable && theme.valine.count && page.comments !== false) - if (theme.busuanzi && theme.busuanzi.page_pv) + if (postWordcount || theme.busuanzi.page_pv) span.post-meta__separator | i.post-meta__icon(class=commentCount aria-hidden="true") span= _p('post.comments') + ':' a(href=url_for(page.path) + '#post-comment' itemprop="discussionUrl") span.valine-comment-count.comment-count(data-xid=url_for(page.path) itemprop="commentCount") else if (theme.gitalk && theme.gitalk.enable && theme.gitalk.count && page.comments !== false) - if (theme.busuanzi && theme.busuanzi.page_pv) + if (postWordcount || theme.busuanzi.page_pv) span.post-meta__separator | i.post-meta__icon(class=commentCount aria-hidden="true") span= _p('post.comments') + ':' diff --git a/scripts/events/404.js b/scripts/events/404.js index e091744..c37daae 100644 --- a/scripts/events/404.js +++ b/scripts/events/404.js @@ -1,8 +1,15 @@ -hexo.extend.generator.register('404', function(locals){ +/** + * Butterfly + * 404 error page + */ + +'use strict' + +hexo.extend.generator.register('404', function (locals) { if (!hexo.theme.config.error_404.enable) return return { path: '404.html', data: locals.posts, layout: ['404'] } -}); \ No newline at end of file +}) diff --git a/scripts/events/replace-config.js b/scripts/events/replace-config.js index 7d7612d..77b7786 100644 --- a/scripts/events/replace-config.js +++ b/scripts/events/replace-config.js @@ -2,6 +2,8 @@ * Note: configs in _data/butterfly.yml will replace configs in hexo.theme.config. */ +'use strict' + hexo.on('generateBefore', function () { const rootConfig = hexo.config if (hexo.locals.get) { @@ -9,4 +11,4 @@ hexo.on('generateBefore', function () { data && data.butterfly && (hexo.theme.config = data.butterfly) } hexo.theme.config.rootConfig = rootConfig -}) \ No newline at end of file +}) diff --git a/scripts/filters/hide-post.js b/scripts/filters/hide-post.js index 7ce8f84..94703b4 100644 --- a/scripts/filters/hide-post.js +++ b/scripts/filters/hide-post.js @@ -4,60 +4,63 @@ * modify by Jerry */ -'use strict'; +'use strict' -var public_generators = []; +var public_generators = [] hexo.extend.filter.register('before_generate', function () { - this._bindLocals(); + this._bindLocals() - const all_posts = this.locals.get('posts'); + const all_posts = this.locals.get('posts') const hidden_posts = all_posts.find({ - 'hide': true - }); - const normal_posts = all_posts.filter(post => !post['hide']); + hide: true + }) + const normal_posts = all_posts.filter(post => !post.hide) - this.locals.set('all_posts', all_posts); - this.locals.set('hidden_posts', hidden_posts); - this.locals.set('posts', normal_posts); -}); + this.locals.set('all_posts', all_posts) + this.locals.set('hidden_posts', hidden_posts) + this.locals.set('posts', normal_posts) +}) hexo.extend.filter.register('after_init', function () { - - const original = {}; + const original = {} for (const name in hexo.extend.generator.list()) { - original[name] = hexo.extend.generator.get(name); + original[name] = hexo.extend.generator.get(name) } hexo.extend.generator.register('post', async function (locals) { - const fg = original.post.bind(this); + const fg = original.post.bind(this) - const generated_public = await fg(locals); - const generated_hidden = await fg(Object.assign({}, locals, { - posts: locals.hidden_posts - })); + const generated_public = await fg(locals) + const generated_hidden = await fg( + Object.assign({}, locals, { + posts: locals.hidden_posts + }) + ) // Remove post.prev and post.next for hidden posts generated_hidden.forEach(ele => { - ele.data.prev = ele.data.next = null; - }); + ele.data.prev = ele.data.next = null + }) - return generated_public.concat(generated_hidden); - }); + return generated_public.concat(generated_hidden) + }) // Then we hack into other generators if necessary - public_generators.filter( - name => Object.keys(original).includes(name) - ).forEach(name => { - // Overwrite original generator - hexo.extend.generator.register(name, function (locals) { - const fg = original[name].bind(this); + public_generators + .filter(name => Object.keys(original).includes(name)) + .forEach(name => { + // Overwrite original generator + hexo.extend.generator.register(name, function (locals) { + const fg = original[name].bind(this) - return fg(Object.assign({}, locals, { - posts: new locals.posts.constructor( - locals.posts.data.concat(locals.hidden_posts.data) + return fg( + Object.assign({}, locals, { + posts: new locals.posts.constructor( + locals.posts.data.concat(locals.hidden_posts.data) + ) + }) ) - })); - }); - }); -}); \ No newline at end of file + }) + }) +}) diff --git a/scripts/filters/post-lazyload.js b/scripts/filters/post-lazyload.js index c0a6624..c1cab31 100644 --- a/scripts/filters/post-lazyload.js +++ b/scripts/filters/post-lazyload.js @@ -1,29 +1,39 @@ -'use strict'; +/** + * Butterfly + * lazyload + * replace src to data-src + * add class 'lazyload' + */ -const url_for = require('hexo-util').url_for.bind(hexo); +'use strict' -function lazyProcess(htmlContent) { - var bg = url_for(hexo.theme.config.lodding_bg.post); - return htmlContent.replace(//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`); - }) +const url_for = require('hexo-util').url_for.bind(hexo) + +function lazyProcess (htmlContent) { + var bg = url_for(hexo.theme.config.lodding_bg.post) + return htmlContent.replace( + //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('>', ` data-src="${p2}">`); + return str.replace(p3, ` class="lazyload" data-src="${p2}" ${p3}`) } - str = str.replace(p2, `${bg}`) - return str.replace(p3, ` class="lazyload" data-src="${p2}" ${p3}`); - }); + ) } -var processPost = function(data) { - if (!hexo.theme.config.lazyload.enable) return; - data.content = lazyProcess.call(this, data.content); - return data; -}; +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); \ No newline at end of file +hexo.extend.filter.register('after_post_render', processPost) diff --git a/scripts/filters/random-cover.js b/scripts/filters/random-cover.js index f660df1..5806921 100644 --- a/scripts/filters/random-cover.js +++ b/scripts/filters/random-cover.js @@ -1,23 +1,34 @@ -hexo.extend.filter.register("before_post_render", function(data) { - if (data.cover == false) { - data.randomcover = random_cover(); - return data; - } - data.cover = data.cover || random_cover(); - return data; -}); +/** + * Butterfly + * ramdom cover + */ -var random_cover = function() { - var cover; - var num; - if (!Array.isArray(hexo.theme.config.cover.default_cover)) { - cover = hexo.theme.config.cover.default_cover; - return cover; - } else { - num = Math.floor( - Math.random() * hexo.theme.config.cover.default_cover.length - ); - cover = hexo.theme.config.cover.default_cover[num]; - return cover; +'use strict' + +hexo.extend.filter.register('before_post_render', function (data) { + if (data.cover === false) { + data.randomcover = randomCover() + return data } -}; + data.cover = data.cover || randomCover() + return data +}) + +var randomCover = function () { + var theme = hexo.theme.config + var cover + var num + + if (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 + } + } else { + return theme.default_top_img + } +} diff --git a/scripts/helpers/list-archives.js b/scripts/helpers/list-archives.js index 534d4fe..9ad40e4 100644 --- a/scripts/helpers/list-archives.js +++ b/scripts/helpers/list-archives.js @@ -1,50 +1,55 @@ -"use strict"; +/** + * Butterfly + * for aside archives + */ -hexo.extend.helper.register("list_archives", function(options = {}) { - const { config } = this; - const archiveDir = config.archive_dir; - const { timezone } = config; - const lang = this.page.lang || this.page.language || config.language; - let { format } = options; - const type = options.type || "monthly"; - const { style = "list", transform, separator = ", " } = options; - const showCount = Object.prototype.hasOwnProperty.call(options, "show_count") +'use strict' + +hexo.extend.helper.register('list_archives', function (options = {}) { + const { config } = this + const archiveDir = config.archive_dir + const { timezone } = config + const lang = this.page.lang || this.page.language || config.language + let { format } = options + const type = options.type || 'monthly' + const { transform } = options + const showCount = Object.prototype.hasOwnProperty.call(options, 'show_count') ? options.show_count - : true; - const order = options.order || -1; - const limit = 8; - let result = ""; + : true + const order = options.order || -1 + const limit = 8 + let result = '' - var more_button; - if (lang === "zh-CN") { - more_button = "查看更多"; - } else if (lang === "zh-TW") { - more_button = "查看更多"; + var moreButton + if (lang === 'zh-CN') { + moreButton = '查看更多' + } else if (lang === 'zh-TW') { + moreButton = '查看更多' } else { - more_button = "More"; + moreButton = 'More' } if (!format) { - format = type === "monthly" ? "MMMM YYYY" : "YYYY"; + format = type === 'monthly' ? 'MMMM YYYY' : 'YYYY' } - const posts = this.site.posts.sort("date", order); - if (!posts.length) return result; + const posts = this.site.posts.sort('date', order) + if (!posts.length) return result - const data = []; - let length = 0; + const data = [] + let length = 0 posts.forEach(post => { // Clone the date object to avoid pollution - let date = post.date.clone(); + let date = post.date.clone() - if (timezone) date = date.tz(timezone); - if (lang) date = date.locale(lang); + if (timezone) date = date.tz(timezone) + if (lang) date = date.locale(lang) - const year = date.year(); - const month = date.month() + 1; - const name = date.format(format); - const lastData = data[length - 1]; + const year = date.year() + const month = date.month() + 1 + const name = date.format(format) + const lastData = data[length - 1] if (!lastData || lastData.name !== name) { length = data.push({ @@ -52,52 +57,50 @@ hexo.extend.helper.register("list_archives", function(options = {}) { year, month, count: 1 - }); + }) } else { - lastData.count++; + lastData.count++ } - }); + }) const link = item => { - let url = `${archiveDir}/${item.year}/`; + let url = `${archiveDir}/${item.year}/` - if (type === "monthly") { - if (item.month < 10) url += "0"; - url += `${item.month}/`; + if (type === 'monthly') { + if (item.month < 10) url += '0' + url += `${item.month}/` } - return this.url_for(url); - }; + return this.url_for(url) + } - result += `' + return result +}) diff --git a/scripts/helpers/my-html.js b/scripts/helpers/my-html.js index d3debb9..9a928c5 100644 --- a/scripts/helpers/my-html.js +++ b/scripts/helpers/my-html.js @@ -1,23 +1,24 @@ -"use strict"; +/** + * Butterfly + * Generate js and css according to user setting + */ -hexo.extend.helper.register("my_html", function(type, data) { - var result = ""; - var tag = type === "css" ? "css" : "script"; +'use strict' + +hexo.extend.helper.register('my_html', function (type, data) { + var result = '' + var tag = type === 'css' ? 'css' : 'script' for (var i = 0; i < data.length; i++) { - var src = data[i].split("||")[0].trim(); - var other = data[i].split("||")[1] ? data[i].split("||")[1].trim() : ""; + var src = data[i].split('||')[0].trim() + var other = data[i].split('||')[1] ? data[i].split('||')[1].trim() : '' - if (tag === "css") { + if (tag === 'css') { result += - '"; + '' } else { result += - '"; + '' } } - return result; -}); + return result +}) diff --git a/scripts/helpers/related-post.js b/scripts/helpers/related-post.js index 455eff0..68e67a1 100644 --- a/scripts/helpers/related-post.js +++ b/scripts/helpers/related-post.js @@ -1,5 +1,13 @@ +/** + * Butterfly + * Related Posts + * According the tag + */ + +'use strict' + hexo.extend.helper.register('related_posts', function (currentPost, allPosts) { - var relatedPosts = []; + var relatedPosts = [] currentPost.tags.forEach(function (tag) { allPosts.forEach(function (post) { if (isTagRelated(tag.name, post.tags)) { @@ -11,80 +19,108 @@ hexo.extend.helper.register('related_posts', function (currentPost, allPosts) { weight: 1, updated: post.updated, created: post.date - }; - var index = findItem(relatedPosts, 'path', post.path); - if (index != -1) { - relatedPosts[index].weight += 1; + } + var index = findItem(relatedPosts, 'path', post.path) + if (index !== -1) { + relatedPosts[index].weight += 1 } else { - if (currentPost.path != post.path) { - relatedPosts.push(relatedPost); - }; - }; - }; - }); - }); - if (relatedPosts.length == 0) { + if (currentPost.path !== post.path) { + relatedPosts.push(relatedPost) + } + } + } + }) + }) + if (relatedPosts.length === 0) { return '' - }; - var result = ""; - const hexoConfig = hexo.theme.config.rootConfig; - const config = hexo.theme.config; + } + var result = '' + const hexoConfig = hexo.theme.config.rootConfig + const config = hexo.theme.config - var limit_num = config.related_post.limit || 6 - var date_type = config.related_post.date_type || 'created' - var headline_lang = this._p('post.recommend') - - relatedPosts = relatedPosts.sort(compare('weight')); - var lazy_src = config.lazyload.enable ? lazy_src = 'data-src' : lazy_src = 'src' - var lazy_class = config.lazyload.enable ? lazy_class = 'lazyload' : lazy_class = '' + var limitNum = config.related_post.limit || 6 + var dateType = config.related_post.date_type || 'created' + var headlineLang = this._p('post.recommend') + var lazySrc = config.lazyload.enable ? 'data-src' : 'src' + var lazyClass = config.lazyload.enable ? 'lazyload' : '' + + relatedPosts = relatedPosts.sort(compare('weight')) if (relatedPosts.length > 0) { result += '