Support » Plugin: Table of Contents Plus » [Patch] Ignore empty tags

  • Resolved archon810

    (@archon810)


    Hi,

    Sometimes an editor inadvertently creates an empty h tag – something I’ve seen countless times in the 10k+ posts on our site so far. I’ve fixed this in Hackadelic TOC, but now that we’re switching to TOC Plus, I wanted to have it fixed there too. Here’s the patch to do so – it’d be great if it could be applied to the core (adjust it for white space though, as my editor inserts spaces instead of tabs):

    Index: toc.php
    ===================================================================
    --- toc.php     (revision 802211)
    +++ toc.php     (working copy)
    @@ -1288,19 +1288,22 @@
    
                                    // list item
                                    if ( in_array($matches[$i][2], $this->options['heading_levels']) ) {
    -                                       $html .= '<a href="#' . $this->url_anchor_target( $matches[$i][0] ) . '">';
    -                                       if ( $this->options['ordered_list'] ) {
    -                                               // attach leading numbers when lower in hierarchy
    -                                               $html .= '<span class="toc_number toc_depth_' . ($current_depth - $numbered_items_min + 1) . '">';
    -                                               for ($j = $numbered_items_min; $j < $current_depth; $j++) {
    -                                                       $number = ($numbered_items[$j]) ? $numbered_items[$j] : 0;
    -                                                       $html .= $number . '.';
    -                                               }
    -
    -                                               $html .= ($numbered_items[$current_depth] + 1) . '</span> ';
    -                                               $numbered_items[$current_depth]++;
    -                                       }
    -                                       $html .= strip_tags($matches[$i][0]) . '</a>';
    +          $text = strip_tags($matches[$i][0]);
    +          if (!empty($text)) {
    +                                         $html .= '<a href="#' . $this->url_anchor_target( $matches[$i][0] ) . '">';
    +                                         if ( $this->options['ordered_list'] ) {
    +                                               // attach leading numbers when lower in hierarchy
    +                                               $html .= '<span class="toc_number toc_depth_' . ($current_depth - $numbered_items_min + 1) . '">';
    +                                               for ($j = $numbered_items_min; $j < $current_depth; $j++) {
    +                                                       $number = ($numbered_items[$j]) ? $numbered_items[$j] : 0;
    +                                                       $html .= $number . '.';
    +                                               }
    +
    +                                               $html .= ($numbered_items[$current_depth] + 1) . '</span> ';
    +                                               $numbered_items[$current_depth]++;
    +                                         }
    +                                         $html .= $text . '</a>';
    +          }
                                    }

    Before this change: http://i.imgur.com/eFXYz5p.png

    After the change: http://i.imgur.com/JKsLbiK.png

    https://wordpress.org/plugins/table-of-contents-plus/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mike

    (@conjur3r)

    Thanks, I’ll put this into the next release.

    Plugin Author Mike

    (@conjur3r)

    Empty headings are now removed in version 1407 but I did it elsewhere so it would cover flat lists as the above only catered to structured/hierarchical lists.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Patch] Ignore empty tags’ is closed to new replies.