feat: only init toc when there are headings

release 2.6.3
This commit is contained in:
olOwOlo 2017-10-12 23:01:06 +08:00
parent 3483df9d83
commit 0ba82dd176
No known key found for this signature in database
GPG key ID: 456394B6E7A15EA6
7 changed files with 21 additions and 8 deletions

View file

@ -51,7 +51,7 @@
<link rel="mask-icon" href="{{ "safari-pinned-tab.svg" | relURL }}" color="#5bbad5"> <link rel="mask-icon" href="{{ "safari-pinned-tab.svg" | relURL }}" color="#5bbad5">
<!-- Styles --> <!-- Styles -->
<link href="{{ "dist/even.min.css?v=2.6.2" | relURL }}" rel="stylesheet"> <link href="{{ "dist/even.min.css?v=2.6.3" | relURL }}" rel="stylesheet">
{{ if .Site.Params.bootcdn -}} {{ if .Site.Params.bootcdn -}}
{{ if .Site.Params.fancybox }}<link href="https://cdn.bootcss.com/fancybox/3.1.20/jquery.fancybox.min.css" rel="stylesheet" crossorigin="anonymous">{{ end }} {{ if .Site.Params.fancybox }}<link href="https://cdn.bootcss.com/fancybox/3.1.20/jquery.fancybox.min.css" rel="stylesheet" crossorigin="anonymous">{{ end }}
{{- else if .Site.Params.publicCDN.enable -}} {{- else if .Site.Params.publicCDN.enable -}}

View file

@ -13,7 +13,7 @@
<script type="text/javascript" src="{{ "lib/slideout/slideout-1.0.1.min.js" | relURL }}"></script> <script type="text/javascript" src="{{ "lib/slideout/slideout-1.0.1.min.js" | relURL }}"></script>
{{ if .Site.Params.fancybox }}<script type="text/javascript" src="{{ "lib/fancybox/jquery.fancybox-3.1.20.min.js" | relURL }}"></script>{{ end }} {{ if .Site.Params.fancybox }}<script type="text/javascript" src="{{ "lib/fancybox/jquery.fancybox-3.1.20.min.js" | relURL }}"></script>{{ end }}
{{- end }} {{- end }}
<script type="text/javascript" src="{{ "dist/even.min.js?v=2.6.2" | relURL }}"></script> <script type="text/javascript" src="{{ "dist/even.min.js?v=2.6.3" | relURL }}"></script>
{{- if and (and .Site.Params.mathjax (ne .Params.mathjax false)) (or .IsPage .IsHome) }} {{- if and (and .Site.Params.mathjax (ne .Params.mathjax false)) (or .IsPage .IsHome) }}
<script type="text/javascript"> <script type="text/javascript">

View file

@ -48,7 +48,7 @@ Even.mobileNavbar = function () {
}) })
} }
Even.toc = function () { Even._initToc = function () {
var SPACING = 20 var SPACING = 20
var $toc = $('.post-toc') var $toc = $('.post-toc')
var $footer = $('.post-footer') var $footer = $('.post-footer')
@ -152,7 +152,21 @@ Even.highlight = function () {
} }
} }
Even.beforeToc = function () { Even.toc = function () {
const tocContainer = document.getElementById('post-toc')
if (tocContainer !== null) {
const toc = document.getElementById('TableOfContents')
if (toc === null) {
// toc = true, but there are no headings
tocContainer.parentNode.removeChild(tocContainer)
} else {
this._linkToc()
this._initToc()
}
}
}
Even._linkToc = function () {
const links = document.querySelectorAll('#TableOfContents a') const links = document.querySelectorAll('#TableOfContents a')
for (let i = 0; i < links.length; i++) links[i].className += ' toc-link' for (let i = 0; i < links.length; i++) links[i].className += ' toc-link'

View file

@ -5,7 +5,6 @@ import '../css/style.scss'
$(document).ready(function () { $(document).ready(function () {
Even.backToTop() Even.backToTop()
Even.mobileNavbar() Even.mobileNavbar()
Even.beforeToc()
Even.toc() Even.toc()
Even.fancybox() Even.fancybox()
}) })

View file

@ -1,6 +1,6 @@
{ {
"name": "even-src", "name": "even-src",
"version": "2.6.2", "version": "2.6.3",
"description": "even source file", "description": "even source file",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View file

@ -1,2 +1,2 @@
!function(e){function n(t){if(o[t])return o[t].exports;var c=o[t]={i:t,l:!1,exports:{}};return e[t].call(c.exports,c,c.exports,n),c.l=!0,c.exports}var o={};n.m=e,n.c=o,n.d=function(e,o,t){n.o(e,o)||Object.defineProperty(e,o,{configurable:!1,enumerable:!0,get:t})},n.n=function(e){var o=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(o,"a",o),o},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=0)}([function(e,n,o){"use strict";var t=o(1);o(2),$(document).ready(function(){t.Even.backToTop(),t.Even.mobileNavbar(),t.Even.beforeToc(),t.Even.toc(),t.Even.fancybox()}),hljs.initHighlighting(),t.Even.highlight()},function(e,n,o){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var t={};t.backToTop=function(){var e=$("#back-to-top");$(window).scroll(function(){$(window).scrollTop()>100?e.fadeIn(1e3):e.fadeOut(1e3)}),e.click(function(){$("body,html").animate({scrollTop:0})})},t.mobileNavbar=function(){var e=$("#mobile-navbar"),n=$(".mobile-navbar-icon"),o=new Slideout({panel:document.getElementById("mobile-panel"),menu:document.getElementById("mobile-menu"),padding:180,tolerance:70});o.disableTouch(),n.click(function(){o.toggle()}),o.on("beforeopen",function(){e.addClass("fixed-open"),n.addClass("icon-click").removeClass("icon-out")}),o.on("beforeclose",function(){e.removeClass("fixed-open"),n.addClass("icon-out").removeClass("icon-click")}),$("#mobile-panel").on("touchend",function(){o.isOpen()&&n.click()})},t.toc=function(){var e=$(".post-toc"),n=$(".post-footer");if(e.length){var o=e.offset().top-20,t=n.offset().top-e.height()-20,c={start:{position:"absolute",top:o},process:{position:"fixed",top:20},end:{position:"absolute",top:t}};$(window).scroll(function(){var n=$(window).scrollTop();n<o?e.css(c.start):n>t?e.css(c.end):e.css(c.process)})}var r=$(".toc-link"),i=$(".headerlink"),a=$.map(i,function(e){return $(e).offset().top});$(window).scroll(function(){for(var e=$(window).scrollTop(),n=0;n<r.length;n++){var o=n+1===r.length,t=a[n]-30,c=o?1/0:a[n+1]-30;t<e&&e<=c?$(r[n]).addClass("active"):$(r[n]).removeClass("active")}})},t.fancybox=function(){$.fancybox&&($(".post-content").each(function(){$(this).find("img").each(function(){$(this).wrap('<a class="fancybox" href="'+this.src+'" data-fancybox="gallery" data-caption="'+this.title+'"></a>')})}),$(".fancybox").fancybox({selector:".fancybox",protect:!0}))},t.highlight=function(){for(var e=document.querySelectorAll("pre code"),n=0;n<e.length;n++){for(var o=e[n],t=o.parentElement,c=o.innerHTML.split(/\n/).slice(0,-1),r=c.length,i="",a=0;a<r;a++)i+='<div class="line">'+(a+1)+"</div>";for(var l="",s=0;s<r;s++)l+='<div class="line">'+c[s]+"</div>";o.className+=" highlight";var f=document.createElement("figure");f.className=o.className,f.innerHTML='<table><tbody><tr><td class="gutter"><pre>'+i+'</pre></td><td class="code"><pre>'+l+"</pre></td></tr></tbody></table>",t.parentElement.replaceChild(f,t)}},t.beforeToc=function(){for(var e=document.querySelectorAll("#TableOfContents a"),n=0;n<e.length;n++)e[n].className+=" toc-link";for(var o=1;o<=6;o++)for(var t=document.querySelectorAll(".post-content>h"+o),c=0;c<t.length;c++){var r=t[c];r.innerHTML='<a href="#'+r.id+'" class="headerlink" title="'+r.innerHTML+'"></a>'+r.innerHTML}},n.Even=t},function(e,n){}]); !function(e){function n(o){if(t[o])return t[o].exports;var c=t[o]={i:o,l:!1,exports:{}};return e[o].call(c.exports,c,c.exports,n),c.l=!0,c.exports}var t={};n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:o})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=0)}([function(e,n,t){"use strict";var o=t(1);t(2),$(document).ready(function(){o.Even.backToTop(),o.Even.mobileNavbar(),o.Even.toc(),o.Even.fancybox()}),hljs.initHighlighting(),o.Even.highlight()},function(e,n,t){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var o={};o.backToTop=function(){var e=$("#back-to-top");$(window).scroll(function(){$(window).scrollTop()>100?e.fadeIn(1e3):e.fadeOut(1e3)}),e.click(function(){$("body,html").animate({scrollTop:0})})},o.mobileNavbar=function(){var e=$("#mobile-navbar"),n=$(".mobile-navbar-icon"),t=new Slideout({panel:document.getElementById("mobile-panel"),menu:document.getElementById("mobile-menu"),padding:180,tolerance:70});t.disableTouch(),n.click(function(){t.toggle()}),t.on("beforeopen",function(){e.addClass("fixed-open"),n.addClass("icon-click").removeClass("icon-out")}),t.on("beforeclose",function(){e.removeClass("fixed-open"),n.addClass("icon-out").removeClass("icon-click")}),$("#mobile-panel").on("touchend",function(){t.isOpen()&&n.click()})},o._initToc=function(){var e=$(".post-toc"),n=$(".post-footer");if(e.length){var t=e.offset().top-20,o=n.offset().top-e.height()-20,c={start:{position:"absolute",top:t},process:{position:"fixed",top:20},end:{position:"absolute",top:o}};$(window).scroll(function(){var n=$(window).scrollTop();n<t?e.css(c.start):n>o?e.css(c.end):e.css(c.process)})}var i=$(".toc-link"),l=$(".headerlink"),a=$.map(l,function(e){return $(e).offset().top});$(window).scroll(function(){for(var e=$(window).scrollTop(),n=0;n<i.length;n++){var t=n+1===i.length,o=a[n]-30,c=t?1/0:a[n+1]-30;o<e&&e<=c?$(i[n]).addClass("active"):$(i[n]).removeClass("active")}})},o.fancybox=function(){$.fancybox&&($(".post-content").each(function(){$(this).find("img").each(function(){$(this).wrap('<a class="fancybox" href="'+this.src+'" data-fancybox="gallery" data-caption="'+this.title+'"></a>')})}),$(".fancybox").fancybox({selector:".fancybox",protect:!0}))},o.highlight=function(){for(var e=document.querySelectorAll("pre code"),n=0;n<e.length;n++){for(var t=e[n],o=t.parentElement,c=t.innerHTML.split(/\n/).slice(0,-1),i=c.length,l="",a=0;a<i;a++)l+='<div class="line">'+(a+1)+"</div>";for(var r="",s=0;s<i;s++)r+='<div class="line">'+c[s]+"</div>";t.className+=" highlight";var f=document.createElement("figure");f.className=t.className,f.innerHTML='<table><tbody><tr><td class="gutter"><pre>'+l+'</pre></td><td class="code"><pre>'+r+"</pre></td></tr></tbody></table>",o.parentElement.replaceChild(f,o)}},o.toc=function(){var e=document.getElementById("post-toc");if(null!==e){null===document.getElementById("TableOfContents")?e.parentNode.removeChild(e):(this._linkToc(),this._initToc())}},o._linkToc=function(){for(var e=document.querySelectorAll("#TableOfContents a"),n=0;n<e.length;n++)e[n].className+=" toc-link";for(var t=1;t<=6;t++)for(var o=document.querySelectorAll(".post-content>h"+t),c=0;c<o.length;c++){var i=o[c];i.innerHTML='<a href="#'+i.id+'" class="headerlink" title="'+i.innerHTML+'"></a>'+i.innerHTML}},n.Even=o},function(e,n){}]);
//# sourceMappingURL=even.min.js.map //# sourceMappingURL=even.min.js.map

File diff suppressed because one or more lines are too long