--- 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](/1.4/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](/1.4/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\CommonMarkConverter; use League\CommonMark\Environment; use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension; use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension; // Obtain a pre-configured Environment with all the CommonMark parsers/renderers ready-to-go $environment = Environment::createCommonMarkEnvironment(); // Add the two extensions $environment->addExtension(new HeadingPermalinkExtension()); $environment->addExtension(new TableOfContentsExtension()); // Set your configuration $config = [ // Extension defaults are shown below // If you're happy with the defaults, feel free to remove them from this array 'table_of_contents' => [ 'html_class' => 'table-of-contents', 'position' => 'top', 'style' => 'bullet', 'min_heading_level' => 1, 'max_heading_level' => 6, 'normalize' => 'relative', ], ]; // Instantiate the converter engine and start converting some Markdown! $converter = new CommonMarkConverter($config, $environment); echo $converter->convertToHtml('# 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 `