benjaminhoegh/parsedown-toc 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

benjaminhoegh/parsedown-toc

Composer 安装命令:

composer require benjaminhoegh/parsedown-toc

包简介

Table of Contents Extension for Parsedown.

README 文档

README

ParsedownToc

ParsedownToc

GitHub release Packagist Downloads GitHub

ParsedownToc is an extension for Parsedown and ParsedownExtra that introduces advanced features for developers working with Markdown. It is based on @KEINOS toc extention

Features:

  • Speed: Super-fast processing.
  • Configurability: Easily customizable for different use-cases.
  • Custom Header IDs: Full support for custom header ids.

Prerequisites:

  • Requires Parsedown 1.7.4 or later.

Installation:

Ensure you have Composer installed on your system.

  1. Install the ParsedownToc package using Composer:

    composer require benjaminhoegh/ParsedownToc
  2. Alternatively, you can download the latest release and include Parsedown.php in your project.

Usage:

Basic example:

<?php
require 'vendor/autoload.php';  // autoload

$content = file_get_contents('sample.md');  // Sample Markdown with '[toc]' tag
$ParsedownToc = new ParsedownToc();

$html = $ParsedownToc->text($content);  // Parses '[toc]' tag to ToC if exists
echo $html;

Separate body and ToC:

<?php
$content = file_get_contents('sample.md');
$ParsedownToc = new \ParsedownToc();

$body = $ParsedownToc->body($content);
$toc  = $ParsedownToc->contentsList();

echo $toc;  // ToC in <ul> list
echo $body; // Main content

Configuration:

The ParsedownToc->setOptions(array $options) method allows you to configure the main class. Below are the available options along with their default values and descriptions:

Option Type Default Description
selectors array ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] HTML heading tags to include in the ToC.
delimiter string - Character used between words and duplicate suffixes.
max_anchor_length int null Maximum character length for generated anchor IDs.
lowercase boolean true Convert anchor IDs to lowercase.
replacements array none Custom regex replacements applied before sanitization.
transliterate boolean true Transliterate non-ASCII characters to ASCII equivalents.
urlencode boolean false Uses PHP built-in urlencode and disables all other options.
reserved_ids array [] Anchor IDs that must not be generated; duplicates are suffixed instead.
base_url string `` Prefixes each ToC anchor link with the specified URL.
anchor_prefix string `` Static string prepended to every generated anchor ID.
toc_tag string [toc] Markdown tag that is replaced with the rendered ToC.
toc_id string toc id attribute of the wrapping <div> around the ToC.

Methods:

The ParsedownToc class offers several methods for different functionalities:

  • text(string $text): Returns the parsed content and [toc] tag(s).
  • body(string $text): Returns the parsed content without the [toc] tag.
  • contentsList([string $type_return='html']): Returns the ToC in HTML, JSON, or as an array.
    • Optional: Specify the return type as html, json, or array.
  • setTocSelectors(array $selectors): Set which heading levels to include in the ToC.
  • setTocDelimiter(string $delimiter): Define a custom delimiter.
  • setTocMaxAnchorLength(?int $length): Set the maximum character length for generated anchor IDs.
  • setTocLowercase(bool $boolean): Choose whether anchor IDs should be lowercased.
  • setTocReplacements(?array $replacements): Provide custom regex replacements applied before sanitization.
  • setTocTransliterate(bool $boolean): Specify if transliterations should be made.
  • setTocUrlencode(bool $boolean): Decide if you want to use PHP's built-in urlencode.
  • setTocReservedIds(array $reservedIds): Define anchor IDs that must not be generated; duplicates are suffixed instead.
  • setTocBaseUrl(string $url): Set a URL prefix for all ToC anchor links.
  • setTocAnchorPrefix(string $prefix): Prepend a static string to every generated anchor ID.
  • setTocTag(string $tag): Set a custom ToC markdown tag.
  • setTocId(string $id): Set a custom id attribute for the ToC wrapper element.

Deprecated methods

The following methods still work but emit an E_USER_DEPRECATED notice. Replace them with the equivalents above.

Deprecated Replacement
setTocLimit(int $limit) setTocMaxAnchorLength(?int $length)
setTocBlacklist(array $blacklist) setTocReservedIds(array $reservedIds)
setTocUrl(string $url) setTocBaseUrl(string $url)

Custom Anchors

If you want to use your own logic for creating slugs for the headings, you can do so by using setCreateAnchorIDCallback.

Example using cocur's slugify:

$ParsedownToc->setCreateAnchorIDCallback(function($text, $level) {
    $slugify = new Slugify();
    return $slugify->slugify($text);
});

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 36.92k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 21
  • 点击次数: 13
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 21
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-26