|
| 1 | +# CommonMark Extension Pygments Highlighter |
| 2 | + |
| 3 | +The `PygmentsHighlighter` extension adds support for using pygments to style |
| 4 | +blocks of code in Markdown documents. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +This extension can be installed from packagist via composer: |
| 9 | + |
| 10 | +```bash |
| 11 | +composer require danielescherzer/commonmark-ext-pygments-highlighter |
| 12 | +``` |
| 13 | + |
| 14 | +It depends on |
| 15 | + |
| 16 | +* `league/commonmark` (version 2.7 or above; all lower versions have security |
| 17 | +issues) |
| 18 | +* `ramsey/pygments` (version 3.0 or above) |
| 19 | + |
| 20 | +Separately (not enforced via composer) you need to have python and the pygments |
| 21 | +python library installed. The extension is currently tested against |
| 22 | + |
| 23 | +* Python 3.11, 3.12, and 3.13 |
| 24 | +* Pygments 2.17, 2.18, and 2.19 |
| 25 | + |
| 26 | +The pygments command is assumed to be at `pygmentize` (in the PATH); if this is |
| 27 | +not the case configure the correct path in the extension setup, see the |
| 28 | +configuration section below. |
| 29 | + |
| 30 | +## Syntax and results |
| 31 | + |
| 32 | +Sample Markdown input: |
| 33 | + |
| 34 | +````markdown |
| 35 | +```php |
| 36 | +<?php |
| 37 | +class User { |
| 38 | + private int $id; |
| 39 | + private string $name; |
| 40 | + |
| 41 | + public function __construct( int $id, string $name ) { |
| 42 | + $this->id = $id; |
| 43 | + $this->name = $name; |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | +```` |
| 48 | + |
| 49 | +Result: |
| 50 | + |
| 51 | +```html |
| 52 | +<div class="pygments-highlighter"><div class="highlight"><pre><span></span><span class="cp"><?php</span> |
| 53 | +<span class="k">class</span> <span class="nc">User</span> <span class="p">{</span> |
| 54 | + <span class="k">private</span> <span class="nx">int</span> <span class="nv">$id</span><span class="p">;</span> |
| 55 | + <span class="k">private</span> <span class="nx">string</span> <span class="nv">$name</span><span class="p">;</span> |
| 56 | + |
| 57 | + <span class="k">public</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">(</span> <span class="nx">int</span> <span class="nv">$id</span><span class="p">,</span> <span class="nx">string</span> <span class="nv">$name</span> <span class="p">)</span> <span class="p">{</span> |
| 58 | + <span class="nv">$this</span><span class="o">-></span><span class="na">id</span> <span class="o">=</span> <span class="nv">$id</span><span class="p">;</span> |
| 59 | + <span class="nv">$this</span><span class="o">-></span><span class="na">name</span> <span class="o">=</span> <span class="nv">$name</span><span class="p">;</span> |
| 60 | + <span class="p">}</span> |
| 61 | +<span class="p">}</span> |
| 62 | +</pre></div> |
| 63 | +</div> |
| 64 | +``` |
| 65 | + |
| 66 | +## Usage |
| 67 | + |
| 68 | +Configure your `Environment` as usual and simply add an instance of the |
| 69 | +`PygmentsHighlighterExtension`: |
| 70 | + |
| 71 | +```php |
| 72 | +<?php |
| 73 | + |
| 74 | +use DanielEScherzer\CommonMarkPygmentsHighlighter\PygmentsHighlighterExtension; |
| 75 | +use League\CommonMark\Environment\Environment; |
| 76 | +use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; |
| 77 | +use League\CommonMark\MarkdownConverter; |
| 78 | + |
| 79 | +// These are the defaults for the extension, you can change them |
| 80 | +$config = [ |
| 81 | + 'pygments_highlighter' => [ |
| 82 | + 'pygmentize_path' => null, // Use `pygmentize` from PATH |
| 83 | + 'on_exception' => 'warn', |
| 84 | + ], |
| 85 | +]; |
| 86 | + |
| 87 | +// Configure the Environment with the desired extensions |
| 88 | +$environment = new Environment( $config ); |
| 89 | +$environment->addExtension( new CommonMarkCoreExtension() ); |
| 90 | +$environment->addExtension( new PygmentsHighlighterExtension() ); |
| 91 | + |
| 92 | +// And convert your markdown |
| 93 | +$converter = new MarkdownConverter($environment); |
| 94 | +echo $converter->convert("```php\n<?php\necho 'testing...';\n```"); |
| 95 | +``` |
| 96 | + |
| 97 | +## Configuration |
| 98 | + |
| 99 | +This extension can be configured by providing a `pygments_highlighter` array |
| 100 | +with the following options (defaults shown above): |
| 101 | + |
| 102 | +### `pygmentize_path` |
| 103 | + |
| 104 | +The path to the pygmentize command, or null to use the default (`pygmentize`). |
| 105 | + |
| 106 | +### `on_exception` |
| 107 | + |
| 108 | +If the attempt to use the pygmentize command fails, this value controls the |
| 109 | +behavior of the rendererer. By default, it is set to 'warn' - it can be: |
| 110 | + |
| 111 | +* 'warn' - show a warning and render the code with the CommonMark-provided |
| 112 | +default output |
| 113 | +* 'ignore' - render the code with the CommonMark-provided default output |
| 114 | +* 'throw' - rethrow the exception with the failure details |
| 115 | + |
| 116 | +## Adding styles |
| 117 | + |
| 118 | +This extension just adds the various classes for the output to be made |
| 119 | +colorful, you still need to add CSS targetting the code. The `ramsey/pygments` |
| 120 | +library offers a method to get an entire stylesheet for any of the pygments |
| 121 | +built-in styles, or you can build your own styling. |
| 122 | + |
| 123 | +All of the highlighted code output from this extension will be within a |
| 124 | +div with the `pygments-highlighter` class, so your CSS rules can be scoped to |
| 125 | +only elements within the `.pygments-highlighter` selector. |
0 commit comments