写在前面

Markdown 语法定义了 1-6 级标题,出于个人习惯,1 级标题我很少s使用,常用 2-4 级标题分别对应1.1.11.1.1三个大纲级别。我们在用Markdown写作的时候,虽然可以一个一个去敲大纲编号,但是却不够优雅,并且修改大量内容后易错排。

我一般用Typora写作,设置Typora正文和侧边栏的自动编号只需要添加css样式即可:

/**************************************
 * Header Counters in Content
 **************************************/

/** initialize css counter */
#write {
    counter-reset: h2
}


h2 {
    counter-reset: h3
}

h3 {
    counter-reset: h4
}

h4 {
    counter-reset: h5
}


/** put counter result into headings */
#write h2:before {
    counter-increment: h2;
    content: counter(h2) ". "
}

#write h3:before,
h3.md-focus.md-heading:before /** override the default style for focused headings */ {
    counter-increment: h3;
    content: counter(h2) "." counter(h3) " "
}

#write h4:before,
h4.md-focus.md-heading:before {
    counter-increment: h4;
    content: counter(h2) "." counter(h3) "." counter(h4) " "
}



/** override the default style for focused headings */
#write>h3.md-focus:before,
#write>h4.md-focus:before,


h3.md-focus:before,
h4.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;
}

/**************************************
 * Header Counters in Sidebar
 **************************************/

 .sidebar-content {
    counter-reset: h2
}
 
/* .outline-h1 {
    counter-reset: h2
} */
 
.outline-h2 {
    counter-reset: h3
}
 
.outline-h3 {
    counter-reset: h4
}
 
.outline-h4 {
    counter-reset: h5
}
 
 
.outline-h2>.outline-item>.outline-label:before {
    counter-increment: h2;
    content: counter(h2) ". " /* <--- delete this dot if not wanted */
}
 
.outline-h3>.outline-item>.outline-label:before {
    counter-increment: h3;
    content: counter(h2) "." counter(h3) " " /* <--- delete this dot if not wanted */
}
 
.outline-h4>.outline-item>.outline-label:before {
    counter-increment: h4;
    content: counter(h2) "." counter(h3) "." counter(h4) " " /* <--- delete this dot if not wanted */
}

下面我将以PaperMod主题为基础,添加目录和正文的自动编号。

添加 CSS 类

编辑 PaperMod/layouts/_default/single.html文件,注释掉原始的post-single类,添加post

{{- define "main" }}
<article class="post" {{- if .Param "autonumbering" }} autonumbering {{- end }}>
<!-- <article class="post-single"> -->

添加 CSS 样式

body {counter-reset: h2}
h2 {counter-reset: h3}
h3 {counter-reset: h4}
h4 {counter-reset: h5}

article[autonumbering] h2:before {counter-increment: h2; content: counter(h2) ". "}
article[autonumbering] h3:before {counter-increment: h3; content: counter(h2) "." counter(h3) " "}
article[autonumbering] h4:before {counter-increment: h4; content: counter(h2) "." counter(h3) "." counter(h4) " "}

article[autonumbering] .toc ul { counter-reset: item }
article[autonumbering] .toc li a:before { content: counters(item, ".") " "; counter-increment: item }

添加属性

在文章头部信息前面添加autonumbering: true即可完成正文和目录的自动编号!