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

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

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

我知道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

其他回答

# 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

如果使用Sublime文本编辑器,MarkdownTOC插件工作得很漂亮!看到的:

https://packagecontrol.io/packages/MarkdownTOC https://github.com/naokazuterada/MarkdownTOC

安装完成后,点击Preferences—> Package Settings—> MarkdownTOC—> Settings—User,自定义设置。以下是你可以选择的选项:https://github.com/naokazuterada/MarkdownTOC#configuration。

我的建议如下:

{
  "defaults": {
    "autoanchor": true,
    "autolink": true,
    "bracket": "round",
    "levels": [1,2,3,4,5,6],
    "indent": "\t",
    "remove_image": true,
    "link_prefix": "",
    "bullets": ["-"],
    "lowercase": "only_ascii",
    "style": "ordered",
    "uri_encoding": true,
    "markdown_preview": ""
  },
  "id_replacements": [
    {
      "pattern": "\\s+",
      "replacement": "-"
    },
    {
      "pattern": "&lt;|&gt;|&amp;|&apos;|&quot;|&#60;|&#62;|&#38;|&#39;|&#34;|!|#|$|&|'|\\(|\\)|\\*|\\+|,|/|:|;|=|\\?|@|\\[|\\]|`|\"|\\.|\\\\|<|>|{|}|™|®|©|%",
      "replacement": ""
    }
  ],
  "logging": false
}

要插入目录,只需单击文档顶部想要插入目录的位置,然后转到工具—> Markdown TOC—> insert TOC。

它将插入如下内容:

<!-- MarkdownTOC -->

1. [Helpful Links:](#helpful-links)
1. [Sublime Text Settings:](#sublime-text-settings)
1. [Packages to install](#packages-to-install)

<!-- /MarkdownTOC -->

注意<!-- -->它为您插入的HTML注释。这些是特殊的标记,帮助程序知道ToC在哪里,以便它可以自动更新它为您每次保存!所以,保持这些完好无损。

为了更加花哨,在它周围添加一些<details>和<summary> HTML标记,使ToC可折叠/可展开,如下所示:

<details>
<summary><b>Table of Contents</b> (click to open)</summary>
<!-- MarkdownTOC -->

1. [Helpful Links:](#helpful-links)
1. [Sublime Text Settings:](#sublime-text-settings)
1. [Packages to install](#packages-to-install)

<!-- /MarkdownTOC -->
</details>

现在,你得到了这个超级酷的效果,如下图所示。在我的eRCaGuy_dotfiles主自述文件中看到它的作用,或者在我的Sublime_Text_editor自述文件中看到它的作用。

崩溃: 扩展:

有关它的使用和限制的额外信息,请务必阅读我在自述书中对MarkdownTOC插件的注释。

MultiMarkdown 4.7有一个{{TOC}}宏,用于插入一个目录表。

由不同Markdown解析器生成的锚标记是不相等的。

如果你正在使用Markdown解析器GFM (GitHub调味Markdown)或Redcarpet,我写了一个Vim插件来处理目录。

特性

生成Markdown文件的目录。 支持的Markdown解析器: GFM (GitHub调味Markdown) Redcarpet 更新现有的目录。 自动更新现有的目录保存。

截图

使用

生成目录

将光标移动到要添加目录的行,然后在下面键入适合您的命令。该命令将在光标进入目录后生成标题。

: GenTocGFM 生成GFM链接样式的目录。 此命令适用于GitHub存储库中的Markdown文件,如README。和Markdown文件的GitBook。 : GenTocRedcarpet 生成红地毯链接样式的目录。 这个命令适用于Jekyll或任何其他使用Redcarpet作为Markdown解析器的地方。 你可以在这里查看GFM和Redcarpet风格的toc链接之间的区别。

手动更新现有的目录

通常你不需要这样做,现有的目录将自动更新保存默认情况下。如果您想手动执行,只需使用:updatettoc命令。

下载和文档

https://github.com/mzlogin/vim-markdown-toc

我刚刚为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模块,因此,上面的命令行调用将不适用于这些版本。