commonmark

How can I show how to make code block in Markdown within a code block?


I tried this:

```text
```bash
```
```

but in CommonMark (see http://spec.commonmark.org/dingus/?text=%60%60%60text%0A%60%60%60bash%0A%60%60%60%0A%60%60%60) it doesn't work.

What I want

<pre><code data-sourcepos="1:1-3:7" class="language-text">```bash
```
</code></pre>

What I get

<pre><code data-sourcepos="1:1-3:7" class="language-text">```bash
</code></pre>
<pre><code data-sourcepos="4:1-4:3"></code></pre>

Solution

  • Simply create an outer code block with more backticks than the inner one:

    ````text
    ```bash
    ```
    ````
    

    commonmark.js dingus

    From the CommonMark spec:

    A code fence is a sequence of at least three consecutive backtick characters (`) or tildes (~). (Tildes and backticks cannot be mixed.) A fenced code block begins with a code fence, indented no more than three spaces.

    [...]

    The content of the code block consists of all subsequent lines, until a closing code fence of the same type as the code block began with (backticks or tildes), and with at least as many backticks or tildes as the opening code fence.

    (Emphasis mine.)