Skip to content

Commit

Permalink
Make it possible to add extension from the CommonMarkConverter class
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Sep 24, 2024
1 parent 43a76d8 commit 76cd387
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/CommonMarkConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\ExtensionInterface;

/**
* Converts CommonMark-compatible Markdown to HTML.
Expand All @@ -43,4 +44,9 @@ public function getEnvironment(): Environment

return $this->environment;
}

public function addExtension(ExtensionInterface $extension): void
{
$this->environment->addExtension($extension);

Check failure on line 50 in src/CommonMarkConverter.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method League\CommonMark\Environment\EnvironmentInterface::addExtension().

Check failure on line 50 in src/CommonMarkConverter.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/CommonMarkConverter.php:50:29: UndefinedInterfaceMethod: Method League\CommonMark\Environment\EnvironmentInterface::addExtension does not exist (see https://psalm.dev/181)
}
}
15 changes: 15 additions & 0 deletions tests/unit/CommonMarkConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use League\CommonMark\Environment\Environment;
use League\CommonMark\Exception\UnexpectedEncodingException;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\Util\HtmlFilter;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -70,4 +71,18 @@ public function testGetEnvironmentReturnsMainEnvironmentClass(): void

$this->assertInstanceOf(Environment::class, $converter->getEnvironment());
}

public function testAddExtensionToEnvironment(): void
{
$converter = new CommonMarkConverter();

$environment = $converter->getEnvironment();

$this->assertCount(1, $environment->getExtensions());
$this->assertInstanceOf(CommonMarkCoreExtension::class, $environment->getExtensions()[0]);

$environment->addExtension(new TableExtension());
$this->assertCount(2, $environment->getExtensions());
$this->assertInstanceOf(TableExtension::class, $environment->getExtensions()[1]);
}
}

0 comments on commit 76cd387

Please sign in to comment.