1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
---
layout: default
title: Upgrading from 1.6 to 2.0 (for integrators)
description: Integrator guide to upgrading to newer versions of this library
---
## Minimum PHP Version
The minimum supported PHP version was increased from 7.1 to 7.4.
## `CommonMarkConverter` and `GithubFlavoredMarkdownConverter` constructors
The constructor methods for both `CommonMarkConverter` and `GithubFlavoredMarkdownConverter` no longer accept passing in a customized `Environment`. If you want to customize the extensions used in your converter you should switch to using `MarkdownConverter`. See the [Basic Usage](/2.0/basic-usage/) documentation for an example.
```diff
-use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\Table\TableExtension;
+use League\CommonMark\MarkdownConverter;
$config = [
'html_input' => 'escape',
'allow_unsafe_links' => false,
'max_nesting_level' => 100,
];
-$environment = new Environment();
+$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new TableExtension());
-$converter = new CommonMarkConverter($config, $environment); // or GithubFlavoredMarkdownConverter
+$converter = new MarkdownConverter($environment);
echo $converter->convertToHtml('Hello World!');
```
## `CommonMarkConverter` Return Type
In 1.x, calling `convertToHtml()` would return a `string`. In 2.x this changed to return a `RenderedContentInterface`. To get the resulting HTML, either cast the return value to a `string` or call `->getContent()`. (This new interface extends from `Stringable` so you can type hint against that instead, if needed.)
```diff
use League\CommonMark\CommonMarkConverter;
$converter = new CommonMarkConverter();
-echo $converter->convertToHtml('# Hello World!');
+echo $converter->convertToHtml('# Hello World!')->getContent();
+// or
+echo (string) $converter->convertToHtml('# Hello World!');
```
## HTML Changes
Table of Contents items are no longer wrapped with `<p>` tags:
```diff
<ul class="table-of-contents">
<li>
- <p><a href="#level-2-heading">Level 2 Heading</a></p>
+ <a href="#level-2-heading">Level 2 Heading</a>
</li>
<li>
- <p><a href="#level-4-heading">Level 4 Heading</a></p>
+ <a href="#level-4-heading">Level 4 Heading</a>
</li>
<li>
- <p><a href="#level-3-heading">Level 3 Heading</a></p>
+ <a href="#level-3-heading">Level 3 Heading</a>
</li>
</ul>
```
See [#613](https://github.com/thephpleague/commonmark/issues/613) for details.
Additionally, the HTML (including URL fragments) for Heading Permalinks have changed:
```diff
-<h1><a id="user-content-hello-world" href="#hello-world" name="hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">¶</a>Hello World!</h1>
+<h1><a id="content-hello-world" href="#content-hello-world" class="heading-permalink" aria-hidden="true" title="Permalink">¶</a>Hello World!</h1>
```
Note that the `href` now targets the `id` attribute instead of the `name`, which is deprecated in HTML 5. Additionally, the default prefix has changed to `content`. See the [Heading Permalink extension documentation](/2.0/extensions/heading-permalinks/#configuration) for more details on how to configure the prefixes.
## Configuration Option Changes
Several configuration options now have new names:
| Old Key/Path | New Key/Path | Notes |
| ----------------------------------- | ----------------------------------- | --------------------------------------- |
| `enable_em` | `commonmark/enable_em` | |
| `enable_strong` | `commonmark/enable_strong` | |
| `use_asterisk` | `commonmark/use_asterisk` | |
| `use_underscore` | `commonmark/use_underscore` | |
| `unordered_list_markers` | `commonmark/unordered_list_markers` | Empty arrays no longer allowed |
| `heading_permalink/id_prefix` | (unchanged) | Default is now `content` |
| `heading_permalink/inner_contents` | `heading_permalink/symbol` | |
| `heading_permalink/slug_normalizer` | `slug_normalizer/instance` | |
| `max_nesting_level` | (unchanged) | Only integer values are supported |
| `mentions/*/symbol` | `mentions/*/prefix` | |
| `mentions/*/regex` | `mentions/*/pattern` | Cannot contain start/end `/` delimiters |
## Method Return Types
Return types have been added to virtually all class and interface methods. If you implement or extend anything from this library, ensure you also have the proper return types added.
## Configuration Classes Relocated
The following classes have been moved to the `league/config` package:
| Old Class Namespace/Name (1.x) | Moved To |
| ---------------------------------------------------- | ------------------------------------------- |
| `League\CommonMark\Util\ConfigurationAwareInterface` | `League\Config\ConfigurationAwareInterface` |
| `League\CommonMark\Util\ConfigurationInterface` | `League\Config\ConfigurationInterface` |
| `League\CommonMark\Util\Configuration` | `League\Config\Configuration` |
## Classes/Namespaces Renamed
Many classes now live in different namespaces, and some have also been renamed. Here's a quick guide showing their new locations:
_(Note that the base namespace of `League\CommonMark` has been omitted from this table for brevity.)_
| Old Class Namespace/Name (1.x) | New Class Namespace/Name (2.0) |
| ------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| `ConfigurableEnvironmentInterface` | `Environment\ConfigurableEnvironmentInterface` |
| `EnvironmentAwareInterface` | `Environment\EnvironmentAwareInterface` |
| `Environment` | `Environment\Environment` |
| `EnvironmentInterface` | `Environment\EnvironmentInterface` |
| `Extension\CommonMarkCoreExtension` | `Extension\CommonMark\CommonMarkCoreExtension` |
| `Delimiter\Processor\EmphasisDelimiterProcessor` | `Extension\CommonMark\Delimiter\Processor\EmphasisDelimiterProcessor` |
| `Block\Element\BlockQuote` | `Extension\CommonMark\Node\Block\BlockQuote` |
| `Block\Element\FencedCode` | `Extension\CommonMark\Node\Block\FencedCode` |
| `Block\Element\Heading` | `Extension\CommonMark\Node\Block\Heading` |
| `Block\Element\HtmlBlock` | `Extension\CommonMark\Node\Block\HtmlBlock` |
| `Block\Element\IndentedCode` | `Extension\CommonMark\Node\Block\IndentedCode` |
| `Block\Element\ListBlock` | `Extension\CommonMark\Node\Block\ListBlock` |
| `Block\Element\ListData` | `Extension\CommonMark\Node\Block\ListData` |
| `Block\Element\ListItem` | `Extension\CommonMark\Node\Block\ListItem` |
| `Block\Element\ThematicBreak` | `Extension\CommonMark\Node\Block\ThematicBreak` |
| `Inline\Element\AbstractWebResource` | `Extension\CommonMark\Node\Inline\AbstractWebResource` |
| `Inline\Element\Code` | `Extension\CommonMark\Node\Inline\Code` |
| `Inline\Element\Emphasis` | `Extension\CommonMark\Node\Inline\Emphasis` |
| `Inline\Element\HtmlInline` | `Extension\CommonMark\Node\Inline\HtmlInline` |
| `Inline\Element\Image` | `Extension\CommonMark\Node\Inline\Image` |
| `Inline\Element\Link` | `Extension\CommonMark\Node\Inline\Link` |
| `Inline\Element\Strong` | `Extension\CommonMark\Node\Inline\Strong` |
| `Block\Parser\BlockQuoteParser` | `Extension\CommonMark\Parser\Block\BlockQuoteParser` |
| `Block\Parser\FencedCodeParser` | `Extension\CommonMark\Parser\Block\FencedCodeParser` |
| `Block\Parser\ATXHeadingParser` and `Block\Parser\SetExtHeadingParser` | `Extension\CommonMark\Parser\Block\HeadingParser` |
| `Block\Parser\HtmlBlockParser` | `Extension\CommonMark\Parser\Block\HtmlBlockParser` |
| `Block\Parser\IndentedCodeParser` | `Extension\CommonMark\Parser\Block\IndentedCodeParser` |
| `Block\Parser\ListParser` | `Extension\CommonMark\Parser\Block\ListParser` |
| `Block\Parser\ThematicBreakParser` | `Extension\CommonMark\Parser\Block\ThematicBreakParser` |
| `Inline\Parser\AutolinkParser` | `Extension\CommonMark\Parser\Inline\AutolinkParser` |
| `Inline\Parser\BacktickParser` | `Extension\CommonMark\Parser\Inline\BacktickParser` |
| `Inline\Parser\BangParser` | `Extension\CommonMark\Parser\Inline\BangParser` |
| `Inline\Parser\CloseBracketParser` | `Extension\CommonMark\Parser\Inline\CloseBracketParser` |
| `Inline\Parser\EntityParser` | `Extension\CommonMark\Parser\Inline\EntityParser` |
| `Inline\Parser\EscapableParser` | `Extension\CommonMark\Parser\Inline\EscapableParser` |
| `Inline\Parser\HtmlInlineParser` | `Extension\CommonMark\Parser\Inline\HtmlInlineParser` |
| `Inline\Parser\OpenBracketParser` | `Extension\CommonMark\Parser\Inline\OpenBracketParser` |
| `Block\Renderer\BlockQuoteRenderer` | `Extension\CommonMark\Renderer\Block\BlockQuoteRenderer` |
| `Block\Renderer\FencedCodeRenderer` | `Extension\CommonMark\Renderer\Block\FencedCodeRenderer` |
| `Block\Renderer\HeadingRenderer` | `Extension\CommonMark\Renderer\Block\HeadingRenderer` |
| `Block\Renderer\HtmlBlockRenderer` | `Extension\CommonMark\Renderer\Block\HtmlBlockRenderer` |
| `Block\Renderer\IndentedCodeRenderer` | `Extension\CommonMark\Renderer\Block\IndentedCodeRenderer` |
| `Block\Renderer\ListBlockRenderer` | `Extension\CommonMark\Renderer\Block\ListBlockRenderer` |
| `Block\Renderer\ListItemRenderer` | `Extension\CommonMark\Renderer\Block\ListItemRenderer` |
| `Block\Renderer\ThematicBreakRenderer` | `Extension\CommonMark\Renderer\Block\ThematicBreakRenderer` |
| `Inline\Renderer\CodeRenderer` | `Extension\CommonMark\Renderer\Inline\CodeRenderer` |
| `Inline\Renderer\EmphasisRenderer` | `Extension\CommonMark\Renderer\Inline\EmphasisRenderer` |
| `Inline\Renderer\HtmlInlineRenderer` | `Extension\CommonMark\Renderer\Inline\HtmlInlineRenderer` |
| `Inline\Renderer\ImageRenderer` | `Extension\CommonMark\Renderer\Inline\ImageRenderer` |
| `Inline\Renderer\LinkRenderer` | `Extension\CommonMark\Renderer\Inline\LinkRenderer` |
| `Inline\Renderer\StrongRenderer` | `Extension\CommonMark\Renderer\Inline\StrongRenderer` |
| `Extension\SmartPunct\PunctuationParser` | `Extension\SmartPunct\DashParser` and `Extension\SmartPunct\EllipsesParser` |
| `Extension\TableOfContents\TableOfContents` | `Extension\TableOfContents\Node\TableOfContents` |
| `Block\Element\AbstractBlock` | `Node\Block\AbstractBlock` |
| `Block\Element\Document` | `Node\Block\Document` |
| `Block\Element\InlineContainerInterface` | `Node\Block\InlineContainerInterface` |
| `Block\Element\Paragraph` | `Node\Block\Paragraph` |
| `Block\Element\StringContainerInterface` | `Node\StringContainerInterface` |
| `Inline\Element\AbstractInline` | `Node\Inline\AbstractInline` |
| `Inline\Element\AbstractStringContainer` | `Node\Inline\AbstractStringContainer` |
| `Inline\AdjacentTextMerger` | `Node\Inline\AdjacentTextMerger` |
| `Inline\Element\Newline` | `Node\Inline\Newline` |
| `Inline\Element\Text` | `Node\Inline\Text` |
| `Block\Parser\BlockParserInterface` | `Parser\Block\BlockContinueParserInterface` and `Parser\Block\BlockStartParserInterface` |
| `Block\Parser\LazyParagraphParser` | `Parser\Block\ParagraphParser` |
| `Cursor` | `Parser\Cursor` |
| `DocParser` | `Parser\MarkdownParser` |
| `DocParserInterface` | `Parser\MarkdownParserInterface` |
| `Inline\Parser\InlineParserInterface` | `Parser\Inline\InlineParserInterface` |
| `Inline\Parser\NewlineParser` | `Parser\Inline\NewlineParser` |
| `InlineParserContext` | `Parser\InlineParserContext` |
| `InlineParserEngine` | `Parser\InlineParserEngine` |
| `Block\Renderer\DocumentRenderer` | `Renderer\Block\DocumentRenderer` |
| `Block\Renderer\ParagraphRenderer` | `Renderer\Block\ParagraphRenderer` |
| `ElementRendererInterface` | `Renderer\ChildNodeRendererInterface` |
| `HtmlRenderer` | `Renderer\HtmlRenderer` |
| `Inline\Renderer\NewlineRenderer` | `Renderer\Inline\NewlineRenderer` |
| `Inline\Renderer\TextRenderer` | `Renderer\Inline\TextRenderer` |
| `Block\Renderer\BlockRendererInterface` and `Inline\Renderer\InlineRendererInterface` | `Renderer\NodeRendererInterface` |
| `HtmlElement` | `Util\HtmlElement` |
Most of these won't be relevant unless your integration allows people to add/remove extensions, parsers, etc. and you might reference those classes directly. Otherwise, if you simply expose the basic classes to developers, check out the shorter, partial list in the [consumer upgrade guide](/2.0/upgrading/consumers/).
## Rendering Changes
This library no longer differentiates between block renderers and inline renderers - everything now uses "node renderers" which allow us to have a unified approach to rendering! As a result, the following changes were made, which you may need to change in your custom extensions:
| Old Method/Interface (1.x) | New Method/Interface (2.0) |
| ------------------------------------------------------- | ------------------------------------------------- |
| `BlockRendererInterface` | `NodeRendererInterface` |
| `InlineRendererInterface` | `NodeRendererInterface` |
| `EnvironmentInterface::getBlockRenderersForClass()` | `EnvironmentInterface::getRenderersForClass()` |
| `EnvironmentInterface::getInlineRenderersForClass()` | `EnvironmentInterface::getRenderersForClass()` |
| `ConfigurableEnvironmentInterface::addBlockRenderer()` | `ConfigurableEnvironmentInterface::addRenderer()` |
| `ConfigurableEnvironmentInterface::addInlineRenderer()` | `ConfigurableEnvironmentInterface::addRenderer()` |
| `ElementRendererInterface::renderBlock()` | `ChildNodeRendererInterface::renderNodes()` |
| `ElementRendererInterface::renderBlocks()` | `ChildNodeRendererInterface::renderNodes()` |
| `ElementRendererInterface::renderInline()` | `ChildNodeRendererInterface::renderNodes()` |
| `ElementRendererInterface::renderInlines()` | `ChildNodeRendererInterface::renderNodes()` |
| `HtmlRenderer::renderBlock($document)` | `HtmlRenderer::renderDocument()` |
Renderers now implement the unified `NodeRendererInterface` which has a similar (but slightly different) signature from
the old `BlockRendererInterface` and `InlineRendererInterface` interfaces:
```php
/**
* @param Node $node
* @param ChildNodeRendererInterface $childRenderer
*
* @return HtmlElement|string|null
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer);
```
The element being rendered is still passed in the first argument, and the object that helps you render children is still
passed in the second argument. Note that blocks are no longer told whether they're being rendered in a tight list - if you
need to know about this, traverse up the `$node` AST yourself and check any `ListBlock` ancestor for tightness.
## Removed Classes
The following classes have been removed:
| Class name in 1.x | Replacement / Notes |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------- |
| `AbstractStringContainerBlock` | Use `extends AbstractBlock implements StringContainerInterface` instead. Note the new method names. |
| `Context` | Use `MarkdownParserState` instead (has different methods but serves a similar purpose) |
| `ContextInterface` | Use `MarkdownParserStateInterface` instead (has different methods but serves a similar purpose) |
| `Converter` | Use `MarkdownConverter` instead. |
| `ConverterInterface` | Use `MarkdownConverterInterface`. This interface has the same methods so it should be a drop-in replacement. |
| `UnmatchedBlockCloser` | No longer needed 2.x |
## Renamed constants
The following constants have been moved/renamed:
| Old Name/Location (1.x) | New Name/Location (2.0) |
| ----------------------------------------- | ------------------------ |
| `EnvironmentInterface::HTML_INPUT_ALLOW` | `HtmlFilter::ALLOW` |
| `EnvironmentInterface::HTML_INPUT_ESCAPE` | `HtmlFilter::ESCAPE` |
| `EnvironmentInterface::HTML_INPUT_STRIP` | `HtmlFilter::STRIP` |
## Renamed Methods
The following methods have been renamed:
| Class | Old Name (1.x) | New Name (2.0) |
| -------------------------------------------------- | ------------------ | ----------------------- |
| `Environment` / `ConfigurableEnvironmentInterface` | `addBlockParser()` | `addBlockStartParser()` |
| `ReferenceMap` / `ReferenceMapInterface` | `addReference()` | `add()` |
| `ReferenceMap` / `ReferenceMapInterface` | `getReference()` | `get()` |
| `ReferenceMap` / `ReferenceMapInterface` | `listReferences()` | `getIterator()` |
## Configuration Method Changes
Calling `EnvironmentInterface::getConfig()` without any parameters is no longer supported.
Calling `ConfigurableEnvironmentInterface::mergeConfig()` without any parameters is no longer supported.
The `ConfigurableEnvironmentInterface::setConfig()` method has been removed. Use `getConfig()` instead.
## `bin/commonmark` command
This command was buggy to test and was relatively unpopular, so it has been removed. If you need this type of functionality, consider writing your own script with a Converter/Environment configured exactly how you want it.
## `CommonMarkConverter::VERSION` constant
This previously-deprecated constant was removed in 2.0 Use `\Composer\InstalledVersions` provided by composer-runtime-api instead.
## `HeadingPermalinkRenderer::DEFAULT_INNER_CONTENTS` constant
This previously-deprecated constant was removed in 2.0. Use `HeadingPermalinkRenderer::DEFAULT_SYMBOL` instead.
|