Skip to content

phpnomad/league-markdown-integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

phpnomad/league-markdown-integration

Latest Version Total Downloads PHP Version License

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 CanConvertMarkdownToHtml and CanConvertHtmlToMarkdown from phpnomad/markdown, so one binding covers both directions.
  • toHtml() delegates to League\CommonMark\CommonMarkConverter and rethrows CommonMarkException as ConvertToHtmlException.
  • toMarkdown() delegates to League\HTMLToMarkdown\HtmlConverter and rethrows InvalidArgumentException and RuntimeException as ConvertToMarkdownException.

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/markdown for the conversion interfaces and exception hierarchy.
  • league/commonmark ^2.7 and league/html-to-markdown ^5.1 for 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.

About

League CommonMark integration for PHPNomad's markdown processing

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages