From 849249302b23c54f62a3c32e836b132dc284580a Mon Sep 17 00:00:00 2001 From: danpros Date: Sun, 25 Feb 2024 11:14:19 +0700 Subject: [PATCH] Allow automatic TOC If enabled, it will automatically add the TOC to post, page/subpage. It will check the shortcode first before add it. --- config/config.ini.example | 11 ++++++-- system/admin/views/config-widget.html.php | 32 +++++++++++++++++++--- system/includes/functions.php | 45 +++++++++++++++++++++++++------ 3 files changed, 75 insertions(+), 13 deletions(-) diff --git a/config/config.ini.example b/config/config.ini.example index 71f409e..29ba522 100644 --- a/config/config.ini.example +++ b/config/config.ini.example @@ -138,12 +138,19 @@ multi.site = "false" ; TOC label toc.label = "Table of Contents" -; TOC inital state +; TOC inital state. Option "close and "open" toc.state = "close" -; TOC style +; Load the default style or not. Option "default" and "theme" toc.style = "default" +; Automatically add TOC, but first it check if the shortcode available or not +; Option "true" and "false" +toc.automatic = "false" + +; Automatically insert the TOC in x paragraph +toc.position = "1" + ; Set the theme here views.root = "themes/twentysixteen" diff --git a/system/admin/views/config-widget.html.php b/system/admin/views/config-widget.html.php index a1b11eb..943f72f 100644 --- a/system/admin/views/config-widget.html.php +++ b/system/admin/views/config-widget.html.php @@ -63,7 +63,7 @@
- +
@@ -77,7 +77,7 @@
- checked> + checked> @@ -90,7 +90,7 @@
- checked> + checked> @@ -104,6 +104,32 @@
+
+ +
+
+
+ checked> + +
+
+ checked> + +
+
+ It will check the shortcode first before add the TOC to post or page/subpage +
+
+
+ +
+ +
+


diff --git a/system/includes/functions.php b/system/includes/functions.php index e058e1f..f5cdb07 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -518,9 +518,15 @@ function get_posts($posts, $page = 1, $perpage = 0) // Get the contents and convert it to HTML $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); + $toc = explode('', $post->body); if (isset($toc['1'])) { - $post->body = insert_toc($toc['0'], $toc['1'], 'post-' . $post->date); + $post->body = insert_toc('post-' . $post->date, $toc['0'], $toc['1']); + } else { + $auto = config('toc.automatic'); + if ($auto === 'true') { + $post->body = automatic_toc($post->body, 'post-' . $post->date); + } } // Convert image tags to figures @@ -589,7 +595,12 @@ function get_pages($pages, $page = 1, $perpage = 0) $toc = explode('', $post->body); if (isset($toc['1'])) { - $post->body = insert_toc($toc['0'], $toc['1'], 'page-' . $post->slug); + $post->body = insert_toc('page-' . $post->slug, $toc['0'], $toc['1']); + } else { + $auto = config('toc.automatic'); + if ($auto === 'true') { + $post->body = automatic_toc($post->body, 'page-' . $post->slug); + } } if (config('views.counter') == 'true') { @@ -664,7 +675,12 @@ function get_subpages($sub_pages, $page = 1, $perpage = 0) $toc = explode('', $post->body); if (isset($toc['1'])) { - $post->body = insert_toc($toc['0'], $toc['1'], 'subpage-' . $post->slug); + $post->body = insert_toc('subpage-' . $post->slug, $toc['0'], $toc['1']); + } else { + $auto = config('toc.automatic'); + if ($auto === 'true') { + $post->body = automatic_toc($post->body, 'subpage-' . $post->slug); + } } if (config('views.counter') == 'true') { @@ -946,7 +962,7 @@ function read_category_info($category) $toc = explode('', $desc->body); if (isset($toc['1'])) { - $desc->body = insert_toc($toc['0'], $toc['1'], 'taxonomy-' . $desc->slug); + $desc->body = insert_toc('taxonomy-' . $desc->slug, $toc['0'], $toc['1']); } $desc->description = get_content_tag("d", $content, get_description($desc->body)); @@ -1178,7 +1194,7 @@ function get_author($name) $toc = explode('', $author->about); if (isset($toc['1'])) { - $author->about = insert_toc($toc['0'], $toc['1'], 'profile-' . $author->slug); + $author->about = insert_toc('profile-' . $author->slug, $toc['0'], $toc['1']); } $author->body = $author->about; @@ -1232,7 +1248,7 @@ function get_frontpage() $front->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); $toc = explode('', $front->body); if (isset($toc['1'])) { - $front->body = insert_toc($toc['0'], $toc['1'], 'page-front'); + $front->body = insert_toc('page-front', $toc['0'], $toc['1']); } } else { $front->title = 'Welcome'; @@ -3580,8 +3596,8 @@ function publish_scheduled() } } -// Prepare toc -function insert_toc($part_1, $part_2, $id) +// Insert toc +function insert_toc($id, $part_1 = null, $part_2 = null) { $state = config('toc.state'); if ($state !== 'open') { @@ -3613,3 +3629,16 @@ function insert_toc($part_1, $part_2, $id) $result = $part_1 . '' . $part_2; return $result; } + +// Automatically add toc in x paragraph +function automatic_toc($content, $id) +{ + $pos = config('toc.position'); + $exp = explode('

', $content); + if (is_null($pos) || $pos > count($exp)){ + return $content; + } + array_splice($exp, $pos, 0, insert_toc($id)); + $content = implode('

', $exp); + return $content; +}