diff --git a/_config.yml b/_config.yml index 0831021..dea86a3 100644 --- a/_config.yml +++ b/_config.yml @@ -59,6 +59,15 @@ local_search: preload: false CDN: +# Docsearch +# https://docsearch.algolia.com/ +docsearch: + enable: false + appId: + apiKey: + indexName: + option: + # Math (數學) # -------------------------------------- # About the per_page @@ -686,6 +695,8 @@ aside: enable: true limit: 40 # if set 0 will show all color: false + orderby: random # Order of tags, random/name/length + order: 1 # Sort of order. 1, asc for ascending; -1, desc for descending sort_order: # Don't modify the setting unless you know how it works card_archives: enable: true @@ -912,6 +923,8 @@ CDN: # algolia_js: # algolia_search_v4: # instantsearch_v4: + # docsearch_js: + # docsearch_css: # pjax: # gitalk: # gitalk_css: diff --git a/layout/includes/additional-js.pug b/layout/includes/additional-js.pug index 4ec9ac1..09a7898 100644 --- a/layout/includes/additional-js.pug +++ b/layout/includes/additional-js.pug @@ -22,14 +22,6 @@ div if theme.pangu.enable != partial("includes/third-party/pangu.pug", {}, { cache: true }) - //- search - if theme.algolia_search.enable - script(src=url_for(theme.asset.algolia_search_v4)) - script(src=url_for(theme.asset.instantsearch_v4)) - script(src=url_for(theme.asset.algolia_js)) - else if theme.local_search.enable - script(src=url_for(theme.asset.local_search)) - .js-pjax if needLoadCountJs != partial("includes/third-party/card-post-count/index", {}, { cache: true }) diff --git a/layout/includes/header/nav.pug b/layout/includes/header/nav.pug index 797066a..f6376ca 100644 --- a/layout/includes/header/nav.pug +++ b/layout/includes/header/nav.pug @@ -7,7 +7,7 @@ nav#nav span.site-name=config.title #menus - if (theme.algolia_search.enable || theme.local_search.enable) + if (theme.algolia_search.enable || theme.local_search.enable || theme.docsearch.enable) #search-button a.site-page.social-icon.search(href="javascript:void(0);") i.fas.fa-search.fa-fw diff --git a/layout/includes/layout.pug b/layout/includes/layout.pug index dcd2c54..6055d1e 100644 --- a/layout/includes/layout.pug +++ b/layout/includes/layout.pug @@ -44,5 +44,5 @@ html(lang=config.language data-theme=theme.display_mode class=htmlClassHideAside include ./404.pug include ./rightside.pug - !=partial('includes/third-party/search/index', {}, {cache: true}) - include ./additional-js.pug \ No newline at end of file + include ./additional-js.pug + !=partial('includes/third-party/search/index', {}, {cache: true}) \ No newline at end of file diff --git a/layout/includes/page/tags.pug b/layout/includes/page/tags.pug index 8caac11..ed7d22c 100644 --- a/layout/includes/page/tags.pug +++ b/layout/includes/page/tags.pug @@ -1,2 +1,2 @@ .tag-cloud-list.is-center - !=cloudTags({source: site.tags, minfontsize: 1.2, maxfontsize: 2.1, limit: 0, unit: 'em'}) \ No newline at end of file + !=cloudTags({source: site.tags, orderby: page.orderby || 'random', order: page.order || 1, minfontsize: 1.2, maxfontsize: 2.1, limit: 0, unit: 'em'}) \ No newline at end of file diff --git a/layout/includes/third-party/comments/waline.pug b/layout/includes/third-party/comments/waline.pug index 55c7b45..3d43fe2 100644 --- a/layout/includes/third-party/comments/waline.pug +++ b/layout/includes/third-party/comments/waline.pug @@ -14,9 +14,13 @@ script. }, !{JSON.stringify(option)})) } - if (typeof Waline === 'object') initWaline() + const walineCSSLoad = document.getElementById('waline-css') + + if (typeof Waline === 'object') { + walineCSSLoad ? initWaline() : getCSS('!{url_for(theme.asset.waline_css)}','waline-css').then(initWaline) + } else { - getCSS('!{url_for(theme.asset.waline_css)}').then(() => { + getCSS('!{url_for(theme.asset.waline_css)}','waline-css').then(() => { getScript('!{url_for(theme.asset.waline_js)}').then(initWaline) }) } diff --git a/layout/includes/third-party/newest-comments/artalk.pug b/layout/includes/third-party/newest-comments/artalk.pug index a51df13..8940275 100644 --- a/layout/includes/third-party/newest-comments/artalk.pug +++ b/layout/includes/third-party/newest-comments/artalk.pug @@ -1,4 +1,4 @@ -- const { server, option } = theme.artalk +- const { server, site, option } = theme.artalk - const avatarCdn = option !== null && option.gravatar ? option.gravatar.mirror : 'https://sdn.geekzu.org/avatar/' - const avatarDefault = option !== null && option.gravatar ? option.gravatar.default : 'mp' @@ -45,8 +45,15 @@ script. window.pjax && window.pjax.refresh($dom) } + const headerList = { + method: 'POST', + headers: { + 'Origin': window.location.origin + } + } + const getComment = () => { - fetch('!{server}/api/stat?type=latest_comments&limit=!{theme.newest_comments.limit}',{method: "POST"}) + fetch('!{server}/api/stat?type=latest_comments&limit=!{theme.newest_comments.limit}&site_name=!{site}', headerList) .then(response => response.json()) .then(d => { const artalk = d.data.map(function (e) { diff --git a/layout/includes/third-party/search/algolia.pug b/layout/includes/third-party/search/algolia.pug index 8a439e0..bf5d019 100644 --- a/layout/includes/third-party/search/algolia.pug +++ b/layout/includes/third-party/search/algolia.pug @@ -16,3 +16,7 @@ .algolia-poweredBy #search-mask + + script(src=url_for(theme.asset.algolia_search_v4)) + script(src=url_for(theme.asset.instantsearch_v4)) + script(src=url_for(theme.asset.algolia_js)) \ No newline at end of file diff --git a/layout/includes/third-party/search/docsearch.pug b/layout/includes/third-party/search/docsearch.pug new file mode 100644 index 0000000..00d8a01 --- /dev/null +++ b/layout/includes/third-party/search/docsearch.pug @@ -0,0 +1,27 @@ +- const { appId, apiKey, indexName, option } = theme.docsearch + +.docsearch-wrap + #docsearch(style="display:none") + link(rel="stylesheet" href=url_for(theme.asset.docsearch_css)) + script(src=url_for(theme.asset.docsearch_js)) + script. + (() => { + docsearch(Object.assign({ + appId: '!{appId}', + apiKey: '!{apiKey}', + indexName: '!{indexName}', + container: '#docsearch', + }, !{JSON.stringify(option)})) + + + const searchClickFn = () => { + document.querySelector('#search-button > .search').addEventListener('click', () => { + document.querySelector('.DocSearch-Button').click() + }) + } + + searchClickFn() + window.addEventListener('pjax:complete', searchClickFn) + })() + + diff --git a/layout/includes/third-party/search/index.pug b/layout/includes/third-party/search/index.pug index c1bd03a..160df79 100644 --- a/layout/includes/third-party/search/index.pug +++ b/layout/includes/third-party/search/index.pug @@ -1,4 +1,6 @@ if theme.algolia_search.enable include ./algolia.pug else if theme.local_search.enable - include ./local-search.pug \ No newline at end of file + include ./local-search.pug +else if theme.docsearch.enable + include ./docsearch.pug \ No newline at end of file diff --git a/layout/includes/third-party/search/local-search.pug b/layout/includes/third-party/search/local-search.pug index 3d1508f..c3a4777 100644 --- a/layout/includes/third-party/search/local-search.pug +++ b/layout/includes/third-party/search/local-search.pug @@ -17,4 +17,6 @@ hr #local-search-results - #search-mask \ No newline at end of file + #search-mask + + script(src=url_for(theme.asset.local_search)) \ No newline at end of file diff --git a/layout/includes/widget/card_tags.pug b/layout/includes/widget/card_tags.pug index de40e90..ed09f39 100644 --- a/layout/includes/widget/card_tags.pug +++ b/layout/includes/widget/card_tags.pug @@ -5,8 +5,10 @@ if theme.aside.card_tags.enable i.fas.fa-tags span= _p('aside.card_tags') - - let tagLimit = theme.aside.card_tags.limit === 0 ? 0 : theme.aside.card_tags.limit || 40 + - let { limit, orderby, order } = theme.aside.card_tags + - limit = limit === 0 ? 0 : limit || 40 + if theme.aside.card_tags.color - .card-tag-cloud!= cloudTags({source: site.tags, minfontsize: 1.15, maxfontsize: 1.45, limit: tagLimit, unit: 'em'}) + .card-tag-cloud!= cloudTags({source: site.tags, orderby: orderby, order: order, minfontsize: 1.15, maxfontsize: 1.45, limit: tagLimit, unit: 'em'}) else - .card-tag-cloud!= tagcloud({min_font: 1.1, max_font: 1.5, amount: tagLimit , color: true, start_color: '#999', end_color: '#99a9bf', unit: 'em'}) + .card-tag-cloud!= tagcloud({orderby: orderby, order: order, min_font: 1.1, max_font: 1.5, amount: limit , color: true, start_color: '#999', end_color: '#99a9bf', unit: 'em'}) diff --git a/layout/includes/widget/index.pug b/layout/includes/widget/index.pug index 5f44ae9..175fcdc 100644 --- a/layout/includes/widget/index.pug +++ b/layout/includes/widget/index.pug @@ -19,7 +19,7 @@ //- page !=partial('includes/widget/card_author', {}, {cache: true}) !=partial('includes/widget/card_announcement', {}, {cache: true}) - !=partial('includes/widget/card_top_self', {}, {cache: true}) + !=partial('includes/widget/card_top_self', {}, {cache: true}) .sticky_layout if showToc diff --git a/package.json b/package.json index d1167a5..dac96ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexo-theme-butterfly", - "version": "4.6.1", + "version": "4.7.0-b1", "description": "A Simple and Card UI Design theme for Hexo", "main": "package.json", "scripts": { diff --git a/plugins.yml b/plugins.yml index c183f69..6a65d03 100644 --- a/plugins.yml +++ b/plugins.yml @@ -198,3 +198,13 @@ pace_default_css: other_name: pace file: themes/blue/pace-theme-minimal.css version: 1.2.4 +docsearch_js: + name: '@docsearch/js' + other_name: docsearch-js + file: dist/umd/index.js + version: 3.3.2 +docsearch_css: + name: '@docsearch/css' + other_name: docsearch-css + file: dist/style.css + version: 3.3.2 diff --git a/scripts/helpers/inject_head_js.js b/scripts/helpers/inject_head_js.js index 88ff5f2..25a65c8 100644 --- a/scripts/helpers/inject_head_js.js +++ b/scripts/helpers/inject_head_js.js @@ -61,12 +61,18 @@ hexo.extend.helper.register('inject_head_js', function () { ` const getCSS = ` - win.getCSS = url => new Promise((resolve, reject) => { + win.getCSS = (url,id = false) => new Promise((resolve, reject) => { const link = document.createElement('link') link.rel = 'stylesheet' link.href = url - link.onload = () => resolve() - link.onerror = () => reject() + if (id) link.id = id + link.onerror = reject + link.onload = link.onreadystatechange = function() { + const loadState = this.readyState + if (loadState && loadState !== 'loaded' && loadState !== 'complete') return + link.onload = link.onreadystatechange = null + resolve() + } document.head.appendChild(link) }) ` diff --git a/scripts/helpers/page.js b/scripts/helpers/page.js index 35e05a8..eaccb0f 100644 --- a/scripts/helpers/page.js +++ b/scripts/helpers/page.js @@ -24,11 +24,8 @@ hexo.extend.helper.register('page_description', function () { hexo.extend.helper.register('cloudTags', function (options = {}) { const env = this - let source = options.source - const minfontsize = options.minfontsize - const maxfontsize = options.maxfontsize - const limit = options.limit - const unit = options.unit || 'px' + let { source, minfontsize, maxfontsize, limit, unit, orderby, order } = options + unit = unit || 'px' let result = '' if (limit > 0) { @@ -43,7 +40,7 @@ hexo.extend.helper.register('cloudTags', function (options = {}) { }) const length = sizes.length - 1 - source.forEach(tag => { + source.sort(orderby, order).forEach(tag => { const ratio = length ? sizes.indexOf(tag.length) / length : 0 const size = minfontsize + ((maxfontsize - minfontsize) * ratio) let style = `font-size: ${parseFloat(size.toFixed(2))}${unit};` diff --git a/scripts/tag/gallery.js b/scripts/tag/gallery.js index 959f412..adf46f3 100644 --- a/scripts/tag/gallery.js +++ b/scripts/tag/gallery.js @@ -3,6 +3,7 @@ * galleryGroup and gallery * {% galleryGroup [name] [descr] [url] [img] %} * {% gallery [lazyload],[rowHeight],[limit] %} + * {% gallery url,[url],[lazyload],[rowHeight],[limit] %} */ 'use strict' @@ -12,28 +13,42 @@ const urlFor = require('hexo-util').url_for.bind(hexo) function gallery (args, content) { const { data, languages } = hexo.theme.i18n args = args.join(' ').split(',') - const rowHeight = args[1] || 220 - const limit = args[2] || 10 - const lazyload = args[0] === 'true' - const regex = /!\[(.*?)\]\(([^\s]*)\s*(?:["'](.*?)["']?)?\s*\)/g - const lazyloadClass = lazyload ? 'lazyload' : '' - let m - const arr = [] - while ((m = regex.exec(content)) !== null) { - if (m.index === regex.lastIndex) { - regex.lastIndex++ + let rowHeight, limit, lazyload, type, dataStr + + // url,[link],[lazyload],[rowHeight],[limit] + if (args[0] === 'url') { + dataStr = args[1] + lazyload = args[2] === 'true' + rowHeight = args[3] || 220 + limit = args[4] || 10 + type = ' url' + } else { + rowHeight = args[1] || 220 + limit = args[2] || 10 + lazyload = args[0] === 'true' + type = ' data' + const regex = /!\[(.*?)\]\(([^\s]*)\s*(?:["'](.*?)["']?)?\s*\)/g + let m + const arr = [] + while ((m = regex.exec(content)) !== null) { + if (m.index === regex.lastIndex) { + regex.lastIndex++ + } + arr.push({ + url: m[2], + alt: m[1], + title: m[3] + }) } - arr.push({ - url: m[2], - alt: m[1], - title: m[3] - }) + dataStr = JSON.stringify(arr) } + const lazyloadClass = lazyload ? 'lazyload' : '' + return `