showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.19k stars 1.56k forks source link

makeMarkdown converts code to ticks even if there is formatting #932

Open jonatanschroeder opened 2 years ago

jonatanschroeder commented 2 years ago

As mentioned in #400, #819, #861, and others, Markdown does not allow formatting inside code blocks (backticks). So makeHtml by design escapes any character inside it to its corresponding entity where required. However, HTML allows formatting inside code blocks (e.g., <code><em>ab</em> cd</code> would make ab italic inside the code block). The problem is that makeMarkdown just blindly converts this <code> block to backticks, even though the result is not equivalent.

I believe the correct behaviour for <code> in makeMarkdown should be to retain the <code> tag whenever there is formatting inside. Converting code to backtick in this case does not generate equivalent Markdown code. So <code>abc<code> would convert to `abc`, but <code><em>ab</em> cd</code> would remain unchanged, maybe change to <code>*ab* cd</code>.

A similar issue is with entities inside code blocks. <code>&lt;</code> in makeMarkdown should not be converted to `&lt;`, as converting that back to HTML would have a result of <code>&amp;lt;</code> instead.