benjaminhoegh/parsedown-toc
Composer 安装命令:
composer require benjaminhoegh/parsedown-toc
包简介
Table of Contents Extension for Parsedown.
关键字:
README 文档
README
ParsedownToc
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.
-
Install the ParsedownToc package using Composer:
composer require benjaminhoegh/ParsedownToc
-
Alternatively, you can download the latest release and include
Parsedown.phpin 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, orarray.
- Optional: Specify the return type as
- 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
idattribute 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 benjaminhoegh/parsedown-toc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 36.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 13
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-26