phpnomad / league-markdown-integration
Implements PHPNomad Markdown implementations using League libraries
Package info
github.com/phpnomad/league-markdown-integration
Type:project
pkg:composer/phpnomad/league-markdown-integration
Requires
- league/commonmark: ^2.7
- league/html-to-markdown: ^5.1
- phpnomad/markdown: ^1.0
README
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.
Installation
composer require phpnomad/league-markdown-integration
Composer pulls in phpnomad/markdown and both League libraries as transitive dependencies.
What This Provides
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.
Requirements
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.
Usage
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.
Documentation
Full PHPNomad documentation lives at phpnomad.com. For the underlying libraries, see the CommonMark and html-to-markdown project pages.
License
MIT. See LICENSE.