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

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

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

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

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

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


当前回答

这是一个简短的PHP代码,我用来生成TOC,并丰富任何标题与锚:

$toc = []; //initialize the toc to an empty array
$markdown = "... your mardown content here...";

$markdown = preg_replace_callback("/(#+)\s*([^\n]+)/",function($matches) use (&$toc){
    static $section = [];
    $h = strlen($matches[1]);

    @$section[$h-1]++;
    $i = $h;
    while(isset($section[$i])) unset($section[$i++]);

    $anchor = preg_replace('/\s+/','-', strtolower(trim($matches[2])));

    $toc[] = str_repeat('  ',$h-1)."* [".implode('.',$section).". {$matches[2]}](#$anchor)";
    return str_repeat('#',$h)." <strong>".implode('.',$section).".</strong> ".$matches[2]."\n<a name=\"$anchor\"></a>\n";
}, $markdown);

然后你可以打印经过处理的markdown和toc:

   print(implode("\n",$toc));
   print("\n\n");
   print($markdown);

其他回答

由不同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

你可以在第一行使用[TOC],然后在底部,你唯一需要做的就是确保标题使用相同的大字体。 目录会自动出来。(但这只出现在一些markdown编辑器,我没有尝试所有)

MultiMarkdown Composer似乎生成了一个目录来辅助编辑。

也可能存在这样或那样的库,它们可以生成TOC:参见Python Markdown TOC扩展。

如果您碰巧使用Eclipse,可以使用Ctrl+O(大纲)快捷键,这将显示相当于目录的内容,并允许在节标题中搜索(自动完成)。

您也可以打开大纲视图(窗口->显示视图->大纲),但它没有自动完成搜索。

正如在其他回答中提到的,有多种方法可以自动生成目录。大多数都是开源软件,可以根据您的需要进行调整。

然而,我所缺少的是,使用Markdown提供的有限选项,在视觉上吸引人的目录格式。我们得出了以下结论:

Code

## Content

**[1. Markdown](#heading--1)**

  * [1.1. Markdown formatting cheatsheet](#heading--1-1)
  * [1.2. Markdown formatting details](#heading--1-2)

**[2. BBCode formatting](#heading--2)**

  * [2.1. Basic text formatting](#heading--2-1)

      * [2.1.1. Not so basic text formatting](#heading--2-1-1)

  * [2.2. Lists, Images, Code](#heading--2-2)
  * [2.3. Special features](#heading--2-3)

----

在你的文档中,你可以像这样放置目标子部分标记:

<div id="heading--1-1"/>
### 1.1. Markdown formatting cheatsheet

根据你在哪里以及如何使用Markdown,以下也应该工作,并提供了更好看Markdown代码:

### 1.1. Markdown formatting cheatsheet <a name="heading--1-1"/>

例子呈现

内容 1. 减价 1.1. 折算格式备忘单 1.2. 折扣价格式细节 2. BBCode格式化 2.1. 基本文本格式 2.1.1. 不是最基本的文本格式 2.2. 列表,图像,代码 2.3. 特殊功能

优势

You can add as many levels of chapters and sub-chapters as you need. In the Table of Contents, these would appear as nested unordered lists on deeper levels. No use of ordered lists. These would create an indentation, would not link the number, and cannot be used to create decimal classification numbering like "1.1.". No use of lists for the first level. Here, using an unordered list is possible, but not necessary: the indentation and bullet just add visual clutter and no function here, so we don't use a list for the first ToC level at all. Visual emphasis on the first-level sections in the table of content by bold print. Short, meaningful subpart markers that look "beautiful" in the browser's URL bar such as #heading--1-1 rather than markers containing transformed pieces of the actual heading. The code uses H2 headings (## …) for sections, H3 headings (### …) for sub-headings etc.. This makes the source code easier to read because ## … provides a stronger visual clue when scrolling through compared to the case where sections would start with H1 headings (# …). It is still logically consistent as you use the H1 heading for the document title itself. Finally, we add a nice horizontal rule to separate the table of contents from the actual content.

有关这项技术以及我们如何在论坛软件Discourse中使用它的更多信息,请参见这里。