Browse Source

Allow automatic TOC

If enabled, it will automatically add the TOC to post, page/subpage. It will check the shortcode first before add it.
pull/686/head
danpros 1 year ago
parent
commit
849249302b
3 changed files with 75 additions and 13 deletions
  1. +9
    -2
      config/config.ini.example
  2. +29
    -3
      system/admin/views/config-widget.html.php
  3. +37
    -8
      system/includes/functions.php

+ 9
- 2
config/config.ini.example View File

@ -138,12 +138,19 @@ multi.site = "false"
; TOC label ; TOC label
toc.label = "Table of Contents" toc.label = "Table of Contents"
; TOC inital state
; TOC inital state. Option "close and "open"
toc.state = "close" toc.state = "close"
; TOC style
; Load the default style or not. Option "default" and "theme"
toc.style = "default" 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 ; Set the theme here
views.root = "themes/twentysixteen" views.root = "themes/twentysixteen"


+ 29
- 3
system/admin/views/config-widget.html.php View File

@ -63,7 +63,7 @@
<div class="form-group row"> <div class="form-group row">
<label for="toc.label" class="col-sm-2 col-form-label">TOC label</label> <label for="toc.label" class="col-sm-2 col-form-label">TOC label</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" name="-config-toc.label" class="form-control" id="toc.label" value="<?php echo valueMaker(config('toc.label'));?>" placeholder="Table of Contents">
<input type="text" name="-config-toc.label" class="form-control" id="toc.label" value="<?php if(is_null(config('toc.label'))) {echo 'Table of Contents ';} else {echo config('toc.label');};?>" placeholder="Table of Contents">
</div> </div>
</div> </div>
<div class="form-group row"> <div class="form-group row">
@ -77,7 +77,7 @@
</label> </label>
</div> </div>
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="radio" name="-config-toc.state" id="toc.state2" value="close" <?php if (config('toc.state') === 'close'):?>checked<?php endif;?>>
<input class="form-check-input" type="radio" name="-config-toc.state" id="toc.state2" value="close" <?php if (config('toc.state') === 'close' || is_null(config('toc.state'))):?>checked<?php endif;?>>
<label class="form-check-label" for="toc.state2"> <label class="form-check-label" for="toc.state2">
Close Close
</label> </label>
@ -90,7 +90,7 @@
<div class="col-sm-10"> <div class="col-sm-10">
<div class="col-sm-10"> <div class="col-sm-10">
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="radio" name="-config-toc.style" id="toc.style1" value="default" <?php if (config('toc.style') === 'default'):?>checked<?php endif;?>>
<input class="form-check-input" type="radio" name="-config-toc.style" id="toc.style1" value="default" <?php if (config('toc.style') === 'default' || is_null(config('toc.style'))):?>checked<?php endif;?>>
<label class="form-check-label" for="toc.style1"> <label class="form-check-label" for="toc.style1">
Default Default
</label> </label>
@ -104,6 +104,32 @@
</div> </div>
</div> </div>
</div> </div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Automatic TOC</label>
<div class="col-sm-10">
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="radio" name="-config-toc.automatic" id="toc.automatic1" value="true" <?php if (config('toc.automatic') === 'true'):?>checked<?php endif;?>>
<label class="form-check-label" for="toc.automatic1">
<?php echo i18n('Enable');?>
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="-config-toc.automatic" id="toc.automatic2" value="false" <?php if (config('toc.automatic') === 'false' || is_null(config('toc.automatic'))):?>checked<?php endif;?>>
<label class="form-check-label" for="toc.automatic2">
<?php echo i18n('Disable');?>
</label>
</div>
</div>
<small><em>It will check the shortcode first before add the TOC to <code>post</code> or <code>page/subpage</code></em></small>
</div>
</div>
<div class="form-group row">
<label for="toc.position" class="col-sm-2 col-form-label">TOC position in x paragraph</label>
<div class="col-sm-10">
<input type="number" name="-config-toc.position" class="form-control" id="toc.position" value="<?php echo config('toc.position');?>">
</div>
</div>
<br> <br>
<h4><?php echo i18n('Comments');?></h4> <h4><?php echo i18n('Comments');?></h4>
<hr> <hr>


+ 37
- 8
system/includes/functions.php View File

@ -518,9 +518,15 @@ function get_posts($posts, $page = 1, $perpage = 0)
// Get the contents and convert it to HTML // Get the contents and convert it to HTML
$post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
$toc = explode('<!--toc-->', $post->body); $toc = explode('<!--toc-->', $post->body);
if (isset($toc['1'])) { 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 // Convert image tags to figures
@ -589,7 +595,12 @@ function get_pages($pages, $page = 1, $perpage = 0)
$toc = explode('<!--toc-->', $post->body); $toc = explode('<!--toc-->', $post->body);
if (isset($toc['1'])) { 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') { if (config('views.counter') == 'true') {
@ -664,7 +675,12 @@ function get_subpages($sub_pages, $page = 1, $perpage = 0)
$toc = explode('<!--toc-->', $post->body); $toc = explode('<!--toc-->', $post->body);
if (isset($toc['1'])) { 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') { if (config('views.counter') == 'true') {
@ -946,7 +962,7 @@ function read_category_info($category)
$toc = explode('<!--toc-->', $desc->body); $toc = explode('<!--toc-->', $desc->body);
if (isset($toc['1'])) { 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)); $desc->description = get_content_tag("d", $content, get_description($desc->body));
@ -1178,7 +1194,7 @@ function get_author($name)
$toc = explode('<!--toc-->', $author->about); $toc = explode('<!--toc-->', $author->about);
if (isset($toc['1'])) { 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; $author->body = $author->about;
@ -1232,7 +1248,7 @@ function get_frontpage()
$front->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); $front->body = MarkdownExtra::defaultTransform(remove_html_comments($content));
$toc = explode('<!--toc-->', $front->body); $toc = explode('<!--toc-->', $front->body);
if (isset($toc['1'])) { 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 { } else {
$front->title = 'Welcome'; $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'); $state = config('toc.state');
if ($state !== 'open') { if ($state !== 'open') {
@ -3613,3 +3629,16 @@ function insert_toc($part_1, $part_2, $id)
$result = $part_1 . '<div class="toc-wrapper" id="toc-wrapper.'. $id .'" style="display:none;" >'. $load . $style .'<details '. $state .'><summary title="'. $label .'"><span class="details">'. $label .'</span></summary><div class="inner"><div class="toc" id="toc.'. $id .'"></div></div></details><script src="'. site_url().'system/resources/js/toc.generator.js"></script></div>' . $part_2; $result = $part_1 . '<div class="toc-wrapper" id="toc-wrapper.'. $id .'" style="display:none;" >'. $load . $style .'<details '. $state .'><summary title="'. $label .'"><span class="details">'. $label .'</span></summary><div class="inner"><div class="toc" id="toc.'. $id .'"></div></div></details><script src="'. site_url().'system/resources/js/toc.generator.js"></script></div>' . $part_2;
return $result; return $result;
} }
// Automatically add toc in x paragraph
function automatic_toc($content, $id)
{
$pos = config('toc.position');
$exp = explode('</p>', $content);
if (is_null($pos) || $pos > count($exp)){
return $content;
}
array_splice($exp, $pos, 0, insert_toc($id));
$content = implode('</p>', $exp);
return $content;
}

Loading…
Cancel
Save