承接 jifish/parsedown-toc-ext 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jifish/parsedown-toc-ext

Composer 安装命令:

composer require jifish/parsedown-toc-ext

包简介

TOC extension for parsedown and parsedown-extra with automatic heading IDs.

README 文档

README

A simple, lightweight, and modern Table of Contents extension for Parsedown and ParsedownExtra. MIT licenced.

Provides:

  • Markdown/HTML TOC generation
  • Automatic slugified heading IDs
  • Programmatic TOC access
  • Configurable depth, numbering, and hierarchy behaviour
  • Works with ParsedownExtra
  • Manual TOC adjustment

This extension works with both original parsedown and the community fork of Parsedown.

Inspired by Benjamin Hoegh's ParsedownToc.

Installation

composer require jifish/parsedown-toc-ext

If you want Parsedown Extra support:

composer require erusev/parsedown-extra

If you want the community fork of Parsedown, install it first.

composer require parsedown/parsedown

Basic Usage

Parsedown

use jifish\ParsedownTocExt\ParsedownToc;

$parser = new ParsedownToc();

$html = $parser->text($markdown);
$toc = $parser->getToc();
$tocText = $parser->renderToc();

Parsedown Extra

use jifish\ParsedownTocExt\ParsedownExtraToc;
$parser = new ParsedownExtraToc();

Substitution of [TOC] marker

$html = $parser->text($markdown);
$html = str_replace("[TOC]", $parser->renderToc(), $html);

Custom id generation

If you need you own id generation strategy, you can override slugify:

use jifish\ParsedownTocExt\ParsedownToc;

class ParsedownTocCustomId extends ParsedownToc
{
    // Generate random ids
    protected function slugify(string $text): string
    {
        return uniqid();
    }
}

$parser = new ParsedownTocCustomId();

Heading ID Generation

Heading IDs are:

  • Lowercased
  • Non-alphanumeric characters removed
  • Whitespace converted to hyphens
  • Guaranteed unique per document, duplicate headings receive numeric suffixes

Example:

### My Heading!

Becomes:

<h3 id="my-heading">My Heading!</h3>

Interfaces

Basic Usage

text(string $markdown): string

As with Parsedown, but adds heading IDs, and builds and stores a new TOC structure.

getToc(): array

Returns the internal TOC structure.

Format:

[
    'heading-id' => [
        'level' => 2,
        'text'  => 'Heading Text',
    ],
]
  • Indexed by generated ID.
  • Ordered in document order.
  • level is the original heading level (1-6).

getTocCount(): int

Returns the number of headings in the TOC.

removeHeading(string $id): void

Removes a heading from the TOC by its ID.

renderToc(...)

Generates a Markdown-formatted TOC.

renderToc(
    bool $asHtml = false,
    bool $collapseSkippedLevels = false,
    ?int $maxDepth = null,
    bool $numbered = false,
    bool $excludeFirstHeading = false
): string

Parameters:

  • $asHtml (default false)

    If true, the generated Markdown TOC is returned as rendered HTML.

    Example:

    echo $parser->renderToc(asHtml: true);

    Returns:

    <ul>
        <li><a href="#title">Title</a></li>
        ...
    </ul>
  • $collapseSkippedLevels (default false)

    Controls how heading level jumps are handled. When enabled, large jumps only increase nesting by one level.

    Example:

    # A
    ### B
    ## C

    false (true depth):

    - [A](#a)
            - [B](#b)
        - [C](#c)

    true (collapsed hierarchy):

    - [A](#a)
        - [B](#b)
        - [C](#c)
  • $maxDepth (default: null)

    Limits how deep headings are included.

    Depth is calculated relative to the lowest heading level in the document.

  • $numbered (default: false)

    Uses ordered list syntax instead of bullet lists. Each nesting level maintains its own numbering sequence.

    Example:

    1. [Title](#title)
        1. [Install](#install)
        2. [Usage](#usage)
  • $excludeFirstHeading (default: false)

    Skips the first heading in the document. Useful when the first heading is the page title and should not appear in the TOC. The next visible heading becomes the root of the rendered TOC, while maxDepth remains relative to the full document.

hasToc(): bool

Returns whether the internal TOC currently contains any entries.

Customising TOC

appendHeading(int $level, string $text, ?string $id = null): string

Manually adds an entry to the end of the internal TOC structure, and returns its id.

This allows you to add headings that are not present in the Markdown source, for use when combining parsed headings with dynamic or generated content, or to generate TOCs without any source markdown.

Note: Calling text() will clear the internal TOC.

Parameters

  • $level Heading level (1-6). A ValueError is thrown if the level is outside this range.

  • $text The display text used in the TOC.

  • $id (optional) Custom ID to use instead of generating one from $text.

    If omitted, the ID is generated from $text using the same slug rules as automatic headings.

    The final ID is always passed through the internal uniqueness check, so collisions are automatically resolved.

Example:

$parser = new ParsedownToc();

$parser->addHeader(2, 'Installation');
$parser->addHeader(3, 'Windows');
$parser->addHeader(3, 'Linux');

echo $parser->getTocText();

Output:

- [Installation](#installation)
    - [Windows](#windows)
    - [Linux](#linux)

prependHeading(int $level, string $text, ?string $id = null): string

As above, but adds the heading to the top instead.

insertHeadingAfter(string $afterId, int $level, string $text, ?string $id = null): string

As above, but inserts the heading directly below $afterId. Throws a ValueError if the id is not found.

jifish/parsedown-toc-ext 适用场景与选型建议

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

它主要适用于以下技术方向: 「markdown」 「TOC」 「parsedown」 「table-of-contents」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 jifish/parsedown-toc-ext 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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