Skip to content

Commit 510f025

Browse files
authored
Add files via upload
Added Search box, fixed styling, fixed toc generation
1 parent 3a6aa7c commit 510f025

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

style.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@
3333

3434
/* TOC List Styling */
3535
.toc-list {
36-
list-style-type: none;
36+
list-style-type: none !important;
3737
padding-left: 0;
3838
}
3939

40+
.toc-list ul {
41+
margin-left: 0;
42+
}
43+
4044
.toc-list li {
4145
margin-bottom: 8px;
4246
}
@@ -235,7 +239,3 @@ h3 {
235239
.article-item {
236240
margin-bottom: 20px;
237241
}
238-
239-
.post-content h1 {
240-
display: none !important;
241-
}

tuts.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
Plugin Name: Shy's Tutorials & Handbooks
44
Description: Create, manage, and restrict access to tutorials and handbooks, with features for manual user assignment and private content sharing.
5-
Version: 1.3
5+
Version: 1.4
66
License: GPLv2
77
Author: HackTheDev
88
*/
@@ -97,7 +97,7 @@ function thp_register_custom_post_type() {
9797
}
9898
add_action('init', 'thp_register_custom_post_type');
9999

100-
// Generate TOC if headings are present
100+
// Generate nested toc
101101
function thp_generate_toc($content) {
102102
global $post;
103103

@@ -109,11 +109,43 @@ function thp_generate_toc($content) {
109109
$toc = '<div class="toc-content-container">';
110110
$toc .= '<aside class="toc-widget">';
111111
$toc .= '<h2>Table of Contents</h2><ul class="toc-list">';
112+
113+
// To track the nesting level
114+
$current_level = 2;
115+
$list_stack = array();
116+
112117
foreach ($matches[2] as $key => $heading) {
118+
$heading_level = (int)$matches[1][$key];
113119
$anchor = sanitize_title($heading);
120+
121+
// Open new lists as needed
122+
if ($heading_level > $current_level) {
123+
// Push current list onto stack
124+
$list_stack[] = $toc;
125+
$toc .= '<ul>';
126+
} elseif ($heading_level < $current_level) {
127+
// Close lists as needed
128+
while ($heading_level < $current_level) {
129+
$toc .= '</ul>';
130+
$current_level--;
131+
$toc = array_pop($list_stack);
132+
}
133+
}
134+
135+
// Add the list item
114136
$toc .= '<li><a href="#' . $anchor . '">' . $heading . '</a></li>';
115-
$content = str_replace($matches[0][$key], '<h' . $matches[1][$key] . ' id="' . $anchor . '">' . $heading . '</h' . $matches[1][$key] . '>', $content);
137+
138+
// Update current level
139+
$current_level = $heading_level;
140+
$content = str_replace($matches[0][$key], '<h' . $heading_level . ' id="' . $anchor . '">' . $heading . '</h' . $heading_level . '>', $content);
116141
}
142+
143+
// Close any remaining open lists
144+
while ($current_level > 2) {
145+
$toc .= '</ul>';
146+
$current_level--;
147+
}
148+
117149
$toc .= '</ul></aside>';
118150
$toc .= '<div class="post-content">';
119151
$toc .= '<h1 class="tutorial_page_title">' . get_the_title($post->ID) . '</h1>'; // Include the post title in the content area
@@ -143,6 +175,8 @@ function thp_generate_toc($content) {
143175
}
144176
add_filter('the_content', 'thp_generate_toc');
145177

178+
179+
146180
// Add a meta box for hiding a tutorial from general listing
147181
function thp_add_hide_meta_box() {
148182
add_meta_box(
@@ -428,6 +462,12 @@ function shy_knowledge_base_shortcode($atts) {
428462

429463
?>
430464
<div class="knowledge-base-container">
465+
466+
<style>
467+
.post-content h1 {
468+
display: none !important;
469+
}
470+
</style>
431471
<h2 class="knowledge-base-title"><?php echo esc_html(get_the_title()); ?></h2>
432472

433473
<!-- Search Bar -->

0 commit comments

Comments
 (0)