Ver Fonte

使用mdbook官方插件实现文章目录,并扩展匹配手机

TBW\tan_b há 3 anos atrás
pai
commit
8580fa7ea9

+ 2 - 16
book.toml

@@ -1,6 +1,3 @@
-
-
-
 [book]
 language = "zh"
 authors = ["Dhammapāla","Kosalla","Dhammavarī","paññāñāṇā"]
@@ -8,21 +5,10 @@ multilingual = false
 src = "src"
 title = "教理部口袋书"
 
-[preprocessor.mermaid]
-command = "mdbook-mermaid"
-
-
-
-
 [output.html]
 git-repository-url = "https://gitee.com/jinghong-dajin-pagoda/fofazhishi.git"
 git-repository-icon = "fa-git"
 
+additional-js = ["theme/myjs/mermaid.min.js", "theme/myjs/mermaid-init.js","theme/myjs/jquery-1.12.3.min.js","theme/myjs/lrc.js","theme/myjs/nosleep.js","theme/myjs/social-share.min.js","theme/myplugin/layer/layer.js","theme/myplugin/pagetoc/sidebar.js"]
 mathjax-support = true
-additional-css = ["theme/fonts/tai-tham-kh-new-v3.ttf","theme/mycss/font.css","theme/mycss/lrc.css","theme/mycss/share.min.css","theme/fonts/iconfont.ttf","theme/fonts/iconfont.woff","theme/myplugin/layer/theme/default/layer.css","theme/myplugin/layer/theme/default/icon.png","theme/css/anchor_list.css"] 
-
-additional-js = ["theme/myjs/mermaid.min.js", "theme/myjs/mermaid-init.js","theme/myjs/jquery-1.12.3.min.js","theme/myjs/lrc.js","theme/myjs/nosleep.js","theme/myjs/anchor_list.js","theme/myjs/social-share.min.js","theme/myplugin/layer/layer.js"]
-
-[output.html.fold]
-enable = true
-level = 0
+additional-css = ["theme/fonts/tai-tham-kh-new-v3.ttf","theme/mycss/font.css","theme/mycss/lrc.css","theme/mycss/share.min.css","theme/fonts/iconfont.ttf","theme/fonts/iconfont.woff","theme/myplugin/layer/theme/default/layer.css","theme/myplugin/layer/theme/default/icon.png","theme/myplugin/pagetoc/style.css"] 

+ 0 - 99
theme/css/anchor_list.css

@@ -1,99 +0,0 @@
-    /*导航*/
-
-    .BlogAnchor {
-        /*background: #f1f1f1;*/
-        background: #ffffff;
-        /*padding: 10px;*/
-        line-height: 180%;
-        position: fixed;
-        /*left: 0px;*/
-		right: 0px;
-        top: 0px;
-        border: 1px solid #aaaaaa;
-        width: var(--sidebar-width);
-        /*height:100%;*/
-		z-index: 105;
-		transition: transform 0.3s ease 0s;
-    }
-    .BlogAnchor p {
-        font-size: 18px;
-        color: #15a230;
-        margin: 0 0 0.3rem 0;
-        text-align: right;
-    }
-    .BlogAnchor .AnchorContent {
-        /*padding: 5px 0px;*/
-        overflow: auto;
-		transition: transform 0.3s ease 0s;
- 
-    }
-    .BlogAnchor li{
-        text-indent: 0.5rem;
-        font-size: 14px;
-        list-style: none;
-    }
-    .BlogAnchor li .nav_item{
-        padding: 3px;	
-		white-space: nowrap;
-    }
-    .BlogAnchor li .item_h1{
-        margin-left: 0rem;
-    }
-    .BlogAnchor li .item_h2{
-        margin-left: 2rem;
-        font-size: 0.8rem;
-    }
- 
-    .BlogAnchor li .item_h3{
-        margin-left: 4rem;
-        font-size: 0.8rem;
-    }
- 
-    .BlogAnchor li .item_h4{
-        margin-left: 5rem;
-        font-size: 0.8rem;
-    }
- 
-    .BlogAnchor li .item_h5{
-        margin-left: 6rem;
-        font-size: 0.8rem;
-    }
- 
- 
-    .BlogAnchor li .item_h6{
-        margin-left: 7rem;
-        font-size: 0.8rem;
-    }
- 
-    .BlogAnchor li .nav_item.current{
-        color: white;
-        background-color: var(--scrollbar);
-    }
-    /*#AnchorContentToggle {
-        font-size: 13px;
-        font-weight: normal;
-        color: #FFF;
-        display: inline-block;
-        line-height: 20px;
-        background: #5cc26f;
-        font-style: normal;
-        padding: 1px 8px;
-    }*/
-    .BlogAnchor a:hover {
-        color: var(--sidebar-active);
-    }
-    .BlogAnchor a {
-        text-decoration: none;
-		color: var(--sidebar-fg);
-    }
-	.page-wrapper,.anchor-visible {
-		transform: translateX(0 - var(--sidebar-width));
-		/*margin-left: var(--sidebar-width);*/
-	}
-	@media only screen and (min-width: 620px) {
-		.page-wrapper,.anchor-visible {
-			transform: none;
-			margin-right: var(--sidebar-width);
-		}
-	}
-	

+ 1 - 2
theme/css/chrome.css

@@ -336,7 +336,7 @@ ul#searchresults span.teaser em {
     line-height: 2em;
 }
 .sidebar .sidebar-scrollbox {
-    overflow-y: scroll;
+    overflow-y: auto;
     position: absolute;
     top: 0;
     bottom: 0;
@@ -368,7 +368,6 @@ ul#searchresults span.teaser em {
 
 .sidebar-visible .page-wrapper {
     transform: translateX(var(--sidebar-width));
-	/*margin-left: var(--sidebar-width);*/
 }
 @media only screen and (min-width: 620px) {
     .sidebar-visible .page-wrapper {

+ 2 - 2
theme/css/general.css

@@ -9,7 +9,7 @@
 
 
 html {
-    font-family: "Open Sans", "Noto Sans", "Noto Sans SC", "Noto Sans TC", "Noto Sans Myanmar", "ATaiThamKHNewV3-Normal", Arial, Verdana;
+    font-family: "Times New Roman","Noto Sans",'ATaiThamKHNewV3-Normal', "Open Sans", sans-serif;
     color: var(--fg);
     background-color: var(--bg);
     text-size-adjust: none;
@@ -76,7 +76,7 @@ h6:target::before {
     box-sizing: border-box;
 }
 .js:not(.sidebar-resizing) .page-wrapper {
-    transition: 0.3s ease, transform 0.3s ease; /* Animation: slide away */
+    transition: margin-left 0.3s ease, transform 0.3s ease; /* Animation: slide away */
 }
 
 .content {

+ 2 - 3
theme/css/variables.css

@@ -2,15 +2,14 @@
 /* Globals */
 
 :root {
-    --sidebar-width: min(300px,50vw);
-    --anchorbar-width: max(-300px,-50vw);
+    --sidebar-width: 300px;
     --page-padding: 15px;
     --content-max-width: 750px;
     --menu-bar-height: 50px;
 }
 
 /* Themes */
-.light,.all,.tai,.roman,.han,.zh{
+.all,.tai,.roman,.han,.zh{
     --bg: hsl(0, 0%, 100%);
     --fg: hsl(0, 0%, 0%);
 

+ 18 - 10
theme/index.hbs

@@ -107,7 +107,7 @@
             </div>
             <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
         </nav>
-
+		
         <div id="page-wrapper" class="page-wrapper">
 
             <div class="page">
@@ -134,10 +134,10 @@
                         {{/if}}
                     </div>
 
-                    <h class="menu-title">{{ book_title }}</h>
+                    <h1 class="menu-title">{{ book_title }}</h1>
 
                     <div class="right-buttons">
-                        {{#if print_enable}}
+						{{#if print_enable}}
                         <a href="{{ path_to_root }}print.html" title="Print this book" aria-label="Print this book">
                             <i id="print-button" class="fa fa-print"></i>
                         </a>
@@ -152,10 +152,7 @@
                             <i id="git-edit-button" class="fa fa-edit"></i>
                         </a>
                         {{/if}}
-                        <a title="显示大纲" >
-                            <i id="AnchorContentToggle" class="fa fa-list-ul" state="show"></i>
-                        </a>
-						<a href="javascript:openQrcode()" ><i class="fa fa-qrcode" aria-hidden="true"></i></a>
+						<a href="javascript:openQrcode()" ><i class="fa fa-qrcode"></i></a>
                     </div>
                 </div>
 
@@ -183,6 +180,9 @@
 
                 <div id="content" class="content">
                     <main>
+						 <!-- Page table of contents -->
+						<div class="sidetoc"><nav class="pagetoc"></nav></div>
+						
                         {{{ content }}}
                     </main>
 
@@ -203,7 +203,7 @@
                     </nav>
                 </div>
             </div>
-
+			
             <nav class="nav-wide-wrapper" aria-label="Page navigation">
                 {{#previous}}
                     <a rel="prev" href="{{ path_to_root }}{{link}}" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
@@ -217,9 +217,7 @@
                     </a>
                 {{/next}}
             </nav>
-
         </div>
-		<div style="height:50vh"></div>
 
         {{#if livereload}}
         <!-- Livereload script (if served using the cli tool) -->
@@ -310,6 +308,8 @@
         {{/if}}
         {{/if}}
 		<div id="qrcode" style="margin:1rem;display:none;"></div>
+		<div id="pop-sidebar"></div>
+		<div id="btn-showsidebar"><a href="javascript:openToc()" >文&nbsp;章&nbsp;目&nbsp;录</a></div>
 		<script>
 		new QRCode($('#qrcode')[0], location.href);  // 设置要生成二维码的链接
 		function openQrcode(){
@@ -319,6 +319,14 @@
 			  content: $('#qrcode') //这里content是一个DOM,注意:最好该元素要存放在body最外层,否则可能被其它的相对元素所影响
 			});
 		}
+		var popTocIndex=-1;
+		function openToc(){
+			popTocIndex=layer.open({
+			  type: 1,
+			  title: false,
+			  content: $('#pop-sidebar') 
+			});
+		}
 		</script>
     </body>
 </html>

+ 0 - 358
theme/myjs/anchor_list.js

@@ -1,358 +0,0 @@
-
-//是否显示导航栏
- var showNavBar = true;
- //是否展开导航栏
- var expandNavBar = true;
- var currentIndex = 0;
- var currentScrollHigh = 0;
- var currentContentScrollHigh = 0;
- /*function sleep(numberMillis) {  
-    var now = new Date();  
-    var exitTime = now.getTime() + numberMillis;  
-    while (true) {  
-        now = new Date();  
-        if (now.getTime() > exitTime)  
-        return;  
-        }  
-}*/
- 
- 
-/*$(window).onbeforeunload = function(){ 
-currentIndex = 0;
-} */
- 
- 
- $(window).load(function(){
-    var h1s = $("body").find("h1");
-    var h2s = $("body").find("h2");
-    var h3s = $("body").find("h3");
-    var h4s = $("body").find("h4");
-    var h5s = $("body").find("h5");
-    var h6s = $("body").find("h6");
- 
-    var headCounts = [h1s.length, h2s.length, h3s.length, h4s.length, h5s.length, h6s.length];
-    var vH1Tag = null;  // 显示的最高层级
-    var vH2Tag = null;   // 显示的最低层级
-var sum = 0;
- 
- 
-    for(var i = 0; i < headCounts.length; i++){
-          if(headCounts[i] > 0){
-          for( var y = 0; y < headCounts[i]; y++)
-                 sum = sum + 1;
-          }
-    }
-    for(var i = 0; i < headCounts.length; i++){
-        if(headCounts[i] > 0){
-            if(vH1Tag == null){
-                vH1Tag = 'h' + (i + 1);
-            }else{
-                vH2Tag = 'h' + (i + 1);
-            }
-        }
-    }
-    if(vH1Tag == null){
-        return;
-    }
- 
-    $("#page-wrapper").after('<div class="BlogAnchor">' + 
-        //'<span style="color:red;position:absolute;top:-6px;left:0px;cursor:pointer;" οnclick="$(\'.BlogAnchor\').hide();">×</span>' +
-		//'<p>' + 
-        //    '<b id="AnchorContentToggle" title="收起" style="cursor:pointer;">目录▲</b>' + 
-        //'</p>' + 
-        '<div class="AnchorContent" id="AnchorContent"> </div>' + 
-    '</div>' );
- 
-    var vH1Index = 0;
-    var vH2Index = 0;
- 
- 
-    var vIndexH1 = 0;
-    var vIndexH2 = 0;
-    var vIndexH3 = 0;
-    var vIndexH4 = 0;
-    var vIndexH5 = 0;
-    var vIndexH6 = 0;
-    var headerALL = [];
-    var headerIDALL = [];
-    var headerHightALL = [];
-    $("body").find("h1,h2,h3,h4,h5,h6").each(function(i,item){
- 
-        var id = '';
-        var name = '';
-        var tag = $(item).get(0).tagName.toLowerCase();
-        var className = '';
-        // i=0 tag=h1          i=1 tag=h2     i=2 tag=h2
- 
-        if(tag == "h1"){
-            id = name = ++vIndexH1;
-    vIndexH2 = 0;
-    vIndexH3 = 0;
-    vIndexH4 = 0;
-    vIndexH5 = 0;
-    vIndexH6 = 0;
-    className = 'item_h1';
- 
-//alert("tag ="+ tag +"----   i = "+ i + " name ="+ name +"    className= "+ className);
-//tag =h1----   i = 0 name =1    className= item_h1
-        }else if(tag == "h2"){
- 
-            id = vIndexH1 + '_' + ++vIndexH2;
-            name = vIndexH1 + '.' + vIndexH2;
-            className = 'item_h2'; 
-    vIndexH3 = 0;
-    vIndexH4 = 0;
-    vIndexH5 = 0;
-    vIndexH6 = 0;
- 
-       }else if(tag == "h3"){
-            id = vIndexH1 + '_' + vIndexH2+ '_' + ++vIndexH3;
-            name = vIndexH1 + '.' + vIndexH2+ '.' + +vIndexH3;
-            className = 'item_h3'; 
-    vIndexH4 = 0;
-    vIndexH5 = 0;
-    vIndexH6 = 0;
- 
-       }
- 
-       else if(tag == "h4"){
-            id = vIndexH1 + '_' + vIndexH2+ '_'  +vIndexH3 +'_'+ ++vIndexH4 ;
-            name =  vIndexH1 + '.' + vIndexH2+ '.'  +vIndexH3 +'.'+  vIndexH4 ;
-            className = 'item_h4';
-    vIndexH5 = 0;
-    vIndexH6 = 0; 
-       }
- 
-       else if(tag == "h5"){
-            id = vIndexH1 + '_' + vIndexH2+ '_'  +vIndexH3 +'_' +vIndexH4+'_'+ ++vIndexH5;
-            name = vIndexH1 + '.' + vIndexH2+ '.'  +vIndexH3 +'.' +vIndexH4+'.'+ vIndexH5;
-            className = 'item_h5'; 
-            vIndexH6 = 0; 
-       }
- 
-       else if(tag == "h6"){
-            id = vIndexH1 + '_' + vIndexH2+ '_'  +vIndexH3 +'_' +vIndexH4+'_' +vIndexH5+'_'+ ++vIndexH6;
-            name = vIndexH1 + '.' + vIndexH2+ '.'  +vIndexH3 +'.' +vIndexH4+'.' +vIndexH5+'.'+ vIndexH6;
-            className = 'item_h6'; 
- 
-       }
-        if(name.length > 2){
- 
-        var mFirstName = name.substring(0,2);
-        while(mFirstName == "0."){
-        name = name.substring(2,name.length);
-        mFirstName = name.substring(0,2);
-         }
- 
-        }
-        $(item).attr("id","wow"+id+"_index_"+i);
-        $(item).addClass("wow_head");
-        var itemHeight = $(item).offset().top
-        $("#AnchorContent").css('max-height', ($(document).height()) + 'px');
-        $("#AnchorContent").css('height', ($(window).height()) + 'px');
-        $("#AnchorContent").css('overflow','auto');
-        $("#AnchorContent").append('<li><a class="nav_item '+className+' anchor-link"  href="#wow'+id+'_index_'+i+'" link="#wow'+id+'" index="'+i+'">'+name+" "+$(this).text()+" "+'</a></li>');
-        var str = "#wow"+id+"_index_"+i;
-        headerALL.push($(item));
-        headerIDALL.push(str);
-        console.log("  i = "+ i +"   id =" + id +"  itemHeight = "+ itemHeight);
-    });
- 
-    // 1
-    // 1.1
-    // 1.1.1
-    // 1.1.1.1
-    // 1.1.1.1.1
-    // 1.1.1.1.1.1
-    $("#AnchorContentToggle").click(function(){
-        var state = $(this).attr("state");
-        if(state=="show"){
-            //$(this).html("目录▼");
-            $(this).attr({"state":"hide"});
-			$("#page-wrapper").css("transform","translateX(0)");
-			//$(".next").css("transform","translateX(0)");
-			//$("#page-wrapper").removeClass('anchor-visible')
-			//$("#page-wrapper").css("max-width","unset");
-			$(".BlogAnchor").css("transform","translateX(var(--sidebar-width))");
-        }else{
-            //$(this).html("目录▲");
-			$(this).attr({"state":"show"});
-			$("#page-wrapper").css("transform","translateX(var(--anchorbar-width))");
-			//$(".next").css("transform","translateX(var(--anchorbar-width))");
-			//$("#page-wrapper").addClass('anchor-visible')
-			//$("#page-wrapper").css("max-width",xlength);
-			$(".BlogAnchor").css("transform","translateX(0)");
-        }
-        $("#AnchorContent").toggle();
-    });
- 
- 
-$(".anchor-link").click(function(){
- 
-        //$("html,body").animate({scrollTop: $($(this).attr("link")).offset().top}, 10);
-        var index  = $(this).attr("index");
-                //$(".BlogAnchor li .nav_item.current").removeClass('current');
-                //$(headerNavs[index]).addClass('current');
-                      var scrollTop = $(window).scrollTop(); // 获得将要到达的点离顶距离
-                      currentScrollHigh = scrollTop;
-                      currentContentScrollHigh = headerHightALL[index];
-      var value = headerTops[index];
-      currentIndex = value;
-      console.log("index = "+ index+ "  headerTops["+index+"] ="+ value + "scrollTop="+ scrollTop);
-    });
- 
- 
- 
- 
- 
-    var headerNavs = $(".BlogAnchor li .nav_item");
-    var headerTops = [];
-    var mHeight = 0;
-    $(".wow_head").each(function(i, n){
-    var value = $(n).offset().top;
- 
-        headerTops.push($(n).offset().top);
-         console.log("i = "+ i+ "  offset().top ="+ value);
-    });
-    headerTops.push($(document).height());
- 
-window.onresize = function(){
- 
-headerTops = [];
-$.each(headerNavs, function(i, n){
-$(n).trigger("click"); 
-//document.querySelector(headerIDALL[i]).scrollIntoView(true);
-//var high = $(n).offset().top;
-var scrollTop = $(window).scrollTop();
-headerTops.push(scrollTop);
-console.log("headerNavs_index="+i +"   scrollTop="+scrollTop +"  headerTops="+headerTops[i]);
-}
-);
-headerTops.push($(document).height());
- 
-//$("#AnchorContent").height($(window).height());
-$("#AnchorContent").css('height', ($(window).height()) + 'px');
-var xcontentWidth =  $("#AnchorContent").width();
-var xWidth = $(window).width();
-var xlength = xWidth - xcontentWidth;
-
-//$("body").css("marginRight",var(--sidebar-width));
-$(".next").css("right","var(--sidebar-width)");
-//$("body").css("max-width",xlength);
-//$(".BlogAnchor").css("top",headbar_height+'px')
-$(headerNavs[currentIndex]).trigger("click"); 
-//document.querySelector(headerIDALL[i]).scrollIntoView(true);
- 
- 
-}
- 
- 
- 
- 
- 
- 
- 
- 
- 
-$(window).scroll(function(){
-        var scrollTop = $(window).scrollTop(); // 获得将要到达的点离顶距离
- 
-        $.each(headerTops, function(i, n){
-            if( (scrollTop >= (headerTops[i])  && scrollTop < (headerTops[i+1] -1))  ){
-                console.log("headerTops[i-1]"+headerTops[i-1]+"headerTops[i]"+headerTops[i]+"  scrollTop ="+ scrollTop+"  headerTops[i+1]= "+headerTops[i+1] +"  i ="+i);
-                //$(".BlogAnchor li .nav_item.current").removeClass('current');
-                //$(headerNavs[i]).addClass('current');
-                currentScrollHigh = scrollTop;
-                currentContentScrollHigh = headerHightALL[i];
-                currentIndex = i;
-                var mxxHeight = $("#AnchorContent").height()
-                var mscrollTop1 = $("#AnchorContent").scrollTop();  // 当前标签的高度
-            console.log("zukgit2 currentIndex="+ currentIndex );
-                console.log("zukgit2 windows.scrollTop="+ scrollTop );
-                console.log("zukgit2 $(window).height()="+ $(window).height() );
-                console.log("zukgit2 currentContentScrollHigh="+ currentContentScrollHigh );
-                console.log("zukgit2 AnchorContent.mscrollTop="+ mscrollTop1 );
-                console.log("zukgit2 AnchorContent.high="+ mxxHeight );
-                if((currentContentScrollHigh - mscrollTop1)>  ($(window).height()/2)){ //↓ // 如果当前index超出  界面的高度
-                //var sum =   Math.floor(headerHightALL(i) / $(window).height());
-                var dif = currentContentScrollHigh - mscrollTop1;
-                $("#AnchorContent").animate({scrollTop: (currentContentScrollHigh)}, 50);
-                console.log("  i(zukgit3) = "+ i +"   currentContentScrollHigh =" + currentContentScrollHigh +"  mscrollTop1 = "+ mscrollTop1);
-                console.log(" $(window).height()="+ $(window).height() );
- 
-                } else if( ( mscrollTop1 - currentContentScrollHigh )>  0 ){  //↑
-                $("#AnchorContent").animate({scrollTop: currentContentScrollHigh-($("#AnchorContent").height()/3)}, 50);
-                console.log("  i(zukgit4) = "+ i +"   currentContentScrollHigh =" + currentContentScrollHigh +"  mscrollTop1 = "+ mscrollTop1);
-                console.log(" $(window).height()="+ $(window).height() +"   $(#AnchorContent).height()="+mxxHeight);
-                }
- 
-                return false;
-            }
-        }
-        );
-        if(scrollTop == 0){
-        $("#AnchorContent").animate({scrollTop: 0}, 50);
-        }
-        if(scrollTop == $(document).height()){
-        $("#AnchorContent").animate({scrollTop: headerHightALL[headerHightALL.length-1]}, 50);
-        }
-    });
- 
- 
-$.each(headerNavs, function(i, n){
-var high = $(n).offset().top;
-headerHightALL.push(high);
-console.log("high"+high);
-}
-);
- 
- 
-headerTops = [];
-$.each(headerNavs, function(i, n){
- 
-$(n).trigger("click"); 
-//document.querySelector(headerIDALL[i]).scrollIntoView(true);
- 
-var scrollTop = $(window).scrollTop();
- 
-headerTops.push(scrollTop);
-console.log("headerNavs_index="+i +"   scrollTop="+scrollTop +"  headerTops="+headerTops[i]);
- 
-}
-);
- 
- 
- 
-headerTops.push($(document).height());
-$(headerNavs[0]).trigger("click"); 
-//document.querySelector(headerIDALL[0]).scrollIntoView(true);
- 
- 
- 
- 
-var xcontentWidth =  $("#AnchorContent").width();
-var headbar_height =  $("#menu-bar-hover-placeholder").height();
-
-var xWidth = $(window).width();
-var xlength = xWidth - xcontentWidth;
-//$("#page-wrapper").css("transform","translateX(var(--sidebar-width))");
-$("#page-wrapper").css("width","100vw");
-//$(".next").css("transform","translateX(var(--sidebar-width))");
-$(".BlogAnchor").css("transform","translateX(var(--sidebar-width))")
-
-    if(!showNavBar){
-        //$('.BlogAnchor').hide();
-		$("#AnchorContentToggle").attr({"state":"hide"});
-		$("#page-wrapper").css(css("transform","translateX(0)"));
-		//$("#page-wrapper").css("max-width",xlength);
-		$(".next").css(css("transform","translateX(0)"));
-		$(".BlogAnchor").css("transform","translateX(0)");
-
-
-    }
-    if(!expandNavBar){
-        //$(this).html("目录▼");
-        //$(this).attr({"title":"展开"});
-        //$("#AnchorContent").hide();
-    }
- });

+ 97 - 0
theme/myplugin/pagetoc/sidebar.js

@@ -0,0 +1,97 @@
+// Un-active everything when you click it
+Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) {
+    el.addEventHandler("click", function() {
+        Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) {
+            el.classList.remove("active");
+        });
+        el.classList.add("active");
+    });
+});
+
+var updateFunction = function() {
+
+    var id;
+    var elements = document.getElementsByClassName("header");
+    Array.prototype.forEach.call(elements, function(el) {
+        if (window.pageYOffset >= el.offsetTop) {
+            id = el;
+        }
+    });
+
+    Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) {
+        el.classList.remove("active");
+    });
+
+    Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el) {
+        if (id && id.href.localeCompare(el.href) == 0) {
+            el.classList.add("active");
+        }
+    });
+};
+
+// Populate sidebar on load
+window.addEventListener('load', function() {
+    var pagetoc = document.getElementsByClassName("pagetoc")[0];
+    var elements = document.getElementsByClassName("header");
+    Array.prototype.forEach.call(elements, function(el) {
+        var link = document.createElement("a");
+
+        // Indent shows hierarchy
+        var indent = "";
+        switch (el.parentElement.tagName) {
+            case "H2":
+                indent = "20px";
+                break;
+            case "H3":
+                indent = "40px";
+                break;
+            case "H4":
+                indent = "60px";
+                break;
+            default:
+                break;
+        }
+
+        link.appendChild(document.createTextNode(el.text));
+        link.style.paddingLeft = indent;
+		link.href = el.href;
+        pagetoc.appendChild(link);
+    });
+		
+    updateFunction.call();
+	
+	var pop_pagetoc = document.getElementById("pop-sidebar");
+	Array.prototype.forEach.call(elements, function(el) {
+		var link = document.createElement("a");
+
+		// Indent shows hierarchy
+		var indent = "";
+		switch (el.parentElement.tagName) {
+			case "H2":
+				indent = "10px";
+				break;
+			case "H3":
+				indent = "20px";
+				break;
+			case "H4":
+				indent = "30px";
+				break;
+			default:
+				break;
+		}
+
+		link.appendChild(document.createTextNode(el.text));
+		link.style.paddingLeft = indent;
+		link.href = "javascript:clickToc('"+el.href+"');";
+		
+		pop_pagetoc.appendChild(link);
+		
+		
+	});
+});
+function clickToc(href){
+	location.href=href;
+	layer.close(popTocIndex);
+}
+// Handle active elements on scroll
+window.addEventListener("scroll", updateFunction);

+ 72 - 0
theme/myplugin/pagetoc/style.css

@@ -0,0 +1,72 @@
+@media only screen and (max-width:1439px) {
+    .sidetoc {
+        display: none;
+    }
+}
+
+@media only screen and (min-width:1440px) {
+    main {
+        position: relative;
+    }
+    .sidetoc {
+        margin-left: auto;
+        margin-right: auto;
+        left: calc(100% + (var(--content-max-width))/4 - 290px);
+        position: absolute;
+    }
+    .pagetoc {
+        position: fixed;
+		z-index:9999;
+		background: var(--sidebar-bg);
+        width: 200px;
+        height: calc(100vh - var(--menu-bar-height) - 0.67em * 4);
+        overflow-x:hidden;
+		word-wrap:break-word;
+		overflow-y:auto;
+    }
+    .pagetoc a {
+        border-left: 1px solid var(--sidebar-bg);
+        color: var(--fg) !important;
+        display: block;
+        padding-bottom: 5px;
+        padding-top: 5px;
+        padding-left: 10px;
+        text-align: left;
+        text-decoration: none;
+    }
+    .pagetoc a:hover,
+    .pagetoc a.active {
+        background: white;
+        color: var(--sidebar-fg) !important;
+    }
+    .pagetoc .active {
+        background: var(--sidebar-bg);
+        color: var(--sidebar-fg);
+    }
+}
+#pop-sidebar {
+	margin:1rem;
+	max-height:80vh;
+	display:none;
+	overflow-y:auto;
+}
+#pop-sidebar a{
+	display:block;
+	margin-top:5px;
+}
+
+#btn-showsidebar {
+	position:fixed;
+	right:0px;
+	top:50%;
+	margin-top:-90px;
+	padding:1rem 1rem 1rem 1rem;
+	writing-mode:tb-rl;
+	box-shadow:0px 0px 20px rgba(0,0,0,.3);
+	z-index:999;
+}
+
+#btn-showsidebar a{
+	display:block;
+	text-decoration:none;
+}