Search code examples
githubescapingpipemarkdown

How to escape a pipe char in a code statement in a markdown table?


On GitHub I want to build a table containing pieces of code in Markdown. It works fine except when I put a pipe char (i.e. | ) between the backtick (i.e. ` ) chars.

Here is what I want:

      a     |  r  
------------|-----
 `a += x;`  |  r1
 `a |= y;`  |  r2

The problem is that the vertical bar in the code statement of the second line is interpreted as a column delimiter. Then the table rendering looks pretty ugly. How could I avoid that?

Note that I already tried to use the | HTML code, but it produces a |= y;.


Solution

  • As of March 2017 using escaped pipes is much easier: \| See other answers.

    If you remove the backticks (`), using the | hack works

          a     |  r  
    ------------|-----
     `a += x;`  |  r1
     a |= y;  |  r2
    

    and produces the following output

    enter image description here

    Alternatively, you can replace the backticks (`) with a <code></code> markup which fixes the issues more nicely by preserving the rendering

          a     |  r  
    ------------|-----
     `a += x;`  |  r1
    <code>a &#124;= y;</code>  |  r2
    

    generating the following output

    enter image description here