Markdown / Rdiscount 是否支持编号标题?

104

我正在尝试生成类似以下的节标题/子节标题的HTML:

  1. 我的一级主题
    1.1 我的第一个子主题
    1.2 另一个子主题
          1.2.1 一个子子主题
  2. 另一个一级主题

有哪些Markdown实现可以生成这种带编号的节标题?

5个回答

73

是的,尝试一下Pandoc。这对我很有效:

pandoc --number-sections < test.md > out.html

(来源)

要生成原帖所提到的有序大纲,Markdown 代码如下:

# My top-level topic

## My first subtopic

## Another subtopic

### A sub-subtopic

## Another top-level topic

如果您想为子部分实现更深的缩进,可以使用内联CSS来实现。例如,将以下内容放置在上述Markdown源文件的顶部将缩进标题:
<style type="text/css">
  h2 { margin-left: 10px; }
  h3 { margin-left: 20px; }
</style>

但是假设您在标题下有一些段落文字...我不知道如何将其缩进到与上面的标题相同的级别。

更新于2015年10月18日Markdeep拥有编号标题(和许多其他高级功能)。也请查看一下!


在输出文件之前需要使用“-o”选项,例如“pandoc --number-sections < test.md > -o out.html”。 - betontalpfa
1
@betontalpfa 不是的。 - Adam Monsen

42
如果您的 Markdown 工具支持使用 CSS 定制主题,请将以下代码片段添加到 CSS 中以启用标题编号:
body {
    counter-reset: h1
}

h1 {
    counter-reset: h2
}

h2 {
    counter-reset: h3
}

h3 {
    counter-reset: h4
}

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

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

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

h4:before {
    counter-increment: h4;
    content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) ". "
}

我使用Typora,它支持这种方法中标题的自动编号


好的,这适用于Atom,如果有人感兴趣:将第一个条目更改为 h1 { counter-increment: h1 },然后注释掉 h1:before 中的 counter-increment: h1。我不明白为什么... - Alex
2
这应该被选为通用解决方案。它适用于Typora、Multimarkdown和Pandoc。 - user5671078
这应该是被接受的解决方案,因为它非常通用。 - Imad

14
如果你想编辑Markdown文件本身,而不仅仅是生成的HTML文件, 请尝试使用Python 3中的 enumerate-markdown
pip install enumerate-markdown
markdown-enum filename.md 1 filename.md

示例 - 输入

# header 1
text
## header 2
text
# header 3
text

输出

# 1.  header 1
text
## 1.1  header 2
text
# 2.  header 3
text

如果您稍后编辑文件并再次运行脚本,则会更新旧枚举。


1
好吧,这是一个曲线球(markdown-enum作为命令而不是包名enumerate-markdown)。虽然如此,但正是我正在寻找的! - ijoseph
1
我必须使用 markdown-enum filename.md <minimal-level> filename.md 才能使其工作。例如,markdown-enum filename.md 2 filename.md,它跳过了第一级的编号。 - JohnShape

2
正文翻译:正如@adam-monsen所指出的,“pandoc --number-sections”就可以搞定。你也可以简单地在YAML头部添加numbersections: true来为你的文件启用编号标题。
注:YAML是一种可读性高、用于序列化数据的格式,常用于配置文件和语言间的数据交换。

-1

Markdown旨在快速、轻便且易于使用,它完美地实现了这一目标。对于更复杂的格式,最好考虑除markdown之外的选项。这不是逃避责任。通常情况下,例如在Microsoft语言和工具中,我想做“xyz”,然后意识到在那个世界里,你会被设计成远离“xyz”,而是采用首选/支持的方式来实现你的目标。

以vscode为例,考虑工具栏/自定义。Vscode并不是以工具栏为中心的。这是有意设计的。设计意图是使用命令面板Ctrl+Shift+P。这样可以节省时间,不必不断地定制工具栏,并提供快速访问所有命令,而不仅仅是工具栏上的一部分命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接