From b96edd8b9ab86d84a2e67515a87de13f660d5cb1 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 26 Jun 2016 15:04:47 -0400 Subject: Added table of contents generator. Table is inserted wherever [toc] is encountered. --- Zotlabs/Module/Wiki.php | 1 + include/wiki.php | 76 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) 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
) + $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)]; + } + $o = ''; + foreach($raw_toc as $t) { + $level = intval($t['level']); + $text = $t['text']; + switch ($level) { + case 1: + $li = '* '; + break; + case 2: + $li = ' * '; + break; + case 3: + $li = ' * '; + break; + case 4: + $li = ' * '; + break; + default: + $li = '* '; + break; + } + $o .= $li . $text . "\n"; + } + return $o; +} -- cgit v1.2.3 From 216f034b6dd0d26282035218126d312080a9adc3 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 26 Jun 2016 15:27:55 -0400 Subject: Also generate table of contents when loading the page --- Zotlabs/Module/Wiki.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index a11960b51..9b89ed967 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -126,6 +126,7 @@ class Wiki extends \Zotlabs\Web\Controller { $content = ($p['content'] !== '' ? $p['content'] : '"# New page\n"'); // Render the Markdown-formatted page content in HTML require_once('library/markdown.php'); + $content = wiki_generate_toc($content); $renderedContent = wiki_convert_links(Markdown(json_decode($content)),argv(0).'/'.argv(1).'/'.$wikiUrlName); $hide_editor = false; $showPageControls = $wiki_editor; -- cgit v1.2.3 From 81da9f99e4ee111623a1ba302a6ab18e369756e9 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 26 Jun 2016 15:41:25 -0400 Subject: Fixed bug with rendering table of contents upon page load --- Zotlabs/Module/Wiki.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index 9b89ed967..a5039fa23 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -126,8 +126,9 @@ class Wiki extends \Zotlabs\Web\Controller { $content = ($p['content'] !== '' ? $p['content'] : '"# New page\n"'); // Render the Markdown-formatted page content in HTML require_once('library/markdown.php'); - $content = wiki_generate_toc($content); - $renderedContent = wiki_convert_links(Markdown(json_decode($content)),argv(0).'/'.argv(1).'/'.$wikiUrlName); + $toc_content = wiki_generate_toc(json_decode($content)); + $html = purify_html(Markdown($toc_content)); + $renderedContent = wiki_convert_links($html,argv(0).'/'.argv(1).'/'.$wikiUrlName); $hide_editor = false; $showPageControls = $wiki_editor; $showNewWikiButton = $wiki_owner; -- cgit v1.2.3 From 7124c0aee5486aab74272c81ceb3e383b2e3a7f7 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Thu, 30 Jun 2016 21:50:38 -0400 Subject: Replace homemade table of content generator with existing jQuery plugin. Now toc is linked to document headings. --- Zotlabs/Module/Wiki.php | 6 ++---- include/wiki.php | 3 ++- view/tpl/wiki.tpl | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Zotlabs/Module/Wiki.php b/Zotlabs/Module/Wiki.php index a5039fa23..f30884bfb 100644 --- a/Zotlabs/Module/Wiki.php +++ b/Zotlabs/Module/Wiki.php @@ -126,8 +126,7 @@ class Wiki extends \Zotlabs\Web\Controller { $content = ($p['content'] !== '' ? $p['content'] : '"# New page\n"'); // Render the Markdown-formatted page content in HTML require_once('library/markdown.php'); - $toc_content = wiki_generate_toc(json_decode($content)); - $html = purify_html(Markdown($toc_content)); + $html = wiki_generate_toc(purify_html(Markdown(json_decode($content)))); $renderedContent = wiki_convert_links($html,argv(0).'/'.argv(1).'/'.$wikiUrlName); $hide_editor = false; $showPageControls = $wiki_editor; @@ -200,8 +199,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)); + $html = wiki_generate_toc(purify_html(Markdown($content))); $w = wiki_get_wiki($resource_id); $wikiURL = argv(0).'/'.argv(1).'/'.$w['urlName']; $html = wiki_convert_links($html,$wikiURL); diff --git a/include/wiki.php b/include/wiki.php index 67757b0f5..424b2d9a0 100644 --- a/include/wiki.php +++ b/include/wiki.php @@ -498,7 +498,8 @@ function wiki_convert_links($s, $wikiURL) { function wiki_generate_toc($s) { if (strpos($s,'[toc]') !== false) { - $toc_md = wiki_toc($s); + //$toc_md = wiki_toc($s); // Generate Markdown-formatted list prior to HTML render + $toc_md = '
    '; // use the available jQuery plugin http://ndabas.github.io/toc/ $s = preg_replace("/\[toc\]/", $toc_md, $s, -1); } return $s; diff --git a/view/tpl/wiki.tpl b/view/tpl/wiki.tpl index 1d8570828..7617808f4 100644 --- a/view/tpl/wiki.tpl +++ b/view/tpl/wiki.tpl @@ -190,6 +190,7 @@ $(document).ready(function () { wiki_refresh_page_list(); + $("#wiki-toc").toc({content: "#wiki-preview", headings: "h1,h2,h3,h4"}); // Show Edit tab first. Otherwise the Ace editor does not load. $("#wiki-nav-tabs li:eq(1) a").tab('show'); }); @@ -203,6 +204,7 @@ $.post("wiki/{{$channel}}/preview", {content: editor.getValue(), resource_id: window.wiki_resource_id}, function (data) { if (data.success) { $('#wiki-preview').html(data.html); + $("#wiki-toc").toc({content: "#wiki-preview", headings: "h1,h2,h3,h4"}); } else { window.console.log('Error previewing page.'); } -- cgit v1.2.3