The flagship of the set. @source directives embed a real file -- not a copy -- into your documentation at parse time, with automatic syntax class detection, precise line-range selection, optional line numbers, and per-line highlighting. Your docs and your source stay in sync by construction.
composer require alto/commonmarkuse Alto\CommonMark\Extension\Source\SourceExtension;
use League\CommonMark\Environment\Environment;
$environment = new Environment();
$environment->addExtension(new SourceExtension(__DIR__));@source "src/Service.php"
@source "src/Service.php" {lines: 10-30, numbers: true, highlight: "14,18-20"}- Constructor:
new SourceExtension(string $basePath='.', array $allowedExtensions=[], bool $escapeHtml=true, int $maxFileSize=1048576). - Supports extension allowlists and max-size protections.
# Service excerpt
@source "src/Service.php" {lines: 1-25, numbers: true}This extension is actively developed in the alto/commonmark monorepo.
MIT License -- Simon André & Alto