Markdown Code Blocks: Language Names, Highlighting and Platform Support
How to write fenced code blocks in Markdown, which language names to use (js, ts, bash, terraform, yaml, diff), and what GitHub, Slack, Discord, Notion and docs sites actually highlight.
A fenced code block is three backticks, an optional language name, your code, and three closing backticks. Almost everything else people search for (colors, line highlighting, titles, copy buttons) depends on the renderer, not on Markdown itself. This page covers what is standard, what is an extension, and which platforms do what.
The basic syntax
```python
def greet(name):
return f"Hello, {name}"
```
The word after the opening fence is called the info string. CommonMark does not say what a renderer must do with it, only that the first word is conventionally used as the language, which renderers usually emit as class="language-python" on the <code> element. A highlighter library such as highlight.js, Prism or Shiki then reads that class.
Rules worth knowing:
- Use backticks or tildes (
~~~). The closing fence must use the same character and be at least as long as the opening one. - The opening fence may be indented up to three spaces. Four spaces turns it into an indented code block.
- Without a language the block still renders as monospaced text, just without colors.
- Indented code blocks (four spaces) cannot carry a language at all, so prefer fences.
Inline code
Single backticks make inline code: `npm install`. If the code contains a backtick, wrap it in double backticks and pad with spaces: `` `code` ``. Inline code never gets syntax highlighting.
Language names that work
Highlighters match on a name or an alias. These are safe in nearly every renderer:
| You are writing | Use | Also accepted in most |
|---|---|---|
| JavaScript | js |
javascript |
| TypeScript | ts |
typescript |
| JSX / TSX | jsx, tsx |
|
| Python | python |
py |
| Shell commands | bash |
sh, shell |
| Terminal session with prompts | console |
shell-session |
| PowerShell | powershell |
ps1 |
| JSON | json |
jsonc for comments |
| YAML | yaml |
yml |
| Terraform | hcl |
terraform, tf on some |
| Dockerfile | dockerfile |
docker |
| SQL | sql |
|
| HTML | html |
xml for generic markup |
| CSS / SCSS | css, scss |
|
| Markdown | markdown |
md |
| Plain text, no color | text |
txt, plaintext |
| Changes between versions | diff |
If a language name is unknown to the highlighter, the block falls back to plain text. It does not break. That is why a typo like javscript gives you gray code instead of an error.
A note on Terraform
"markdown terraform code block" is a common search. The most portable choice is hcl, because HCL is the syntax Terraform files use and most highlighters know it by that name. GitHub also recognizes terraform. If you see no colors, switch to hcl.
Showing a diff
The diff language colors lines that start with + green and - red in most highlighters, including GitHub:
```diff
- const timeout = 1000;
+ const timeout = 5000;
```
This is the best way to show a change in a README or pull request description without screenshots.
Titles, line numbers and highlighted lines
None of these are part of Markdown. They are extensions that sit in the info string after the language, and each tool has its own syntax.
| Feature | Docusaurus | VitePress | Astro / Starlight | GitHub |
|---|---|---|---|---|
| Title / filename | title="app.js" |
[app.js] after the language |
title="app.js" |
not supported |
| Highlight lines | {1,3-5} |
{1,3-5} |
{1,3-5} |
not supported |
| Line numbers | showLineNumbers |
:line-numbers |
showLineNumbers |
not supported (file view only) |
Check your site generator's documentation for the exact current syntax. The important point is that the same fence renders as a plain block on GitHub, since GitHub ignores everything after the first word. Keep that in mind if the same file is read in both places.
Where highlighting works
| Platform | Syntax colors | Notes |
|---|---|---|
| GitHub (README, issues, PRs, comments) | Yes | Uses a large language list; unknown names fall back to plain |
| GitLab | Yes | Same fence syntax |
| Discord | Yes, limited | Uses a smaller language set; wrap in triple backticks with a name like js or py |
| Slack | No | Triple backticks give a monospaced block, language names are ignored |
| Notion | Yes | Pick the language from a dropdown on the block; typing a language after the fence in Markdown import maps to it when recognized |
| Jira | Partly | Use {code:java} in the legacy editor or the language picker in the newer one |
| Confluence | Yes | Code macro with a language selector |
| Obsidian | Yes | Standard fences; plugins add more |
| Email clients | No | Usually stripped to plain text |
For Slack and Jira specifically, our Markdown to Slack and Markdown to Jira converters rewrite fences into what those apps expect.
Code blocks inside code blocks
To show a fence inside a fence, make the outer one longer. This page uses four backticks around three to demonstrate examples:
````markdown
```js
console.log("nested");
```
````
The full treatment, including list items and escaping, is in the nested code blocks guide.
Code blocks inside lists
Indent the whole fence to match the list item's text column:
1. Install the package:
```bash
npm install marked
```
2. Import it.
If the fence is not indented enough, the list ends and the numbering restarts. See nested lists in Markdown for the indentation rule.
Why my code block does not render
| Symptom | Likely cause | Fix |
|---|---|---|
| Whole page after the block is monospaced | Closing fence missing or shorter than the opening one | Add the closing fence, same character and length |
| Backticks show literally | Fence is indented four or more spaces, or inside a table cell | Dedent; tables cannot hold fenced blocks, use <pre> or <br> |
| No colors | Unknown language name or the platform does not highlight | Try the table above; check the platform column |
| Line highlight syntax shows as text | Renderer does not support the extension | Remove {1,3} or move to a site generator that does |
$ prompts get copied |
Copy button copies everything | Use console only for sessions; use bash without prompts for commands people will paste |
Content like <div> disappears |
Fence not recognized, so HTML was parsed | Check the fence is at line start with matching backticks |
Checking your Markdown
Paste a page into the Markdown linter to catch unclosed fences, or into the live preview editor to see how it renders. To keep code formatting when converting a document, the Markdown to HTML converter outputs language-* classes you can style with any highlighter.