Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML entities in markdown block code

Tags:

markdown

I have tried to use HTML entities in code but it's not rendering properly when compiling it using a markdown processor. For example, &Theta; renders properly outside codes, but when within codes, it appears as <p><code>&amp;Theta;</code></p> after compilation. Is it possible to use HTML entities in markdown code and compile them so they can be rendered properly ?

like image 787
Noor Avatar asked Dec 26 '17 18:12

Noor


2 Answers

You can't, which is an intentional design decision. Otherwise, how would you be able to use a code block to display HTML source code? As the rules state:

Within a code block, ampersands (&) and angle brackets (< and >) are automatically converted into HTML entities. This makes it very easy to include example HTML source code using Markdown -- just paste it and indent it, and Markdown will handle the hassle of encoding the ampersands and angle brackets. For example, this:

    <div class="footer">
        &copy; 2004 Foo Corporation
    </div>

will turn into:

<pre><code>&lt;div class="footer"&gt;
    &amp;copy; 2004 Foo Corporation
&lt;/div&gt;
</code></pre>
like image 156
Waylan Avatar answered Oct 08 '22 01:10

Waylan


If it is - for some reason - important to include HTML entities in a code block, you always have the option to use HTML inside a Markdown document (see Inline HTML in the rules).

That is, instead of using indentation (or code fencing in GitHub Flavored Markdown/GitLab Flavored Markdown with three backticks ```), enclose the code block with appropriate HTML elements yourself:

<pre><code>Greek letters &Theta; &Pi; and &alpha;
in a code block
</code></pre>

will turn into:

Greek letters Θ Π and α
in a code block

Note that you'll have to start your code at the same line as the opening <pre><code> tags or otherwise will end up having an empty line at the beginning of your code block.

It's not as nice to read in code and you have to decide whether the rendered or the raw view is more important in your case.

like image 37
Elpy Avatar answered Oct 08 '22 00:10

Elpy