Manual talk:Table of contents

From mediawiki.org
Latest comment: 2 months ago by Rebulka in topic External links


Make TOC list Accordion sections?[edit]

Question - I have been trying to find a way to make the TOC on one page display accordion section titles, to be used on a FAQ page with only collapsed Accordions. The closest i have found of information is to implement new hooks and edit php files directly, but i would prefer to avoid this. Is there a way to accomplish this? And if not, what would be a "recommended" way to edit the files?


CSS To Change Numbering for the ToC[edit]

Stashing here for later reference. CSS i was playing with to change the numbering scheme on the ToC:

html #toc ul, .toc ul {
list-style-image:none;
list-style-position:inside;
list-style-type:armenian /*replace this with the type of number you want*/;
}

span.tocnumber {
display:none;
}

Bawolff (talk) 03:11, 16 April 2012 (UTC)Reply

Auto-collapse TOC on a given page?[edit]

Question - is there a way to auto-collapse the TOC on a given page? NOTOC gets rid of it, but I just want it collapsed, with the reader able to show it again should they wish. Manning Bartlett (talk) 04:08, 13 March 2013 (UTC)Reply

Yes! Under the "Collapse (Hide) TOCs When Pages Load" section below, see the "Improved Solution" section. It collapses TOCs on all pages in the wiki, though, not for a specific given page. --Lance E Sloan 21:13, 31 August 2017 (UTC)Reply

NOTOC for selected headers[edit]

Here my question, I want to have the Table of Contents in the page, but there are one header & subheaders that I don't want to show in it. Is that possible?

I show and example:

 = Show 1 = 
 == Show 1.1 == 
 === Show 1.1.1 === 
 == Show 1.2 == 
 = Hide 2 = 
 == Hide 2.1 == 
 === Hide 2.1.1 === 
 = Show 3 = 
 

I hope that be clear that I mean.


There is no easy way: http://en.wikibooks.org/wiki/MediaWiki_User_Guide/Sections_and_Headings#Headings_not_in_TOC --Albert25 (talk) 12:32, 13 June 2013 (UTC)Reply

Permanently repositioning the ToC directly after the title?[edit]

I cannot figure out how to permanently move the position of the Table of Contents. I have changed the CSS to float the ToC left, but I need it to come directly after the H1 heading on the page, rather than before the first H2 heading, for the text to wrap how I want it to. I can accomplish this with __TOC__, but doing this on every single page is infeasible. I have not found an extension that does this either.

My question is: how would I even begin to change that? Would a skin do it? I've looked through my skin, I've looked through all the skinning manuals I can find, I even looked through SkinTemplate.php and Skin.php in the MediaWiki files, but I can't figure out which file it is -- if there even is a single file -- that simply lays out the order the different elements appear in the page code. There are a ton of variables and hooks and arrays that I suspect this is buried in, and I only have moderate programming ability so I can't dig it up on my own. It would generally be helpful to know where I'd go if I wanted to add something like a containing div around things like the ToC. Natrashafierce (talk) 20:23, 23 August 2014 (UTC)Reply

I'm trying to so something similar. Cant find the answer for the life of me.
Vapblack (talk) 13:53, 12 December 2014 (UTC)Reply

Solution[edit]

This is hard-code solution, if you find extension solution, please mention it.

  1. Go to includes/parser/Parser.php
  2. Find doDoubleUnderscore method
  3. Add the following line at the beginning of the method
    $text = "__TOC__" . $text;

This will force to place TOC at the beginning of all documents

Hi, this method is deprecated in MW 1.34, I did not manage to find another hack with the new method. --Varlin (talk) 20:26, 7 February 2020 (UTC)Reply

Is there a way to Apply Multilingualism to Contents Label in the TOC Template or Even Custom Title?[edit]

For example, is there a way to have the header within the TOC modified to match the applicable language if an individual selects the other language page under the navigation menu (left side) and it automatically modifies the applicable language setting, as well (top right) with that click rather than an individual having to change the language setting in top right as an additional steps? As example text, the header text would be Contents in English and Sommaire in French. Thanks!

Quadra23 (talk) 17:02, 20 January 2015 (UTC)Reply

Header numbers but no titles[edit]

The strangest thing... I have an instance running that produces a TOC of just nested numbers, without the associated titles. The syntax of the sections hasn't changes (since when it used to work). If I poke into the html, I can see <a name> anchor tags that are blank or have "_#". So, I suspect something in MediaWiki text parser if failing to properly turn my section headers into anchors that TOC depends on.

Any hints as to how to track this bug down?

Collapse (Hide) TOCs When Pages Load[edit]

An quick an dirty way to have the TOC start collapsed

Open MediaWiki:Common.js

Add this to the js

//collaps default the TOC on opening an pages 
function hideToc() { 
  var toc = document.getElementById('toc').getElementsByTagName('ul')[0]; 
  var toggleLink = document.getElementById('togglelink'); 
  toc.style.display = 'none'; 
} 
hideToc();
thank you so much for this! Flylikeaseagull (talk) 07:24, 23 September 2020 (UTC)Reply

Improved Solution[edit]

This is working for me on 1.34.0:

var toc, toggleLink;
try {
  toc = document.getElementById('toc').getElementsByTagName('ul')[0];
  toggleLink = document.getElementById('toctogglecheckbox');
  // if (tocIsHidden()) {
  toggleToc();
  // }
} catch (error) {
  console.log('erred', error);
}
function tocIsHidden () {
    return !toc || !toggleLink || window.getComputedStyle(toc).display !== 'block';
}

function toggleToc() {
  var hidden = tocIsHidden();
  if (hidden && document.cookie.indexOf('hidetoc=0') > -1) {
    toggleLink.click();
    // changeText(toggleLink, tocShowText);
    // toc.style.display = 'none';
  } else if (!hidden && document.cookie.indexOf('hidetoc=1') > -1) {
    toggleLink.click();
    // changeText(toggleLink, tocHideText);
    // toc.style.display = 'block';
  }
}
toggleLink && toggleLink.addEventListener('click', function () {
  var isHidden = tocIsHidden();
  document.cookie = isHidden
    ? "hidetoc=1"
    : "hidetoc=0";
});
The solution below, no longer works since MediaWiki 1.29
I have some problems with the "quick an dirty" [sic] technique shown above:
  1. It doesn't always work. When the code executes, the page may not have been fully loaded or parsed. Therefore, the element with the toc ID may not be available and the code will fail. (When code is added to make it wait for the page to load and be parsed, then it works to some degree.)
  2. When the code hides the TOC, it doesn't change the "hide" link text to "show". That looks bad.
  3. The toggleLink variable is never used. Getting that element wastes time.
A better solution would:
  1. Wait until until after the page has been loaded and parsed by the web browser before trying to hide the TOC.
  2. Use the toggleToc() JavaScript function provided by MediaWiki to:
    1. Hide or show the TOC as necessary.
    2. Change the "hide" link text to "show" or vice versa.
    3. Set a hidetoc cookie in the web browser to indicate whether the TOC should be hidden or shown the next time a page is loaded from this wiki.
Add the following JavaScript code to your wiki's MediaWiki:Common.js page:
// The enclosed code runs only after the page has been loaded and parsed.
window.addEventListener('DOMContentLoaded', function() {
  try {
    // Detect whether the page's TOC is being displayed.
    if (document.getElementById('toc').getElementsByTagName('ul')[0].style.display != 'none') {
      // Use MW's toggleToc() to hide TOC, change "hide/show" link text, and set cookie.
      toggleToc();
    }
  } catch (exception) {
    // Probably this page doesn't have a TOC, ignore the exception to prevent console clutter.
  }
}, false);
Or use this shorter version without comments:
window.addEventListener('DOMContentLoaded', function() { try {
  if (document.getElementById('toc').getElementsByTagName('ul')[0].style.display != 'none') { toggleToc(); }
} catch (exception) {} }, false);
Remember: By default, MediaWiki will not use the JavaScript in your wiki's MediaWiki:Common.js page. For documentation about enabling this feature, see the following manual pages:
--Lance E Sloan 21:07, 31 August 2017 (UTC)Reply
I am trying to implement this solution on a MediWiki site I admin. I checked that MediaWiki:Common.js is loaded and that the above-proposed code is executed, but I keep getting a ReferenceError: toggleToc is not defined error. Any idea why this is happening and how to solve it? Thanks in advance for any help you might provide Lucamauri (talk) 12:53, 18 February 2018 (UTC)Reply

Relevent CSS rules for TOC depth.[edit]

Manual:Table of contents#Depth refers to MediaWiki:Common.css in order to find the relevant toclimit-<limit> styles, but it appears as though Common.css is just empty. I'm not sure if this is a result of most of the styles being moved into a default location and that file just being used for overrides or something. I'm not sure where to go about finding these.

Can I add Table of Content of one page to another page?[edit]

I want to make an index page that always show table of contents of another pages. I want it to always sync and don't need to update it manually. Is it possible? — Preceding unsigned comment added by 14.207.179.110 (talk • contribs) 05:39, 23 May 2017‎

I would also like to find a way to do that. I had a look in the list of wikipedia templates, but I did not found anything. I'm pretty sure this could be done through a lua module. --Varlin (talk) 16:43, 13 July 2021 (UTC)Reply

Still show a little TOC button when __NOTOC__ is used[edit]

IDEA: when __NOTOC__ is used, a little [TOC} etc. or whatever tiny marker should still show up, that the user can push, revealing the Table of contents. Jidanni (talk) 12:28, 12 December 2017 (UTC)Reply

Automatically display TOC when there are 3+ headings ?[edit]

Hi, I'm wondering if there is an option somewhere (it seems not) or an easy hack to automatically display the TOC when there are 3 or more headings, instead of the treshold of 4.--Varlin (talk) 21:25, 24 January 2020 (UTC)Reply

Hard-code solution :
  1. Go to includes/parser/Parser.php
  2. Replace 4 with 3 at line 4509 ( ( $numMatches >= 3 ) || $this->mForceTocPosition );
--Varlin (talk) 19:44, 7 February 2020 (UTC)Reply

Another possible solution to hide (collapse) the Table of content by default when you open a page[edit]

  • This solution is working for me on 1.34.0 (I was unable to make it work in Vector skin with the Improved Solution above, most probably because I'm not an expert).
  • This solution is based on: Manual:Collapsible elements and CSS.
  • First, I opened Mediawiki:Vector.css and added the code below (to open Mediawiki:Vector.css, just type Mediawiki:Vector.css in the search bar and edit it):
 #toc { border: 0px; }
 .toc h2 { display: none; }
 .toctogglespan { display: none; }
 .toctogglelabel { display: none; }
  • Then, on a page, I added the following code, where I want my Table of content:
 {| class="mw-collapsible mw-collapsed wikitable"
 ! Table of content
 |-
 | __TOC__
 |}
  • As is, it works, but it could be a better thing to create a template with the code. Example: create Template:My_TOC and add the code in it... Then on the page, just add:
 {{Template:My_TOC}}
  • I can't say if it can have possible side effects on something else, but for my needs...it works. If someone have some concern on this solution, it would be nice to advise... Thanks !

Is there a scroll-able support / hack for TOC in mediawiki?[edit]

When a TOC becomes too long, is it possible to make it scroll-able within a particular box? I'm not sure this is possible at the moment from my finding but I do see a hack to use the 'Scroll box' template but it does come with side effects. The effect here is that I'll have 2 boxes (the TOC box in the scroll box) which makes it not look good. I just want to simple have a box with the TOC and which is scroll-able. Thanks for your support in advance. --X-Savitar (talk) 16:08, 8 April 2020 (UTC)Reply

The page says: {{TOC left}} is available?[edit]

What is this? I can't find it. Isn't this something en.wp specific? I found the template there. --HirnSpuk (talk) 12:01, 25 April 2021 (UTC)Reply

Positioning the TOC without FORCETOC ?[edit]

Hi, I'd like to set a position for the TOC on a set of pages. That, I can do with __TOC__ inside a template.

But doing this forces TOC as with __FORCETOC__. I'd like to avoid this (cause I don't want a TOC displayed for only 1 or 2 headings...). I there a way ? --Varlin (talk) 19:06, 4 July 2021 (UTC)Reply

Hide footnotes links inside the TOC ?[edit]

Hi, everything is in the title. I think everyone could agree that seeing the footnotes links ( e.g "[2]" ) inside the TOC is not good looking, and also useless (since in any case you have to go the targeted title before you can click on this footnote link). So I wonder if someone knows a simple way to hide these links ? I could simple use a CSS rule to hide them, except they have no class defined. The rule could target the sup tag, but it would risk to hide things that should not be hidden. Varlin (talk) 14:19, 29 July 2022 (UTC)Reply

External links[edit]

This paragraph contains text that is commonly found at the bottom of paragraphs such as "Related Links" and "Notes" Rebulka (talk) 09:43, 24 February 2024 (UTC)Reply