Integrates League CommonMark and league/html-to-markdown with PHPNomad's phpnomad/markdown abstraction. It ships a single strategy class that satisfies both conversion contracts so your application can bind the interfaces in its container and stay unaware of the underlying library.
composer require phpnomad/league-markdown-integrationComposer pulls in phpnomad/markdown and both League libraries as transitive dependencies.
PHPNomad\LeagueMarkdownIntegration\Strategies\MarkdownConversionStrategy is the only class in the package. It does three things.
- Implements both
CanConvertMarkdownToHtmlandCanConvertHtmlToMarkdownfromphpnomad/markdown, so one binding covers both directions. toHtml()delegates toLeague\CommonMark\CommonMarkConverterand rethrowsCommonMarkExceptionasConvertToHtmlException.toMarkdown()delegates toLeague\HTMLToMarkdown\HtmlConverterand rethrowsInvalidArgumentExceptionandRuntimeExceptionasConvertToMarkdownException.
Both League converters are constructed with their defaults. If you need custom CommonMark extensions or a non-default HTML-to-markdown configuration, write your own strategy class implementing the same interfaces and bind that instead.
phpnomad/markdownfor the conversion interfaces and exception hierarchy.league/commonmark ^2.7andleague/html-to-markdown ^5.1for the actual conversion work. Both come in through Composer automatically.
Register the strategy against both interfaces in a PHPNomad initializer so anything type-hinting CanConvertMarkdownToHtml or CanConvertHtmlToMarkdown resolves to the same implementation.
<?php
namespace MyApp\Content;
use PHPNomad\LeagueMarkdownIntegration\Strategies\MarkdownConversionStrategy;
use PHPNomad\Loader\Interfaces\HasClassDefinitions;
use PHPNomad\Markdown\Interfaces\CanConvertHtmlToMarkdown;
use PHPNomad\Markdown\Interfaces\CanConvertMarkdownToHtml;
final class Initializer implements HasClassDefinitions
{
public function getClassDefinitions(): array
{
return [
MarkdownConversionStrategy::class => [
CanConvertMarkdownToHtml::class,
CanConvertHtmlToMarkdown::class,
],
];
}
}Consumers type-hint the interfaces, not MarkdownConversionStrategy itself. Swapping implementations later is a one-line change in this initializer.
Full PHPNomad documentation lives at phpnomad.com. For the underlying libraries, see the CommonMark and html-to-markdown project pages.
MIT. See LICENSE.