From d1bd500a14dd78b69a51f864399f62fb8500fff6 Mon Sep 17 00:00:00 2001 From: ppoffice Date: Sat, 27 Oct 2018 12:16:23 -0400 Subject: [PATCH] fix(cdn): patch cdn.js to make jsdelivr and unpkg work --- README.md | 2 +- includes/helpers/cdn.js | 36 +++++++++++++++++++++++------- includes/specs/providers.spec.js | 2 +- layout/common/head.ejs | 4 ++-- layout/common/scripts.ejs | 4 ++-- layout/plugin/gallery.ejs | 8 +++---- layout/plugin/outdated-browser.ejs | 4 ++-- layout/share/sharejs.ejs | 4 ++-- 8 files changed, 42 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 86f63a4..05651f0 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Share plugins - [AddThis](http://ppoffice.github.io/hexo-theme-icarus/Plugins/Share/addthis-share-plugin/) - [AddToAny](http://ppoffice.github.io/hexo-theme-icarus/Plugins/Share/addtoany-share-plugin/) - [Baidu Share](http://ppoffice.github.io/hexo-theme-icarus/Plugins/Share/baidu-share-plugin/) -- [Share.js](http://ppoffice.github.io/hexo-theme-icarus/Plugins/Share/sharejs-share-plugin/) +- [Share.js](http://ppoffice.github.io/hexo-theme-icarus/Plugins/Share/share-js-share-plugin/) - [ShareThis](http://ppoffice.github.io/hexo-theme-icarus/Plugins/Share/sharethis-share-plugin/) Other plugins diff --git a/includes/helpers/cdn.js b/includes/helpers/cdn.js index 14f1b61..2a45615 100644 --- a/includes/helpers/cdn.js +++ b/includes/helpers/cdn.js @@ -9,36 +9,56 @@ const cdn_providers = { cdnjs: 'https://cdnjs.cloudflare.com/ajax/libs/${ package }/${ version }/${ filename }', jsdelivr: 'https://cdn.jsdelivr.net/npm/${ package }@${ version }/${ filename }', - jsdelivr_github: 'https://cdn.jsdelivr.net/gh/user/${ package }@${ version }/${ filename }', unpkg: 'https://unpkg.com/${ package }@${ version }/${ filename }' }; const font_providers = { - google: 'https://fonts.googleapis.com/css?family=${ fontname }' + google: 'https://fonts.googleapis.com/${ type }?family=${ fontname }' }; const icon_providers = { - fontawesome: 'https://use.fontawesome.com/releases/v5.4.1/css/all.css', - material: 'https://fonts.googleapis.com/icon?family=Material+Icons' + fontawesome: 'https://use.fontawesome.com/releases/v5.4.1/css/all.css' }; module.exports = function (hexo) { - hexo.extend.helper.register('cdn', function (package, version, filename) { + hexo.extend.helper.register('cdn', function (_package, version, filename) { let provider = hexo.extend.helper.get('get_config').bind(this)('providers.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 (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 (provider !== null && cdn_providers.hasOwnProperty(provider)) { provider = cdn_providers[provider]; } - return provider.replace(/\${\s*package\s*}/gi, package) + return provider.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) { + hexo.extend.helper.register('fontcdn', function (fontName, type = 'css') { let provider = hexo.extend.helper.get('get_config').bind(this)('providers.fontcdn'); if (provider !== null && font_providers.hasOwnProperty(provider)) { provider = font_providers[provider]; } - return provider.replace(/\${\s*fontname\s*}/gi, fontName); + return provider.replace(/\${\s*fontname\s*}/gi, fontName) + .replace(/\${\s*type\s*}/gi, type); }); hexo.extend.helper.register('iconcdn', function (provider = null) { diff --git a/includes/specs/providers.spec.js b/includes/specs/providers.spec.js index 2b7af1a..570d8d2 100644 --- a/includes/specs/providers.spec.js +++ b/includes/specs/providers.spec.js @@ -6,7 +6,7 @@ module.exports = { cdn: { [type]: 'string', [doc]: 'Name or URL of the JavaScript and/or stylesheet CDN provider', - [defaultValue]: 'cdnjs' + [defaultValue]: 'jsdelivr' }, fontcdn: { [type]: 'string', diff --git a/layout/common/head.ejs b/layout/common/head.ejs index 26c4fcc..4a50b78 100644 --- a/layout/common/head.ejs +++ b/layout/common/head.ejs @@ -23,9 +23,9 @@ <%- css(cdn('bulma', '0.7.2', 'css/bulma.css')) %> <%- css(iconcdn()) %> -<%- css(iconcdn('material')) %> +<%- css(fontcdn('Material+Icons', 'icon')) %> <%- css(fontcdn('Ubuntu:400,600|Source+Code+Pro')) %> -<%- css(cdn('highlight.js', '9.12.0', 'styles/' + get_config('article.highlight') + '.min.css')) %> +<%- css(cdn('highlight.js', '9.12.0', 'styles/' + get_config('article.highlight') + '.css')) %> <%- css('css/style') %> <% if (has_config('plugins')) { %> diff --git a/layout/common/scripts.ejs b/layout/common/scripts.ejs index b3b5bc2..1584921 100644 --- a/layout/common/scripts.ejs +++ b/layout/common/scripts.ejs @@ -1,5 +1,5 @@ -<%- js(cdn('jquery', '3.3.1', 'jquery.min.js')) %> -<%- js(cdn('moment.js', '2.22.2', 'moment-with-locales.min.js')) %> +<%- js(cdn('jquery', '3.3.1', 'dist/jquery.min.js')) %> +<%- js(cdn('moment', '2.22.2', 'min/moment-with-locales.min.js')) %> <% if (has_config('plugins')) { %> diff --git a/layout/plugin/gallery.ejs b/layout/plugin/gallery.ejs index 3c1ce42..12ef1ef 100644 --- a/layout/plugin/gallery.ejs +++ b/layout/plugin/gallery.ejs @@ -1,10 +1,10 @@ <% if (plugin !== false) { %> <% if (head) { %> - <%- css(cdn('lightgallery', '1.6.8', 'css/lightgallery.min.css')) %> - <%- css(cdn('justifiedGallery', '3.7.0', 'css/justifiedGallery.min.css')) %> + <%- css(cdn('lightgallery', '1.6.8', 'dist/css/lightgallery.min.css')) %> + <%- css(cdn('justifiedGallery', '3.7.0', 'dist/css/justifiedGallery.min.css')) %> <% } else { %> - <%- js(cdn('lightgallery', '1.6.8', 'js/lightgallery-all.min.js')) %> - <%- js(cdn('justifiedGallery', '3.7.0', 'js/jquery.justifiedGallery.min.js')) %> + <%- js(cdn('lightgallery', '1.6.8', 'dist/js/lightgallery.min.js')) %> + <%- js(cdn('justifiedGallery', '3.7.0', 'dist/js/jquery.justifiedGallery.min.js')) %>