Collapse and expand wiki page content: Difference between revisions

From GoalStriving Tutorials
Jump to navigation Jump to search
Matt (talk | contribs)
Matt (talk | contribs)
Line 84: Line 84:
$wgCollapsibleSectionsEnableDesktop = true;
$wgCollapsibleSectionsEnableDesktop = true;
$wgCollapsibleSectionsTag = 'h3';</syntaxhighlight>The CollapsibleSections extension has three configuration settings, all of which you have included in your <code>LocalSettings.php</code>. They control the initial state of sections, whether collapsing works on desktop browsers, and which heading levels are collapsible.
$wgCollapsibleSectionsTag = 'h3';</syntaxhighlight>The CollapsibleSections extension has three configuration settings, all of which you have included in your <code>LocalSettings.php</code>. They control the initial state of sections, whether collapsing works on desktop browsers, and which heading levels are collapsible.
{| class="wikitable"
! Ctrl+
! VisualEditor Name
! Wikitext Sample
! HTML Level
|-
| 0
| Paragraph
| <nowiki>Normal paragraph text</nowiki>
| <code>&lt;p&gt;</code>
|-
| 1
| Page title (not normally used)
| <nowiki>= Page title =</nowiki>
| <code>&lt;h1&gt;</code>
|-
| 2
| Heading
| <nowiki>== Heading ==</nowiki>
| <code>&lt;h2&gt;</code>
|-
| 3
| Sub-heading 1
| <nowiki>=== Sub-heading 1 ===</nowiki>
| <code>&lt;h3&gt;</code>
|-
| 4
| Sub-heading 2
| <nowiki>==== Sub-heading 2 ====</nowiki>
| <code>&lt;h4&gt;</code>
|-
| 5
| Sub-heading 3
| <nowiki>===== Sub-heading 3 =====</nowiki>
| <code>&lt;h5&gt;</code>
|-
| 6
| Sub-heading 4
| <nowiki>====== Sub-heading 4 ======</nowiki>
| <code>&lt;h6&gt;</code>
|-
| 7
| Preformatted
| <nowiki><pre>Preformatted text</pre></nowiki>
| <code>&lt;pre&gt;</code>
|-
| 8
| Block quote
| <nowiki><blockquote>Quoted text</blockquote></nowiki>
| <code>&lt;blockquote&gt;</code>
|}


To answer your main question: No, CollapsibleSections does not provide a documented way to make an individual section collapsed by default while leaving all other sections expanded. However, MediaWiki has a built-in feature that can accomplish exactly that.
To answer your main question: No, CollapsibleSections does not provide a documented way to make an individual section collapsed by default while leaving all other sections expanded. However, MediaWiki has a built-in feature that can accomplish exactly that.

Revision as of 21:01, 14 October 2026

This page contains notes on using MediaWiki features that allow you to collapse and expand content sections.

MediaWiki default capabilities

For a block that starts collapsed:

<div class="mw-collapsible mw-collapsed">
<div class="mw-collapsible-content">
This content is hidden initially.

You can put normal wikitext here:
* Item one
* Item two
* Item three
</div>
</div>

mw-collapsible makes the element collapsible, while mw-collapsed makes it start collapsed. MediaWiki then supplies the Expand/Collapse control. MediaWiki

For something more useful, with a visible title:

<div class="mw-collapsible mw-collapsed">
My additional information
<div class="mw-collapsible-content">
Here is all of the additional information that I don't
want displayed until the reader expands it.

* Item one
* Item two
* Item three
</div>
</div>

You can also change the words used for the control:

Additional information
<div class="mw-collapsible-content">
This is the hidden content.
</div>
</div>
That functionality is built into MediaWiki's jquery.makeCollapsible module.

MediaWiki Details extension

There is now an Extension:Details extension that I think is particularly worth considering for your personal wiki. It lets you write:

<details>
<summary>My additional information</summary>

Here is the information.

* Item one
* Item two
* Item three

</details>

The result behaves like the familiar disclosure/accordion control on modern websites: click the summary and the content opens underneath it.

You can also make it initially open:

<details open>
<summary>My additional information</summary>

Content here.

</details>

The Details extension is currently marked stable, supports MediaWiki 1.39+, and uses the browser's semantic <details>/<summary> mechanism. One advantage is that browsers understand the hidden material as actual page content; things such as links to anchors and Find-in-Page can interact with it more intelligently. MediaWiki

Installation is simply:

wfLoadExtension( 'Details' );

after putting the extension in the extensions/Details directory. MediaWiki

MediaWiki CollapsibleSections extension

CollapsibleSections can make entire MediaWiki sections collapsible, which is useful if you want something like:

=== Travel Plans ===

Lots of content...

=== Books to Read ===

Lots more content...

and want each section to collapse from its heading. It can be configured for desktop use and whether sections start collapsed. MediaWiki

Server configurations

The following commands were added to LocalSettings.php

# CollapsibleSections
wfLoadExtension( 'CollapsibleSections' );

$wgCollapsibleSectionsCollapsedByDefault = false;
$wgCollapsibleSectionsEnableDesktop = true;
$wgCollapsibleSectionsTag = 'h3';

The CollapsibleSections extension has three configuration settings, all of which you have included in your LocalSettings.php. They control the initial state of sections, whether collapsing works on desktop browsers, and which heading levels are collapsible.

Ctrl+ VisualEditor Name Wikitext Sample HTML Level
0 Paragraph Normal paragraph text <p>
1 Page title (not normally used) = Page title = <h1>
2 Heading == Heading == <h2>
3 Sub-heading 1 === Sub-heading 1 === <h3>
4 Sub-heading 2 ==== Sub-heading 2 ==== <h4>
5 Sub-heading 3 ===== Sub-heading 3 ===== <h5>
6 Sub-heading 4 ====== Sub-heading 4 ====== <h6>
7 Preformatted <pre>Preformatted text</pre> <pre>
8 Block quote <blockquote>Quoted text</blockquote> <blockquote>

To answer your main question: No, CollapsibleSections does not provide a documented way to make an individual section collapsed by default while leaving all other sections expanded. However, MediaWiki has a built-in feature that can accomplish exactly that.

1. Explanation of the three settings

Setting Values Behavior
$wgCollapsibleSectionsCollapsedByDefault true (default), false true: all targeted sections start collapsed. false: all start expanded.
$wgCollapsibleSectionsEnableDesktop true, false (default) Enables or disables CollapsibleSections functionality for desktop browsers.
$wgCollapsibleSectionsTag 'h2' (default), 'h3', 'h4', etc. Specifies which HTML heading level is the root level for collapsible sections.

These are the three settings documented by the extension's maintainers.

MediaWiki

+1

Understanding the heading level

MediaWiki's wikitext heading syntax maps to HTML heading levels:

Wikitext HTML level
== Heading == h2
=== Heading === h3
==== Heading ==== h4

With your configuration:

$wgCollapsibleSectionsTag = 'h3';

You're designating level-three headings as the root level for collapsible sections. This is appropriate because you primarily use === Heading === for your wiki's section headings.

The extension can also recognize nested heading structures. However, the exact behavior with nested headings should be tested, especially when changing the root heading level.

2. How to collapse one specific section by default

MediaWiki includes a built-in collapsible-content mechanism through its ResourceLoader jquery.makeCollapsible module.

For example, add the following directly to the wikitext of a page:

Example: A section that starts collapsed

<div class="mw-collapsible mw-collapsed">
 '''Additional Information'''

This information is initially hidden.

It becomes visible when the reader expands the section.
</div>

The important classes are:

  • mw-collapsible: Makes the content collapsible.
  • mw-collapsed: Makes it initially collapsed.

If you omit mw-collapsed, the content starts expanded.

You can use this technique even when:

$wgCollapsibleSectionsCollapsedByDefault = false;

The global setting applies to CollapsibleSections, whereas mw-collapsed is a separate MediaWiki collapsing mechanism.

Note that the code above uses HTML-like tags in wikitext; it does not require you to install another extension. You should test it with your current skin and VisualEditor.

3. Another option: Native HTML details

Since you're planning to install the Details extension, this may actually be a better approach for selectively collapsing content.

Initially collapsed

<details>
<summary>Additional Information</summary>

This content is initially hidden.

</details>

Initially expanded

<details open>
<summary>Additional Information</summary>

This content is initially visible.

</details>

With the Details extension installed and enabled, this provides a convenient way to manage individual collapsible blocks without changing any global settings.

4. Recommended configuration for your wiki

I would keep your current configuration:

wfLoadExtension( 'CollapsibleSections' );

$wgCollapsibleSectionsCollapsedByDefault = false;
$wgCollapsibleSectionsEnableDesktop = true;
$wgCollapsibleSectionsTag = 'h3';

Then use two complementary approaches:

  • CollapsibleSections: Make your normal level-three wiki sections collapsible, but expanded by default.
  • Details or mw-collapsible: Create individually controlled expandable blocks that start collapsed or expanded as needed.

This provides greater flexibility without changing the default appearance of your existing pages.

One distinction worth emphasizing: A CollapsibleSections section is a genuine MediaWiki heading and participates in the page's heading hierarchy and table of contents. A Details block is an expandable container, not automatically a MediaWiki section. If preserving the heading hierarchy is important, that distinction may influence which approach you choose.

MediaWiki Spoilers extension

Provides {{#spoiler:...}} https://www.mediawiki.org/wiki/Extension%3ASpoilers?utm_source=chatgpt.com

Its purpose is specifically hiding spoiler-like content. MediaWiki

MediaWiki custom template

One other possibility is creating your own {{Collapse}} template around MediaWiki's built-in functionality. MediaWiki.org itself has a Collapse template with parameters such as title, content, background, alignment, and initial state. MediaWiki