php-markdown icon indicating copy to clipboard operation
php-markdown copied to clipboard

How to avoid ampersand entity encodings?

Open bertoost opened this issue 11 years ago • 5 comments

First off; this is a rocking PHP Markdown reader! I'm using it to render some help docs for a module documentation website.

I am using (fenced) code blocks to show usage examples. To pass properties to some functional parts, as example, I need to use the ampersand (&) sign. This is being converted to &amp ; amp; (without spaces) entities and that is not what I want in my code examples.

How can I avoid it to convert it to & and just let be written as & sign inside code blocks?

bertoost avatar Dec 08 '14 21:12 bertoost

Can you give me a sample input that does that? Fenced code block don't normally double-encode ampersands.

michelf avatar Dec 08 '14 21:12 michelf

Here you can see te rendered Markdown; https://rtfm.modxsimplecart.com/snippets/scgetcart The right content is generated by a Markdown file. The contents of the Markdown file is just with & sign without entities etc.

bertoost avatar Dec 09 '14 09:12 bertoost

That doesn't really tell me what is the input given to Markdown. If I had to guess, I'd say the input is something like this:

The default snippet call for this snippet is just like this;

~~~
[[!scGetCart? &tpl=`myCustomCart`]]
~~~

And let's say we want to include all product colors (own custom TV for products);

But then if I paste this in the PHP Markdown dingus the result is as expected.

Is there another text filter function used on that site that could cause this? Or is the Markdown parser configured in a non-default way?

michelf avatar Dec 09 '14 12:12 michelf

I am working with MarkdownExtra and that's exactly what I did. Except that I have added a class to code block to trigger the syntax highlight on my website (the black boxes). Like;

For example, with some fictive custom chunks to influence the output;
~~~~ {.language-markup}
[[!scGetCart:tag? &tpl=`myCustomCart`]]
~~~~
And let's say we want to include all product colors (own custom TV for products);

Also configured "code_attr_on_pre". Set to true. The PHP part;

/** @var \Michelf\MarkdownExtra $parser */
$parser = new MarkdownExtra;
$parser->code_attr_on_pre = true;
$content = file_get_contents($file);
$content = $parser->defaultTransform($content);

bertoost avatar Dec 09 '14 18:12 bertoost

So, I'm trying this small PHP program:

$parser = new MarkdownExtra;
$parser->code_attr_on_pre = true;

$content = "For example, with some fictive custom chunks to influence the output;

~~~~ {.language-markup}
[[!scGetCart:tag? &tpl=`myCustomCart`]]
~~~~

And let's say we want to include all product colors (own custom TV for products);";

$content = $parser->transform($content);
echo $content;

and I get this output:

<p>For example, with some fictive custom chunks to influence the output;</p>

<pre class="language-markup"><code>[[!scGetCart:tag? &amp;tpl=`myCustomCart`]]
</code></pre>

<p>And let's say we want to include all product colors (own custom TV for products);</p>

All seems perfectly fine. I'm not sure how you get this double encoding. Which version of PHP Markdown are you using?

(Note: you're using defaultTransform above, which is a static function using the default parameters. It thus ignores your code_attr_on_pre setting. It does not seem to have any effect on the &amp;, but you should use transform in this case.)

michelf avatar Dec 09 '14 18:12 michelf