--- layout: default title: Table of Contents Extension description: The Table of Contents extension automatically inserts links to the headings in your document. --- # Table of Contents Extension The `TableOfContentsExtension` automatically inserts a table of contents into your document with links to the various headings. The [Heading Permalink](/2.2/extensions/heading-permalinks/) extension must also be included for this to work. ## Installation This extension is bundled with `league/commonmark`. This library can be installed via Composer: ```bash composer require league/commonmark ``` See the [installation](/2.2/installation/) section for more details. ## Usage Configure your `Environment` as usual and simply add the `TableOfContentsExtension` and `HeadingPermalinkExtension` provided by this package: ```php use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension; use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension; use League\CommonMark\MarkdownConverter; // Define your configuration, if needed // Extension defaults are shown below // If you're happy with the defaults, feel free to remove them from this array $config = [ 'table_of_contents' => [ 'html_class' => 'table-of-contents', 'position' => 'top', 'style' => 'bullet', 'min_heading_level' => 1, 'max_heading_level' => 6, 'normalize' => 'relative', 'placeholder' => null, ], ]; // Configure the Environment with all the CommonMark parsers/renderers $environment = new Environment($config); $environment->addExtension(new CommonMarkCoreExtension()); // Add the two extensions $environment->addExtension(new HeadingPermalinkExtension()); $environment->addExtension(new TableOfContentsExtension()); // Instantiate the converter engine and start converting some Markdown! $converter = new MarkdownConverter($environment); echo $converter->convert('# Awesome!'); ``` ## Configuration This extension can be configured by providing a `table_of_contents` array with several nested configuration options. The defaults are shown in the code example above. ### `html_class` The value of this nested configuration option should be a `string` that you want set as the `