Markdown code blocks
A Markdown code block starts with a line of three backticks and ends with another line of three backticks. Add a language name right after the opening backticks, such as python or bash, and most renderers color the code for that language. For a short piece of code inside a sentence, wrap it in single backticks: `npm install`.
```python
def greet(name):
return f"Hello, {name}"
```
Everything inside a code block or inline code is shown exactly as typed. Markdown syntax there is not interpreted.
How do you add syntax highlighting?
Write the language name after the opening fence. CommonMark calls the text after the fence the info string and does not define highlighting itself; renderers conventionally treat its first word as the language, which in HTML output becomes a language- class on the code element.
Common names: bash, shell, javascript or js, typescript or ts, python, json, yaml, html, css, sql, go, rust, java, diff. Which names are recognized depends on the highlighter each app uses, so the full name is the safer choice. Use text or leave the info string empty for output with no highlighting.
diff is handy for showing changes: most highlighters color lines starting with + and - as added and removed.
- timeout: 30
+ timeout: 60
What are the rules for fenced code blocks?
These come from the CommonMark spec:
- A fence is at least three backticks or at least three tildes (
~~~). The two cannot be mixed in one fence. - The closing fence uses the same character and is at least as long as the opening fence.
- A fence with no closing line runs to the end of the document, so a forgotten closing fence turns the rest of the file into code.
- An info string after backticks cannot contain a backtick. After tildes it can.
- A fenced block can start right after a paragraph line; no blank line is needed.
How do you show backticks inside code?
For inline code, wrap it in more backticks than it contains. Two backticks around the text let you show one backtick inside:
Use `` `code` `` to format inline code.
CommonMark strips one space from each side when the content both starts and ends with a space, which is why the example above has spaces inside the double backticks.
For a code block that itself contains a fenced block, such as Markdown documentation, open with four backticks or use tildes. The inner three-backtick lines are then ordinary content:
````markdown
```js
console.log("hello")
```
````
What is an indented code block?
Indenting every line by four spaces also makes a code block. This is the original Markdown style, still part of CommonMark. It has no place for a language name, so there is no highlighting, and it cannot start directly after a paragraph line. Fenced blocks are clearer and are what most people use today.
Paragraph text.
indented code line
another code line
Code blocks in Noteline
Noteline renders fenced code blocks with syntax highlighting as you type. Clicking into a block switches it to editing, with a small bar to change the language and copy the code, and Tab indents inside the block. Cmd+E on a Mac (Ctrl+E on Windows and Linux) wraps the selection in backticks as inline code. A mermaid block renders as a diagram, as the Mermaid page shows.
Common questions
What is the difference between inline code and a code block?
Inline code sits inside a sentence and uses single backticks. A code block is its own block of one or more lines between fences and can carry a language name for highlighting.
Why is my code block not highlighted?
The renderer may not know the language name, or it may not highlight code at all. Check the spelling of the name after the fence and try the full language name.
Can I use tildes instead of backticks?
Yes. ~~~ fences are part of CommonMark and work like backtick fences. They are useful when the code contains backtick fences.