alto/commonmark-source

CommonMark extension that displays source files as highlighted code blocks at @source directives, with title, line numbers, and highlighting.

Maintainers

Package info

github.com/altophp/commonmark-source

Homepage

Issues

Documentation

pkg:composer/alto/commonmark-source

Transparency log

Fund package maintenance!

smnandre

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

v1.0.0 2026-07-30 02:00 UTC

This package is auto-updated.

Last update: 2026-07-30 02:56:48 UTC


README

Summary

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.

Installation

composer require

composer require alto/commonmark

Registration

use Alto\CommonMark\Extension\Source\SourceExtension;
use League\CommonMark\Environment\Environment;

$environment = new Environment();
$environment->addExtension(new SourceExtension(__DIR__));

Usage

@source "src/Service.php"
@source "src/Service.php" {lines: 10-30, numbers: true, highlight: "14,18-20"}

Configuration

  • Constructor: new SourceExtension(string $basePath='.', array $allowedExtensions=[], bool $escapeHtml=true, int $maxFileSize=1048576).
  • Supports extension allowlists and max-size protections.

Minimal Example

# Service excerpt

@source "src/Service.php" {lines: 1-25, numbers: true}

Development

This extension is actively developed in the alto/commonmark monorepo.

License

MIT License -- Simon André & Alto