WP-MarkDown icon indicating copy to clipboard operation
WP-MarkDown copied to clipboard

Markdownify fails for GitHub flavor-like codeblocks

Open magadanskiuchen opened this issue 8 years ago • 5 comments

I've noticed that the 1.6 alpha version here in GitHub has support for parsing MarkDown with ability to specify a language such as:

```language-name

which is great for the alternative prismjs syntax highlighter I'm using!

However, when the post is edited the Markdownify class fails to properly display that with MarkDown code. So in my particular case this:

\```html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Slider</title>
</head>
<body>
</body>
</html>
\```

is rendered as:

<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;title&gt;Slider&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

which no longer allows me to easily make edits to my blogpost.

I've tried sorting this out (and had I succeeded I would have submitted a pull request), however, I only got that to:

\```html
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;title&gt;Slider&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
\```

where still html entities are used.

You can see patch of my code here: https://gist.github.com/magadanskiuchen/74aa03205f2167c89ded7bffcf412ee2

If you're able to further resolve this and merge into master that would be awesome!

magadanskiuchen avatar Apr 10 '16 12:04 magadanskiuchen

I think it needs to be rendered that way. Otherwise, the browser renders the html in the code block as html rather than text.

mAAdhaTTah avatar Apr 10 '16 19:04 mAAdhaTTah

@mAAdhaTTah maybe I wasn't clear but this is not the raw output, rather what's displayed in the editor.

The actual markup is double-escaped:

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset=&quot;utf-8&quot; /&amp;gt;
    &amp;lt;title&amp;gt;Slider&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

So my guess would be that I need to run it once through htmlspecialchars_decode, however, since this function does not seem to be called in the plugin anywhere else (at least I cannot find it) I would guess a different approach is taken in other similar cases and I'd like to keep is consistent. I just seem to be missing something.

magadanskiuchen avatar Apr 12 '16 05:04 magadanskiuchen

OK, I've found that the Markdownify class has a decode method that's used with that purpose in other placed within the class, so I've just ran the code though that and it works nicely.

I've just submitted a PR for this ( #70 ).

magadanskiuchen avatar Apr 14 '16 19:04 magadanskiuchen

I've added a (failing-but-skipped) test for this. It is a bug, but unfortunately #70 breaks some other tests.

I have found that it only affects code blocks with language identifiers. Code blocks without and rendered correctly into MarkDown (albeit not GitHub flavoured markdown - i.e. the blocks appear using the indentation syntax, not the fences).

stephenharris avatar Oct 07 '16 15:10 stephenharris

@stephenharris How to use c# code blocks instead of writing csharp. Actually git flavour markdown supports other terms of languages.

```C# instead of ```csharp

sivyel avatar Jul 20 '20 11:07 sivyel