Skip to content

Commit bf557d3

Browse files
committed
Changing to incude headlings by default, with option to disable in container.
1 parent 0b23593 commit bf557d3

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

plugin.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: SimpleTOC - Table of Contents Block
44
* Plugin URI: https://marc.tv/simpletoc-wordpress-inhaltsverzeichnis-plugin-gutenberg/
55
* Description: SEO-friendly Table of Contents Gutenberg block. No JavaScript and no CSS means faster loading.
6-
* Version: 6.9.7
6+
* Version: 7.0.0
77
* Author: Marc Tönsing
88
* Author URI: https://toensing.com
99
* Text Domain: simpletoc
@@ -153,13 +153,14 @@ function ( $toc_plugins ) {
153153
* Adds IDs to the headings of the provided post content using a recursive block structure.
154154
*
155155
* @param string $content The content to add IDs to.
156+
* @param string $skip_regex The regex to skip the content from being processed.
156157
* @return string The content with IDs added to its headings
157158
*/
158-
function simpletoc_add_ids_to_content( $content ) {
159+
function simpletoc_add_ids_to_content( $content, $skip_regex = false ) {
159160

160161
// Return early if the content does not contain a simpletoc shortcode.
161162
$maybe_shortcode_result = preg_match( '/\[simpletoc ([^\]]*)\]/m', $content, $matches );
162-
if ( ! $maybe_shortcode_result ) {
163+
if ( ! $maybe_shortcode_result && ! $skip_regex ) {
163164
return $content;
164165
}
165166

@@ -272,30 +273,45 @@ function add_ids_to_blocks( $content ) {
272273
}
273274
}
274275

276+
// If tag has a parent with 'simpletoc-excluded' class, then skip it.
277+
// Check if any parent has the 'simpletoc-excluded' class (similar to JS .closest).
278+
$parent = $tag->parentNode; // phpcs:ignore.
279+
while ( $parent ) {
280+
$parent_classes = isset( $parent->className ) ? $parent->className : ''; // phpcs:ignore.
281+
if ( $parent_classes && strpos( $parent_classes, 'simpletoc-excluded' ) !== false ) {
282+
continue 2; // Skip this tag, jump out of both while and foreach.
283+
}
284+
$parent = $parent->parentNode ?? null; // phpcs:ignore.
285+
if ( ! $parent ) {
286+
continue;
287+
}
288+
}
289+
275290
/**
276291
* Filter to skip headings inside container blocks.
277292
*
278293
* @param bool $skip_in_wrapper Whether to skip headings inside container blocks.
279294
* @return bool The filtered value.
295+
* @since 7.0.0
280296
*/
281-
$skip_in_wrapper = apply_filters( 'simpletoc_skip_in_wrapper', true );
297+
$skip_in_wrapper = apply_filters( 'simpletoc_skip_in_wrapper', false );
282298
if ( strpos( $tag_classes, 'simpletoc-include' ) !== false ) {
283299
// If someone has added the simpletoc-include class, then don't skip it, regardless of wrapper.
284300
$skip_in_wrapper = false;
285301
}
286302
if ( $skip_in_wrapper ) {
287303
// Try to get parent tag.
288-
$parent_tag = $tag->parentNode;
289-
if ( $parent_tag && isset( $parent_tag->tagName ) ) {
290-
if ( in_array( strtolower( strtolower( $parent_tag->tagName ) ), array( 'div', 'section', 'article', 'main', 'header', 'footer' ), true ) ) {
304+
$parent_tag = $tag->parentNode; // phpcs:ignore.
305+
if ( $parent_tag && isset( $parent_tag->tagName ) ) { // phpcs:ignore.
306+
if ( in_array( strtolower( strtolower( $parent_tag->tagName ) ), array( 'div', 'section', 'article', 'main', 'header', 'footer' ), true ) ) { // phpcs:ignore.
291307
continue;
292308
}
293309
}
294310
}
295311

296312
// Set the ID attribute of the headline anchor if it doesn't exist.
297313
$tag_id = $tag->getAttribute( 'id' );
298-
$headline_anchor = SimpleTOC_Headline_Ids::get_headline_anchor( $tag->ownerDocument->saveHTML( $tag ), true );
314+
$headline_anchor = SimpleTOC_Headline_Ids::get_headline_anchor( $tag->ownerDocument->saveHTML( $tag ), true ); // phpcs:ignore.
299315
if ( empty( $tag_id ) ) {
300316
$tag->setAttribute( 'id', $headline_anchor );
301317
}
@@ -332,7 +348,7 @@ function render_callback_simpletoc( $attributes ) {
332348

333349
// This processes post content and stores the toc headlines for later rendering. Very simple content processing and fast. The placeholder is skipped.
334350
$post_content = do_blocks( $post_content );
335-
$post_content = simpletoc_add_ids_to_content( $post_content );
351+
$post_content = simpletoc_add_ids_to_content( $post_content, true );
336352

337353
// Now get the toc html only.
338354
$return = simpletoc_render_toc( $post_content, true, $attributes, $wrapper_attrs );
@@ -434,29 +450,40 @@ function filter_headings( $content ) {
434450
continue;
435451
}
436452
}
453+
// If tag has a parent with 'simpletoc-excluded' class, then skip it.
454+
// Check if any parent has the 'simpletoc-excluded' class (similar to JS .closest).
455+
$parent = $tag->parentNode; // phpcs:ignore.
456+
while ( $parent && isset( $parent->getAttribute ) ) { // phpcs:ignore.
457+
$parent_classes = $parent->getAttribute( 'class' );
458+
if ( $parent_classes && strpos( $parent_classes, 'simpletoc-excluded' ) !== false ) {
459+
continue 2; // Skip this tag, jump out of both while and foreach.
460+
}
461+
$parent = $parent->parentNode; // phpcs:ignore.
462+
}
437463

438464
/**
439465
* Filter to skip headings inside container blocks.
440466
*
441467
* @param bool $skip_in_wrapper Whether to skip headings inside container blocks.
442468
* @return bool The filtered value.
469+
* @since 7.0.0
443470
*/
444-
$skip_in_wrapper = apply_filters( 'simpletoc_skip_in_wrapper', true );
471+
$skip_in_wrapper = apply_filters( 'simpletoc_skip_in_wrapper', false );
445472
if ( strpos( $tag_classes, 'simpletoc-include' ) !== false ) {
446473
// If someone has added the simpletoc-include class, then don't skip it, regardless of wrapper.
447474
$skip_in_wrapper = false;
448475
}
449476
if ( $skip_in_wrapper ) {
450477
// Try to get parent tag.
451-
$parent_tag = $tag->parentNode;
452-
if ( $parent_tag && isset( $parent_tag->tagName ) ) {
453-
if ( in_array( strtolower( strtolower( $parent_tag->tagName ) ), array( 'div', 'section', 'article', 'main', 'header', 'footer' ), true ) ) {
478+
$parent_tag = $tag->parentNode; // phpcs:ignore.
479+
if ( $parent_tag && isset( $parent_tag->tagName ) ) { // phpcs:ignore.
480+
if ( in_array( strtolower( strtolower( $parent_tag->tagName ) ), array( 'div', 'section', 'article', 'main', 'header', 'footer' ), true ) ) { // phpcs:ignore.
454481
continue;
455482
}
456483
}
457484
}
458485

459-
$arr[] = $tag->ownerDocument->saveHTML( $tag ); // This gets the full HTML of the heading tag.
486+
$arr[] = $tag->ownerDocument->saveHTML( $tag ); // phpcs:ignore.
460487
}
461488

462489
return $arr;

0 commit comments

Comments
 (0)