diff --git a/_config.yml b/_config.yml index 4d92fab80d0dcb394cf8d977ee0797cf26b2cb78..f99ee6c280450de6de149e2ec5f581a92f03e87f 100644 --- a/_config.yml +++ b/_config.yml @@ -524,6 +524,7 @@ math: # If true, it will load mathjax / katex srcipt EVERY PAGE. per_page: false + # value: mathjax / katex (suggest: katex) engine: katex # See: https://github.com/mathjax/MathJax/ @@ -601,31 +602,28 @@ lazyload: # See: https://github.com/GoogleChromeLabs/quicklink/ quicklink: enable: false - # Quicklink (quicklink.umd.js script) is loaded on demand. # Add `quicklink: true` in Front-matter of the page or post you need. # Home page and archive page can be controlled through home and archive options below. home: true archive: true - # Initialize quicklink after the load event fires. delay: true # Custom a time in milliseconds by which the browser must execute prefetching. timeout: 10000 # Enable fetch() or falls back to XHR. priority: true - # For more flexibility you can add some patterns (RegExp, Function, or Array) to ignores. # See: https://github.com/GoogleChromeLabs/quicklink#custom-ignore-patterns # ! --------------------------------------------------------------- # ! Leave "ignores" as empty if you don't understand what it means. # ! --------------------------------------------------------------- - # Example: - # ignores: - # - /\/api\/?/ - # - uri => uri.includes('.zip') - # - (uri, el) => el.hasAttribute('nofollow') ignores: + - /\/api\/?/ + - uri => uri.includes('.xml') + - uri => uri.includes('.zip') + - (uri, el) => el.hasAttribute('nofollow') + - (uri, el) => el.hasAttribute('noprefetch') # Pjax # See: https://github.com/MoOx/pjax/ diff --git a/layout/_common/head.pug b/layout/_common/head.pug index 86126c95db4c5b38a9aab29cd20eee6921601aad..c87bea22fef778c1b110e831499072436e32b2be 100644 --- a/layout/_common/head.pug +++ b/layout/_common/head.pug @@ -96,5 +96,5 @@ if theme.tencent_analytics link(rel="dns-prefetch" href="https://tajs.qq.com") include ../_third-party/math/katex_css.pug -include ../_third-party/analytics/analytics-widget.pug +include ../_third-party/analytics/index.pug include ./config.pug diff --git a/layout/_common/layout.pug b/layout/_common/layout.pug index 957c4a4fb6e6907769ea8b672184ffbc8933eb3c..506dff33ff12818b5dc717d37801e0c03dbe412b 100644 --- a/layout/_common/layout.pug +++ b/layout/_common/layout.pug @@ -15,13 +15,13 @@ html(lang=config.language) include ../_scripts/vendors.pug include ../_third-party/quicklink.pug - include ../_third-party/advertising/index.pug - include ../_third-party/analytics/index.pug - include ../_third-party/comments/index.pug - include ../_third-party/math/index.pug include ../_third-party/search/index.pug + include ../_third-party/comments/index.pug if theme.pjax && theme.pjax.enable include ../_third-party/pjax.pug + include ../_third-party/analytics/busuanzi.pug + include ../_third-party/advertising/index.pug + include ../_third-party/math/index.pug include ../_scripts/stun.pug diff --git a/layout/_scripts/vendors.pug b/layout/_scripts/vendors.pug index 9f07784a36ed5fe058efa0ae07b15f6b0c00dc97..95d1f4429af54171a26082d92c804356e367ffbf 100644 --- a/layout/_scripts/vendors.pug +++ b/layout/_scripts/vendors.pug @@ -1,10 +1,3 @@ -//- Reset `window.Promise` when it was not a function. -//- IE refers the element whose id is `Promise` as `window.Promise`, this causes Velocity throwing an exception -script. - if (Object.prototype.toString.call(window.Promise) !== '[object Function]') { - window.Promise = null; - } - - var jquery_js = "https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js" - var velocity_js = "https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js" - var velocity_ui_js = "https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js" diff --git a/layout/_third-party/analytics/analytics-widget.pug b/layout/_third-party/analytics/analytics-widget.pug deleted file mode 100644 index ab6cb82962e8aa71665bdd3ec0b0ef25cea9cbde..0000000000000000000000000000000000000000 --- a/layout/_third-party/analytics/analytics-widget.pug +++ /dev/null @@ -1,6 +0,0 @@ -if theme.google_analytics - include ./google-analytics.pug -if theme.baidu_analytics - include ./baidu-analytics.pug -if theme.tencent_analytics - include ./tencent-analytics.pug diff --git a/layout/_third-party/analytics/busuanzi.pug b/layout/_third-party/analytics/busuanzi.pug index 37e612087c8ab2ab006551f83aabfbc6f8cb8dcd..9c2e9faf178521c7480a2e67de5e6b7ed8552807 100644 --- a/layout/_third-party/analytics/busuanzi.pug +++ b/layout/_third-party/analytics/busuanzi.pug @@ -1,5 +1,27 @@ -- - var busuanzi_js = "https://cdn.jsdelivr.net/gh/sukkaw/busuanzi@latest/bsz.pure.mini.js" - if (theme.cdn.busuanzi) busuanzi_js = theme.cdn.busuanzi +if theme.busuanzi.enable + - var busuanzi_js = "https://cdn.jsdelivr.net/gh/sukkaw/busuanzi@latest/bsz.pure.mini.js" + - if (theme.cdn.busuanzi) busuanzi_js = theme.cdn.busuanzi -script(async src=busuanzi_js) + script. + window.addEventListener('load', function () { + Stun.utils.pjaxReloadBusuanzi = function () { + (function(d, s) { + // Remove old script of busuanzi + var old = d.getElementById('busuazi_statistics'); + + if (old) { + var parent = d.querySelector('head'); + parent.removeChild(old); + } + + var d = document, s = d.createElement('script'); + s.id = 'busuazi_statistics' + s.src = '!{ busuanzi_js }'; + s.setAttribute('async', ''); + (d.head || d.body).appendChild(s); + })(document, 'script'); + }; + + // Initialization + Stun.utils.pjaxReloadBusuanzi(); + }, false); diff --git a/layout/_third-party/analytics/index.pug b/layout/_third-party/analytics/index.pug index 7bdfba674856c1246c02072a50815b7614b1f5d3..ab6cb82962e8aa71665bdd3ec0b0ef25cea9cbde 100644 --- a/layout/_third-party/analytics/index.pug +++ b/layout/_third-party/analytics/index.pug @@ -1,2 +1,6 @@ -if theme.busuanzi.enable - include ./busuanzi.pug +if theme.google_analytics + include ./google-analytics.pug +if theme.baidu_analytics + include ./baidu-analytics.pug +if theme.tencent_analytics + include ./tencent-analytics.pug diff --git a/layout/_third-party/comments/disqus.pug b/layout/_third-party/comments/disqus.pug index 84bf15a4cce46ec338f7294f29e7c5b40afe8991..0e9f5e3a49698f26e8461ae30b4e35bf86c7e779 100644 --- a/layout/_third-party/comments/disqus.pug +++ b/layout/_third-party/comments/disqus.pug @@ -5,13 +5,19 @@ script. this.page.title = '!{ page.title }'; }; - (function() { - var d = document, s = d.createElement('script'); - s.src = 'https://!{ theme.disqus.shortname }.disqus.com/embed.js'; - s.setAttribute('data-timestamp', '' + +new Date()); - (d.head || d.body).appendChild(s); - })(); + window.addEventListener('load', function () { + Stun.utils.pjaxReloadDisqus = function () { + (function(d, s) { + var d = document, s = d.createElement('script'); + s.src = 'https://!{ theme.disqus.shortname }.disqus.com/embed.js'; + s.setAttribute('data-timestamp', '' + +new Date()); + (d.head || d.body).appendChild(s); + })(document, 'script'); + }; + + // Initialization + Stun.utils.pjaxReloadDisqus(); + }, false); if theme.disqus.count - script#dsq-count-scr( - src='https://' + theme.disqus.shortname + '.disqus.com/count.js' async) + script(async src='https://' + theme.disqus.shortname + '.disqus.com/count.js')#dsq-count-scr diff --git a/layout/_third-party/comments/gitalk.pug b/layout/_third-party/comments/gitalk.pug index 85c84a1c8dba692413e2fbb72458c0af0fa3f5b4..ab2075e7e88fabae80187c03ce05f31c7aaaf700 100644 --- a/layout/_third-party/comments/gitalk.pug +++ b/layout/_third-party/comments/gitalk.pug @@ -14,15 +14,20 @@ script(src=gitalk_md5) script. window.addEventListener('load', function () { - var gitalk = new Gitalk({ - id: md5('!{ page.path }'), - clientID: '!{ theme.gitalk.client_id }', - clientSecret: '!{ theme.gitalk.client_secret }', - repo: '!{ theme.gitalk.repo }', - owner: '!{ theme.gitalk.owner }', - admin: ['!{ theme.gitalk.admin }'], - distractionFreeMode: '!{ theme.gitalk.distraction_free_mode }', - language: '!{ lang }' - }); - gitalk.render('gitalk-container'); + Stun.utils.pjaxReloadGitalk = function () { + var gitalk = new Gitalk({ + id: md5('!{ page.path }'), + clientID: '!{ theme.gitalk.client_id }', + clientSecret: '!{ theme.gitalk.client_secret }', + repo: '!{ theme.gitalk.repo }', + owner: '!{ theme.gitalk.owner }', + admin: ['!{ theme.gitalk.admin }'], + distractionFreeMode: '!{ theme.gitalk.distraction_free_mode }', + language: '!{ lang }' + }); + gitalk.render('gitalk-container'); + }; + + // Initialization + Stun.utils.pjaxReloadGitalk(); }, false); diff --git a/layout/_third-party/comments/livere.pug b/layout/_third-party/comments/livere.pug index 022820c23861ad50fa46651755415dd2c02ea3e7..79fbd61c30f316233bccea98f9e73e2822582115 100644 --- a/layout/_third-party/comments/livere.pug +++ b/layout/_third-party/comments/livere.pug @@ -1,12 +1,17 @@ script. window.addEventListener('load', function () { - (function(d, s) { - var j, e = d.getElementsByTagName(s)[0]; - if (typeof LivereTower === 'function') { return; } + Stun.utils.pjaxReloadLivere = function () { + (function(d, s) { + var j, e = d.getElementsByTagName(s)[0]; + if (typeof LivereTower === 'function') { return; } - j = d.createElement(s); - j.src = 'https://cdn-city.livere.com/js/embed.dist.js'; - j.async = true; - e.parentNode.insertBefore(j, e); - })(document, 'script'); + j = d.createElement(s); + j.src = 'https://cdn-city.livere.com/js/embed.dist.js'; + j.async = true; + e.parentNode.insertBefore(j, e); + })(document, 'script'); + }; + + // Initialization + Stun.utils.pjaxReloadLivere(); }, false); diff --git a/layout/_third-party/comments/valine.pug b/layout/_third-party/comments/valine.pug index c9bec10cb35ffc8a6c290bcca390efa5e62a92f2..6f6f4888a933496ae1d927437378c9a27f422c87 100644 --- a/layout/_third-party/comments/valine.pug +++ b/layout/_third-party/comments/valine.pug @@ -10,25 +10,30 @@ script(src=valine_js) script. window.addEventListener('load', function () { - var GUEST_INFO = ['nick', 'mail', 'link']; - var guest_info = '!{ theme.valine.meta }'; + Stun.utils.pjaxReloadValine = function () { + var GUEST_INFO = ['nick', 'mail', 'link']; + var guest_info = '!{ theme.valine.meta }'; - guest_info = guest_info.split(',').filter(function(item) { - return GUEST_INFO.indexOf(item) > -1; - }); + guest_info = guest_info.split(',').filter(function(item) { + return GUEST_INFO.indexOf(item) > -1; + }); - new Valine({ - el: '#comments', - appId: '!{ theme.valine.appid }', - appKey: '!{ theme.valine.appkey }', - notify: !{ theme.valine.notify }, - verify: !{ theme.valine.verify }, - placeholder: '!{ theme.valine.placeholder }', - avatar: '!{ theme.valine.avatar }', - meta: guest_info, - pageSize: '!{ theme.valine.pageSize }' || 10, - visitor: !{ theme.valine.visitor }, - recordIP: !{ theme.valine.recordIP }, - lang: '!{ theme.valine.language }' || 'zh-cn' - }); + new Valine({ + el: '#comments', + appId: '!{ theme.valine.appid }', + appKey: '!{ theme.valine.appkey }', + notify: !{ theme.valine.notify }, + verify: !{ theme.valine.verify }, + placeholder: '!{ theme.valine.placeholder }', + avatar: '!{ theme.valine.avatar }', + meta: guest_info, + pageSize: '!{ theme.valine.pageSize }' || 10, + visitor: !{ theme.valine.visitor }, + recordIP: !{ theme.valine.recordIP }, + lang: '!{ theme.valine.language }' || 'zh-cn' + }); + }; + + // Initialization + Stun.utils.pjaxReloadValine(); }, false); diff --git a/layout/_third-party/math/index.pug b/layout/_third-party/math/index.pug index 20afc772c47a38939c59ca7c8cd39d7a7c32d865..51e0fdfaa1828a6f500e2e4eaff38355fe0efe29 100644 --- a/layout/_third-party/math/index.pug +++ b/layout/_third-party/math/index.pug @@ -8,7 +8,7 @@ if theme.math.enable - }) if !is_tag() && !is_category() && !is_archive() - if isHomeHasMath || (theme.math.per_page || page.math) + if theme.pjax.enable || isHomeHasMath || (theme.math.per_page || page.math) if theme.math.engine === "mathjax" include ./mathjax.pug else if theme.math.engine === "katex" diff --git a/layout/_third-party/math/katex.pug b/layout/_third-party/math/katex.pug index 68f82104474e2561fbe01546a380cdda70c61ce1..e3d535336592c962e5c9fb394f7d0f594dc34ef0 100644 --- a/layout/_third-party/math/katex.pug +++ b/layout/_third-party/math/katex.pug @@ -1,3 +1,16 @@ if theme.math.katex.copy_tex.enable if theme.math.katex.copy_tex.copy_tex_js - script(async src=theme.math.katex.copy_tex.copy_tex_js) + script. + window.addEventListener('load', function () { + Stun.utils.pjaxReloadCopyKatex = function () { + (function(d, s) { + var d = document, s = d.createElement('script'); + s.src = '!{ theme.math.katex.copy_tex.copy_tex_js }'; + s.setAttribute('async', ''); + (d.head || d.body).appendChild(s); + })(document, 'script'); + }; + + // Initialization + Stun.utils.pjaxReloadCopyKatex(); + }, false); diff --git a/layout/_third-party/math/katex_css.pug b/layout/_third-party/math/katex_css.pug index 95dd85aaf4b22851fad1c7a23b48fc21c2744a88..25f629c0cd01b9c35601c37dd3bc8ce1f4018878 100644 --- a/layout/_third-party/math/katex_css.pug +++ b/layout/_third-party/math/katex_css.pug @@ -8,7 +8,7 @@ if theme.math.enable - }) if !is_tag() && !is_category() && !is_archive() - if isHomeHasMath || (theme.math.per_page || page.math) + if theme.pjax.enable || isHomeHasMath || (theme.math.per_page || page.math) if theme.math.engine === "katex" link(href=theme.math.katex.cdn rel="stylesheet" type="text/css") diff --git a/layout/_third-party/math/mathjax.pug b/layout/_third-party/math/mathjax.pug index 9734fdf2f1768c0bd7eac6bea40bd2169e434eec..129e098ea6c7202e9ef35ad7fe85d47f7d89c9f6 100644 --- a/layout/_third-party/math/mathjax.pug +++ b/layout/_third-party/math/mathjax.pug @@ -1,8 +1,8 @@ script(type="text/x-mathjax-config"). var mathjaxMhchem = ''; - if (!!'!{theme.math.mathjax.mhchem}') { - mathjaxMhchem = '!{theme.math.mathjax.mhchem}' + if (!{ theme.math.mathjax.mhchem.enable }) { + mathjaxMhchem = '!{ theme.math.mathjax.mhchem.mhchem_js }'; MathJax.Ajax.config.path['mhchem'] = mathjaxMhchem; } @@ -24,13 +24,12 @@ script(type="text/x-mathjax-config"). MathJax.InputJax.TeX.prefilterHooks.Add(function(data) { if (data.display) { var next = data.script.nextSibling; - while (next && next.nodeName.toLowerCase() === '#text') { next = next.nextSibling } - if (next && next.nodeName.toLowerCase() === 'br') { next.parentNode.removeChild(next) } + while (next && next.nodeName.toLowerCase() === '#text') { next = next.nextSibling; } + if (next && next.nodeName.toLowerCase() === 'br') { next.parentNode.removeChild(next); } } }); }); -script(type="text/x-mathjax-config"). MathJax.Hub.Queue(function() { var all = MathJax.Hub.getAllJax(), i; for (i = 0; i < all.length; i++) { @@ -38,4 +37,4 @@ script(type="text/x-mathjax-config"). } }); -script(src=theme.math.mathjax.cdn async) +script(async src=theme.math.mathjax.cdn) diff --git a/layout/_third-party/pjax.pug b/layout/_third-party/pjax.pug index 9a61eace0250bef358b5905dca473f130f41a0ea..52f437e05d25b35b45f292057088a17164ab4a6b 100644 --- a/layout/_third-party/pjax.pug +++ b/layout/_third-party/pjax.pug @@ -32,6 +32,28 @@ pjaxArgs = JSON.stringify(pjaxArgs) + var enabledCommentName = ''; + var isEnableComment = false; + + if (page.comments) { + if (theme.gitment && theme.gitment.enable) { + isEnableComment = true; + enabledCommentName = 'Gitment'; + } else if (theme.gitalk && theme.gitalk.enable) { + isEnableComment = true; + enabledCommentName = 'Gitalk'; + } else if (theme.livere && theme.livere.enable) { + isEnableComment = true; + enabledCommentName = 'Livere'; + } else if (theme.disqus && theme.disqus.enable) { + isEnableComment = true; + enabledCommentName = 'Disqus'; + } else if (theme.valine && theme.valine.enable) { + isEnableComment = true; + enabledCommentName = 'Valine'; + } + } + if theme.pjax && theme.pjax.enable - var pjax_js = "https://cdn.jsdelivr.net/npm/pjax@latest/pjax.min.js" - if (theme.cdn.pjax) pjax_js = theme.cdn.pjax @@ -44,9 +66,13 @@ script. var pjax = new Pjax(!{ pjaxArgs }); // 加载进度条的计时器 var loadingTimer = null; + // 是否第一次加载 pjax + var isFirstLoad = true; // 重置页面 Y 方向上的滚动偏移量 document.addEventListener('pjax:send', function () { + isFirstLoad = false; + $('html').velocity('scroll', { duration: 500, offset: $('#header').height(), @@ -84,5 +110,21 @@ script. Stun.utils.pjaxReloadBoot(); Stun.utils.pjaxReloadScroll(); Stun.utils.pjaxReloadSidebar(); + + // 重载评论 + if (!{ isEnableComment }) { + var reloadFnName = 'pjaxReload' + '!{ enabledCommentName }'; + var comment = Stun.utils[reloadFnName]; + + comment && comment(); + } + + // 重载 KaTex(只有当第一次加载 pjax 才重载) + if (isFirstLoad) { + Stun.utils.pjaxReloadCopyKatex(); + } + + // 重载 busuanzi + Stun.utils.pjaxReloadBusuanzi(); }, false); }, false);