29

I tried to put 3-backticks code block(```) in 3-backticks code block. but I could not get a correct result.

How can I escape that in 3-backticks code block?

Original markdown file:

# Markdown example

```
here is an example code.

```
// this area is nested 3-backticks code block.
const hello = "hello";
```

```

Expected result:

Markdown example

here is an example code.

```
// this area is nested 3-backticks code block.
const hello = "hello";
```
1
  • What is the actual result? Commented Mar 14, 2018 at 0:06

2 Answers 2

55

Depending on which implementation you use, there may be a few options to chose from.

Use more than three backticks.

The rules for fenced code blocks do not strictly require the use of three backticks. Rather, it is three or more. The important thing is that the opening and closing deliminators each contain the same number of backticks. Any sets of backticks between the deliminators may contain a different number of backticks (usually less as some implementations are buggy). Like this:

# Markdown example

````
here is an example code.

```
// this area is nested 3-backticks code block.
const hello = "hello";
```

````

Note that the opening and closing deliminators each contain four backticks, while the three-backtick string within the code block is preserved.

Use tildes

Fenced code blocks were originally designed with tildes (~) rather than backticks. The first few implementations only supported using three or more tildes as deliminators. A short time later, GitHub introduced fenced code blocks with backticks. After a bug report was filed, they added tilde support and now most implementations of fenced code blocks support both characters. Again, the key is that the same sequence of characters are used for both the opening and closing deliminators. Like this:

# Markdown example

~~~
here is an example code.

```
// this area is nested 3-backticks code block.
const hello = "hello";
```

~~~

Note that the fenced code block uses three tildes (~~~), while the nested block of three backticks is preserved.

Escaping may not work.

Some people would first try using character escaping (backslash precedes the escaped character). However, generally escaping is ignored within code blocks. Otherwise, how would you be able to demonstrate how to do escaping within code blocks. Of course, different implementations may act differently with some of these details so YMMV.

4
  • I didn't know tildes works as same as 3-backticks. that is my wanted result. thanks a lot!!
    – mitsuruog
    Commented Mar 14, 2018 at 9:09
  • @waylan Fantastic answer! Helped me tremendously. Can you also help with inline backticks? I need to see a string including enclosing backticks included in a paragraph of my rendered post (e.g. "blah blah some code blah blah". How can I escape those backticks or otherwise make them visible? (I can't even figure out how to display the backticks surrounding 'some code' here on SO; the docs at stackoverflow.com/editing-help#code say double backticks should work but they didn't.) Commented Apr 8, 2018 at 9:07
  • @KeithBennett escaping backticks in code spans is covered clearly in the syntax documentation. You can also find a summary in the answers to How do I escape a backtick ` in Markdown?.
    – Waylan
    Commented Apr 9, 2018 at 15:59
  • The 4 backticks approach works with GitHub Flavored Markdown. 👍
    – Benny Code
    Commented Feb 16, 2023 at 15:00
1

Have you tried:

# Markdown example

```
here is an example code.

\`\`\`
// this area is nested 3-backticks code block.
const hello = "hello";
\`\`\`

```

?

2
  • 1
    aha, that's the bingo!
    – mitsuruog
    Commented Mar 14, 2018 at 0:33
  • i presume you only need to espace one of the three backticks
    – Scover
    Commented Jan 14 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.