chore: refactor scripts code style

This commit is contained in:
Dillon 2020-02-14 21:09:21 +08:00
parent 12bd1e1935
commit 1088f86b6d
8 changed files with 317 additions and 336 deletions

View file

@ -2,16 +2,20 @@ jQuery(function($) {
'use strict';
var _Blog = window._Blog || {};
var _Theme = window._Theme || {};
_Blog.toggleMobileMenu = function() {
_Theme.scroll = function () {
window.scroll = new SmoothScroll('[data-scroll]', {speed: 300, speedAsDuration: true});
}
_Theme.toggleMobileMenu = function () {
$('#menu-toggle').on('click', () => {
$('#menu-toggle').toggleClass('active');
$('#menu-mobile').toggleClass('active');
});
};
_Blog.toggleTheme = function() {
_Theme.toggleTheme = function () {
$('.theme-switch').on('click', () => {
$('body').toggleClass('dark-theme');
window.isDark = !window.isDark;
@ -20,17 +24,7 @@ jQuery(function($) {
});
};
_Blog.changeTitle = function() {
var currentTitle = document.title;
window.onblur = function() {
document.title = currentTitle;
};
window.onfocus = function() {
document.title = currentTitle;
};
};
_Blog.dynamicToTop = function() {
_Theme.dynamicToTop = function () {
const min = 300;
var $toTop = $('#dynamic-to-top');
$(window).scroll(() => {
@ -73,7 +67,7 @@ jQuery(function($) {
});
};
_Blog.chroma = function () {
_Theme.chroma = function () {
const blocks = document.querySelectorAll('.highlight > .chroma');
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
@ -101,7 +95,7 @@ jQuery(function($) {
}
};
_Blog.responsiveTable = function() {
_Theme.responsiveTable = function () {
const tables = document.querySelectorAll('.content table');
for (let i = 0; i < tables.length; i++) {
const table = tables[i];
@ -112,7 +106,7 @@ jQuery(function($) {
}
};
_Blog._refactorToc = function(toc) {
_Theme._refactorToc = function(toc) {
// when headings do not start with `h1`
const oldTocList = toc.children[0];
let newTocList = oldTocList;
@ -124,7 +118,7 @@ jQuery(function($) {
if (newTocList !== oldTocList) toc.replaceChild(newTocList, oldTocList);
};
_Blog._linkToc = function() {
_Theme._linkToc = function () {
const links = document.querySelectorAll('#TableOfContents a:first-child');
for (let i = 0; i < links.length; i++) links[i].className += ' toc-link';
@ -137,7 +131,7 @@ jQuery(function($) {
}
};
_Blog._initToc = function() {
_Theme._initToc = function () {
const $toc = $('#post-toc');
if ($toc.length && $toc.css('display') !== 'none') {
const SPACING = 80;
@ -145,7 +139,7 @@ jQuery(function($) {
const minTop = $toc.position().top;;
const mainTop = $('main').position().top;
const minScrollTop = minTop + mainTop - SPACING;
const changeTocState = function() {
const changeTocState = function () {
const scrollTop = $(window).scrollTop();
const maxTop = $footer.position().top - $toc.height();
const maxScrollTop = maxTop + mainTop - SPACING;
@ -213,7 +207,7 @@ jQuery(function($) {
}
};
_Blog.toc = function() {
_Theme.toc = function () {
const tocContainer = document.getElementById('post-toc');
if (tocContainer !== null) {
const toc = document.getElementById('TableOfContents');
@ -225,15 +219,16 @@ jQuery(function($) {
this._linkToc();
this._initToc();
// Listen for orientation changes
window.addEventListener("resize", function() {
this.setTimeout(_Blog._initToc, 0);
window.addEventListener("resize", function () {
this.setTimeout(_Theme._initToc, 0);
}, false);
}
}
};
_Blog.mermaid = function() {
_Theme.mermaid = function () {
if (window.mermaidMap) {
mermaid.initialize({startOnLoad: false, theme: null});
const mermaidAPI = mermaid.mermaidAPI
Object.keys(mermaidMap).forEach((id) => {
const element = document.getElementById(id);
@ -246,7 +241,7 @@ jQuery(function($) {
}
}
_Blog.echarts = function() {
_Theme.echarts = function () {
if (window.echartsMap) {
for (let i = 0; i < echartsArr.length; i++) {
echartsArr[i].dispose();
@ -257,7 +252,7 @@ jQuery(function($) {
myChart.setOption(echartsMap[id]);
echartsArr.push(myChart);
});
window.addEventListener("resize", function() {
window.addEventListener("resize", function () {
this.setTimeout(() => {
for (let i = 0; i < echartsArr.length; i++) {
echartsArr[i].resize();
@ -267,7 +262,7 @@ jQuery(function($) {
}
}
_Blog.countdown = function() {
_Theme.countdown = function () {
if (window.countdownMap) {
Object.keys(countdownMap).forEach(function(id) {
$(`#${id}`).countdown(countdownMap[id]['date'], {elapse: true})
@ -278,7 +273,7 @@ jQuery(function($) {
}
};
_Blog.typeit = function() {
_Theme.typeit = function () {
if (window.typeitArr) {
for (let i = 0; i < typeitArr.length; i++) {
const group = typeitArr[i];
@ -303,16 +298,16 @@ jQuery(function($) {
};
$(document).ready(() => {
_Blog.toggleMobileMenu();
_Blog.toggleTheme();
_Blog.changeTitle();
_Blog.dynamicToTop();
_Blog.chroma();
_Blog.responsiveTable();
_Blog.mermaid();
_Blog.echarts();
_Blog.countdown();
_Blog.typeit();
_Blog.toc();
_Theme.scroll();
_Theme.toggleMobileMenu();
_Theme.toggleTheme();
_Theme.dynamicToTop();
_Theme.chroma();
_Theme.responsiveTable();
_Theme.mermaid();
_Theme.echarts();
_Theme.countdown();
_Theme.typeit();
_Theme.toc();
});
});

View file

@ -11,12 +11,12 @@
</p>
</div>
<script>
const emojiArray = [
var emojiArray = [
'\\(o_o)/', '(o^^)o', '(˚Δ˚)b', '(^-^*)', '(≥o≤)', '(^_^)b', '(·_·)',
'(=\'X\'=)', '(>_<)', '(;-;)', '\\(^Д^)/',
];
const errorEmoji = emojiArray[Math.floor(Math.random() * emojiArray.length)];
const errorEmojiContainer = document.getElementById('error-emoji');
var errorEmoji = emojiArray[Math.floor(Math.random() * emojiArray.length)];
var errorEmojiContainer = document.getElementById('error-emoji');
errorEmojiContainer.appendChild(document.createTextNode(errorEmoji));
</script>
{{- end -}}

View file

@ -3,6 +3,11 @@
{{- errorf "\n\nThere are two possible situations that led to this error:\n 1. You haven't copied the config.toml yet. See https://github.com/dillonzq/LoveIt#installation \n 2. You have an incompatible update. See https://github.com//dillonzq/LoveIt/blob/master/CHANGELOG.md \n\n有两种可能的情况会导致这个错误发生:\n 1. 你还没有复制 config.toml 参考 https://github.com/dillonzq/LoveIt#installation \n 2. 你进行了一次不兼容的更新 参考 https://github.com//dillonzq/LoveIt/blob/master/CHANGELOG.md \n" -}}
{{- end -}}
{{- if eq (getenv "HUGO_ENV") "production" -}}
{{- .Scratch.Set "production" true -}}
{{- .Scratch.Set "CDN" .Site.Params.cdn -}}
{{- end -}}
<!DOCTYPE html>
<html lang="{{ .Site.LanguageCode }}">
<head>
@ -51,7 +56,7 @@
<span>&nbsp;</span>
</a>
{{- /* Load scripts */ -}}
{{- partial "scripts.html" . -}}
{{- /* Load script */ -}}
{{- partial "script.html" . -}}
</body>
</html>

View file

@ -1,84 +1,94 @@
{{- if eq (getenv "HUGO_ENV") "production" | and .IsPage | and (ne .Params.comment false) -}}
{{- if .Scratch.Get "production" | and .IsPage | and (ne .Params.comment false) -}}
{{- $CDN := .Scratch.Get "CDN" -}}
{{- /* Disqus Comment System */ -}}
{{- with .Site.Params.disqus.shortname -}}
<div id="disqus_thread"></div>
<script src="https://{{ . }}.disqus.com/embed.js"></script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
{{- $script := printf `<script src="https://%s.disqus.com/embed.js"></script>` . -}}
{{- slice $script | $.Scratch.Add "scriptCDN" -}}
<noscript>
Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
</noscript>
{{- end -}}
{{- /* Gitalk Comment System */ -}}
{{- if .Site.Params.gitalk.owner -}}
<div id="gitalk_container"></div>
{{- if .Site.Params.cdn.gitalk_css -}}
{{- .Site.Params.cdn.gitalk_css | safeHTML -}}
{{- with $CDN.gitalk_css -}}
{{- slice . | $.Scratch.Add "linkCDN" -}}
{{- else -}}
{{- $res := resources.Get "css/lib/gitalk/gitalk.css" | minify -}}
<link rel="stylesheet" href="{{ $res.RelPermalink }}">
{{- slice "css/lib/gitalk/gitalk.css" | .Scratch.Add "linkLocal" -}}
{{- end -}}
{{- if .Site.Params.cdn.gitalk_js -}}
{{ .Site.Params.cdn.gitalk_js | safeHTML -}}
{{- with $CDN.gitalk_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- $res := resources.Get "js/lib/gitalk/gitalk.min.js" -}}
<script src="{{ $res.RelPermalink }}"></script>
{{- slice "css/lib/gitalk/gitalk.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
<script>
var gitalk = new Gitalk({
id: '{{ .Date }}',
title: '{{ .Title }}',
clientID: '{{ .Site.Params.gitalk.clientId }}',
clientSecret: '{{ .Site.Params.gitalk.clientSecret }}',
repo: '{{ .Site.Params.gitalk.repo }}',
owner: '{{ .Site.Params.gitalk.owner }}',
admin: ['{{ .Site.Params.gitalk.owner }}'],
body: decodeURI(location.href),
document.addEventListener("DOMContentLoaded", function(event) {
var gitalk = new Gitalk({
id: '{{ .Date }}',
title: '{{ .Title }}',
clientID: '{{ .Site.Params.gitalk.clientId }}',
clientSecret: '{{ .Site.Params.gitalk.clientSecret }}',
repo: '{{ .Site.Params.gitalk.repo }}',
owner: '{{ .Site.Params.gitalk.owner }}',
admin: ['{{ .Site.Params.gitalk.owner }}'],
body: decodeURI(location.href),
});
gitalk.render('gitalk_container');
});
gitalk.render('gitalk_container');
</script>
<noscript>Please enable JavaScript to view the <a href="https://github.com/gitalk/gitalk">comments powered by Gitalk.</a></noscript>
<noscript>
Please enable JavaScript to view the <a href="https://github.com/gitalk/gitalk">comments powered by Gitalk.</a>
</noscript>
{{- end -}}
{{- /* Valine Comment System */ -}}
{{- if .Site.Params.valine.enable -}}
<div id="valine_container"></div>
{{- if .Site.Params.cdn.valine_js -}}
{{- .Site.Params.cdn.valine_js | safeHTML -}}
{{- with $CDN.valine_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- $res := resources.Get "js/lib/valine/Valine.min.js" -}}
<script src="{{ $res.RelPermalink }}"></script>
{{- slice "js/lib/valine/Valine.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
<script>
new Valine({
el: '#valine_container',
appId: '{{ .Site.Params.valine.appId }}',
appKey: '{{ .Site.Params.valine.appKey }}',
{{- with .Site.Params.valine.placeholder -}}
placeholder: '{{ . }}',
{{- end -}}
{{- if .Site.Params.valine.notify -}}
notify: true,
{{- end -}}
{{- if .Site.Params.valine.verify -}}
verify: true,
{{- end -}}
{{- with .Site.Params.valine.avatar -}}
avatar: '{{ . }}',
{{- end -}}
{{- with .Site.Params.valine.meta -}}
meta: {{ . | safeJS }},
{{- end -}}
{{- with .Site.Params.valine.pageSize -}}
pageSize: {{ . | safeJS }},
{{- end -}}
lang: '{{ T "valineLang" }}',
{{- if .Site.Params.valine.visitor -}}
visitor: true,
{{- end -}}
{{- if .Site.Params.valine.recordIP -}}
recordIP: true,
{{- end -}}
document.addEventListener("DOMContentLoaded", function(event) {
new Valine({
el: '#valine_container',
appId: '{{ .Site.Params.valine.appId }}',
appKey: '{{ .Site.Params.valine.appKey }}',
{{- with .Site.Params.valine.placeholder -}}
placeholder: '{{ . }}',
{{- end -}}
{{- if .Site.Params.valine.notify -}}
notify: true,
{{- end -}}
{{- if .Site.Params.valine.verify -}}
verify: true,
{{- end -}}
{{- with .Site.Params.valine.avatar -}}
avatar: '{{ . }}',
{{- end -}}
{{- with .Site.Params.valine.meta -}}
meta: {{ . | safeJS }},
{{- end -}}
{{- with .Site.Params.valine.pageSize -}}
pageSize: {{ . | safeJS }},
{{- end -}}
lang: '{{ T "valineLang" }}',
{{- if .Site.Params.valine.visitor -}}
visitor: true,
{{- end -}}
{{- if .Site.Params.valine.recordIP -}}
recordIP: true,
{{- end -}}
});
});
</script>
<noscript>Please enable JavaScript to view the <a href="https://valine.js.org/">comments powered by Valine.</a></noscript>
<noscript>
Please enable JavaScript to view the <a href="https://valine.js.org/">comments powered by Valine.</a>
</noscript>
{{- end -}}
{{- /* Facebook Comment System */ -}}
@ -90,12 +100,11 @@
data-width="{{ .Site.Params.facebook.width }}"
data-numposts="{{ .Site.Params.facebook.numPosts }}"
></div>
<script
async
defer
crossorigin="anonymous"
src="https://connect.facebook.net/{{ .Site.Params.facebook.languageCode }}/sdk.js#xfbml=1&version=v5.0&appId={{ .Site.Params.facebook.appId }}&autoLogAppEvents=1"
></script>
<noscript>Please enable JavaScript to view the <a href="https://developers.facebook.com/docs/plugins/comments/">comments powered by Facebook.</a></noscript>
{{- $script := `<script src="https://connect.facebook.net/%s/sdk.js#xfbml=1&version=v5.0&appId=%s&autoLogAppEvents=1" crossorigin="anonymous"></script>` -}}
{{- $script = printf $script .Site.Params.facebook.languageCode .Site.Params.facebook.appId -}}
{{- slice $script | .Scratch.Add "scriptCDN" -}}
<noscript>
Please enable JavaScript to view the <a href="https://developers.facebook.com/docs/plugins/comments/">comments powered by Facebook.</a>
</noscript>
{{- end -}}
{{- end -}}

View file

@ -1,3 +1,5 @@
{{- $CDN := .Scratch.Get "CDN" -}}
<link rel="canonical" href="{{ .Permalink }}" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
@ -28,8 +30,8 @@
<link rel="stylesheet" href="{{ $res.RelPermalink }}">
{{- /* Font Awesome https://fontawesome.com/ */ -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.fontawesome_free_css -}}
{{- .Site.Params.cdn.fontawesome_free_css | safeHTML -}}
{{- with $CDN.fontawesome_free_css -}}
{{- . | safeHTML -}}
{{- else -}}
{{- $res := resources.Get "css/lib/fontawesome-free/all.min.css" -}}
<link rel="stylesheet" href="{{ $res.RelPermalink }}">
@ -41,8 +43,8 @@
<link rel="stylesheet" href="{{ $res.RelPermalink }}">
{{- /* Animate.css https://github.com/daneden/animate.css */ -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.animate_css -}}
{{- .Site.Params.cdn.animate_css | safeHTML -}}
{{- with $CDN.animate_css -}}
{{- . | safeHTML -}}
{{- else -}}
{{ $res := resources.Get "css/lib/animate/animate.min.css" -}}
<link rel="stylesheet" href="{{ $res.RelPermalink }}">

View file

@ -0,0 +1,194 @@
{{- $CDN := .Scratch.Get "CDN" -}}
{{- /* Google analytics async */ -}}
{{- if .Scratch.Get "production" | and .Site.GoogleAnalytics -}}
{{- template "_internal/google_analytics_async.html" . -}}
{{- end -}}
{{- /* jQuery https://github.com/jquery/jquery */ -}}
{{- with $CDN.jquery_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/jquery/jquery.slim.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- /* lazysizes https://github.com/aFarkas/lazysizes */ -}}
{{- with $CDN.lazysizes_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/lazysizes/lazysizes.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- /* Smooth Scroll https://github.com/cferdinandi/smooth-scroll */ -}}
{{- with $CDN.smooth_scroll_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/smooth-scroll/smooth-scroll.polyfills.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- /* TypeIt https://github.com/alexmacarthur/typeit */ -}}
{{- with .Scratch.Get "typeitMap" -}}
{{- with $CDN.typeit_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/typeit/typeit.min.js" | $.Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- range $key, $val := . -}}
{{- slice $val | $.Scratch.Add "typeitArr" -}}
{{- end -}}
<script>
window.typeitArr = {{ $.Scratch.Get "typeitArr" | jsonify | safeJS }};
</script>
{{- end -}}
{{- /* KaTeX https://github.com/KaTeX/KaTeX */ -}}
{{- if .Site.Params.math.enable | and (ne .Params.math false) -}}
{{- with $CDN.katex_css -}}
{{- slice . | $.Scratch.Add "linkCDN" -}}
{{- else -}}
{{- slice "css/lib/katex/katex.min.css" | .Scratch.Add "linkLocal" -}}
{{- end -}}
{{- with $CDN.katex_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/katex/katex.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- with $CDN.katex_auto_render_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/katex/auto-render.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- $math := .Site.Params.math -}}
{{- if $math.copy_tex -}}
{{- with $CDN.katex_copy_tex_css -}}
{{- slice . | $.Scratch.Add "linkCDN" -}}
{{- else -}}
{{- slice "css/lib/katex/copy-tex.min.css" | .Scratch.Add "linkLocal" -}}
{{- end -}}
{{- with $CDN.katex_copy_tex_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/katex/copy-tex.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- end -}}
{{- if $math.mhchem -}}
{{- with $CDN.katex_mhchem_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/katex/mhchem.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- end -}}
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "\\(", right: "\\)", display: false },
{ left: "\\[", right: "\\]", display: true },
{{- if and $math.block_left_delimiter $math.block_right_delimiter -}}
{ left: "{{ $math.block_left_delimiter }}", right: "{{ $math.block_right_delimiter }}", display: true },
{{- end -}}
{{- if and $math.inline_left_delimiter $math.inline_right_delimiter -}}
{ left: "{{ $math.inline_left_delimiter }}", right: "{{ $math.inline_right_delimiter }}", display: false },
{{- end -}}
]
});
});
</script>
{{- end -}}
{{- /* mermaid https://github.com/knsv/mermaid */ -}}
{{- with .Scratch.Get "mermaidMap" -}}
{{- with $CDN.mermaid_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/mermaid/mermaid.min.js" | $.Scratch.Add "scriptLocal" -}}
{{- end -}}
<script>
window.mermaidMap = {{ jsonify . | safeJS }};
</script>
{{- end -}}
{{- /* Music */ -}}
{{- if .Scratch.Get "music" -}}
{{- /* APlayer https://github.com/MoePlayer/APlayer */ -}}
{{- with $CDN.aplayer_css -}}
{{- slice . | $.Scratch.Add "linkCDN" -}}
{{- else -}}
{{- slice "css/lib/aplayer/APlayer.min.css" | .Scratch.Add "linkLocal" -}}
{{- end -}}
{{- with $CDN.aplayer_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/aplayer/APlayer.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- /* MetingJS https://github.com/metowolf/MetingJS */ -}}
{{- with $CDN.meting_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/meting/Meting.min.js" | .Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- end -}}
{{- /* jQuery.countdown https://github.com/hilios/jQuery.countdown */ -}}
{{- with .Scratch.Get "countdownMap" -}}
{{- with $CDN.jquery_countdown_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/jquery-countdown/jquery.countdown.min.js" | $.Scratch.Add "scriptLocal" -}}
{{- end -}}
<script>
window.countdownMap = {{ jsonify . | safeJS }};
</script>
{{- end -}}
{{- /* dev feature */ -}}
{{- if .Params.dev -}}
{{- /* ECharts https://github.com/apache/incubator-echarts */ -}}
{{- with .Scratch.Get "echartsMap" -}}
{{- with $CDN.echarts_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/echarts/echarts.min.js" | $.Scratch.Add "scriptLocal" -}}
{{- end -}}
{{- with $CDN.echarts_macarons_js -}}
{{- slice . | $.Scratch.Add "scriptCDN" -}}
{{- else -}}
{{- slice "js/lib/echarts/macarons.js" | $.Scratch.Add "scriptLocal" -}}
{{- end -}}
<script>
window.echartsMap = {
{{- range $key, $var := . -}}
{{- $key }}: {{ $var | safeJS -}},
{{- end -}}
};
window.echartsArr = [];
</script>
{{- end -}}
{{- end -}}
{{- /* Theme script */ -}}
{{- slice "js/theme.js" | .Scratch.Add "scriptLocal" -}}
{{- range .Scratch.Get "linkCDN" -}}
{{- safeHTML . -}}
{{- end -}}
{{- range .Scratch.Get "linkLocal" -}}
{{- $res := resources.Get . -}}
{{- if not (strings.HasSuffix . ".min.css") -}}
{{- $res = minify $res -}}
{{- end -}}
<link rel="stylesheet" href="{{ $res.RelPermalink }}">
{{- end -}}
{{- range .Scratch.Get "scriptCDN" -}}
{{- safeHTML . -}}
{{- end -}}
{{- range .Scratch.Get "scriptLocal" -}}
{{- $res := resources.Get . -}}
{{- if not (strings.HasSuffix . ".min.js") -}}
{{- $res = minify $res -}}
{{- end -}}
<script src="{{ $res.RelPermalink }}"></script>
{{- end -}}

View file

@ -1,224 +0,0 @@
{{- /* Google analytics async */ -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.GoogleAnalytics -}}
{{- template "_internal/google_analytics_async.html" . -}}
{{- end -}}
{{- /* jQuery https://github.com/jquery/jquery */ -}}
{{- $jquery := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.jquery_js -}}
{{- $jquery = .Site.Params.cdn.jquery_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/jquery/jquery.slim.min.js" -}}
{{- $jquery = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $jquery | safeHTML -}}
{{- /* lazysizes https://github.com/aFarkas/lazysizes */ -}}
{{- $lazysizes := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.lazysizes_js -}}
{{- $lazysizes = .Site.Params.cdn.lazysizes_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/lazysizes/lazysizes.min.js" -}}
{{- $lazysizes = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $lazysizes | safeHTML -}}
{{- /* Smooth Scroll https://github.com/cferdinandi/smooth-scroll */ -}}
{{- $smooth_scroll := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.smooth_scroll_js -}}
{{- $smooth_scroll = .Site.Params.cdn.smooth_scroll_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/smooth-scroll/smooth-scroll.polyfills.min.js" -}}
{{- $smooth_scroll = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $smooth_scroll = delimit (slice $smooth_scroll "<script>window.scroll = new SmoothScroll('[data-scroll]', {speed: 300, speedAsDuration: true});</script>") "" -}}
{{- $smooth_scroll | safeHTML -}}
{{- /* TypeIt https://github.com/alexmacarthur/typeit */ -}}
{{- if .Scratch.Get "typeitMap" -}}
{{- $typeit := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.typeit_js -}}
{{- $typeit = .Site.Params.cdn.typeit_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/typeit/typeit.min.js" -}}
{{- $typeit = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $typeit | safeHTML -}}
{{- $typeitArr := slice -}}
{{- range $key, $val := .Scratch.Get "typeitMap" -}}
{{- $typeitArr = $typeitArr | append (slice $val) -}}
{{- end -}}
<script>
{{- printf "window.typeitArr=%s;" ($typeitArr | jsonify) | safeJS -}}
</script>
{{- end -}}
{{- /* KaTeX https://github.com/KaTeX/KaTeX */ -}}
{{- if .Site.Params.math.enable | and (ne .Params.math false) -}}
{{- $math := .Site.Params.math -}}
{{- $katex_css := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_css -}}
{{- $katex_css = .Site.Params.cdn.katex_css -}}
{{- else -}}
{{- $res := resources.Get "css/lib/katex/katex.min.css" -}}
{{- $katex_css = printf "<link rel=\"stylesheet\" href=\"%s\">" $res.RelPermalink -}}
{{- end -}}
{{- $katex_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_js -}}
{{- $katex_js = .Site.Params.cdn.katex_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/katex/katex.min.js" -}}
{{- $katex_js = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $katex_auto_render_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_auto_render_js -}}
{{- $katex_auto_render_js = .Site.Params.cdn.katex_auto_render_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/katex/auto-render.min.js" -}}
{{- $katex_auto_render_js = printf "<script defer src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $katex := delimit (slice $katex_css $katex_js $katex_auto_render_js) "" -}}
{{- if $math.copy_tex -}}
{{- $katex_copy_tex_css := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_copy_tex_css -}}
{{- $katex_copy_tex_css = .Site.Params.cdn.katex_copy_tex_css -}}
{{- else -}}
{{- $res := resources.Get "css/lib/katex/copy-tex.min.css" -}}
{{- $katex_copy_tex_css = printf "<link rel=\"stylesheet\" href=\"%s\">" $res.RelPermalink -}}
{{- end -}}
{{- $katex_copy_tex_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_copy_tex_js -}}
{{- $katex_copy_tex_js = .Site.Params.cdn.katex_copy_tex_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/katex/copy-tex.min.js" -}}
{{- $katex_copy_tex_js = printf "<script defer src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $katex = delimit (slice $katex $katex_copy_tex_css $katex_copy_tex_js) "" -}}
{{- end -}}
{{- if $math.mhchem -}}
{{- $katex_mhchem_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.katex_mhchem_js -}}
{{- $katex_mhchem_js = .Site.Params.cdn.katex_mhchem_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/katex/mhchem.min.js" -}}
{{- $katex_mhchem_js = printf "<script defer src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $katex = delimit (slice $katex $katex_mhchem_js) "" -}}
{{- end -}}
{{- $katex | safeHTML -}}
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "\\(", right: "\\)", display: false },
{ left: "\\[", right: "\\]", display: true },
{{- if and $math.block_left_delimiter $math.block_right_delimiter -}}
{ left: "{{ $math.block_left_delimiter }}", right: "{{ $math.block_right_delimiter }}", display: true },
{{- end -}}
{{- if and $math.inline_left_delimiter $math.inline_right_delimiter -}}
{ left: "{{ $math.inline_left_delimiter }}", right: "{{ $math.inline_right_delimiter }}", display: false },
{{- end -}}
]
});
});
</script>
{{- end -}}
{{- /* mermaid https://github.com/knsv/mermaid */ -}}
{{- if .Scratch.Get "mermaidMap" -}}
{{- $mermaid := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.mermaid_js -}}
{{- $mermaid = .Site.Params.cdn.mermaid_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/mermaid/mermaid.min.js" -}}
{{- $mermaid = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $mermaidInit := "<script>mermaid.initialize({startOnLoad: false, theme: null});</script>" -}}
{{- $mermaid = delimit (slice $mermaid $mermaidInit) "" -}}
{{- $mermaid | safeHTML -}}
<script>
{{- printf "window.mermaidMap=%s;" (.Scratch.Get "mermaidMap" | jsonify) | safeJS -}}
</script>
{{- end -}}
{{- /* Music */ -}}
{{- if .Scratch.Get "music" -}}
{{- /* APlayer https://github.com/MoePlayer/APlayer */ -}}
{{- $aplayer_css := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.aplayer_css -}}
{{- $aplayer_css = .Site.Params.cdn.aplayer_css -}}
{{- else -}}
{{- $res := resources.Get "css/lib/aplayer/APlayer.min.css" -}}
{{- $aplayer_css = printf "<link rel=\"stylesheet\" href=\"%s\">" $res.RelPermalink -}}
{{- end -}}
{{- $aplayer_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.aplayer_js -}}
{{- $aplayer_js = .Site.Params.cdn.aplayer_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/aplayer/APlayer.min.js" -}}
{{- $aplayer_js = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $aplayer := delimit (slice $aplayer_css $aplayer_js) "" -}}
{{- $aplayer | safeHTML -}}
{{- /* MetingJS https://github.com/metowolf/MetingJS */ -}}
{{- $meting := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.meting_js -}}
{{- $meting = .Site.Params.cdn.meting_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/meting/Meting.min.js" -}}
{{- $meting = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $meting | safeHTML -}}
{{- end -}}
{{- /* jQuery.countdown https://github.com/hilios/jQuery.countdown */ -}}
{{- if .Scratch.Get "countdownMap" -}}
{{- $countdown := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.jquery_countdown_js -}}
{{- $countdown = .Site.Params.cdn.jquery_countdown_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/jquery-countdown/jquery.countdown.min.js" -}}
{{- $countdown = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $countdown | safeHTML -}}
<script>
{{- printf "window.countdownMap=%s;" (.Scratch.Get "countdownMap" | jsonify) | safeJS -}}
</script>
{{- end -}}
{{- /* dev feature */ -}}
{{- if .Params.dev -}}
{{- /* ECharts https://github.com/apache/incubator-echarts */ -}}
{{- if .Scratch.Get "echartsMap" -}}
{{- $echarts_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.echarts_js -}}
{{- $echarts_js = .Site.Params.cdn.echarts_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/echarts/echarts.min.js" -}}
{{- $echarts_js = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $echarts_macarons_js := "" -}}
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.cdn.echarts_macarons_js -}}
{{- $echarts_macarons_js = .Site.Params.cdn.echarts_macarons_js -}}
{{- else -}}
{{- $res := resources.Get "js/lib/echarts/macarons.js" | minify -}}
{{- $echarts_macarons_js = printf "<script src=\"%s\"></script>" $res.RelPermalink -}}
{{- end -}}
{{- $echarts := delimit (slice $echarts_js $echarts_macarons_js) "" -}}
{{- $echarts | safeHTML -}}
<script>
window.echartsMap = {
{{- range $key, $var := .Scratch.Get "echartsMap" -}}
{{- $key }}: {{ $var | safeJS -}},
{{- end -}}
};
window.echartsArr = [];
</script>
{{- end -}}
{{- end -}}
{{- /* Blog script */ -}}
{{- $res := resources.Get "/js/blog.js" | minify -}}
<script src="{{ $res.RelPermalink }}"></script>

View file

@ -30,7 +30,7 @@
<i class="far fa-calendar-alt fa-fw"></i><time datetime={{ $publish_date }}>{{ $publish_date }}</time>&nbsp;
<i class="fas fa-pencil-alt fa-fw"></i>{{ T "wordCount" .WordCount }}&nbsp;
<i class="far fa-clock fa-fw"></i>{{ T "readingTime" .ReadingTime }}&nbsp;
{{- if eq (getenv "HUGO_ENV") "production" | and .Site.Params.valine.enable | and .Site.Params.valine.visitor -}}
{{- if .Scratch.Get "production" | and .Site.Params.valine.enable | and .Site.Params.valine.visitor -}}
<span id="{{ .RelPermalink | relURL }}" class="leancloud_visitors" data-flag-title="{{ .Title }}">
<i class="far fa-eye fa-fw"></i>{{ T "pageviews" | safeHTML }}
</span>&nbsp;