Download the PHP package jifish/parsedown-toc-ext without Composer
On this page you can find all versions of the php package jifish/parsedown-toc-ext. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jifish/parsedown-toc-ext
More information about jifish/parsedown-toc-ext
Files in jifish/parsedown-toc-ext
Package parsedown-toc-ext
Short Description TOC extension for parsedown and parsedown-extra with automatic heading IDs.
License MIT
Informations about the package parsedown-toc-ext
Parsedown TOC Extension
A simple, lightweight, and modern Table of Contents extension for Parsedown and ParsedownExtra. MIT licenced.
Provides:
- Markdown/HTML TOC generation
- Automatic slugified heading IDs
- Programmatic TOC access
- Configurable depth, numbering, and hierarchy behaviour
- Works with ParsedownExtra
- Manual TOC adjustment
This extension works with both original parsedown and the community fork of Parsedown.
Inspired by Benjamin Hoegh's ParsedownToc.
Installation
If you want Parsedown Extra support:
If you want the community fork of Parsedown, install it first.
Basic Usage
Parsedown
Parsedown Extra
Substitution of [TOC] marker
Custom id generation
If you need you own id generation strategy, you can override slugify:
Heading ID Generation
Heading IDs are:
- Lowercased
- Non-alphanumeric characters removed
- Whitespace converted to hyphens
- Guaranteed unique per document, duplicate headings receive numeric suffixes
Example:
Becomes:
Interfaces
Basic Usage
text(string $markdown): string
As with Parsedown, but adds heading IDs, and builds and stores a new TOC structure.
getToc(): array
Returns the internal TOC structure.
Format:
- Indexed by generated ID.
- Ordered in document order.
levelis the original heading level (1-6).
getTocCount(): int
Returns the number of headings in the TOC.
removeHeading(string $id): void
Removes a heading from the TOC by its ID.
renderToc(...)
Generates a Markdown-formatted TOC.
Parameters:
-
$asHtml(defaultfalse)If
true, the generated Markdown TOC is returned as rendered HTML.Example:
Returns:
-
$collapseSkippedLevels(defaultfalse)Controls how heading level jumps are handled. When enabled, large jumps only increase nesting by one level.
Example:
false(true depth):true(collapsed hierarchy): -
$maxDepth(default:null)Limits how deep headings are included.
Depth is calculated relative to the lowest heading level in the document.
-
$numbered(default:false)Uses ordered list syntax instead of bullet lists. Each nesting level maintains its own numbering sequence.
Example:
-
$excludeFirstHeading(default:false)Skips the first heading in the document. Useful when the first heading is the page title and should not appear in the TOC. The next visible heading becomes the root of the rendered TOC, while
maxDepthremains relative to the full document.
hasToc(): bool
Returns whether the internal TOC currently contains any entries.
Customising TOC
appendHeading(int $level, string $text, ?string $id = null): string
Manually adds an entry to the end of the internal TOC structure, and returns its id.
This allows you to add headings that are not present in the Markdown source, for use when combining parsed headings with dynamic or generated content, or to generate TOCs without any source markdown.
Note: Calling text() will clear the internal TOC.
Parameters
-
$levelHeading level (1-6). AValueErroris thrown if the level is outside this range. -
$textThe display text used in the TOC. -
$id(optional) Custom ID to use instead of generating one from$text.If omitted, the ID is generated from
$textusing the same slug rules as automatic headings.The final ID is always passed through the internal uniqueness check, so collisions are automatically resolved.
Example:
Output:
prependHeading(int $level, string $text, ?string $id = null): string
As above, but adds the heading to the top instead.
insertHeadingAfter(string $afterId, int $level, string $text, ?string $id = null): string
As above, but inserts the heading directly below $afterId. Throws a ValueError if
the id is not found.