背景
Typora是一个WYSIWYG(所见即所得)的Markdown编辑器,十分好用。此处省略1万字推荐功能介绍。
近期使用它整理文档的时候发现需要为各级标题自动编号。Typora并没有这项配置。手动编写编号非常麻烦,而且不会随着标题的新增和删除自动更新。
经过一番研究,本人发现了解决办法,这里分享给大家。
实现方法
首先找到Typora存放主题文件的位置。点击主界面文件
-> 偏好设置
。打开的新界面依次点击外观
-> 打开主题文件夹
。如下图所示:
点击之后打开了Typora主题存放目录。我们编写一个base.user.css
文件放入此目录。文件内容如下:
/** initialize css counter */
#write, .sidebar-content,.md-toc-content {
counter-reset: h1
}
#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,
h1.md-focus.md-heading:before,
.outline-h1>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h1>.md-toc-inner:before{
counter-increment: h1;
content: counter(h1) " "
}
#write h2:before,
h2.md-focus.md-heading:before,
.outline-h2>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h2>.md-toc-inner:before{
counter-increment: h2;
content: counter(h1) "." counter(h2) " "
}
#write h3:before,
h3.md-focus.md-heading:before,
.outline-h3>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h3>.md-toc-inner:before {
counter-increment: h3;
content: counter(h1) "." counter(h2) "." 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 {
counter-increment: h4;
content: counter(h1) "." counter(h2) "." 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 {
counter-increment: h5;
content: counter(h1) "." counter(h2) "." 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 {
counter-increment: h6;
content: counter(h1) "." counter(h2) "." 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;
}
该CSS文件支持各级标题生成自动编号。同时也支持目录中标题的自动编号。主要注意的是标题级别不要跳跃(比如一级标题之下直接写三级标题),否则自动编号会有异常。
最后一定要记得重启Typora,使主题生效。
其实markdown就是一种HTML的简写形式,markdown的所有格式都和HTML标签或CSS class对应。上面的CSS就是覆盖了默认的标题和目录中标题的CSS样式。如果不知道markdown对应的HTML元素或CSS class是什么,可以调出Typora的开发者工具(
视图
->开发者工具
),使用Inspect工具(ctrl + shift + c),在调试界面查看Markdown对应的HTML代码。
参考链接
官网添加自动编号:Auto Numbering for Headings
官网添加自定义CSS:Add Custom CSS