alto / commonmark-content-slicer
CommonMark extension that wraps heading-based sections in nested <section> elements for semantic document structure.
Package info
github.com/altophp/commonmark-content-slicer
pkg:composer/alto/commonmark-content-slicer
Fund package maintenance!
Requires
- php: ^8.3
- league/commonmark: ^2.7
README
Summary
Genuinely rare in the CommonMark ecosystem. Most processors stop at rendering headings as <h1>…<h6> tags -- this one goes further and restructures the document into a properly nested <section> tree that mirrors the heading hierarchy. The result is semantic HTML you can actually target with CSS, query with JavaScript, and traverse with accessibility tooling. No custom syntax required.
Installation
composer require
composer require alto/commonmark
Registration
use Alto\CommonMark\Extension\ContentSlicer\ContentSlicerExtension; use League\CommonMark\Environment\Environment; $environment = new Environment(); $environment->addExtension(new ContentSlicerExtension());
Usage
No custom markdown directive is required; the extension processes standard headings after parse.
Configuration
- Constructor:
new ContentSlicerExtension(int $minSectionLevel = 1). 0wraps fromh1,1wraps fromh2,2wraps fromh3, etc.
Minimal Example
# Title Intro ## Section Body ### Subsection Details
Development
This extension is actively developed in the alto/commonmark monorepo.
License
MIT License -- Simon André & Alto