Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve (specific) HTML comments #177

Closed
Rarst opened this issue Oct 22, 2019 · 4 comments
Closed

Preserve (specific) HTML comments #177

Rarst opened this issue Oct 22, 2019 · 4 comments

Comments

@Rarst
Copy link

Rarst commented Oct 22, 2019

The converter currently discards all of HTML comments, but some of them might have special meaning and need to be preserved.

For example some systems use <!--more--> to separate excerpt from the rest of the content.

I've tried overriding CommentConverter and it mostly works, but I get erroneous slash prepended somewhere (e.g. resulting output is \<!--more-->)...

class CommentConverter extends \League\HTMLToMarkdown\Converter\CommentConverter
{
    public function convert(ElementInterface $element)
    {
        if ('more' === $element->getValue()) {
            return '<!--more-->';
        }

        return '';
    }
}
@straube
Copy link
Contributor

straube commented Oct 31, 2019

@Rarst Does it make sense giving developers the choice to tell the library which comments to preserve? Perhaps through configuration. Something like:

$config = [
    'preserve_comments' => [ 'more', /* ... */ ],
],

Just suggesting that because I don't think hardcoding the values is a good idea. Every new comment with a special meaning would lead to a code change.

I know Wordpress uses the <!--more--> tag, in case this is a common use case, we could set the default the value of preserve_comments to [ 'more' ]. Users who don't want to preserve any comment at all could override the option passing an empty array.

Any thoughts?

@Rarst
Copy link
Author

Rarst commented Oct 31, 2019

I think the meaningful configuration options might be something like:

  1. false preserve none
  2. true preserve all
  3. string[] preserve specific

Being able to preserve all matters because strict matches might be insufficient if comments contain something variable (yeah, that happens too).

@colinodell
Copy link
Member

Implemented in #179. Thanks for the proposal!

@colinodell
Copy link
Member

Released as 4.9.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants