diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index de5863d2e..a11960b51 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -198,6 +198,7 @@ class Wiki extends \Zotlabs\Web\Controller { $content = $_POST['content']; $resource_id = $_POST['resource_id']; require_once('library/markdown.php'); + $content = wiki_generate_toc($content); $html = purify_html(Markdown($content)); $w = wiki_get_wiki($resource_id); $wikiURL = argv(0).'/'.argv(1).'/'.$w['urlName']; diff --git a/include/wiki.php b/include/wiki.php index 63cf70f3c..67757b0f5 100644 --- a/include/wiki.php +++ b/include/wiki.php @@ -493,4 +493,78 @@ function wiki_convert_links($s, $wikiURL) { } } return $s; -} \ No newline at end of file +} + +function wiki_generate_toc($s) { + + if (strpos($s,'[toc]') !== false) { + $toc_md = wiki_toc($s); + $s = preg_replace("/\[toc\]/", $toc_md, $s, -1); + } + return $s; +} + +// This function is derived from +// http://stackoverflow.com/questions/32068537/generate-table-of-contents-from-markdown-in-php +function wiki_toc($content) { + // ensure using only "\n" as line-break + $source = str_replace(["\r\n", "\r"], "\n", $content); + + // 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