New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

jedamzik/statamic-toc

Auto-Generated Table Of Contents from Markdown Fields for Statamic.
2,133 3
Install
composer require jedamzik/statamic-toc
Latest Version:v1.4.0
PHP:^8.0 | ^8.1 | ^8.2
Older releases:7
License:MIT
Last Updated:Sep 24, 2024
Links: GitHub  ·  Packagist
Maintainer: jedamzik

Latest Version Tests

Toc

Auto-Generated Table Of Contents from Markdown Fields for Statamic 3.

This generates a Table Of Contents from a Markdown field.

A list of Headings is saved in the entry as html and can either be rendered directly using {{ table_of_contents }} or through the provided tag {{ toc }} with a customizable wrapper view.

Installation

Require it using Composer.

composer require jedamzik/statamic-toc

Set up a Collection

Publish the wrapper view and package configuration from Njed\Toc\ServiceProvider:

php artisan vendor:publish

Provide a collection handle and a field handle in config/toc.php to activate Toc for a given collection:

return [
    'collections' => [
        'posts' => 'content'
    ],
    ...
];

Heading Depth

By default only h1 and h2 are used for the Table of Contents. If you want to include further heading levels, add them to the config:

return [
    ...
    'includeLevels' => [3, 4]
];

List items for all headings with a level > 2 will have a .child class added so you can style them separately.

Items in the Table of Contents can function as anchor links to the heading fragment:

return [
    ...
    'anchorLinks' => true
];

Linked page fragments are a slugified version of the Heading string (Example Title -> #example-title).

You can extend your Markdown Parser with the provided Njed\Toc\Extensions\CommonMark\TitleAnchorIdExtension to provide these ids for your heading nodes in your rendered views.

To extend the default Parser for all Markdown fields in your Statamic instance, add it to your boot method in the AppServiceProvider:

Markdown::addExtension(function () {
    return new \Njed\Toc\Extensions\CommonMark\TitleAnchorIdExtension;
});

To only use this Extension on a specific Markdown field, you can create a new Parser and specify it for your Markdown field.

Markdown::extend('special', function ($parser) {
    return $parser
        ->withStatamicDefaults()
        ->addExtension(function () {
            return new \Njed\Toc\Extensions\CommonMark\TitleAnchorIdExtension;
        });
});

You can set a custom parser for markdown fields either in the control panel or through the parser attribute in your blueprint file:

-
    handle: content
    field:
        ...
        type: markdown
        parser: special

Changelog

Please see CHANGELOG for more information what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

Related Packages

statamic/cli

Statamic CLI Tool

95,417 77
linksderisar/statamic-custom-select

Custom select field

1,161 0
pecotamic/sitemap

Simple sitemap addon for Statamic

175,468 6
mitydigital/sitemapamic

An XML sitemap generator for Statamic that includes all collections and related...

124,386 15
stillat/relationships

Provides bi-directional entry relationship automation

72,975 25

Version History

Version Released PHP Laravel License
v1.4.0 ^8.0 | ^8.1 | ^8.2 MIT
v1.3.1 ^8.0 | ^8.1 MIT
v1.3.0 ^8.0 | ^8.1 MIT
v1.2.0 ^8.0 MIT
v1.1.0 ^7.3|^8.0 MIT