NanoFile

Markdown code blocks

One backtick either side gives inline code. Three backticks on their own line open and close a block, and a language name after the opening fence turns on highlighting.

Inline
Run `npm install` first.

Run npm install first.

Fenced block with a language
```ts
export function render(source: string) {
  return marked.parse(source);
}
```
export function render(source: string) {
  return marked.parse(source);
}
Indented block, the older style
    four spaces of indent
    also makes a code block
four spaces of indent
also makes a code block

Works, but you cannot set a language, and it breaks the moment someone reformats the file.

Showing backticks inside code
``a literal ` backtick``

````
```
A fenced block inside a fenced block
```
````

a literal ` backtick

```
A fenced block inside a fenced block
```

Use more backticks on the outside than appear on the inside.

What usually goes wrong

  • The closing fence must have at least as many backticks as the opening one, and nothing else on the line.
  • Inside a list, a fenced block has to be indented to line up with the item's text or it ends the list.
  • A language name is free text. An unknown one is not an error, it simply gets no colours.
  • Tildes work as a fence too (~~~), which is handy when the code itself is full of backticks.

Where it behaves differently

PlatformBehaviour
GitHubAround 500 languages, plus diff, mermaid and math as special cases.
DiscordFenced blocks with a language work and are highlighted.
SlackFenced blocks work; the language name is ignored.

Related