From c0e2cb1a9be7ff92e31ac1fbc9b3ca123920caf3 Mon Sep 17 00:00:00 2001 From: ppoffice Date: Thu, 26 Dec 2019 21:07:35 -0500 Subject: [PATCH] chore(*): rewrite cdn helper & bug fixes --- include/helper/cdn.js | 124 ++++++++++++++------------- include/helper/page.js | 22 +---- include/helper/thumbnail.js | 24 ++++++ include/schema/common/comment.json | 2 +- include/schema/common/providers.json | 2 +- include/schema/common/search.json | 2 +- include/schema/common/share.json | 2 +- include/schema/common/widgets.json | 2 +- include/util/schema.js | 3 +- layout/archive.jsx | 2 +- layout/comment/disqusjs.jsx | 2 +- layout/comment/gitalk.jsx | 2 +- layout/common/article.jsx | 2 +- scripts/index.js | 1 + 14 files changed, 104 insertions(+), 88 deletions(-) create mode 100644 include/helper/thumbnail.js diff --git a/include/helper/cdn.js b/include/helper/cdn.js index 7d06346..7b65de9 100644 --- a/include/helper/cdn.js +++ b/include/helper/cdn.js @@ -6,86 +6,94 @@ * <%- fontcdn(fontName) %> * <%- iconcdn() %> */ -const cdn_providers = { - cdnjs: 'https://cdnjs.cloudflare.com/ajax/libs/${ package }/${ version }/${ filename }', - jsdelivr: 'https://cdn.jsdelivr.net/npm/${ package }@${ version }/${ filename }', - unpkg: 'https://unpkg.com/${ package }@${ version }/${ filename }' + +const PROVIDERS = { + LIBRARY: { + cdnjs: '[cdnjs]https://cdnjs.cloudflare.com/ajax/libs/${ package }/${ version }/${ filename }', + loli: '[cdnjs]https://cdnjs.loli.net/ajax/libs/${ package }/${ version }/${ filename }', + jsdelivr: 'https://cdn.jsdelivr.net/npm/${ package }@${ version }/${ filename }', + unpkg: 'https://unpkg.com/${ package }@${ version }/${ filename }' + }, + FONT: { + google: 'https://fonts.googleapis.com/${ type }?family=${ fontname }', + loli: 'https://fonts.loli.net/${ type }?family=${ fontname }' + }, + ICON: { + fontawesome: 'https://use.fontawesome.com/releases/v5.12.0/css/all.css' + } }; -const font_providers = { - google: 'https://fonts.googleapis.com/${ type }?family=${ fontname }' -}; - -const icon_providers = { - fontawesome: 'https://use.fontawesome.com/releases/v5.4.1/css/all.css' +/** + * Convert npm library path to CDN.js path + */ +const CDNJS_FIXTURES = { + 'moment': (ver, fname) => [ + 'moment.js', ver, fname.startsWith('min/') ? fname.substr(4) : fname + ], + 'outdatedbrowser': (ver, fname) => [ + 'outdated-browser', ver, fname.startsWith('outdatedbrowser/') ? fname.substr(16) : fname + ], + 'highlight.js': (ver, fname) => [ + 'highlight.js', ver, fname.endsWith('.css') && fname.indexOf('.min.') === -1 + ? fname.substr(0, fname.length - 4) + '.min.css' : fname + ], + 'mathjax': (ver, fname) => [ + 'mathjax', ver, fname.startsWith('unpacked/') ? fname.substr(9) : fname + ], + 'katex': (ver, fname) => [ + 'KaTeX', ver, fname + ], + 'pace-js': (ver, fname) => [ + 'pace', ver, fname + ], + 'clipboard': (ver, fname) => [ + 'clipboard.js', ver, fname + ], + // disqusjs is not hosted on CDN.js + 'disqusjs': (ver, fname) => [] }; module.exports = function(hexo) { hexo.extend.helper.register('cdn', function(_package, version, filename) { - let provider = this.config.provider && 'cdn' in this.config.provider ? this.config.provider.cdn : 'jsdelivr'; - + let { cdn = 'jsdelivr' } = typeof this.config.providers === 'object' ? this.config.providers : {}; + if (cdn in PROVIDERS.LIBRARY) { + cdn = PROVIDERS.LIBRARY[cdn]; + } // cdn.js does not follow a GitHub npm style like jsdeliver and unpkg do. Patch it! - if (provider === 'cdnjs' || provider.startsWith('[cdnjs]')) { - if (provider.startsWith('[cdnjs]')) { - provider = provider.substr(7); + if (cdn === 'cdnjs' || cdn.startsWith('[cdnjs]')) { + if (cdn.startsWith('[cdnjs]')) { + cdn = cdn.substr(7); } if (filename.startsWith('dist/')) { filename = filename.substr(5); } - if (_package === 'moment') { - _package = 'moment.js'; - filename = filename.startsWith('min/') ? filename.substr(4) : filename; - } - if (_package === 'outdatedbrowser') { - _package = 'outdated-browser'; - filename = filename.startsWith('outdatedbrowser/') ? filename.substr(16) : filename; - } - if (_package === 'highlight.js') { - filename = filename.endsWith('.css') && filename.indexOf('.min.') === -1 - ? filename.substr(0, filename.length - 4) + '.min.css' : filename; - } - if (_package === 'mathjax') { - filename = filename.startsWith('unpacked/') ? filename.substr(9) : filename; - } - if (_package === 'pace-js') { - _package = 'pace'; - } - if (_package === 'clipboard') { - _package = 'clipboard.js'; - } - if (_package === 'disqusjs') { - provider = 'jsdelivr'; - } - if (_package === 'katex') { - _package = 'KaTeX.js'; + if (Object.prototype.hasOwnProperty.call(CDNJS_FIXTURES, _package)) { + [_package, version, filename] = CDNJS_FIXTURES[_package](version, filename); + // package is not hosted on CDN.js + if (!_package) { + cdn = 'jsdelivr'; + } } } - if (provider !== null && provider in cdn_providers) { - provider = cdn_providers[provider]; - } - return provider.replace(/\${\s*package\s*}/gi, _package) + return cdn.replace(/\${\s*package\s*}/gi, _package) .replace(/\${\s*version\s*}/gi, version) .replace(/\${\s*filename\s*}/gi, filename); }); hexo.extend.helper.register('fontcdn', function(fontName, type = 'css') { - let provider = this.config.provider && 'fontcdn' in this.config.provider ? this.config.provider.fontcdn : 'google'; - if (provider !== null && provider in font_providers) { - provider = font_providers[provider]; + let { fontcdn = 'google' } = typeof this.config.providers === 'object' ? this.config.providers : {}; + if (fontcdn in PROVIDERS.FONT) { + fontcdn = PROVIDERS.FONT[fontcdn]; } - return provider.replace(/\${\s*fontname\s*}/gi, fontName) + return fontcdn.replace(/\${\s*fontname\s*}/gi, fontName) .replace(/\${\s*type\s*}/gi, type); }); - hexo.extend.helper.register('iconcdn', function(provider = null) { - if (provider !== null && provider in icon_providers) { - provider = icon_providers[provider]; - } else { - provider = this.config.provider && 'iconcdn' in this.config.provider ? this.config.provider.iconcdn : 'fontawesome'; - if (provider !== null && provider in icon_providers) { - provider = icon_providers[provider]; - } + hexo.extend.helper.register('iconcdn', function() { + let { iconfont = 'fontawesome' } = typeof this.config.providers === 'object' ? this.config.providers : {}; + if (iconfont in PROVIDERS.ICON) { + iconfont = PROVIDERS.ICON[iconfont]; } - return provider; + return iconfont; }); }; diff --git a/include/helper/page.js b/include/helper/page.js index 17a0996..c010e6b 100644 --- a/include/helper/page.js +++ b/include/helper/page.js @@ -4,31 +4,13 @@ * @example * <%- is_categories(page) %> * <%- is_tags(page) %> -* <%- has_thumbnail(post) %> -* <%- get_thumbnail(post) %> */ module.exports = function(hexo) { hexo.extend.helper.register('is_categories', function(page = null) { - return (page === null ? this.page : page).__categories; + return (page === null ? this.page : page).__categories === true; }); hexo.extend.helper.register('is_tags', function(page = null) { - return (page === null ? this.page : page).__tags; - }); - - hexo.extend.helper.register('has_thumbnail', function(post) { - const { article } = this.config; - if (typeof post !== 'object') { - return false; - } - if (article && article.thumbnail === false) { - return false; - } - return 'thumbnail' in post && post.thumbnail; - }); - - hexo.extend.helper.register('get_thumbnail', function(post) { - const { url_for, has_thumbnail } = this.helper; - return url_for(has_thumbnail.call(this, post) ? post.thumbnail : '/img/thumbnail.svg'); + return (page === null ? this.page : page).__tags === true; }); }; diff --git a/include/helper/thumbnail.js b/include/helper/thumbnail.js new file mode 100644 index 0000000..98f9a51 --- /dev/null +++ b/include/helper/thumbnail.js @@ -0,0 +1,24 @@ +/** +* Helper functions for post thumbnail. +* +* @example +* <%- has_thumbnail(post) %> +* <%- get_thumbnail(post) %> +*/ +module.exports = function(hexo) { + hexo.extend.helper.register('has_thumbnail', function(post) { + const { article } = this.config; + if (typeof post !== 'object') { + return false; + } + if (article && article.thumbnail === false) { + return false; + } + return 'thumbnail' in post && post.thumbnail; + }); + + hexo.extend.helper.register('get_thumbnail', function(post) { + const { url_for, has_thumbnail } = this.helper; + return url_for(has_thumbnail.call(this, post) ? post.thumbnail : '/img/thumbnail.svg'); + }); +}; diff --git a/include/schema/common/comment.json b/include/schema/common/comment.json index d01e8da..5a68c06 100644 --- a/include/schema/common/comment.json +++ b/include/schema/common/comment.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "/common/comment.json", - "description": "Comment plugin configurations", + "description": "Comment plugin configurations\nhttps://ppoffice.github.io/hexo-theme-icarus/categories/Plugins/Comment/", "type": "object", "oneOf": [ { diff --git a/include/schema/common/providers.json b/include/schema/common/providers.json index 398003c..eea32bd 100644 --- a/include/schema/common/providers.json +++ b/include/schema/common/providers.json @@ -18,7 +18,7 @@ }, "iconcdn": { "type": "string", - "description": "Name or URL of the webfont Icon CDN provider", + "description": "Name or URL of the fontawesome icon font CDN provider", "default": "fontawesome", "nullable": true } diff --git a/include/schema/common/search.json b/include/schema/common/search.json index 72df43e..d4f851c 100644 --- a/include/schema/common/search.json +++ b/include/schema/common/search.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "/common/search.json", - "description": "Search plugin configurations", + "description": "Search plugin configurations\nhttps://ppoffice.github.io/hexo-theme-icarus/categories/Plugins/Search/", "type": "object", "oneOf": [ { diff --git a/include/schema/common/share.json b/include/schema/common/share.json index b9314fb..dd6d228 100644 --- a/include/schema/common/share.json +++ b/include/schema/common/share.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "/common/share.json", - "description": "Share plugin configurations", + "description": "Share plugin configurations\nhttps://ppoffice.github.io/hexo-theme-icarus/categories/Plugins/Share/", "type": "object", "oneOf": [ { diff --git a/include/schema/common/widgets.json b/include/schema/common/widgets.json index 3be97f6..dab6518 100644 --- a/include/schema/common/widgets.json +++ b/include/schema/common/widgets.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "/common/widgets.json", - "description": "Sidebar widget configurations", + "description": "Sidebar widget configurations\nhttp://ppoffice.github.io/hexo-theme-icarus/categories/Widgets/", "type": "array", "items": { "type": "object", diff --git a/include/util/schema.js b/include/util/schema.js index 8f25557..abbfb03 100644 --- a/include/util/schema.js +++ b/include/util/schema.js @@ -157,7 +157,8 @@ class Schema { } const defaultValue = new DefaultValue(value, def.description); if ('oneOf' in def && Array.isArray(def.oneOf) && def.oneOf.length) { - return defaultValue.merge(this.getDefaultValue(def.oneOf[0])); + defaultValue.merge(this.getDefaultValue(def.oneOf[0])); + defaultValue.description = def.description; } return defaultValue; } diff --git a/layout/archive.jsx b/layout/archive.jsx index b3e5f4c..0283073 100644 --- a/layout/archive.jsx +++ b/layout/archive.jsx @@ -19,7 +19,7 @@ module.exports = class extends Component { {posts.map(post => { const categories = []; post.categories.forEach((category, i) => { - categories.push({category.name}); + categories.push({category.name}); if (i < post.categories.length - 1) { categories.push(' / '); } diff --git a/layout/comment/disqusjs.jsx b/layout/comment/disqusjs.jsx index 922fb4b..fb22307 100644 --- a/layout/comment/disqusjs.jsx +++ b/layout/comment/disqusjs.jsx @@ -34,7 +34,7 @@ class DisqusJs extends Component { api: '${api}', admin: '${admin}', adminLabel: '${adminLabel}', - nesting: ${nesting}%> + nesting: ${nesting} });`; return diff --git a/layout/comment/gitalk.jsx b/layout/comment/gitalk.jsx index ac41392..ab1d27e 100644 --- a/layout/comment/gitalk.jsx +++ b/layout/comment/gitalk.jsx @@ -25,7 +25,7 @@ class Gitalk extends Component { if (!id || !repo || !owner || !admin || !clientId || !clientSecret) { return
You forgot to set the owner, admin, repo, - clientId, or clientSecret for Gittalk. + client_id, or client_secret for Gitalk. Please set it in _config.yml.
; } diff --git a/layout/common/article.jsx b/layout/common/article.jsx index e8984b2..958f1b4 100644 --- a/layout/common/article.jsx +++ b/layout/common/article.jsx @@ -43,7 +43,7 @@ module.exports = class extends Component { {(() => { const categories = []; page.categories.forEach((category, i) => { - categories.push({category.name}); + categories.push({category.name}); if (i < page.categories.length - 1) { categories.push( / ); } diff --git a/scripts/index.js b/scripts/index.js index 3e1ac5b..022fd50 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -121,6 +121,7 @@ require('../include/generator/insight')(hexo); require('../include/filter/locals')(hexo); require('../include/helper/cdn')(hexo); require('../include/helper/page')(hexo); +require('../include/helper/thumbnail')(hexo); /** * Remove Hexo filters that could cause OOM