The Problem
When authoring articles using Markdown that include Markdown special characters in the content, questions can come up about how to escape those special characters.
For instance, this article about updating Hugo posts with code syntax highlighting includes Markdown code fences within code fences, which required figuring out how to escape code fences within code fences.
The Process
A web search quickly led to this helpful article, which explained the options for escaping code fencing.
The Solution
Markdown provides two sets of special characters for escaping code fences:
````
, that is, four backticks~~~
, that is, three tildes
Example 1: One nested code fence
To display one nested code fence within a code block (like in the examples in the above linked code syntax highlighting article) use one set of the special characters.
Example 1 display output
```toml
<code block>
```
Example 1 Markdown input
````markdown
```toml
<code block>
```
````
Example 2: Two nested code fences
To display two nested code fences within a code block (like that shown in the Markdown input in example 1 above) alternate the two sets of special characters.
Example 2 display output
````markdown
```toml
<code block>
```
````
Example 2 Markdown input
~~~markdown
````markdown
```toml
<code block>
```
````
~~~
Example 3: Three nested code fences
The first two examples are explained in the linked article. Some additional testing indicates that it is possible (at least in Visual Studio Code) to produce three nested code fences within a code block by appending text to the inner closing code fence(s).
The Markdown input in example 2 was produced using this technique. This was done using an invisible character, specifically the Left-To-Right Mark.
Example 3 display output (with invisible characters)
~~~markdown
````markdown
```toml
<code block>
```
````
~~~
Example 3 Markdown input (with invisible characters)
````markdown
~~~markdown
````markdown
```toml
<code block>
```
````
~~~
````
Because the Left-To-Right Mark has no visual representation it’s not obvious what is happening, so let’s replace it with an X in the following examples to demonstrate the technique.
Example 3 display output (with visible characters)
~~~markdown
````markdown
```toml
<code block>
```
````X
~~~
Example 3 Markdown input (with visible characters)
````markdown
~~~markdown
````markdown
```toml
<code block>
```
````X
~~~X
````
Example 4: Arbitrary N nested code fences
In this way, it appears possible to produce an arbitrary number of nested code fences within a code block by appending text to the inner closing code fences.
Example 4 display output (with invisible characters)
````markdown
~~~markdown
````markdown
```toml
<code block>
```
````
~~~
````
Example 4 Markdown input (with invisible characters)
~~~markdown
````markdown
~~~markdown
````markdown
```toml
<code block>
```
````
~~~
````
~~~
Example 4 display output (with visible characters)
````markdown
~~~markdown
````markdown
```toml
<code block>
```
````X
~~~X
````
Example 4 Markdown input (with visible characters)
~~~markdown
````markdown
~~~markdown
````markdown
```toml
<code block>
```
````X
~~~X
````X
~~~
Example 5: Arbitrary N nested code fences without alternating special character sets
In fact, it appears possible to produce an arbitrary number of nested code fences within a code block without alternating the escape character sets or using syntax highlighting language tags.
Example 5 display output (with invisible characters)
````
````
````
```toml
<code block>
```
````
````
````
Example 5 Markdown input (with invisible characters)
````
````
````
````
```toml
<code block>
```
````
````
````
````
Example 5 display output (with visible characters)
````X
````X
````X
```toml
<code block>
```
````X
````X
````X
Example 5 Markdown input (with visible characters)
````X
````X
````X
````X
```toml
<code block>
```
````X
````X
````X
````X