typora markdown 标题自动编号

typora markdown 标题自动编号

1、参考资料

  • https://blog.youkuaiyun.com/HackerTom/article/details/96640782
  • https://zhuanlan.zhihu.com/p/110257979

2、具体操作步骤

  • 在这儿打开主题文件夹

image-20200529100432557

  • 文件夹目录为:C:\Users\Heygo\AppData\Roaming\Typora\themes,在该目录下添加一个名为 base.user.css 的 css 文件

image-20200529100535802

  • 在 base.user.css 中添加如下代码
/** initialize css counter */
#write {
    counter-reset: h1
}

h1 {
    counter-reset: h2
}

h2 {
    counter-reset: h3
}

h3 {
    counter-reset: h4
}

h4 {
    counter-reset: h5
}

h5 {
    counter-reset: h6
}

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

#write h2:before {
    counter-increment: h2;
    content: counter(h1) "." counter(h2) ". "
}

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

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

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

#write h6:before,
h6.md-focus.md-heading: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;
}
  • 效果:四级、五级标题太长了

image-20200712182722498

3、我的自定义格式

3.1、样式一

  • base.user.css 中的代码
/** initialize css counter */
#write {
    counter-reset: h1
}

h1 {
    counter-reset: h2
}

h2 {
    counter-reset: h3
}

h3 {
    counter-reset: h4
}

h4 {
    counter-reset: h5
}

h5 {
    counter-reset: h6
}

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

#write h2:before {
    counter-increment: h2;
    content: counter(h1) "." counter(h2) "、 "
}

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

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

#write h5:before,
h5.md-focus.md-heading:before {
    counter-increment: h5;
    content: counter(h5) ")、 "
}

#write h6:before,
h6.md-focus.md-heading:before {
    counter-increment: h6;
    content:  "(" 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;
}
  • 效果还不错

image-20200711103543809

3.2、样式二

  • emmm… 上面的样式有一个缺点,一般我们都会使用一级标题作为文章的题目,这样后面的编号都是 1.x 、1.x.x 这样的格式,不太好看,我们再来改下吧
  • base.user.css 中的代码
/** initialize css counter */
#write {
    counter-reset: h2
}

h2 {
    counter-reset: h3
}

h3 {
    counter-reset: h4
}

h4 {
    counter-reset: h5
}

h5 {
    counter-reset: h6
}

/** 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) "、"
}

#write h5:before,
h5.md-focus.md-heading:before {
    counter-increment: h5;
    content: counter(h5) "、 "
}

#write h6:before,
h6.md-focus.md-heading:before {
    counter-increment: h6;
    content:   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;
}
  • 效果更不错了

image-20200711103835493

### 实现 Markdown 文件中多级标题自动编号Markdown 中实现多级标题自动编号可以通过多种方式完成,具体取决于所使用的工具和环境。以下是几种常见的解决方案及其特点: #### 方法一:通过 VSCode 插件 `markdown_index` 实现 可以利用 Visual Studio Code (VSCode) 的插件 `markdown_index` 来为 Markdown 文件中的标题自动生成多级序号[^1]。安装此插件后,只需运行命令 `markdown add index`,即可快速为文档的所有标题添加对应的编号。 需要注意的是,默认情况下该插件会从一级标题 (`#`) 开始编号。如果希望调整默认行为(例如仅针对二级及以上标题进行编号),可以通过修改配置项来满足需求。这种方式适合那些已经熟悉并依赖于 VSCode 编辑器工作的用户。 #### 方法二:借助 Typora 和 CSS 自定义样式 另一种可行的方法是使用支持实时预览功能的强大 Markdown 编辑器 —— **Typora**。虽然原生状态下它并不提供内置选项去直接生成带有层次结构的数字列表作为章节编号,但是我们能够通过引入外部CSS文件的方式间接达成目标[^3][^4]。 操作流程如下: 1. 首先确保已正确安装最新版本的 Typora 应用程序; 2. 创建一个新的 `.css` 文件命名为如 `base.user.css` 或者其他任意名称; 3. 将专门设计好的 AutoNumber 样式规则粘贴到上述新建的 css 文件里保存好位置路径; 4. 接着回到 Typora 设置界面找到对应的主题目录下加载刚才创建的那个额外样式表文件即完成了整个过程。 这种方法的优势在于无需频繁手动干预每次编辑完成后重新执行某些特定脚本指令就能看到即时效果呈现出来而且更加贴近最终渲染后的视觉观感体验良好。 #### 方法三:基于 Pandoc 工具链处理 除了以上提到过的两种途径之外还有一种更为通用的技术手段那就是依靠强大的文档转换引擎Pandoc来进行批量自动化加工制作含有序列化索引标签的文章内容。其基本原理就是先把原始未经修饰过的纯文本形式源码按照指定模板格式转化为富媒体类型的HTML页面然后再从中提取所需部分再反向导回简化版的目标输出物之中从而达到预期目的。 不过考虑到实际应用场景复杂度以及学习成本等因素这里不做过多展开介绍只简单提及一下供有兴趣深入研究的朋友参考借鉴而已。 --- ```python from pandocfilters import toJSONFilter, Header, Str, Space def number_sections(key, value, format_, meta): if key == 'Header': level, text, attr = value if level > 0 and level < 7: prefix = '.'.join([str(i+1) for i in range(level)]) + '. ' return Header(level, ('', [], []), [Str(prefix)] + text) if __name__ == "__main__": toJSONFilter(number_sections) ``` 上面展示了一段简单的 Python 脚本来演示如何利用 Pandoc Filters API 对输入数据流里的头部信息附加相应的计数前缀字符串形成类似树状分支式的逻辑关系表达模式以便后续进一步解析利用等等。 --- ### 总结 综上所述,无论是选用便捷高效的 IDE 插件扩展还是灵活自由程度更高的第三方应用程序配合个性化定制方案亦或者是追求极致跨平台兼容性的开源项目框架组合都能很好地解决关于 Markdown 文档内部各级别大小标题之间相互关联定位显示的需求问题只是各自适用范围略有差异罢了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值