gpt4 book ai didi

php - 在 php 中从 Markdown 生成目录

转载 作者:可可西里 更新时间:2023-10-31 22:14:48 25 4
gpt4 key购买 nike

我想用 Markdown 创建一个目录。
例如在 stackedit.io https://stackedit.io/editor#table-of-contents当你插入时:

[TOC]

有什么方法可以从 Markdown 中生成这个吗?

例如如果你有:

## header 1
## header 2

目录应该是:

<ol>
<li><a href="#header1">Header 1</a></li>
<li><a href="#header2">Header 2</a></li>
</ol>

我是否应该创建自己的 Markdown 解析器来获取 ToC?

最佳答案

以下是一个完成基本工作的函数:它返回找到的标题的 JSON 列表,每个标题都有其级别和文本。
此 JSON 元素可进一步用于生成所需的 HTML 结构或任何其他内容。

它的工作原理是这样的:

  1. 获取字符串形式的 markdown 文件,并将换行符规范化为仅 \n (这对下面的第 3 步很重要)
  2. 应用一个简单的正则表达式 /^(?:=|-|#).*$/mPREG_OFFSET_CAPTURE : so 匹配符合以下任一条件的所有行:
    • <h1>的“下划线” (当“=”时)或 <h2> (当“-”时)标题
    • 是标题本身(以“#”开头)
  3. 迭代匹配的行:
    • 对于“下划线”,查看前一行的源文件,位于当前行偏移量和前一个换行符之间的字符串;然后从下划线类型和上一行的文本中获取级别
    • 否则只需从当前行本身获取级别和文本

函数如下:

function markdown_toc($file_path) {
$file = file_get_contents($file_path);

// ensure using only "\n" as line-break
$source = str_replace(["\r\n", "\r"], "\n", $file);

// look for markdown TOC items
preg_match_all(
'/^(?:=|-|#).*$/m',
$source,
$matches,
PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE
);

// preprocess: iterate matched lines to create an array of items
// where each item is an array(level, text)
$file_size = strlen($source);
foreach ($matches[0] as $item) {
$found_mark = substr($item[0], 0, 1);
if ($found_mark == '#') {
// text is the found item
$item_text = $item[0];
$item_level = strrpos($item_text, '#') + 1;
$item_text = substr($item_text, $item_level);
} else {
// text is the previous line (empty if <hr>)
$item_offset = $item[1];
$prev_line_offset = strrpos($source, "\n", -($file_size - $item_offset + 2));
$item_text =
substr($source, $prev_line_offset, $item_offset - $prev_line_offset - 1);
$item_text = trim($item_text);
$item_level = $found_mark == '=' ? 1 : 2;
}
if (!trim($item_text) OR strpos($item_text, '|') !== FALSE) {
// item is an horizontal separator or a table header, don't mind
continue;
}
$raw_toc[] = ['level' => $item_level, 'text' => trim($item_text)];
}

// create a JSON list (the easiest way to generate HTML structure is using JS)
return json_encode($raw_toc);
}

这是它从 home page of the link you provided 返回的结果:

[
{"level":1,"text":"Welcome to StackEdit!"},
{"level":2,"text":"Documents"},
{"level":4,"text":"<\/i> Create a document"},
{"level":4,"text":"<\/i> Switch to another document"},
{"level":4,"text":"<\/i> Rename a document"},
{"level":4,"text":"<\/i> Delete a document"},
{"level":4,"text":"<\/i> Export a document"},
{"level":2,"text":"Synchronization"},
{"level":4,"text":"<\/i> Open a document"},
{"level":4,"text":"<\/i> Save a document"},
{"level":4,"text":"<\/i> Synchronize a document"},
{"level":4,"text":"<\/i> Manage document synchronization"},
{"level":2,"text":"Publication"},
{"level":4,"text":"<\/i> Publish a document"},
{"level":2,"text":"- Markdown, to publish the Markdown text on a website that can interpret it (**GitHub** for instance),"},
{"level":2,"text":"- HTML, to publish the document converted into HTML (on a blog for example),"},
{"level":4,"text":"<\/i> Update a publication"},
{"level":4,"text":"<\/i> Manage document publication"},
{"level":2,"text":"Markdown Extra"},
{"level":3,"text":"Tables"},
{"level":3,"text":"Definition Lists"},
{"level":3,"text":"Fenced code blocks"},
{"level":3,"text":"Footnotes"},
{"level":3,"text":"SmartyPants"},
{"level":3,"text":"Table of contents"},
{"level":3,"text":"MathJax"},
{"level":3,"text":"UML diagrams"},
{"level":3,"text":"Support StackEdit"}
]

关于php - 在 php 中从 Markdown 生成目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32068537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com