From 3685685b28bbd107a738f5612f62be63675fdc38 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Wed, 9 Feb 2022 22:32:50 +0800 Subject: [PATCH] Make the source of the static assets configurable - Easy to switch between different CDN - Allow self-host static assets --- _config.yml | 10 ++++- _data/assets/cross_origin.yml | 65 ++++++++++++++++++++++++++++++ _data/assets/self_host.yml | 54 +++++++++++++++++++++++++ _includes/assets-origin.html | 12 ++++++ _includes/comments/utterances.html | 2 +- _includes/css-selector.html | 15 ------- _includes/head.html | 41 +++++++++++++------ _includes/js-selector.html | 51 ++++++++++++++--------- _includes/jsdelivr-combine.html | 32 +++++++++++++++ _includes/mermaid.html | 2 +- _includes/search-loader.html | 2 +- _includes/toc.html | 2 +- _layouts/default.html | 2 + _layouts/page.html | 2 + _sass/addon/commons.scss | 1 - 15 files changed, 240 insertions(+), 53 deletions(-) create mode 100644 _data/assets/cross_origin.yml create mode 100644 _data/assets/self_host.yml create mode 100644 _includes/assets-origin.html delete mode 100644 _includes/css-selector.html create mode 100644 _includes/jsdelivr-combine.html diff --git a/_config.yml b/_config.yml index 45ca553..f364986 100644 --- a/_config.yml +++ b/_config.yml @@ -53,7 +53,7 @@ social: google_site_verification: # fill in to your verification string # ↑ -------------------------- - +# The end of `jekyll-seo-tag` settings google_analytics: id: # fill in your Google Analytics ID @@ -98,6 +98,14 @@ comments: repo: # / issue_term: # < url | pathname | title | ...> +# Self-hosted static assets, optional › https://github.com/cotes2020/chirpy-static-assets +assets: + self_host: + enabled: # boolean, keep empty means false + # specify the Jekyll environment, empty means both + # only works if `assets.self_host.enabled` is 'true' + env: # [development|production] + paginate: 10 # ------------ The following options are not recommended to be modified ------------------ diff --git a/_data/assets/cross_origin.yml b/_data/assets/cross_origin.yml new file mode 100644 index 0000000..4fda9bc --- /dev/null +++ b/_data/assets/cross_origin.yml @@ -0,0 +1,65 @@ +# CDNs + +cdns: + # Google Fonts + - url: https://fonts.googleapis.com + - url: https://fonts.gstatic.com + args: crossorigin + - url: https://fonts.googleapis.com + # jsDelivr CDN + - url: https://cdn.jsdelivr.net + +# fonts + +webfonts: https://fonts.googleapis.com/css2?family=Lato&family=Source+Sans+Pro:wght@400;600;700;900&display=swap + +# Libraries + +jquery: + js: https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js + +bootstrap: + css: https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css + js: https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.bundle.min.js + +bootstrap-toc: + css: https://cdn.jsdelivr.net/gh/afeld/bootstrap-toc@1.0.1/dist/bootstrap-toc.min.css + js: https://cdn.jsdelivr.net/gh/afeld/bootstrap-toc@1.0.1/dist/bootstrap-toc.min.js + +fontawesome: + css: https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.11.2/css/all.min.css + +search: + js: https://cdn.jsdelivr.net/npm/simple-jekyll-search@1.10.0/dest/simple-jekyll-search.min.js + +mermaid: + js: https://cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js + +dayjs: + js: + common: https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js + locale: https://cdn.jsdelivr.net/npm/dayjs@1/locale/:LOCALE.min.js + relativeTime: https://cdn.jsdelivr.net/npm/dayjs@1/plugin/relativeTime.min.js + localizedFormat: https://cdn.jsdelivr.net/npm/dayjs@1/plugin/localizedFormat.min.js + +countup: + js: https://cdn.jsdelivr.net/npm/countup.js@1.9.3/dist/countUp.min.js + +magnific-popup: + css: https://cdn.jsdelivr.net/npm/magnific-popup@1/dist/magnific-popup.min.css + js: https://cdn.jsdelivr.net/npm/magnific-popup@1/dist/jquery.magnific-popup.min.js + +lozad: + js: https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js + +clipboard: + js: https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js + +polyfill: + js: https://polyfill.io/v3/polyfill.min.js?features=es6 + +mathjax: + js: https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js + +utterances: + js: https://utteranc.es/client.js diff --git a/_data/assets/self_host.yml b/_data/assets/self_host.yml new file mode 100644 index 0000000..3d662d6 --- /dev/null +++ b/_data/assets/self_host.yml @@ -0,0 +1,54 @@ +# fonts + +webfonts: /assets/lib/fonts/main.css + +# Libraries + +jquery: + js: /assets/lib/jquery-3.6.0/jquery.min.js + +bootstrap: + css: /assets/lib/bootstrap-4.6.1/bootstrap.min.css + js: /assets/lib/bootstrap-4.6.1/bootstrap.bundle.min.js + +bootstrap-toc: + css: /assets/lib/bootstrap-toc-1.0.1/bootstrap-toc.min.css + js: /assets/lib/bootstrap-toc-1.0.1/bootstrap-toc.min.js + +fontawesome: + css: /assets/lib/fontawesome-free-5.15.4/css/all.min.css + +search: + js: /assets/lib/simple-jekyll-search-1.10.0/simple-jekyll-search.min.js + +mermaid: + js: /assets/lib/mermaid-8.13.10/mermaid.min.js + +dayjs: + js: + common: /assets/lib/dayjs-1.10.7/dayjs.min.js + locale: /assets/lib/dayjs-1.10.7/locale/en.min.js + relativeTime: /assets/lib/dayjs-1.10.7/plugin/relativeTime.min.js + localizedFormat: /assets/lib/dayjs-1.10.7/plugin/localizedFormat.min.js + +countup: + js: /assets/lib/countup.js-1.9.3/countUp.min.js + +magnific-popup: + css: /assets/lib/magnific-popup-1.1.0/magnific-popup.css + js: /assets/lib/magnific-popup-1.1.0/jquery.magnific-popup.min.js + +lozad: + js: /assets/lib/lozad-1.16.0/lozad.min.js + +clipboard: + js: /assets/lib/clipboard-2.0.9/clipboard.min.js + +polyfill: + js: /assets/lib/polyfill-v3-es6/polyfill.min.js + +mathjax: + js: /assets/lib/mathjax-3.2.0/tex-chtml.js + +utterances: + js: /assets/lib/utterances/client.js diff --git a/_includes/assets-origin.html b/_includes/assets-origin.html new file mode 100644 index 0000000..841e252 --- /dev/null +++ b/_includes/assets-origin.html @@ -0,0 +1,12 @@ +{% comment %} Site static assets origin {% endcomment %} +{% assign origin = 'cross_origin' %} + +{% if site.assets.self_host.enabled %} + {% if site.assets.self_host.env %} + {% if site.assets.self_host.env == jekyll.environment %} + {% assign origin = 'self_host' %} + {% endif %} + {% else %} + {% assign origin = 'self_host' %} + {% endif %} +{% endif %} diff --git a/_includes/comments/utterances.html b/_includes/comments/utterances.html index afd7cd3..3b6303e 100644 --- a/_includes/comments/utterances.html +++ b/_includes/comments/utterances.html @@ -1,5 +1,5 @@ - + diff --git a/_includes/js-selector.html b/_includes/js-selector.html index 0b32169..c0402bf 100644 --- a/_includes/js-selector.html +++ b/_includes/js-selector.html @@ -7,21 +7,27 @@ {% if page.layout == 'post' %} {% if site.google_analytics.pv.proxy_endpoint or site.google_analytics.pv.cache_path %} - + {% endif %} {% endif %} {% if page.layout == 'post' or page.layout == 'page' %} - - + + {% assign _urls = site.data.assets[origin].magnific-popup.js + | append: ',' | append: site.data.assets[origin].lozad.js + | append: ',' | append: site.data.assets[origin].clipboard.js + %} + + {% include jsdelivr-combine.html urls=_urls %} + {% endif %} {% if page.layout == 'home' - or page.layout == 'post' - or page.layout == 'archives' - or page.layout == 'category' - or page.layout == 'tag' %} + or page.layout == 'post' + or page.layout == 'archives' + or page.layout == 'category' + or page.layout == 'tag' %} {% if site.prefer_datetime_locale %} {% assign locale = site.prefer_datetime_locale | downcase %} @@ -29,24 +35,32 @@ {% assign locale = site.lang | split: '-' | first %} {% endif %} - + {% assign _urls = site.data.assets[origin].dayjs.js.common + | append: ',' | append: site.data.assets[origin].dayjs.js.locale + | replace: ':LOCALE', locale + | append: ',' | append: site.data.assets[origin].dayjs.js.relativeTime + | append: ',' | append: site.data.assets[origin].dayjs.js.localizedFormat + %} + + {% include jsdelivr-combine.html urls=_urls %} + {% endif %} {% if page.layout == 'home' - or page.layout == 'categories' - or page.layout == 'post' - or page.layout == 'page' %} + or page.layout == 'categories' + or page.layout == 'post' + or page.layout == 'page' %} {% assign type = page.layout %} {% elsif page.layout == 'archives' - or page.layout == 'category' - or page.layout == 'tag' %} + or page.layout == 'category' + or page.layout == 'tag' %} {% assign type = "misc" %} {% else %} {% assign type = "commons" %} {% endif %} -{% assign js = type | prepend: '/assets/js/dist/' | append: '.min.js' %} - +{% capture script %}/assets/js/dist/{{ type }}.min.js{% endcapture %} + {% if page.math %} @@ -65,15 +79,14 @@ } }; - - + {% endif %} - + {% if jekyll.environment == 'production' %} diff --git a/_includes/jsdelivr-combine.html b/_includes/jsdelivr-combine.html new file mode 100644 index 0000000..960eeb1 --- /dev/null +++ b/_includes/jsdelivr-combine.html @@ -0,0 +1,32 @@ +{% assign urls = include.urls | split: ',' %} + +{% assign combined_urls = nil %} + +{% assign domain = 'https://cdn.jsdelivr.net/' %} + +{% for url in urls %} + + {% if url contains domain %} + {% assign url_snippet = url | slice: domain.size, url.size %} + + {% if combined_urls %} + {% assign combined_urls = combined_urls | append: ',' | append: url_snippet %} + {% else %} + {% assign combined_urls = domain | append: 'combine/' | append: url_snippet %} + {% endif %} + + {% elsif url contains '//' %} + + + + {% else %} + + + + {% endif %} + +{% endfor %} + +{% if combined_urls %} + +{% endif %} diff --git a/_includes/mermaid.html b/_includes/mermaid.html index e8a0cd4..51eea69 100644 --- a/_includes/mermaid.html +++ b/_includes/mermaid.html @@ -2,7 +2,7 @@ mermaid-js loader --> - + + +
{{- site.data.locales[lang].panel.toc -}}
diff --git a/_layouts/default.html b/_layouts/default.html index 0304a72..1634cb4 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -7,6 +7,8 @@ layout: compress {% include lang.html %} +{% include assets-origin.html %} + {% capture prefer_mode %} {% if site.theme_mode %} data-mode="{{ site.theme_mode }}" diff --git a/_layouts/page.html b/_layouts/page.html index 5eafb93..d4990c5 100644 --- a/_layouts/page.html +++ b/_layouts/page.html @@ -4,6 +4,8 @@ layout: default {% include lang.html %} +{% include assets-origin.html %} +
diff --git a/_sass/addon/commons.scss b/_sass/addon/commons.scss index 033865e..d1fad37 100644 --- a/_sass/addon/commons.scss +++ b/_sass/addon/commons.scss @@ -1,7 +1,6 @@ /* The common styles */ -@import url('https://fonts.googleapis.com/css2?family=Lato&family=Source+Sans+Pro:wght@400;600;900&display=swap'); html { @media (prefers-color-scheme: light) {