typora生成html后 添加左侧导航记录

注:
     jquery-3.3.1.min.js,2h.js,style.css  在head标签中引入就可以了,jquery-3.3.1.min.js可在网上下载

 

1、创建base.user.css主题文件,将文件放到该目录下

base.user.css(有的只需要这个css生成的html文件左侧导航中就有序号,那么2h.js中添加序号的方法就可以注释掉)

/* 正文标题区: #write */
/* [TOC]目录树区: .md-toc-content */
/* 侧边栏的目录大纲区: .sidebar-content */

/** 
 * 说明:
 *     Typora的标题共有6级,从h1到h6。
 *     我个人觉得h1级的标题太大,所以我的标题都是从h2级开始。
 *     个人习惯每篇文章都有一个总标题,有一个目录,所以h2级的标题前两个都不会计数。
 *     一般情况下,我虽然不使用h1级的标题,但是为了以防万一,h1级的标题前两个也都不会计数。
 *     若想启用h1级标题,就取消包含“content: counter(h1) "."”项的注释,然后将包含“content: counter(h2) "."”的项注释掉即可。
 */ 
/** initialize css counter */
#write, .sidebar-content,.md-toc-content{
	/* 设置全局计数器的基准 */
	/* 因为我喜欢从h2级标题用起,所以这里设置为h2 */
    counter-reset: h2
}

#write h1, .outline-h1, .md-toc-item.md-toc-h1 {
    counter-reset: h2
}

#write h2, .outline-h2, .md-toc-item.md-toc-h2 {
    counter-reset: h3
}

#write h3, .outline-h3, .md-toc-item.md-toc-h3 {
    counter-reset: h4
}

#write h4, .outline-h4, .md-toc-item.md-toc-h4 {
    counter-reset: h5
}

#write h5, .outline-h5, .md-toc-item.md-toc-h5 {
    counter-reset: h6
}

/** put counter result into headings */
#write h1:before,
.outline-h1>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h1>.md-toc-inner:before {
    counter-increment: h1;
    content: counter(h1) ". "
}

/* 使用h1标题时,去掉前两个h1标题的序号,包括正文标题、目录树和大纲 */
/* nth-of-type中的数字表示获取第几个h1元素,请根据情况自行修改。 */
#write h1:nth-of-type(1):before,
.outline-h1:nth-of-type(1)>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h1:nth-of-type(1)>.md-toc-inner:before,
#write h1:nth-of-type(2):before,
.outline-h1:nth-of-type(2)>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h1:nth-of-type(2)>.md-toc-inner:before{
	counter-reset: h1;
	content: ""
}

/*#write h2:before,
.outline-h2>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h2>.md-toc-inner:before {
    counter-increment: h2;
    content: counter(h2) ". "
}*/

/* 使用h2标题时,去掉前两个h2标题的序号,包括正文标题、目录树和大纲 */
/* nth-of-type中的数字表示获取第几个h2元素,请根据情况自行修改。 */
#write h2:nth-of-type(1):before,
.outline-h2:nth-of-type(1)>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h2:nth-of-type(1)>.md-toc-inner:before,
#write h2:nth-of-type(2):before,
.outline-h2:nth-of-type(2)>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h2:nth-of-type(2)>.md-toc-inner:before{
	counter-reset: h2;
	content: ""
}

#write h3:before,
h3.md-focus.md-heading:before, /** override the default style for focused headings */
.outline-h3>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h3>.md-toc-inner:before {
/*	text-decoration: none;*/
    counter-increment: h3;
    /* content: counter(h1) "." counter(h2) "." counter(h3) ". " */
     content:  counter(h3) ". "
}

#write h4:before,
h4.md-focus.md-heading:before,
.outline-h4>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h4>.md-toc-inner:before {
/*	text-decoration: none;*/
    counter-increment: h4;
    /* content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) ". " */
    content: counter(h3) "." counter(h4) ". "
}

#write h5:before,
h5.md-focus.md-heading:before,
.outline-h5>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h5>.md-toc-inner:before {
/*	text-decoration: none;*/
    counter-increment: h5;
    /* content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) ". " */
    content: counter(h3) "." counter(h4) "." counter(h5) ". "
}

#write h6:before,
h6.md-focus.md-heading:before,
.outline-h6>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h6>.md-toc-inner:before {
/*	text-decoration: none;*/
    counter-increment: h6;
    /* content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) ". " */
    content: counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) ". "
}

/** override the default style for focused headings */
#write>h3.md-focus:before,
#write>h4.md-focus:before,
#write>h5.md-focus:before,
#write>h6.md-focus:before,
h3.md-focus:before,
h4.md-focus:before,
h5.md-focus:before,
h6.md-focus:before {
    color: inherit;
    border: inherit;
    border-radius: inherit;
    position: inherit;
    left:initial;
    float: none;
    top:initial;
    font-size: inherit;
    padding-left: inherit;
    padding-right: inherit;
    vertical-align: inherit;
    font-weight: inherit;
    line-height: inherit;
}

 

 2、引入2h.js(左侧导航及序号),网上找的,根据自己的需要修改,因为我的h1标题不设置序号,setHTagValue方法的循环中i从1开始,如果h1需要序号,可修改为0

//标题序号计数器
var hCount = [0, 0, 0, 0, 0, 0];
//设置计数器
function setHCount(number) {
    //当前计数器加一
    hCount[number - 1]++;
    for (var i = number, length = hCount.length; i < length; i++) {
        //子目录计数器全部置零
        hCount[i] = 0;
    }
}
//重命名目录名称
function setHTagValue(item, number) {
    //获取标题名
    var text = $(item).get(0).innerHTML;
    //初始化空字符串
    var before = "";
    //生成序号
    for (var i = 1, length = hCount.length; i < number; i++) {
        if (i < number - 1)
            before += hCount[i] + ".";
        else
            before += hCount[i] + " ";
    }
    //在标题前面加上序号
    $(item).get(0).innerHTML = before + text;
}
function renameHTag(item) {
    var tag = $(item).get(0).localName;
    if (tag === "h1") {
        setHCount(1);
        //console\.log("捕获到标签:%s", tag);
        setHTagValue(item, 1);
    }
    if (tag === "h2") {
        setHCount(2);
        //console.log("捕获到标签:%s", tag);
        setHTagValue(item, 2);
    }
    if (tag === "h3") {
        setHCount(3);
        //console.log("捕获到标签:%s", tag);
        setHTagValue(item, 3);
    }
    if (tag === "h4") {
        setHCount(4);
        //console.log("捕获到标签:%s", tag);
        setHTagValue(item, 4);
    }
    if (tag === "h5") {
        setHCount(5);
        //console.log("捕获到标签:%s", tag);
        setHTagValue(item, 5);
    }
    if (tag === "h6") {
        setHCount(6);
        //console.log("捕获到标签:%s", tag);
        setHTagValue(item, 6)
    }
}

//是否显示导航栏
 var showNavBar = true;
 //是否展开导航栏
 var expandNavBar = true;

 $(document).ready(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 links = document.links;
   for (var i = 0; i < links.length; i++) {
    if (!links[i].target) {
        if (
            links[i].hostname !== window.location.hostname || 
            /\.(?!html?)([a-z]{0,3}|[a-zt]{0,4})$/.test(links[i].pathname)
        ) {
            links[i].target = '_blank';
            } 
        }
    }

    var headCounts = [h1s.length, h2s.length, h3s.length, h4s.length, h5s.length, h6s.length];
    var vH1Tag = null;
    var vH2Tag = null;
    var vH3Tag = null;
    var vH4Tag = null;
    var vH5Tag = null;
    var vH6Tag = null;
    for(var i = 0; i < headCounts.length; i++){
        if(headCounts[i] > 0){
            if(vH1Tag == null){
                vH1Tag = 'h' + (i + 1);
            }else if (vH2Tag == null){
                vH2Tag = 'h' + (i + 1);
            }else if (vH3Tag == null){
                vH3Tag = 'h' + (i + 1);
            }else if (vH4Tag == null){
                vH4Tag = 'h' + (i + 1);
            }else if (vH5Tag == null){
                vH5Tag = 'h' + (i + 1);
            }else {
                vH6Tag = 'h' + (i + 1);
            }
        }
    }
    if(vH1Tag == null){
        return;
    }

    $("body").prepend('<div class="BlogAnchor">' + 
        
        //'<p class="html_header">' + 
        //    '<span></span>' + 
        //'</p>' + 
        '<div class="AnchorContent" id="AnchorContent"> </div>' + 
    '</div>' );

    var vH1Index = 0;
    var vH2Index = 0;
    var vH3Index = 0;
    var vH4Index = 0;
    var vH5Index = 0;
    var vH6Index = 0;
    $("body").find("h1,h2,h3,h4,h5,h6").each(function(i,item){
        //给<H>类标签编号
        renameHTag(item);

        var id = '';
        var name = '';
        var tag = $(item).get(0).tagName.toLowerCase();
        var className = '';
        
        if(tag == vH1Tag){
            id = name = ++vH1Index;
            name = id;
            vH2Index = 0;
            className = 'item_h1';
        }else if(tag == vH2Tag){
            id = vH1Index + '_' + ++vH2Index;
            name = vH1Index + '.' + vH2Index;
            className = 'item_h2';
        }else if(tag == vH3Tag){
            id = vH1Index + '_' + vH2Index + '_' + ++vH3Index;
            name = vH1Index + '.' + vH2Index + '.' + vH3Index;
            className = 'item_h3';
        }else if(tag == vH4Tag){
            id = vH1Index + '_' + vH2Index + '_' + vH3Index + '_' + ++vH4Index;
            name = vH1Index + '.' + vH2Index + '.' + vH3Index + '.' + vH4Index;
            className = 'item_h4';
        }else if(tag == vH5Tag){
            id = vH1Index + '_' + vH2Index + '_' + vH3Index + '_' + vH4Index + '_' + ++vH5Index;
            name = vH1Index + '.' + vH2Index + '.' + vH3Index + '.' + vH4Index + '.' + vH5Index;
            className = 'item_h4';
        }else if(tag == vH6Tag){
            id = vH1Index + '_' + vH2Index + '_' + vH3Index + '_' + vH4Index + vH5Index + '_' + ++vH6Index;
            name = vH1Index + '.' + vH2Index + '.' + vH3Index + '.' + vH4Index + vH5Index + '.' + vH6Index;
            className = 'item_h6';
        }

        //只有h1和h4拿到了id,h2、h3都没拿到
        //console.log("所有id",id)
        $(item).attr("id","wow"+id);
        $(item).addClass("wow_head");
        $("#AnchorContent").css('max-height', ($(window).height() - 80) + 'px');
        $("#AnchorContent").append('<li><a class="nav_item '+ ' new' + tag + ' ' + className+' anchor-link" onclick="return false;" href="#" link="#wow'+id+'">'+""+""+$(this).text()+'</a></li>');
		$(".newh1").css("margin-left", 0);
		$(".newh2").css("margin-left", 10);
		$(".newh3").css("margin-left", 20);
		$(".newh4").css("margin-left", 55);
		$(".newh5").css("margin-left", 80);
		$(".newh6").css("margin-left", 100);
    });

			
    $(".anchor-link").click(function(){
        $("html,body").animate({scrollTop: $($(this).attr("link")).offset().top}, 500);
    });

    var headerNavs = $(".BlogAnchor li .nav_item");
    var headerTops = [];
    $(".wow_head").each(function(i, n){
        headerTops.push($(n).offset().top);
    });
    $(window).scroll(function(){
        var scrollTop = $(window).scrollTop();
        $.each(headerTops, function(i, n){
            var distance = n - scrollTop;
            if(distance >= 0){
                $(".BlogAnchor li .nav_item.current").removeClass('current');
                $(headerNavs[i]).addClass('current');
                return false;
            }
        });
    });

    if(!showNavBar){
        $('.BlogAnchor').hide();
    }
    if(!expandNavBar){
        $(this).html("目录▼");
        $(this).attr({"title":"展开"});
        $("#AnchorContent").hide();
    }
 });


 $(window).resize(function() {
  $("#AnchorContent").css('max-height', ($(window).height() - 80) + 'px');
});


//插入title的ico图标  
var ico_link = "<link rel=icon type=image/png sizes=32x32 href=2html/ico.png>";
$("head").prepend(ico_link);

3.style.css,左侧导航的样式

 /*markdown_box*/
    html{background: #F4F6F9; font-family:"Microsoft YaHei", "微软雅黑", Arial; font-weight: 400}
    body{background:#F4F6F9;font-size: 16px; color: #515254; line-height: 188%; margin: 0 !important; padding: 0 !important;}
    .html_header{display:block; height: 60px; line-height: 46px; border-bottom: 1px solid #E6ECF1; text-align: left !important;}
    .html_header span::after{content: "Title"; font-size: 17px; font-weight: bold;}
    .html_header span::before{content: "❖"; font-size: 19px; font-weight: bold; margin:0 8px 0 5px; color: #1284d9}
    blockquote{border-radius:0 4px 4px 0;color:#708090;border-left:3px solid #1284d9; background:rgba(238,242,249,.7); padding:8px 16px; font-size: 15px;}
    code{padding: 2px 4px 1px; font-weight: bold; margin:0 3px; border:1px solid rgba(255,130,130,.3);background:rgba(255,130,120,.05); color:#F66; font-family:arial;}
    a{color: #1284d9}
    a:visited{color:#1284d9}
    a:hover{text-decoration: none; color: #087cd2}

    h2{margin: 40px 0 20px; font-size: 36px; font-weight: 400}
    h3{margin-top: 40px; font-size: 30px; font-weight: 400}
    img{border-radius:5px;margin:30px 0 46px !important;width:100%;max-width: 1100px; opacity: .6; transition: opacity .3s}
    img:hover{opacity: 1}


    #write{margin:50px 50px 50px 260px; padding: 20px 60px; max-width: unset; background: #fff;border: 1px solid #E6ECF1; box-shadow: 0 2px 8px rgba(116, 129, 141, .08); border-radius: 4px}
    .BlogAnchor {width: 210px !important}

    @media screen and (max-width: 1620px) {
        #write{margin:40px 40px 40px 250px; padding: 20px 40px;}
        .BlogAnchor {width: 210px !important}
    }

    @media screen and (max-width: 1370px) {
        #write{margin:30px 30px 30px 230px; padding: 20px 40px;}
        .BlogAnchor {width: 200px !important}
    }

    @media screen and (max-width: 1220px) {
        #write{margin:20px 20px 20px 220px; padding: 20px 40px;}
    }

    @media screen and (max-width: 820px) {
        #write{margin:20px 20px 20px 200px; padding: 20px 20px; font-size: 15px; line-height: 170%}
        .BlogAnchor {width: 180px !important}
        .html_header span::before{ margin-left: 0}
        .html_header span::after{ font-size: 15px;}
        h2{ font-size: 30px; }
        h3{ font-size: 24px; }
    }

    @media screen and (max-width: 640px) {
        #write{margin:10px; padding: 16px; font-size: 14px; line-height: 160%}
        .BlogAnchor {display: none;}
        h2{ font-size: 28px; }
        h3{ font-size: 22px; }
    }


/*scroll_style*/
.AnchorContent {
    overflow-y: auto !important;
    overflow-x: hidden !important;
    margin-right:0 !important
}

.AnchorContent::-webkit-scrollbar {
    width: 6px;
    height: 10px;
    background-color: rgba(0,0,0,0) !important;
    position: absolute;
    right: 10px
}
.AnchorContent::-webkit-scrollbar-track {
    -webkit-box-shadow: none;
    border-radius: 0;
    background-color: rgba(0,0,0,0) !important
}
.AnchorContent::-webkit-scrollbar-thumb {
    border-radius: 0px;
    -webkit-box-shadow: none;
    background-color: none;
    opacity: 0 !important;
    transition: all .3s
}

.AnchorContent:hover::-webkit-scrollbar-thumb {
    border-radius: 0px;
    -webkit-box-shadow: none;
    background-color: #d4d6d8;
    border-right:2px solid #fff;
    opacity:1;
}

.AnchorContent::-webkit-scrollbar-thumb:hover {
    background-color: #c0c2c4 !important
}


/*导航*/
    .BlogAnchor {
        background: #fff;
        padding: 0px 0 ;
        line-height: 180%;
        position: fixed;
        height: 100%;
        width: 250px;
        left: 0;
        top: 0;
        border-right: 1px solid #E6ECF1;

    }
    .BlogAnchor p {
        font-size: 20px;
        color: #444;
        margin: 0 16px;
        text-align: center;
    }
    .BlogAnchor .AnchorContent {
        padding: 0px 0px;
        overflow: auto;
    }
    .BlogAnchor li{
        text-indent: 0rem;
        font-size: 14px;
        list-style: none;
        padding: 0 0px;
    }
    .BlogAnchor li .nav_item{
        padding:0px 16px;

    }


    .BlogAnchor li .item_h1{
        margin-left: 0rem;
        padding-left:0px;
        font-weight: bold;
    }
    .BlogAnchor li .item_h2{
        margin-left: 0;
        font-size: 0.95rem;
        padding-left: 0px;
        color: #708090
    }
    .BlogAnchor li .nav_item.current{
        color: #087cd2;
        background-color: rgba(238,242,249,.8);
        border-left:0px solid #087cd2;
        text-indent: 2px;
        font-weight: bold;

    }
    #AnchorContentToggle {
        font-size: 13px;
        font-weight: normal;
        color: #FFF;
        display: inline-block;
        line-height: 20px;
        background: rgb(7,196,189);
        font-style: normal;
        padding: 1px 8px;
    }
    .BlogAnchor a:hover {
        color: #087cd2  ;
        background:rgba(238,242,249,.5);
    }
    .BlogAnchor a {
        text-decoration: none;
        color: #333;
        display: block;
        border-radius: 0px;
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值