From 75ef37e4329188bd257d7179ec2b910dfe58d6f9 Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:20:53 +1300 Subject: [PATCH] Move logic to hide ToC heading if content is empty from JS to render --- mu-plugins/blocks/sidebar-container/src/view.js | 6 ------ mu-plugins/blocks/table-of-contents/index.php | 13 ++++++++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/mu-plugins/blocks/sidebar-container/src/view.js b/mu-plugins/blocks/sidebar-container/src/view.js index d80f0c766..ae5f2b023 100644 --- a/mu-plugins/blocks/sidebar-container/src/view.js +++ b/mu-plugins/blocks/sidebar-container/src/view.js @@ -100,12 +100,6 @@ function init() { // Avoids footer collisions (ex, when linked to #reply-title). onResize(); window.addEventListener( 'resize', onResize ); - - // If there is no table of contents, hide the heading. - if ( ! document.querySelector( '.wp-block-wporg-table-of-contents' ) ) { - const heading = document.querySelector( '.wp-block-wporg-sidebar-container h2' ); - heading?.style.setProperty( 'display', 'none' ); - } } window.addEventListener( 'load', init ); diff --git a/mu-plugins/blocks/table-of-contents/index.php b/mu-plugins/blocks/table-of-contents/index.php index 61d679acc..d15b61ab1 100644 --- a/mu-plugins/blocks/table-of-contents/index.php +++ b/mu-plugins/blocks/table-of-contents/index.php @@ -56,11 +56,14 @@ function render( $attributes, $content, $block ) { $title = apply_filters( 'wporg_table_of_contents_heading', __( 'In this article', 'wporg' ), $post->ID ); $content = '
'; - $content .= do_blocks( - ' -

' . esc_html( $title ) . '

- ' - ); + // If there is no content, don't render the heading. + $content .= empty( trim( wp_strip_all_tags( $post_content ) ) ) + ? '' + : do_blocks( + ' +

' . esc_html( $title ) . '

+ ' + ); $content .= '';