7

I'm running my own installation of MediaWiki, which has been great! I haven't been able to find the answer to this small problem in any post, how to, etc. Here's the setup:

Article

TOC (limited to showing only H1 and H2)
==H1==
===H2===
====H3====
====H3====

I don't want the H3 to show up on the main table of contents, because it would make the list very long. Instead, under the H2, I would like to display another TOC with all the H3's under that listing.

From my understanding, you cannot have multiple table of contents on a single page. I've thought about making a template for each H2 that has the H3 links, but that seems like it duplicates a lot of work and creates loads of pages. I'd love a template that sucks all subsection names and spits them out, but I don't see how to do that.

Alternatively, is there a way to enable multiple TOCs in a custom install of MediaWiki that I'm missing? Even that would get closer to what I'm trying to do.

1
  • Uh, custom tocs are very limited in MediaWiki...
    – Bergi
    Commented Sep 27, 2012 at 17:15

2 Answers 2

1

Not an exact answer, but perhaps an idea: You could split your page to single pages for your top-level chapters and create a TOC yourself by using a template which is included in every of these pages; e.g.:

Template:My-linked-pages-TOC:

<div style="border:thin gray;padding:0.5em;margin:0.5em 0;">
# [[First top-level chapter]]
# [[Second top-level chapter]]
...
# [[Last top-level chapter]]
</div>

Then you could use {{My-linked-pages-TOC}} at the beginning of each page. The current page will be rendered bold, while the others will be linked.

1

Because you're going against the grain by suppressing the normal, useful MediaWiki behavior of auto-populating the TOC down to $wgMaxTOCLevel, I think it will be a bit of a challenge to get that behavior across your wiki. Hopefully you found some other way to achieve your aims.

Regardless, without writing a custom extension (discussed below), my gut tells me this is going to be a fairly manual process, best limited to a handful of pages where you want that behavior.

Anyway, here goes…

Create a template that styles an unordered list so that it looks like the standard TOC. Extension:TemplateStyles might be useful here. Then, transclude that below your <h2>s. Within that "fake" TOC, create a bulleted list of anchors to the child sections.

In your MediaWiki:Common.css or MediaWiki:SkinName.css (for each skin your users use), style <h4>s, <h5>s, or <h6>s to look like <h3>s, and use one of those for the child sections under your <h2>s. Use these section names as the targets of the anchors mentioned above.

Probably a bad idea, because what happens when you want a real <h5>?

—or—

Instead of using === Wikitext sections === or <h3>HTML headings</h3>, both of which show up in the top TOC by design, you could create a template for the child headings with some CSS that merely mimics the style of an <h3>.

This gets complicated if your users can select from multiple skins, though.

Also, unless you take extra steps, e.g., to embed manual section anchors, a change so slight as to rename a section would break your secondary TOC.

These steps would need to be repeated for each page where you want want the secondary TOC—creating all those manual anchors, copy-pasting them into your fake TOC, adding new ones as new fake <h3>s are added…

Soooo… maybe just go with the flow, and be OK with the normal, good, out-of-the-box behavior of the <h3>s automatically showing up in the table of contents? ;)

Alternatively, is there a way to enable multiple TOCs in a custom install of MediaWiki that I'm missing? Even that would get closer to what I'm trying to do.

Yes, to write an extension. I'm not sure what that would entail, because I've never done it myself (only lightly modified existing extensions), but there is a discussion here which might put you in the neighborhood.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.