alirezasedghi/laravel-toc
Composer 安装命令:
composer require alirezasedghi/laravel-toc
包简介
Table of contents for Laravel
README 文档
README
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
- Automatic TOC generation from HTML headings.
- Configurable heading levels to include (
h1toh6). - Customizable list types (ul, ol) and HTML classes for TOC structure.
- Customizable HTML classes for the TOC list and items.
- Adds unique
idattributes to headings for internal linking.
Installation
Install the package via Composer:
composer require alirezasedghi/laravel-toc
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 = "<h1>Main Title</h1><h2>Section 1</h2><p>Content...</p>"; // 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):
<div class="toc-container"> {!! $toc !!} </div> <div class="content"> {!! $content !!} </div>
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 (ulfor unordered,olfor ordered).toc_class: The CSS class for the main TOC list (<ul>or<ol>).internal_list_class: The CSS class for internal nested lists.toc_item_class: The CSS class for each item (<li>) in the TOC.toc_link_class: The CSS class for each link (<a>) 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.,1forh1).max_level: The maximum heading level to include in the TOC (e.g.,6forh6).
Example with Custom Options
use Alirezasedghi\LaravelTOC\TOCGenerator; class PageController extends Controller { public function show(Request $request) { // Example HTML content $html = "<h1>Main Title</h1><h2>Section 1</h2><h3>Subsection</h3><p>Content...</p>"; // Define custom options $options = [ 'list_type' => 'ol', // Use an ordered list for the TOC 'toc_class' => 'custom-toc', // Class for TOC <ul>/<ol> 'internal_list_class' => 'nested-toc', // Class for nested <ul>/<ol> 'toc_item_class' => 'toc-item', // Class for each <li> 'toc_link_class' => 'toc-link', // Class for each <a> '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
| Option | Type | Default | Description |
|---|---|---|---|
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 <li> inside the TOC |
toc_link_class |
string | '' |
CSS class for each link (<a>) 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
Here’s how you can render both the TOC and the processed HTML content in a Blade view:
<div class="toc-container"> {!! $toc !!} </div> <div class="content"> {!! $content !!} </div>
Output Example
Given the HTML:
<h1>Main Title</h1> <h2>Section 1</h2> <h3>Subsection 1.1</h3> <h2>Section 2</h2> <h3>Subsection 2.1</h3>
Generated TOC:
<ul class="toc"> <li><a href="#toc-1">Main Title</a> <ul> <li><a href="#toc-2">Section 1</a> <ul> <li><a href="#toc-3">Subsection 1.1</a></li> </ul> </li> <li><a href="#toc-4">Section 2</a> <ul> <li><a href="#toc-5">Subsection 2.1</a></li> </ul> </li> </ul> </li> </ul>
License
This package is open-sourced software licensed under the MIT license.
alirezasedghi/laravel-toc 适用场景与选型建议
alirezasedghi/laravel-toc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.9k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2024 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「TOC」 「table of contents」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alirezasedghi/laravel-toc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alirezasedghi/laravel-toc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alirezasedghi/laravel-toc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Plug-ins for DataTables
A component that allows creating responsive HTML tables or lists from data object
Simple Table-of-Contents Generator for PHP. Generates TOCs based off H1...H6 tags
Adjacency List’ed Closure Table database design pattern implementation for Laravel. Includes restore of tree
Provides a table of contents for elements based on one or more articles within the page.
Dynamically generate the table of contents from $Content
统计信息
- 总下载量: 2.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-23