Refactor next_js to support jsdelivr & unpkg

This commit is contained in:
Mimi 2020-06-22 17:13:37 +08:00
parent 40e32d1821
commit ae21b8b8e0
6 changed files with 62 additions and 55 deletions

View File

@ -1,3 +1,9 @@
# ===============================================================
# It's recommended to use Alternate Theme Config to configure the theme
# Modifying this file may result in merge conflict
# See: https://theme-next.js.org/docs/getting-started/configuration
# ===============================================================
# --------------------------------------------------------------- # ---------------------------------------------------------------
# Theme Core Configuration Settings # Theme Core Configuration Settings
# See: https://theme-next.js.org/docs/theme-settings/ # See: https://theme-next.js.org/docs/theme-settings/
@ -480,9 +486,6 @@ baidu_push: false
# See: https://theme-next.js.org/docs/third-party-services/ # See: https://theme-next.js.org/docs/third-party-services/
# More plugins: https://github.com/next-theme/awesome-next # More plugins: https://github.com/next-theme/awesome-next
# You may need to install dependencies or set CDN URLs in `vendors` # You may need to install dependencies or set CDN URLs in `vendors`
# There are two different CDN providers by default:
# - jsDelivr (cdn.jsdelivr.net), works everywhere even in China
# - CDNJS (cdnjs.cloudflare.com), provided by cloudflare
# --------------------------------------------------------------- # ---------------------------------------------------------------
# Math Formulas Render Support # Math Formulas Render Support
@ -857,47 +860,49 @@ canvas_ribbon:
zIndex: -1 # The display level of the ribbon zIndex: -1 # The display level of the ribbon
#! --------------------------------------------------------------- #! ==============================================================
#! DO NOT EDIT THE FOLLOWING SETTINGS #! DO NOT EDIT THE FOLLOWING SETTINGS
#! UNLESS YOU KNOW WHAT YOU ARE DOING #! UNLESS YOU KNOW WHAT YOU ARE DOING
#! See: https://theme-next.js.org/docs/advanced-settings #! See: https://theme-next.js.org/docs/advanced-settings
#! --------------------------------------------------------------- #! ==============================================================
# Script Vendors. Set a CDN address for the vendor you want to customize. # Script Vendors. Set a CDN address for the vendor you want to customize.
# Be aware that you would better use the same version as internal ones to avoid potential problems. # CDN URLs from UNPKG and CDNJS are provided in the comments for selection.
# If left blank, jsDelivr will be used as the default CDN provider.
# It's recommended to use the same version as internal ones to avoid potential problems.
# Remember to use the https protocol of CDN files when you enable https on your site. # Remember to use the https protocol of CDN files when you enable https on your site.
vendors: vendors:
# Internal path prefix. # Available values: local, jsdelivr, unpkg
_internal: lib internal: local
# Internal version: 3.1.0 # Internal version: 3.1.0
# anime: //cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js # anime: //unpkg.com/animejs@3.1.0/lib/anime.min.js
# anime: //cdnjs.cloudflare.com/ajax/libs/animejs/3.1.0/anime.min.js # anime: //cdnjs.cloudflare.com/ajax/libs/animejs/3.1.0/anime.min.js
anime: anime:
# Internal version: 5.13.0 # Internal version: 5.13.0
# fontawesome: //cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5/css/all.min.css # fontawesome: //unpkg.com/@fortawesome/fontawesome-free@5/css/all.min.css
# fontawesome: //cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css # fontawesome: //cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css
fontawesome: fontawesome:
# Prism # Prism
# prism: //cdn.jsdelivr.net/npm/prismjs@1/components/prism-core.min.js # prism: //unpkg.com/prismjs@1/components/prism-core.min.js
# prism_autoloader: //cdn.jsdelivr.net/npm/prismjs@1/plugins/autoloader/prism-autoloader.min.js # prism_autoloader: //unpkg.com/prismjs@1/plugins/autoloader/prism-autoloader.min.js
# prism_line_numbers: //cdn.jsdelivr.net/npm/prismjs@1/plugins/line-numbers/prism-line-numbers.min.js # prism_line_numbers: //unpkg.com/prismjs@1/plugins/line-numbers/prism-line-numbers.min.js
prism: prism:
prism_autoloader: prism_autoloader:
prism_line_numbers: prism_line_numbers:
# MathJax # MathJax
# mathjax: //cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js # mathjax: //unpkg.com/mathjax@3/es5/tex-mml-chtml.js
# mathjax: //cdnjs.cloudflare.com/ajax/libs/mathjax/3.0.5/es5/tex-mml-chtml.js # mathjax: //cdnjs.cloudflare.com/ajax/libs/mathjax/3.0.5/es5/tex-mml-chtml.js
mathjax: mathjax:
# KaTeX # KaTeX
# katex: //cdn.jsdelivr.net/npm/katex@0/dist/katex.min.css # katex: //unpkg.com/katex@0/dist/katex.min.css
# katex: //cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css # katex: //cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css
# copy_tex_js: //cdn.jsdelivr.net/npm/katex@0/dist/contrib/copy-tex.min.js # copy_tex_js: //unpkg.com/katex@0/dist/contrib/copy-tex.min.js
# copy_tex_css: //cdn.jsdelivr.net/npm/katex@0/dist/contrib/copy-tex.min.css # copy_tex_css: //unpkg.com/katex@0/dist/contrib/copy-tex.min.css
katex: katex:
copy_tex_js: copy_tex_js:
copy_tex_css: copy_tex_css:
@ -907,79 +912,79 @@ vendors:
pjax: pjax:
# FancyBox # FancyBox
# jquery: //cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js # jquery: //unpkg.com/jquery@3/dist/jquery.min.js
# fancybox: //cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js # fancybox: //unpkg.com/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js
# fancybox_css: //cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css # fancybox_css: //unpkg.com/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css
jquery: jquery:
fancybox: fancybox:
fancybox_css: fancybox_css:
# Medium-zoom # Medium-zoom
# mediumzoom: //cdn.jsdelivr.net/npm/medium-zoom@1/dist/medium-zoom.min.js # mediumzoom: //unpkg.com/medium-zoom@1/dist/medium-zoom.min.js
# mediumzoom: //cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.0.5/medium-zoom.min.js # mediumzoom: //cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.0.5/medium-zoom.min.js
mediumzoom: mediumzoom:
# Lazyload # Lazyload
# lazyload: //cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js # lazyload: //unpkg.com/lozad@1/dist/lozad.min.js
# lazyload: //cdnjs.cloudflare.com/ajax/libs/lozad.js/1.15.0/lozad.min.js # lazyload: //cdnjs.cloudflare.com/ajax/libs/lozad.js/1.15.0/lozad.min.js
lazyload: lazyload:
# Pangu # Pangu
# pangu: //cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js # pangu: //unpkg.com/pangu@4/dist/browser/pangu.min.js
# pangu: //cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js # pangu: //cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js
pangu: pangu:
# Quicklink # Quicklink
# quicklink: //cdn.jsdelivr.net/npm/quicklink@2/dist/quicklink.umd.js # quicklink: //unpkg.com/quicklink@2/dist/quicklink.umd.js
# quicklink: //cdnjs.cloudflare.com/ajax/libs/quicklink/2.0.0/quicklink.umd.js # quicklink: //cdnjs.cloudflare.com/ajax/libs/quicklink/2.0.0/quicklink.umd.js
quicklink: quicklink:
# DisqusJS # DisqusJS
# disqusjs_js: //cdn.jsdelivr.net/npm/disqusjs@1/dist/disqus.js # disqusjs_js: //unpkg.com/disqusjs@1/dist/disqus.js
# disqusjs_css: //cdn.jsdelivr.net/npm/disqusjs@1/dist/disqusjs.css # disqusjs_css: //unpkg.com/disqusjs@1/dist/disqusjs.css
disqusjs_js: disqusjs_js:
disqusjs_css: disqusjs_css:
# Valine # Valine
# valine: //cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js # valine: //unpkg.com/valine@1/dist/Valine.min.js
# valine: //cdnjs.cloudflare.com/ajax/libs/valine/1.4.14/Valine.min.js # valine: //cdnjs.cloudflare.com/ajax/libs/valine/1.4.14/Valine.min.js
valine: valine:
# Gitalk # Gitalk
# gitalk_js: //cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js # gitalk_js: //unpkg.com/gitalk@1/dist/gitalk.min.js
# gitalk_css: //cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css # gitalk_css: //unpkg.com/gitalk@1/dist/gitalk.min.css
gitalk_js: gitalk_js:
gitalk_css: gitalk_css:
# Algolia Search # Algolia Search
# algolia_search: //cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js # algolia_search: //unpkg.com/algoliasearch@4/dist/algoliasearch-lite.umd.js
# instant_search: //cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js # instant_search: //unpkg.com/instantsearch.js@4/dist/instantsearch.production.min.js
algolia_search: algolia_search:
instant_search: instant_search:
# Mermaid # Mermaid
# mermaid: //cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js # mermaid: //unpkg.com/mermaid@8/dist/mermaid.min.js
# mermaid: //cdnjs.cloudflare.com/ajax/libs/mermaid/8.5.2/mermaid.min.js # mermaid: //cdnjs.cloudflare.com/ajax/libs/mermaid/8.5.2/mermaid.min.js
mermaid: mermaid:
# Internal version: 1.2.1 # Internal version: 1.2.1
# velocity: //cdn.jsdelivr.net/npm/velocity-animate@1/velocity.min.js # velocity: //unpkg.com/velocity-animate@1/velocity.min.js
# velocity: //cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.min.js # velocity: //cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.min.js
# velocity_ui: //cdn.jsdelivr.net/npm/velocity-animate@1/velocity.ui.min.js # velocity_ui: //unpkg.com/velocity-animate@1/velocity.ui.min.js
# velocity_ui: //cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.ui.min.js # velocity_ui: //cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.ui.min.js
velocity: velocity:
velocity_ui: velocity_ui:
# Internal version: 1.0.2 # Internal version: 1.0.2
# pace: //cdn.jsdelivr.net/npm/pace-js@1/pace.min.js # pace: //unpkg.com/pace-js@1/pace.min.js
# pace: //cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js # pace: //cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js
# pace_css: //cdn.jsdelivr.net/npm/pace-js@1/themes/blue/pace-theme-minimal.min.css # pace_css: //unpkg.com/pace-js@1/themes/blue/pace-theme-minimal.min.css
# pace_css: //cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css # pace_css: //cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css
pace: pace:
pace_css: pace_css:
# JavaScript 3D library # JavaScript 3D library
# three: //cdn.jsdelivr.net/npm/three@0/build/three.min.js # three: //unpkg.com/three@0/build/three.min.js
# three_waves: //cdn.jsdelivr.net/gh/next-theme/theme-next-three@1/three-waves.min.js # three_waves: //cdn.jsdelivr.net/gh/next-theme/theme-next-three@1/three-waves.min.js
# canvas_lines: //cdn.jsdelivr.net/gh/next-theme/theme-next-three@1/canvas_lines.min.js # canvas_lines: //cdn.jsdelivr.net/gh/next-theme/theme-next-three@1/canvas_lines.min.js
# canvas_sphere: //cdn.jsdelivr.net/gh/next-theme/theme-next-three@1/canvas_sphere.min.js # canvas_sphere: //cdn.jsdelivr.net/gh/next-theme/theme-next-three@1/canvas_sphere.min.js
@ -989,7 +994,7 @@ vendors:
canvas_sphere: canvas_sphere:
# Canvas ribbon # Canvas ribbon
# canvas_ribbon: //cdn.jsdelivr.net/npm/ribbon.js@1/dist/ribbon.min.js # canvas_ribbon: //unpkg.com/ribbon.js@1/dist/ribbon.min.js
canvas_ribbon: canvas_ribbon:
# Assets # Assets

View File

@ -43,7 +43,7 @@
{{ next_font() }} {{ next_font() }}
{%- set font_awesome_uri = theme.vendors.fontawesome or next_vendors('font-awesome/css/all.min.css') %} {%- set font_awesome_uri = theme.vendors.fontawesome or url_for('lib/font-awesome/css/all.min.css') %}
<link rel="stylesheet" href="{{ font_awesome_uri }}"> <link rel="stylesheet" href="{{ font_awesome_uri }}">
{%- if theme.fancybox %} {%- if theme.fancybox %}

View File

@ -2,15 +2,15 @@
{%- set three_uri = theme.vendors.three or '//cdn.jsdelivr.net/npm/three@0/build/three.min.js' %} {%- set three_uri = theme.vendors.three or '//cdn.jsdelivr.net/npm/three@0/build/three.min.js' %}
<script defer src="{{ three_uri }}"></script> <script defer src="{{ three_uri }}"></script>
{%- if theme.three.three_waves %} {%- if theme.three.three_waves %}
{%- set waves_uri = theme.vendors.three_waves or next_vendors('three/three-waves.min.js') %} {%- set waves_uri = theme.vendors.three_waves or url_for('lib/three/three-waves.min.js') %}
<script defer src="{{ waves_uri }}"></script> <script defer src="{{ waves_uri }}"></script>
{%- endif %} {%- endif %}
{%- if theme.three.canvas_lines %} {%- if theme.three.canvas_lines %}
{%- set lines_uri = theme.vendors.canvas_lines or next_vendors('three/canvas_lines.min.js') %} {%- set lines_uri = theme.vendors.canvas_lines or url_for('lib/three/canvas_lines.min.js') %}
<script defer src="{{ lines_uri }}"></script> <script defer src="{{ lines_uri }}"></script>
{%- endif %} {%- endif %}
{%- if theme.three.canvas_sphere %} {%- if theme.three.canvas_sphere %}
{%- set sphere_uri = theme.vendors.canvas_sphere or next_vendors('three/canvas_sphere.min.js') %} {%- set sphere_uri = theme.vendors.canvas_sphere or url_for('lib/three/canvas_sphere.min.js') %}
<script defer src="{{ sphere_uri }}"></script> <script defer src="{{ sphere_uri }}"></script>
{%- endif %} {%- endif %}
{%- endif %} {%- endif %}
@ -21,6 +21,6 @@
{%- endif %} {%- endif %}
{%- for name, internal in js_vendors() %} {%- for name, internal in js_vendors() %}
{%- set internal_script = next_vendors(internal) %} {%- set internal_script = url_for(internal) %}
<script src="{{ theme.vendors[name] or internal_script }}"></script> <script src="{{ theme.vendors[name] or internal_script }}"></script>
{%- endfor %} {%- endfor %}

View File

@ -1,5 +1,5 @@
{%- if theme.pdf.enable %} {%- if theme.pdf.enable %}
{%- set pdf_uri = next_vendors('pdf/web/viewer.html') %} {%- set pdf_uri = url_for('lib/pdf/web/viewer.html') %}
<script{{ pjax }}> <script{{ pjax }}>
document.querySelectorAll('.pdfobject-container').forEach(element => { document.querySelectorAll('.pdfobject-container').forEach(element => {
let url = element.dataset.target; let url = element.dataset.target;

View File

@ -10,15 +10,17 @@ hexo.extend.helper.register('next_inject', function(point) {
.join(''); .join('');
}); });
hexo.extend.helper.register('next_js', function(...urls) { hexo.extend.helper.register('next_js', function(url) {
const { next_version } = this;
const { js } = this.theme; const { js } = this.theme;
return urls.map(url => this.js(`${js}/${url}`)).join(''); const { internal } = this.theme.vendors;
}); let src = `${js}/${url}`;
if (internal === 'jsdelivr') {
hexo.extend.helper.register('next_vendors', function(url) { src = `//cdn.jsdelivr.net/npm/hexo-theme-next@${next_version}/source/js/${url}`;
if (url.startsWith('//')) return url; } else if (internal === 'unpkg') {
const internal = this.theme.vendors._internal; src = `//unpkg.com/hexo-theme-next@${next_version}/source/js/${url}`;
return this.url_for(`${internal}/${url}`); }
return this.js(src);
}); });
hexo.extend.helper.register('post_edit', function(src) { hexo.extend.helper.register('post_edit', function(src) {

View File

@ -5,7 +5,7 @@
hexo.extend.helper.register('js_vendors', function() { hexo.extend.helper.register('js_vendors', function() {
let { config, theme } = this; let { config, theme } = this;
let vendors = { let vendors = {
anime: 'anime.min.js' anime: 'lib/anime.min.js'
}; };
if (config.prismjs.enable && !config.prismjs.preprocess) { if (config.prismjs.enable && !config.prismjs.preprocess) {
vendors.prism = '//cdn.jsdelivr.net/npm/prismjs@1/components/prism-core.min.js'; vendors.prism = '//cdn.jsdelivr.net/npm/prismjs@1/components/prism-core.min.js';
@ -31,8 +31,8 @@ hexo.extend.helper.register('js_vendors', function() {
vendors.pangu = '//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js'; vendors.pangu = '//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js';
} }
if (theme.motion.enable) { if (theme.motion.enable) {
vendors.velocity = 'velocity/velocity.min.js'; vendors.velocity = 'lib/velocity/velocity.min.js';
vendors.velocity_ui = 'velocity/velocity.ui.min.js'; vendors.velocity_ui = 'lib/velocity/velocity.ui.min.js';
} }
return vendors; return vendors;
}); });