我开始使用标记来做笔记。
我用标记来查看我的笔记,它很漂亮。
但是随着我的笔记变长,我发现很难找到我想要的东西。
我知道markdown可以创建表,但它是否能够创建目录,跳转到部分,或定义页面部分markdown?
或者,是否有降价阅读器/编辑器可以做这些事情。搜索也是一个不错的功能。
简而言之,我想让它成为我很棒的笔记工具,功能就像写一本书一样。
我开始使用标记来做笔记。
我用标记来查看我的笔记,它很漂亮。
但是随着我的笔记变长,我发现很难找到我想要的东西。
我知道markdown可以创建表,但它是否能够创建目录,跳转到部分,或定义页面部分markdown?
或者,是否有降价阅读器/编辑器可以做这些事情。搜索也是一个不错的功能。
简而言之,我想让它成为我很棒的笔记工具,功能就像写一本书一样。
当前回答
我不确定,markdown的官方文件是什么? 交叉引用可以只用括号[Heading],也可以用空括号[Heading][]。
两者都使用pandoc进行工作。 所以我创建了一个快速bash脚本,它将用其TOC替换md文件中的$__TOC__。(你需要envsubst,它可能不是你发行版的一部分)
#!/bin/bash
filename=$1
__TOC__=$(grep "^##" $filename | sed -e 's/ /1. /;s/^##//;s/#/ /g;s/\. \(.*\)$/. [\1][]/')
export __TOC__
envsubst '$__TOC__' < $filename
其他回答
# 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
有两种方法可以在你的降价文档中创建你的TOC(摘要)。
1. 手动
# My Table of content
- [Section 1](#id-section1)
- [Section 2](#id-section2)
<div id='id-section1'/>
## Section 1
<div id='id-section2'/>
## Section 2
2. 以编程方式
例如,你可以使用一个脚本,为你生成摘要,看看我的项目在github - summarizeMD -
我也尝试过其他脚本/npm模块(例如doctoc),但没有人用工作锚重现TOC。
这是一个简短的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);
下面是一个生成目录的简单bash脚本。不需要特殊的依赖项,只需要bash。
https://github.com/Lirt/markdown-toc-bash
它可以很好地处理标题内的特殊符号,标记标题中的链接和忽略代码块。
您还可以使用pandoc,它是将“一种标记格式转换为另一种标记格式”的“瑞士军刀”。如果您提供——toc参数,它可以在输出文档中自动生成一个内容表。
提示:——toc需要-s(它会生成一个独立的文档),否则不会生成目录。
示例shell命令行:
./pandoc -s --toc input.md -o output.md