|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Configuration |
| 4 | +redirect_from: /configuration/ |
| 5 | +--- |
| 6 | + |
| 7 | +# Configuration |
| 8 | + |
| 9 | +Many aspects of this library's behavior can be tweaked using configuration options. |
| 10 | + |
| 11 | +You can provide an array of configuration options to the `Environment` or converter classes when creating them: |
| 12 | + |
| 13 | +```php |
| 14 | +$config = [ |
| 15 | + 'renderer' => [ |
| 16 | + 'block_separator' => "\n", |
| 17 | + 'inner_separator' => "\n", |
| 18 | + 'soft_break' => "\n", |
| 19 | + ], |
| 20 | + 'commonmark' => [ |
| 21 | + 'enable_em' => true, |
| 22 | + 'enable_strong' => true, |
| 23 | + 'use_asterisk' => true, |
| 24 | + 'use_underscore' => true, |
| 25 | + 'unordered_list_markers' => ['-', '*', '+'], |
| 26 | + ], |
| 27 | + 'html_input' => 'escape', |
| 28 | + 'allow_unsafe_links' => false, |
| 29 | + 'max_nesting_level' => PHP_INT_MAX, |
| 30 | + 'slug_normalizer' => [ |
| 31 | + 'max_length' => 255, |
| 32 | + ], |
| 33 | +]; |
| 34 | +``` |
| 35 | + |
| 36 | +If you're using the basic `CommonMarkConverter` or `GithubFlavoredMarkdown` classes, simply pass the configuration array into the constructor: |
| 37 | + |
| 38 | +```php |
| 39 | +use League\CommonMark\CommonMarkConverter; |
| 40 | +use League\CommonMark\GithubFlavoredMarkdownConverter; |
| 41 | + |
| 42 | +$converter = new CommonMarkConverter($config); |
| 43 | +// or |
| 44 | +$converter = new GithubFlavoredMarkdownConverter($config); |
| 45 | +``` |
| 46 | + |
| 47 | +Otherwise, if you're using `MarkdownConverter` to customize the extensions in your parser, pass the configuration into the [Environment](/2.6/customization/environment/)'s constructor instead: |
| 48 | + |
| 49 | +```php |
| 50 | +use League\CommonMark\Environment\Environment; |
| 51 | +use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension; |
| 52 | +use League\CommonMark\MarkdownConverter; |
| 53 | + |
| 54 | +// Here's where we set the configuration array: |
| 55 | +$environment = new Environment($config); |
| 56 | + |
| 57 | +// TODO: Add any/all the extensions you wish; for example: |
| 58 | +$environment->addExtension(new InlinesOnlyExtension()); |
| 59 | + |
| 60 | +// Go forth and convert you some Markdown! |
| 61 | +$converter = new MarkdownConverter($environment); |
| 62 | +``` |
| 63 | + |
| 64 | +Here's a list of the core configuration options available: |
| 65 | + |
| 66 | +- `renderer` - Array of options for rendering HTML |
| 67 | + - `block_separator` - String to use for separating renderer block elements |
| 68 | + - `inner_separator` - String to use for separating inner block contents |
| 69 | + - `soft_break` - String to use for rendering soft breaks |
| 70 | +- `html_input` - How to handle HTML input. Set this option to one of the following strings: |
| 71 | + - `strip` - Strip all HTML (equivalent to `'safe' => true`) |
| 72 | + - `allow` - Allow all HTML input as-is (default value; equivalent to `'safe' => false) |
| 73 | + - `escape` - Escape all HTML |
| 74 | +- `allow_unsafe_links` - Remove risky link and image URLs by setting this to `false` (default: `true`) |
| 75 | +- `max_nesting_level` - The maximum nesting level for blocks (default: `PHP_INT_MAX`). Setting this to a positive integer can help protect against long parse times and/or segfaults if blocks are too deeply-nested. |
| 76 | +- `slug_normalizer` - Array of options for configuring how URL-safe slugs are created; see [the slug normalizer docs](/2.6/customization/slug-normalizer/#configuration) for more details |
| 77 | + - `instance` - An alternative normalizer to use (defaults to the included `SlugNormalizer`) |
| 78 | + - `max_length` - Limits the size of generated slugs (defaults to 255 characters) |
| 79 | + - `unique` - Controls whether slugs should be unique per `'document'` (default) or per `'environment'`; can be disabled with `false` |
| 80 | + |
| 81 | +Additional configuration options are available for most of the [available extensions](/2.6/customization/extensions/) - refer to their individual documentation for more details. For example, the CommonMark core extension offers these additional options: |
| 82 | + |
| 83 | +- `commonmark` - Array of options for configuring the CommonMark core extension: |
| 84 | + - `enable_em` - Disable `<em>` parsing by setting to `false`; enable with `true` (default: `true`) |
| 85 | + - `enable_strong` - Disable `<strong>` parsing by setting to `false`; enable with `true` (default: `true`) |
| 86 | + - `use_asterisk` - Disable parsing of `*` for emphasis by setting to `false`; enable with `true` (default: `true`) |
| 87 | + - `use_underscore` - Disable parsing of `_` for emphasis by setting to `false`; enable with `true` (default: `true`) |
| 88 | + - `unordered_list_markers` - Array of characters that can be used to indicate a bulleted list (default: `["-", "*", "+"]`) |
| 89 | + |
| 90 | +## Environment |
| 91 | + |
| 92 | +The configuration is ultimately passed to (and managed via) the `Environment`. If you're creating your own `Environment`, simply pass your config array into its constructor instead. |
| 93 | + |
| 94 | +[Learn more about customizing the Environment](/2.6/customization/environment/) |
0 commit comments