0

I was posting a code that used If <> Condition, however when I tried to use the {} to Format the code after an Item List -. It just didn't work, as can be seen below:

  • sdad

    fdsfds fdsfs dsfsd

So I tried using <pre><code>, however, the <> would disappear. As can be seen on some examples:

Using {}:
Different <> Diff

Using ``

Different <> Diff

Using <pre><code>

Different 

Using <pre>

Different 

Using <code>

Different <>

Why does it happen and is there a way to fix it? Or to get around?

2
  • 3
    I fail to see what's wrong with any of the code formatting options that you've shown. <pre> isn't for code formatting, and so understandably doesn't properly format code.
    – Servy
    Commented Aug 15, 2017 at 18:18
  • 1
    It is for preformatted text, however i didn't know them. Googling i found a thread that said to use <pre><code>, so i went to read meta.stackoverflow.com/editing-help#code, it seems confusing if you don't know what is a preformatting. Because people will try to use them both together. At least i did. Commented Aug 15, 2017 at 18:23

3 Answers 3

10

If you indent the code by eight spaces, it works:

  • sdad

    fdsfds
    fdsfs
    dsfsd
    

If for some reason a < doesn't display, you can use &lt; instead, e.g.

<pre>&lt;test></pre>

becomes:

<test>
2

As an alternative to indenting code after a list item by 8 spaces, use an HTML comment or <anyinvalidtag> to end the list before the code block:

  • sdad

fdsfds fdsfs dsfsd

Source:

- sdad

<something>

    fdsfds fdsfs dsfsd
2

When you use the {} code formatting tool, it indents your code by four spaces. This indentation is then converted by the Markdown parser into a <pre><code> block with all the special characters inside suitably escaped.

However, when the indented code appears after a list item, the four spaces acts instead as a continuation of the list item.

If you want the code block to appear inside the list item, you have to indent it using 8 spaces. (Note that it is not possible to start a list item with a code block; You need to put some text in first.)

If instead you want it to appear after the list, you can break them up with an HTML comment (<!-- -->) or a language hint to trigger the correct syntax colouring if appropriate (<!-- language: lang-css -->).

Most languages are rendered correctly without hints, but CSS in particular is rendered badly by the default parser.


If you don't want to use the Markdown features and instead want to write your own HTML, then you need to escape all special characters (eg. < and &) yourself. For example:

<pre><code>Different &lt;> Diff</code></pre>

will be rendered as:

Different <> Diff

You can learn more about Markdown at: https://meta.stackoverflow.com/editing-help

Not the answer you're looking for? Browse other questions tagged .