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

Html entity < escaped in code block if inside html container #183

Open
samuelgfeller opened this issue Aug 7, 2024 · 2 comments
Open

Comments

@samuelgfeller
Copy link

samuelgfeller commented Aug 7, 2024

There is a weird escaping going on in certain cases in code blocks that are inside html tags with markdown="1"
In reality I'm using a <details markdown="1"> tag as container but will use a <p> for the demonstration.

I have set

$markdownParser->setMarkupEscaped(false);

Steps to reproduce

  1. Feed this code to parsedown extra v.0.8.1: https://pastebin.com/V9v2mtnk
$output = $ParsedownExtra->text($markdownCodeFromPastebin);
echo $output;
  1. Have a look at the generated HTML

Actual result

< converted to &lt; sometimes

The following:

image

results to this:
image

Expected result

The code inside a markdown code block should not be escaped as it's expected to be code.

Edit

If I remove the markdown="1" from the p tag the markdown is taken literally as html which I don't understand if this comment meant that markdown inside html tags are supposed to work even without the markdown="1" addition, just like github for instance.
image

If I remove the html parent block, the markdown code block is interpreted correctly:

image

@davidbyoung
Copy link
Contributor

This also happens with blockquote sections inside of an HTML element:

<div class="foo" markdown="1">

> **Foo**

</div>

...incorrectly interprets the > as &gt; rather than a blockquote.

@samuelgfeller
Copy link
Author

The char & has the same issue.

My fix is creating a ParsedownExtraExtended class that extends ParsedownExtra and overwrites blockFencedCodeComplete and inlineCode functions and replace the escaped chars by their original value:

class ParsedownExtraExtended extends \ParsedownExtra
{
    protected function inlineCode($Excerpt)
    {
        $code = parent::inlineCode($Excerpt);
        $code['element']['text'] = $this->replaceSpecialCharacters($code['element']['text']);
        return $code;
    }

    protected function blockFencedCodeComplete($Block)
    {
        $Block['element']['text']['text'] = $this->replaceSpecialCharacters($Block['element']['text']['text']);
        return $Block;
    }

    protected function replaceSpecialCharacters($text): string
    {
        return str_replace(['&lt;', '&gt;', '&amp;'], ['<', '>', '&'], $text);
    }
}

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

No branches or pull requests

2 participants