thephpleague / html-to-markdown

Convert HTML to Markdown with PHP
MIT License
1.77k stars 205 forks source link

convert link to Jira markdown #56

Closed lwrbakro closed 9 years ago

lwrbakro commented 9 years ago

Hi,

I am about to convert HTML code to JIRA markdown.

Is it possible to use custom converter function instead the default one?

<a href="http://example.com">link title</a>  ==> [link title|http://example.com]
colinodell commented 9 years ago

You could use a custom converter if you had the ability to configure the Environment:

  1. Create the custom converter (implement the ConverterInterface)
  2. Configure your own Environment

At this point you've got the Environment configured how you need it, but no way to inject this into the HtmlConverter. This is an oversight on my part. For now, you could use (ab)use inheritence:

class CustomHtmlConverter extends HtmlConverter
{
    public function __construct(Environment $environment, array $options = array()) {
        parent::__construct($options);

        $this->environment = $environment;
        $this->environment->getConfig()->merge($options);
    }
}

I'll open a separate issue to make the Environment injectable.

colinodell commented 9 years ago

Oops, I misread my own code (I haven't had my coffee yet this morning). Turns out you can do this with the existing code:

  1. Create the custom converter (implement the ConverterInterface)
  2. Initialize a new HtmlConverter.
  3. Call $htmlConverter->addConverter(new MyCustomLinkConverter())

That third call will replace the existing link converter with yours :)