Topic on Extension talk:Header Tabs/Archive 2

Hide Level 1 headings in TOC

14
Krabina (talkcontribs)

Usually, Level 1 headings (marked with =) are not used in regular wiki pages. HeaderTabs uses them to define headers. The problem now is that the regular TOC in mediawiki pages also includes the level 1 headings (that already are used and therefore visible on the page in headertabs) are displayes in the TOC again.

Therefore I would need some ability of telling mediawiki NOT to include level 1 headings in the TOC.

As this is only a problem with headertabs it would be great to have a config option...

2.238.45.117 (talkcontribs)

This is my solution.

  1. If you are NOT an expert on mediawiki/semantic read the TOC and SECTIONS related help pages. You will not find at the time I am writing the solution for your problem, but for sure (like in my case) you will end up with a better understanding of it :)
  2. I ended up debugging the code and here the solution.

Considering that the best practice is not to use single "=" for sections this might be taken in consideration as change in the core mediawiki code.. (anyone reading up there?!?)


The solution consists in changing includes/parser/Parser.php.

I left the logic for TOC creation unaltered, otherwise you will end up having problem with the header tabs excentions rendering.

The goal is to declare the Header Tab section within a proper header defined with double "=" and HIDE in the TOC the section declared with single "=".

This last part might be changed in order to show the tabs with an appropriare TOC level, but in this in my opinion/experience is useless because

  1. the header tab section is rendered as unique block
  2. the TOC links are not able to point to the appropriate headed tab.


So..


NOTES:

This change will be applied to every level 1 header defined in your WIKI!!

USAGE:

  1. Declare your section header as == MY SECTION ==
  2. Write within your section your header tab content ( = tab1 = bla bla bla = tab2 = bla2 bla2 bla2 ... <headertabs/>
  3. Add in your page __TOC__ or {{TOC}} (if you are using a template for TOC)

PATCH:

edit your_wiki_path/includes/parser/Parser.php and apply the following changes in the function formatHeadings:

  ....
  4272                 foreach ( $matches[3] as $headline ) {
  4273                         $isTemplate = false;
  4274                         $titleText = false;
  4275                         $sectionIndex = false;
  4276                         $numbering = ;
  4277                         $markerMatches = array();
  4278                         if ( preg_match( "/^$markerRegex/", $headline, $markerMatches ) ) {
  4279                                 $serial = $markerMatches[1];
  4280                                 list( $titleText, $sectionIndex ) = $this->mHeadings[$serial];
  4281                                 $isTemplate = ( $titleText != $baseTitleText );
  4282                                 $headline = preg_replace( "/^$markerRegex\\s*/", "", $headline );
  4283                         }
  4284                         if ( $toclevel ) {
  4285                                 $prevlevel = $level;
  4286                         }
  4287                         $level = $matches[1][$headlineCount];
  4288                         if (
  4289                                 ( $level > $prevlevel && $prevlevel <> 1 )
  4290                                 ||
  4291                                 ( $level == 1 && $prevlevel > 1 )
  4292                         ) {
  4293                                 # Increase TOC level
  4294                                 $toclevel++;
  4295                                 $sublevelCount[$toclevel] = 0;
  4296                                 if ( $toclevel < $wgMaxTocLevel ) {
  4297                                         $prevtoclevel = $toclevel;
  4298                                         if ( $level <> 1 ) { # REMOVE THIS IF IF YOU WANT TO SHOW THE SECTION IN TOC
  4299                                                 $toc .= Linker::tocIndent();
  4300                                         }
  4301                                         $numVisible++;
  4302                                 }
  4303                         } elseif (  $level < $prevlevel && $toclevel > 1 ) {
  4304                                 # Decrease TOC level, find level to jump to
  4305                                 for ( $i = $toclevel; $i > 0; $i-- ) {
  4306                                         if ( $levelCount[$i] == $level ) {
  4307                                                 # Found last matching level
  4308                                                 $toclevel = $i;
  4309                                                 break;
  4310                                         } elseif ( $levelCount[$i] < $level ) {
  4311                                                 # Found first matching level below current level
  4312                                                 $toclevel = $i + 1;
  4313                                                 break;
  4314                                         }
  4315                                 }
  4316                                 if ( $i == 0 ) {
  4317                                         $toclevel = 1;
  4318                                 }
  4319                                 if ( $toclevel < $wgMaxTocLevel ) {
  4320                                         if ( $prevtoclevel < $wgMaxTocLevel ) {
  4321                                                 # Unindent only if the previous toc level was shown :p
  4322                                                 $toc .= Linker::tocUnindent( $prevtoclevel - $toclevel );
  4323                                                 $prevtoclevel = $toclevel;
  4324                                         } else {
  4325                                                 $toc .= Linker::tocLineEnd();
  4326                                         }
  4327                                 }
  4328                         } elseif (  $level > $prevlevel && $prevlevel == 1 ) {
  4329                                 # MIK
  4330                                 # Decrease TOC level for headed tab
  4331                                 for ( $i = $toclevel-1; $i > 0; $i-- ) {
  4332                                         if ( $levelCount[$i] == $level ) {
  4333                                                 # Found last matching level
  4334                                                 $toclevel = $i;
  4335                                                 break;
  4336                                         } elseif ( $levelCount[$i] < $level ) {
  4337                                                 # Found first matching level below current level
  4338                                                 $toclevel = $i + 1;
  4339                                                 break;
  4340                                         }
  4341                                 }
  4342                                 if ( $i == 0 ) {
  4343                                         $toclevel = 1;
  4344                                 }
  4345                                 if ( $toclevel < $wgMaxTocLevel ) {
  4346                                         if ( $prevtoclevel < $wgMaxTocLevel ) {
  4347                                                 # Unindent only if the previous toc level was shown :p
  4348                                                 //$toc .= Linker::tocUnindent( $prevtoclevel - $toclevel );
  4349                                                 $prevtoclevel = $toclevel;
  4350                                         }
  4351                                         //else {
  4352                                         //      $toc .= Linker::tocLineEnd();
  4353                                         //}
  4354                                 }
  4355                         } else {
  4356                                 # No change in level, end TOC line
  4357                                 if ( $toclevel < $wgMaxTocLevel ) {
  4358                                         if ( $level <> 1 ) {  # REMOVE THIS IF IF YOU WANT TO SHOW THE SECTION IN TOC
  4359                                                 $toc .= Linker::tocLineEnd();
  4360                                         }
  4361                                 }
  4362                         }
  4363
  4364                         $levelCount[$toclevel] = $level;
  4365
  ....
  4477                         if ( $enoughToc && ( !isset( $wgMaxTocLevel ) || $toclevel < $wgMaxTocLevel ) ) {
  4478                                 if ( $level <> 1 ) { # REMOVE THIS IF IF YOU WANT TO SHOW THE SECTION IN TOC
  4479                                         $toc .= Linker::tocLine( $anchor, $tocline,
  4480                                                 $numbering, $toclevel, ( $isTemplate ? false : $sectionIndex ) );
  4481                                 }
  4482                         }
  ....
  4550                 if ( $enoughToc ) {
  4551                         if ( $prevtoclevel > 0 && $prevtoclevel < $wgMaxTocLevel ) {
  4552                                 if ( $level <> 1 ) { # REMOVE THIS IF IF YOU WANT TO SHOW THE SECTION IN TOC
  4553                                         $toc .= Linker::tocUnindent( $prevtoclevel - 1 );
  4554                                 }
  4555                         }
  4556                         $toc = Linker::tocList( $toc, $this->mOptions->getUserLangObj() );
  4557                         $this->mOutput->setTOCHTML( $toc );
  4558                         $toc = self::TOC_START . $toc . self::TOC_END;
  4559                 }
  ....
  

ENJOY

This post was posted by 2.238.45.117, but signed as mailto:michele.fella@gmail.com.

Kghbln (talkcontribs)

This is again an issue with recent versions of MediaWiki and HeaderTabs.

Kghbln (talkcontribs)

I had hoped that the new version 2.1 fixes the issue, however it does not. I am on MW 1.35.x

Yaron Koren (talkcontribs)

It seems strange to follow up now on an issue that was first reported nine years ago, but: is the issue that the tabs appear in the TOC, or that they appear more important than everything else? I think this is part of why I ignored this issue before: because it seem to me that tabs should appear in the TOC, since they're part of the page structure. However, I admit that it's strange right now if you have normal section headers below all the tabs, because then you end up with a TOC that looks like:

1. Tab 1
2. Tab 2
2.1 Some tab sub-header
3. Tab 3
3.1 Overview
3.2 See also
3.3 References

Everything in the main page looks like it belongs to Tab 3, but it doesn't. What if the structure were fixed, so it instead looked like:

1. Tab 1
2. Tab 2
2.1 Some tab sub-header
3. Tab 3
4 Overview
5 See also
6 References

Would that be good enough? Or would it still be better to get rid of the tabs entirely in the TOC?

Kghbln (talkcontribs)

For quite some years the tabs were not shown and this was fine. Usually you do not use = to create headings within text but start with ==. The latter and lower level headings should be shown. For me the suggestion does not work.

@Krabina: What about you?

Yaron Koren (talkcontribs)

@Kghbln - thanks for the response. Can you explain why you think tabs should not be included in the TOC? After all, they're a part of the page structure...

Kghbln (talkcontribs)

Predominantly I create tabs to have a tabbed infobox. The tabs of the infobox should therefore not show up in the since they are not part of the regular content structure. Same with forms (had to add NOTOC to every form after the extension changed its behaviour). Probably I am misusing this extension.

Replacing second level headers with first level headers appears to be a wrong solution for the right reason.

Yaron Koren (talkcontribs)

Okay, that's helpful. I wouldn't say it's the wrong solution, just not the right solution for your case.

Krabina (talkcontribs)

I agree with Karsten. Also in my case, the TOC shows the structure of the main text as it should (usually level 1 headings are not used anyway). And HeaderTabs are showing tabs for various infoboxes. They should never be part of the regular TOC. Of course, different usecases are possible, but once you are using HeaderTabs it seems to me that they will almost any time not part of the rest of the page structure.

Yaron Koren (talkcontribs)

I don't think I understood most of that, but it's good to know that you don't want tab names in your TOC.

Krabina (talkcontribs)

I just realized that in the current version of HT there seems to be a configruation option for this. However, for me this does not work. If I put $wgHeaderTabsNoTabsInTOC = true; in LocalSeettings.php, nothing changes, if I put <notabtoc/> in the page, the tabs are not shown in the tabs, but no other headings are shown. After some testing I realized that this only works, if the tabs are below the content which heading should be in the TOC.

If I put first level headings and <headertabs /> BEFORE the other content, this does not work. This is unfortunate, because most of the times I use HeaderTabs for templates, where the template content is usually on the top of the page followed by the rest of the content.

Kghbln (talkcontribs)

I can confirm that the new configuration does not work.

Krabina (talkcontribs)

Unfortunately, the same does not work in V 2.2.2 and MW 1.39

Reply to "Hide Level 1 headings in TOC"