我开始使用标记来做笔记。

我用标记来查看我的笔记,它很漂亮。

但是随着我的笔记变长,我发现很难找到我想要的东西。

我知道markdown可以创建表,但它是否能够创建目录,跳转到部分,或定义页面部分markdown?

或者,是否有降价阅读器/编辑器可以做这些事情。搜索也是一个不错的功能。

简而言之,我想让它成为我很棒的笔记工具,功能就像写一本书一样。


当前回答

基于albertodebortoli answer创建了附加检查和标点符号替换功能。

# @fn       def generate_table_of_contents markdown # {{{
# @brief    Generates table of contents for given markdown text
#
# @param    [String]  markdown Markdown string e.g. File.read('README.md')
#
# @return   [String]  Table of content in markdown format.
#
def generate_table_of_contents markdown
  table_of_contents = ""
  i_section = 0
  # to track markdown code sections, because e.g. ruby comments also start with #
  inside_code_section = false
  markdown.each_line do |line|
    inside_code_section = !inside_code_section if line.start_with?('```')

    forbidden_words = ['Table of contents', 'define', 'pragma']
    next if !line.start_with?('#') || inside_code_section || forbidden_words.any? { |w| line =~ /#{w}/ }

    title = line.gsub("#", "").strip
    href = title.gsub(/(^[!.?:\(\)]+|[!.?:\(\)]+$)/, '').gsub(/[!.,?:; \(\)-]+/, "-").downcase

    bullet = line.count("#") > 1 ? " *" : "#{i_section += 1}."
    table_of_contents << "  " * (line.count("#") - 1) + "#{bullet} [#{title}](\##{href})\n"
  end
  table_of_contents
end

其他回答

我刚刚为python-markdown编写了一个扩展,它使用它的解析器来检索标题,并将TOC输出为带有本地链接的markdown格式的无序列表。文件是

Md_toc.py(是Md_toc.py)

... 它应该放在markdown安装的markdown/extensions/目录中。然后,你所要做的就是输入anchor <a> tags,带一个id="…"属性作为引用-对于这样的输入文本:

$ cat test.md 
Hello
=====

## <a id="sect one"></a>SECTION ONE ##

something here

### <a id='sect two'>eh</a>SECTION TWO ###

something else

#### SECTION THREE

nothing here

### <a id="four"></a>SECTION FOUR

also...

... 扩展可以这样调用:

$ python -m markdown -x md_toc test.md 
* Hello
    * [SECTION ONE](#sect one)
        * [SECTION TWO](#sect two)
            * SECTION THREE
        * [SECTION FOUR](#four)

... 然后您可以将这个toc粘贴回您的标记文档中(或者在您的文本编辑器中有一个快捷方式,在当前打开的文档上调用脚本,然后将结果toc插入到同一文档中)。

注意,旧版本的python-markdown没有__main__.py模块,因此,上面的命令行调用将不适用于这些版本。

如果你正在使用Discount markdown,启用一个标志-ftoc来自动生成,并使用-T来在一个目录的前面,例如:

markdown -T -ftoc <<EOT
#heading 1

content 1

##heading 2

content 2
EOT

将会产生

<ul>
 <li><a href="#heading-1">heading 1</a>
 <ul>
  <li><a href="#heading-2">heading 2</a></li>
 </ul>
 </li>
</ul>
<a name="heading-1"></a>
<h1>heading 1</h1>
...

显然你也可以使用markdown -toc,这个人没有提到,但是USAGE信息可以(由markdown -h这样的非法选项触发)。


我花了一段时间阅读源代码才明白这一点,所以我把它写下来主要是为了未来的我。我在Arch Linux上使用折扣降价,从折扣包。这个人并没有真正告诉你这是折扣,但提到了大卫·帕森斯的作者。

markdown --version
# markdown: discount 2.2.7

只需要增加幻灯片的数量!它与markdown ioslides和revealjs演示一起工作

## Table of Contents

 1. [introduction](#3)
 2. [section one](#5)
# Table of Contents
1. [Example](#example)
2. [Example2](#example2)
3. [Third Example](#third-example)

## Example [](#){name=example}
## Example2 [](#){name=example2}
## [Third Example](#){name=third-example}

如果您使用markdown extra,不要忘记您可以为链接、标题、代码围栏和图像添加特殊属性。 https://michelf.ca/projects/php-markdown/extra/#spe-attr

如果你的Markdown文件要在bitbucket.org上的回购中显示,你应该在你想要你的目录的位置添加[TOC]。然后它将自动生成。更多信息:

https://confluence.atlassian.com/bitbucket/add-a-table-of-contents-to-a-wiki-221451163.html