This optional plugin allows you to reduce long code examples to their relevant parts by collapsing any line ranges that are not relevant to the example.
The lines of collapsed sections will be replaced by a clickable X collapsed lines element. When clicked, the collapsed section will be expanded, showing the previously hidden lines.
Installation
Before being able to use collapsible sections in your code blocks, you need to install the plugin as a dependency and add it to your configuration:
To mark a section as collapsible, you need to add meta information to your code blocks. This is done by appending collapse={X-Y} to your opening code fence, indicating a collapsed section from line X to (and including) line Y.
You can also collapse multiple sections in a single code block by separating them with commas:
Available styles
The plugin supports different section styles, which can be set per block using the collapseStyle prop:
You can also change the prop’s default value globally to avoid having to specify it for each code block. To do so, add it to the defaultProps configuration option:
In the following sections, you can see the different styles in action.
github (default style)
This style is similar to the one used by GitHub. A summary line with an expand icon and the default text X collapsed lines is shown. When expanded, the summary line is replaced by the section’s code lines. It is not possible to re-collapse the section.
collapsible-start
When collapsed, the summary line looks like the github style. However, when expanded, it remains visible before the expanded code lines, making it possible to re-collapse the section.
collapsible-end
Same as collapsible-start, but the summary line remains visible after the expanded code lines.
collapsible-auto
Automatically selects collapsible-start or collapsible-end based on the location of the collapsible section in the code block. Uses collapsible-start unless the section ends at the end of the code block, in which case collapsible-end is used.
Usage in the <Code> component
The collapsible sections plugin adds the following props to the <Code> component that allow direct access to its features:
Props
collapse
Type:string | string[]
Collapses the given line range or ranges.
collapsePreserveIndent
Type:booleanDefault:true
Determines if the summary line content of collapsible sections should be indented to match the minimum indent level of the contained code lines.
Allows to select one of the following collapsible section styles:
github: The default style, similar to the one used by GitHub. A summary line with an expand icon and the default text X collapsed lines is shown. When expanded, the summary line is replaced by the section’s code lines. It is not possible to re-collapse the section.
collapsible-start: When collapsed, the summary line looks like the github style. However, when expanded, it remains visible above the expanded code lines, making it possible to re-collapse the section.
collapsible-end: Same as collapsible-start, but the summary line remains visible below the expanded code lines.
collapsible-auto: Automatically selects collapsible-start or collapsible-end based on the location of the collapsible section in the code block. Uses collapsible-start unless the section ends at the bottom of the code block, in which case collapsible-end is used.
Styling
This plugin adds a collapsibleSections object to the styleOverrides engine config option, allowing you to customize the visual appearance of the sections:
Note: Despite the setting prefix closed, summary lines are also visible while the section is open when using any of the collapsible-* styles. This is the same for all closed* settings.