Download the PHP package baraja-core/table-of-content without Composer
On this page you can find all versions of the php package baraja-core/table-of-content. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download baraja-core/table-of-content
More information about baraja-core/table-of-content
Files in baraja-core/table-of-content
Package table-of-content
Short Description A tool for easily compiling Table of content from the article content.
License
Homepage https://github.com/baraja-core/table-of-content
Informations about the package table-of-content
Table of Content
A lightweight PHP library for automatically generating a Table of Contents from HTML article content. The library parses your HTML, extracts headings, creates anchor links, and provides structured data for building navigation.
🎯 Key Features
- Automatic heading extraction - Parses
<h2>tags and generates URL-friendly anchor IDs - Title and perex detection - Automatically extracts the main title (
<h1>) and introductory paragraph - XSS-safe output - All generated attributes are properly escaped to prevent security vulnerabilities
- Immutable response object - Returns a clean, typed
Responseentity with all extracted data - Zero configuration - Works out of the box with sensible defaults
- PHP 8.0+ support - Uses modern PHP features including named arguments and constructor property promotion
🏗️ Architecture Overview
The library consists of two main components working together:
🔧 Components
ContentManager
The main service class responsible for parsing HTML content. It provides a single public method:
parse(string $html): Response- Accepts raw HTML and returns a structuredResponseobject
Processing steps:
- Scans for all
<h2>tags in the content - For each heading, generates a URL-friendly ID using
Nette\Utils\Strings::webalize() - Injects an anchor
<div>element before each heading for smooth scroll navigation - Extracts the page title from the first
<h1>tag - Extracts the perex (lead paragraph) from the first
<p>tag - Returns all data wrapped in an immutable
Responseobject
Response
An immutable data transfer object implementing Stringable. When cast to string, it returns the processed content with anchors.
Available methods:
| Method | Return Type | Description |
|---|---|---|
getOriginal() |
string |
Returns the original unmodified HTML input |
getContent() |
string |
Returns HTML with injected anchor elements |
getPureContent() |
string |
Returns content without the <h1> title tag |
getTitle() |
?string |
Returns the extracted title or null |
getPerex() |
?string |
Returns the extracted perex or null |
getItems() |
array<string, string> |
Returns anchor ID to heading title mapping |
📦 Installation
It's best to use Composer for installation, and you can also find the package on Packagist and GitHub.
To install, simply use the command:
You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework.
Requirements
- PHP 8.0 or higher
nette/utils^3.0
🚀 Basic Usage
Simple Example
Accessing Parsed Data
Rendering the Table of Contents
Using Response as String
The Response object implements Stringable, so you can use it directly where a string is expected:
📸 Visual Examples
Response Entity Structure
The following image shows the structure of the Response object after parsing:
Rendered Table of Contents
Example of how a rendered table of contents looks in a real application:
💡 How Anchor Generation Works
When the parser encounters an <h2> heading like:
It transforms it to:
The anchor ID is generated using Nette\Utils\Strings::webalize() which:
- Converts text to lowercase
- Replaces spaces with hyphens
- Removes diacritics (accents)
- Strips special characters
This ensures clean, URL-friendly anchor IDs that work reliably across all browsers.
🔒 Security
The library implements proper XSS protection:
- All generated
idattributes are escaped usinghtmlspecialchars()withENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTEflags - Protection against innerHTML mXSS vulnerability (nette/nette#1496) is included
- Original content is preserved without modification in
getOriginal()
⚙️ Integration with Nette Framework
For Nette Framework users, you can register the service in your configuration:
Then inject it into your presenters or services:
🎨 Styling Recommendations
For smooth scroll behavior to anchors, add this CSS:
👤 Author
Jan Barasek
- Website: https://baraja.cz
- GitHub: @baraja-core
📄 License
baraja-core/table-of-content is licensed under the MIT license. See the LICENSE file for more details.