PHPackages                             alirezasedghi/laravel-toc - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. alirezasedghi/laravel-toc

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alirezasedghi/laravel-toc
=========================

Table of contents for Laravel

v1.0.3(10mo ago)42.2k—7.7%MITPHPPHP &gt;=8.0.0

Since Sep 23Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/AlirezaSedghi/laravel-toc)[ Packagist](https://packagist.org/packages/alirezasedghi/laravel-toc)[ RSS](/packages/alirezasedghi-laravel-toc/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

Laravel TOC Generator
=====================

[](#laravel-toc-generator)

 ![Required PHP Version](https://camo.githubusercontent.com/2d1d30d3c307e293f90e4a610b56d0705014e3821abfb741c0437444e7e8816b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d3e3d382e302e302d626c75653f7374796c653d666c61742d737175617265) [![Total Downloads](https://camo.githubusercontent.com/d35c30eb47b9ed5f1a83c3363b32750bc354319f8796eb535207273f3ab63777/68747470733a2f2f706f7365722e707567782e6f72672f616c6972657a617365646768692f6c61726176656c2d746f632f646f776e6c6f6164733f7374796c653d666c61742d737175617265)](https://packagist.org/packages/alirezasedghi/laravel-toc) [![Latest Stable Version](https://camo.githubusercontent.com/754268e91a625d5e7b61c28512fee0953ed80866cc5e025c7091537bf93d8a46/68747470733a2f2f706f7365722e707567782e6f72672f616c6972657a617365646768692f6c61726176656c2d746f632f762f737461626c653f7374796c653d666c61742d737175617265)](https://packagist.org/packages/alirezasedghi/laravel-toc) [![Latest Stable Version](https://camo.githubusercontent.com/284b49200066e00d2c0b324739c5b73f47d541ae39f955f9bad297da0b160a79/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f416c6972657a615365646768692f6c61726176656c2d746f633f7374796c653d666c61742d737175617265)](https://github.com/AlirezaSedghi/laravel-toc/releases) [![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](https://raw.githubusercontent.com/AlirezaSedghi/laravel-toc/master/LICENSE) [![GitHub issues](https://camo.githubusercontent.com/ffb02fabb4c87004dec74af1c4acb86b98e0fbcb7efa4da7e906a5e9832994dd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f416c6972657a615365646768692f6c61726176656c2d746f632e7376673f7374796c653d666c61742d737175617265)](https://github.com/AlirezaSedghi/laravel-toc/issues)

A Laravel package to generate a Table of Contents (TOC) from HTML headings (`h1, h2, ... h6`). This package allows you to create a dynamic TOC with customizable options, including the list type (ul or ol), HTML classes, and level filtering.

Features
--------

[](#features)

- **Automatic TOC generation** from HTML headings.
- **Configurable heading levels** to include (`h1` to `h6`).
- **Customizable list types (ul, ol)** and HTML classes for TOC structure.
- **Customizable HTML classes** for the TOC list and items.
- **Adds unique `id` attributes** to headings for internal linking.

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require alirezasedghi/laravel-toc
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

Here’s how to use the package to generate a Table of Contents and return both the TOC and the processed HTML content.

```
use Alirezasedghi\LaravelTOC\TOCGenerator;

class PageController extends Controller
{
    public function show(Request $request)
    {
        // Example HTML content
        $html = "Main TitleSection 1Content...";

        // Initialize TOC generator
        $tocGenerator = new TOCGenerator($html);

        // Generate the TOC and get processed HTML
        $toc = $tocGenerator->generateTOC();
        $processedHtml = $tocGenerator->getProcessedHtml();

        // Return to view
        return view('page', [
            'toc' => $toc,
            'content' => $processedHtml
        ]);
    }
}
```

In your Blade template (`resources/views/page.blade.php`):

```

    {!! $toc !!}

    {!! $content !!}

```

### Custom Options

[](#custom-options)

You can customize the TOC generation by passing an options array. The following options are available:

- `list_type`: The type of list to use for the TOC (`ul` for unordered, `ol` for ordered).
- `toc_class`: The CSS class for the main TOC list (`` or ``).
- `internal_list_class`: The CSS class for internal nested lists.
- `toc_item_class`: The CSS class for each item (``) in the TOC.
- `toc_link_class`: The CSS class for each link (``) in the TOC.
- `heading_class`: A CSS class to add to each heading (`h1`, `h2`, etc.) in the HTML content.
- `min_level`: The minimum heading level to include in the TOC (e.g., `1` for `h1`).
- `max_level`: The maximum heading level to include in the TOC (e.g., `6` for `h6`).

#### Example with Custom Options

[](#example-with-custom-options)

```
use Alirezasedghi\LaravelTOC\TOCGenerator;

class PageController extends Controller
{
    public function show(Request $request)
    {
        // Example HTML content
        $html = "Main TitleSection 1SubsectionContent...";

        // Define custom options
        $options = [
            'list_type' => 'ol',             // Use an ordered list for the TOC
            'toc_class' => 'custom-toc',     // Class for TOC /
            'internal_list_class' => 'nested-toc', // Class for nested /
            'toc_item_class' => 'toc-item',  // Class for each
            'toc_link_class' => 'toc-link',  // Class for each
            'heading_class' => 'heading',    // Class for headings
            'min_level' => 2,                // Include only h2-h6
            'max_level' => 3                 // Include up to h3
        ];

        // Initialize TOC generator with custom options
        $tocGenerator = new TOCGenerator($html, $options);

        // Generate the TOC and get processed HTML
        $toc = $tocGenerator->generateTOC();
        $processedHtml = $tocGenerator->getProcessedHtml();

        // Return to view
        return view('page', [
            'toc' => $toc,
            'content' => $processedHtml
        ]);
    }
}
```

### Available Options

[](#available-options)

OptionTypeDefaultDescription`list_type`string`'ul'`Type of list for TOC (`ul` for unordered, `ol` for ordered)`toc_class`string`'toc'`CSS class for the main TOC list`internal_list_class`string`''`CSS class for internal nested lists`toc_item_class`string`''`CSS class for each `` inside the TOC`toc_link_class`string`''`CSS class for each link (``) inside the TOC`heading_class`string`''`CSS class added to each heading (`h1`-`h6`)`min_level`int`1`Minimum heading level to include (e.g., `1` for `h1`)`max_level`int`6`Maximum heading level to include (e.g., `6` for `h6`)### Example Blade Template

[](#example-blade-template)

Here’s how you can render both the TOC and the processed HTML content in a Blade view:

```

    {!! $toc !!}

    {!! $content !!}

```

### Output Example

[](#output-example)

#### Given the HTML:

[](#given-the-html)

```
Main Title
Section 1
Subsection 1.1
Section 2
Subsection 2.1
```

#### Generated TOC:

[](#generated-toc)

```

    Main Title

            Section 1

                    Subsection 1.1

            Section 2

                    Subsection 2.1

```

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance54

Moderate activity, may be stable

Popularity25

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~97 days

Total

4

Last Release

309d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/776d857905afc82f582e4d843ae3e26821e37bc7c586a2e2d1c24aa86b6ece5a?d=identicon)[AlirezaSedghi](/maintainers/AlirezaSedghi)

---

Top Contributors

[![AlirezaSedghi](https://avatars.githubusercontent.com/u/8061051?v=4)](https://github.com/AlirezaSedghi "AlirezaSedghi (5 commits)")

---

Tags

laravelTOCtable-of-contents

### Embed Badge

![Health badge](/badges/alirezasedghi-laravel-toc/health.svg)

```
[![Health](https://phpackages.com/badges/alirezasedghi-laravel-toc/health.svg)](https://phpackages.com/packages/alirezasedghi-laravel-toc)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)[nedwors/navigator

A Laravel package to ease defining navigation menus

433.1k](/packages/nedwors-navigator)[xefi/faker-php-laravel

Faker php integration with laravel

1915.1k](/packages/xefi-faker-php-laravel)[dcblogdev/laravel-junie

Install pre-configured guides for Jetbrains Junie

392.5k](/packages/dcblogdev-laravel-junie)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
