Solves the copy-paste drift problem between documentation and source code. @import directives pull external file content directly into fenced code blocks at parse time -- with optional line-range selection, language hinting, and indentation control. Your code samples stay in sync with the actual source by definition, not by discipline.
composer require alto/commonmarkuse Alto\CommonMark\Extension\Import\ImportExtension;
use League\CommonMark\Environment\Environment;
$environment = new Environment();
$environment->addExtension(new ImportExtension(__DIR__ . '/docs'));@import "path/to/file.md"
@import "src/Handler.php" {lines: 10-40, lang: php, indent: 2}- Constructor:
new ImportExtension(string $basePath = '.', int $maxDepth = 10). - Guards against circular imports and excessive nesting depth.
# API
@import "snippets/auth.md"
@import "src/Auth.php" {lines: 1-30, lang: php}This extension is actively developed in the alto/commonmark monorepo.
MIT License -- Simon André & Alto