定制 bit-mx/statamic-toc 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bit-mx/statamic-toc

Composer 安装命令:

composer require bit-mx/statamic-toc

包简介

Reusable TOC extraction and anchor injection for Statamic Bard and Markdown fields.

README 文档

README

A reusable Table of Contents package for Statamic that extracts headings from HTML, Markdown, and Bard content, generates stable anchors, and optionally injects heading IDs into rendered HTML.

Requirements

  • PHP 8.3+
  • Laravel 12
  • Statamic 6

Installation

composer require bit-mx/statamic-toc

Publish Configuration

php artisan vendor:publish --tag=statamic-toc-config

This publishes config/statamic-toc.php.

Configuration

return [
    'min_level' => 1,
    'max_level' => 6,
    'preserve_existing_ids' => true,
    'default_source' => 'html',
    'default_tree' => true,

    'cache' => [
        'enabled' => false,
        'ttl' => 600,
        'store' => null,
        'prefix' => 'statamic_toc',
    ],
];

Key Options

  • min_level, max_level: Heading level range included in TOC.
  • preserve_existing_ids: Keep existing heading IDs when injecting anchors.
  • cache.enabled: Enable caching for tag output.
  • cache.ttl: Cache lifetime in seconds.

Usage

Blade Tag Compatibility (<s:toc>)

This package is compatible with Statamic Blade tag usage like:

<s:toc :content="$content" :is_flat="false" depth="3" from="h1">
    @isset($toc_id)
        <a href="#{{ $toc_id }}">{{ $toc_title }}</a>

        @if($has_children)
            @foreach($children as $child)
                <a href="#{{ $child['toc_id'] }}">{{ $child['toc_title'] }}</a>
            @endforeach
        @endif
    @endisset
</s:toc>

Supported legacy params:

  • content
  • field (default: article)
  • depth (default: 3)
  • from (default: h1)
  • is_flat (default: false)

Legacy output keys are provided for drop-in compatibility:

  • toc_title
  • toc_id
  • toc_level
  • children
  • has_children
  • total_children
  • total_results
  • no_results

Tag Usage (Antlers / Blade context)

Use the toc tag to extract TOC items.

{{ toc:items source="html" :content="content" min_level="2" max_level="4" }}
  <li><a href="#{{ id }}">{{ text }}</a></li>
{{ /toc:items }}

Parameters:

  • source: html, markdown, or bard.
  • content: The input payload to parse.
  • min_level / max_level: Optional level limits.
  • tree: true/false to return hierarchical or flat output.

Modifier Usage

Inject heading IDs into rendered HTML:

{{ content | toc }}

Optional params:

{{ content | toc:2:4:true }}

Meaning:

  1. min level
  2. max level
  3. preserve existing IDs

Service / Facade Usage (PHP)

use BitMx\StatamicToc\Facades\Toc;

$items = Toc::extractAsArray('markdown', $markdown, 1, 6, true);

Or via DI:

use BitMx\StatamicToc\Toc\TocService;

public function show(TocService $tocService): array
{
    return $tocService->extractAsArray('html', $html, 2, 4, true);
}

Output Shape

Each item includes:

  • text
  • level
  • id
  • url (for example, #installation)
  • children (when tree=true)

Bard Integration Notes

The Bard extractor walks nested node structures and set values, so headings inside nested/complex Bard content are included.

Caching

When cache.enabled=true, tag results are cached using a key that includes source, content, level range, and tree mode.

Migration from In-App TOC Logic

If you already have custom parser/tag/modifier classes in your project:

  1. Install this package.
  2. Replace custom TOC tag calls with package tag usage.
  3. Replace custom heading-id injection with | toc modifier.
  4. Keep your front-end template markup, only swap data source.
  5. Remove legacy TOC classes after parity verification.

Troubleshooting

IDs are not added to headings

  • Ensure content passed to modifier is rendered HTML.
  • Confirm heading levels are within configured min/max.

Duplicate anchors

  • Duplicate heading text is expected to produce suffixes (-2, -3, ...).

Missing Bard headings

  • Verify the field content passed to source bard is raw Bard data (array structure).

Cache not updating

  • Disable cache during development.
  • Check cache store and TTL settings.

Testing

composer test

License

MIT

bit-mx/statamic-toc 适用场景与选型建议

bit-mx/statamic-toc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bit-mx/statamic-toc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bit-mx/statamic-toc 我们能提供哪些服务?
定制开发 / 二次开发

基于 bit-mx/statamic-toc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 41
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-06