typora 标题自动加编号

打印 上一主题 下一主题

主题 914|帖子 914|积分 2742

typora 标题自动加编号


目录

网上看了挺多其他复制粘贴的方法,但是缺点都是第一级 h1 ,都算进去了
按照规范的 markdown 规范,一个 md 文件中只能出现一个 h1,就是标题
所以最大的编号应该就是 h2,所以我拿来改了一下
新建样式

在 Typora\themes 目录下,新建一个base.user.css文件
导入我修改好的 css

已经集成了 标题自动编号, TOC 目录自动编号, 左侧导航栏自动编号
编号默认从 h2 开始计算
  1. /** initialize css counter */
  2. #write {
  3.     counter-reset: h1
  4. }
  5. h1 {
  6.     counter-reset: h2
  7. }
  8. h2 {
  9.     counter-reset: h3
  10. }
  11. h3 {
  12.     counter-reset: h4
  13. }
  14. h4 {
  15.     counter-reset: h5
  16. }
  17. h5 {
  18.     counter-reset: h6
  19. }
  20. /** put counter result into headings */
  21. #write h2:before {
  22.     counter-increment: h2;
  23.     content: counter(h2) ". "
  24. }
  25. #write h3:before,
  26. h3.md-focus.md-heading:before /** override the default style for focused headings */ {
  27.     counter-increment: h3;
  28.     content: counter(h2) "." counter(h3) ". "
  29. }
  30. #write h4:before,
  31. h4.md-focus.md-heading:before {
  32.     counter-increment: h4;
  33.     content: counter(h2) "." counter(h3) "." counter(h4) ". "
  34. }
  35. #write h5:before,
  36. h5.md-focus.md-heading:before {
  37.     counter-increment: h5;
  38.     content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) ". "
  39. }
  40. #write h6:before,
  41. h6.md-focus.md-heading:before {
  42.     counter-increment: h6;
  43.     content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) ". "
  44. }
  45. /** override the default style for focused headings */
  46. #write>h3.md-focus:before,
  47. #write>h4.md-focus:before,
  48. #write>h5.md-focus:before,
  49. #write>h6.md-focus:before,
  50. h3.md-focus:before,
  51. h4.md-focus:before,
  52. h5.md-focus:before,
  53. h6.md-focus:before {
  54.     color: inherit;
  55.     border: inherit;
  56.     border-radius: inherit;
  57.     position: inherit;
  58.     left:initial;
  59.     float: none;
  60.     top:initial;
  61.     font-size: inherit;
  62.     padding-left: inherit;
  63.     padding-right: inherit;
  64.     vertical-align: inherit;
  65.     font-weight: inherit;
  66.     line-height: inherit;
  67. }
  68. /**************************************
  69. * Header Counters in TOC
  70. **************************************/
  71. /* No link underlines in TOC */
  72. .md-toc-inner {
  73.     text-decoration: none;
  74. }
  75. .md-toc-content {
  76.     counter-reset: h1toc
  77. }
  78. .md-toc-h1 {
  79.     margin-left: 0;
  80.     font-size: 1.5rem;
  81.     counter-reset: h2toc
  82. }
  83. .md-toc-h2 {
  84.     font-size: 1.1rem;
  85.     margin-left: 2rem;
  86.     counter-reset: h3toc
  87. }
  88. .md-toc-h3 {
  89.     margin-left: 3rem;
  90.     font-size: .9rem;
  91.     counter-reset: h4toc
  92. }
  93. .md-toc-h4 {
  94.     margin-left: 4rem;
  95.     font-size: .85rem;
  96.     counter-reset: h5toc
  97. }
  98. .md-toc-h5 {
  99.     margin-left: 5rem;
  100.     font-size: .8rem;
  101.     counter-reset: h6toc
  102. }
  103. .md-toc-h6 {
  104.     margin-left: 6rem;
  105.     font-size: .75rem;
  106. }
  107. .md-toc-h2:before {
  108.     counter-increment: h2toc;
  109.     content: counter(h2toc) ". "
  110. }
  111. .md-toc-h2 .md-toc-inner {
  112.     margin-left: 0;
  113. }
  114. .md-toc-h3:before {
  115.     counter-increment: h3toc;
  116.     content: counter(h2toc) ". " counter(h3toc) ". "
  117. }
  118. .md-toc-h3 .md-toc-inner {
  119.     margin-left: 0;
  120. }
  121. .md-toc-h4:before {
  122.     counter-increment: h4toc;
  123.     content: counter(h2toc) ". " counter(h3toc) ". " counter(h4toc) ". "
  124. }
  125. .md-toc-h4 .md-toc-inner {
  126.     margin-left: 0;
  127. }
  128. .md-toc-h5:before {
  129.     counter-increment: h5toc;
  130.     content: counter(h2toc) ". " counter(h3toc) ". " counter(h4toc) ". " counter(h5toc) ". "
  131. }
  132. .md-toc-h5 .md-toc-inner {
  133.     margin-left: 0;
  134. }
  135. .md-toc-h6:before {
  136.     counter-increment: h6toc;
  137.     content: counter(h2toc) ". " counter(h3toc) ". " counter(h4toc) ". " counter(h5toc) ". " counter(h6toc) ". "
  138. }
  139. .md-toc-h6 .md-toc-inner {
  140.     margin-left: 0;
  141. }
  142. /**************************************
  143. * Header Counters in Content
  144. **************************************/
  145. /** initialize css counter */
  146. #write {
  147.     counter-reset: h1
  148. }
  149. h1 {
  150.     counter-reset: h2
  151. }
  152. h2 {
  153.     counter-reset: h3
  154. }
  155. h3 {
  156.     counter-reset: h4
  157. }
  158. h4 {
  159.     counter-reset: h5
  160. }
  161. h5 {
  162.     counter-reset: h6
  163. }
  164. /** put counter result into headings */
  165. #write h2:before {
  166.     counter-increment: h2;
  167.     content: counter(h2) ". "
  168. }
  169. #write h3:before, h3.md-focus.md-heading:before { /*override the default style for focused headings */
  170.     counter-increment: h3;
  171.     content: counter(h2) "." counter(h3) ". "
  172. }
  173. #write h4:before, h4.md-focus.md-heading:before {
  174.     counter-increment: h4;
  175.     content: counter(h2) "." counter(h3) "." counter(h4) ". "
  176. }
  177. #write h5:before, h5.md-focus.md-heading:before {
  178.     counter-increment: h5;
  179.     content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) ". "
  180. }
  181. #write h6:before, h6.md-focus.md-heading:before {
  182.     counter-increment: h6;
  183.     content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) ". "
  184. }
  185. /** override the default style for focused headings */
  186. #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 {
  187.     color: inherit;
  188.     border: inherit;
  189.     border-radius: inherit;
  190.     position: inherit;
  191.     left: initial;
  192.     float: none;
  193.     top: initial;
  194.     font-size: inherit;
  195.     padding-left: inherit;
  196.     padding-right: inherit;
  197.     vertical-align: inherit;
  198.     font-weight: inherit;
  199.     line-height: inherit;
  200. }
  201. /* Auto-Numbering Outline panel */
  202. .sidebar-content {
  203.     counter-reset: h1
  204. }
  205. .outline-h1 {
  206.     counter-reset: h2
  207. }
  208. .outline-h2 {
  209.     counter-reset: h3
  210. }
  211. .outline-h3 {
  212.     counter-reset: h4
  213. }
  214. .outline-h4 {
  215.     counter-reset: h5
  216. }
  217. .outline-h5 {
  218.     counter-reset: h6
  219. }
  220. .outline-h2>.outline-item>.outline-label:before {
  221.     counter-increment: h2;
  222.     content: counter(h2) ". "
  223. }
  224. .outline-h3>.outline-item>.outline-label:before {
  225.     counter-increment: h3;
  226.     content: counter(h2) "." counter(h3) ". "
  227. }
  228. .outline-h4>.outline-item>.outline-label:before {
  229.     counter-increment: h4;
  230.     content: counter(h2) "." counter(h3) "." counter(h4) ". "
  231. }
  232. .outline-h5>.outline-item>.outline-label:before {
  233.     counter-increment: h5;
  234.     content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) ". "
  235. }
  236. .outline-h6>.outline-item>.outline-label:before {
  237.     counter-increment: h6;
  238.     content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) ". "
  239. }
复制代码
enjoy

然后开始使用
需要注意的是,只有导出 html 的时候才能看得见这个样式,如果是直接复制是无法复制走这个序号的,如果想要复制的时候也有需要请自己写 python 脚本,或者有时间我可能会写一个

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表