Code Blocks
Syntax highlighting, titles, line numbers, markers, and diff support
Code Blocks
Code blocks display formatted code with syntax highlighting, powered by Shiki (same as VS Code). This guide covers all the features available.
Basic Syntax
Wrap code in triple backticks with an optional language identifier:
```javascript
const greeting = 'Hello, World!';
console.log(greeting);
```
MarkdownResult:
const greeting = 'Hello, World!';
console.log(greeting);
JAVASCRIPTSupported Languages
Common languages include:
| Language | Identifier |
|---|---|
| JavaScript | javascript or js |
| TypeScript | typescript or ts |
| PHP | php |
| Python | python or py |
| HTML | html |
| CSS | css |
| JSON | json |
| Bash | bash or sh |
| SQL | sql |
| Markdown | markdown or md |
Code Block Titles
Add a filename or title using the title attribute:
```js title="app.js"
import express from 'express';
const app = express();
app.listen(3000);
```
MarkdownResult:
import express from 'express';
const app = express();
app.listen(3000);
Line Numbers
Show line numbers with showLineNumbers:
```js showLineNumbers
const a = 1;
const b = 2;
const c = 3;
```
MarkdownResult:
1const a = 1;
2const b = 2;
3const c = 3;
JavaScriptStart from a specific line with startLineNumber:
```js showLineNumbers startLineNumber=10
// This starts at line 10
const x = 1;
```
MarkdownLine Highlighting
Highlight specific lines using curly braces:
```js {2,4}
const a = 1;
const b = 2; // highlighted
const c = 3;
const d = 4; // highlighted
```
MarkdownResult:
const a = 1;
const b = 2;
const c = 3;
const d = 4;
JavaScriptLine Ranges
Specify ranges with a hyphen:
```js {1-3}
// Lines 1-3 highlighted
const x = 1;
const y = 2;
const z = 3;
```
MarkdownInsert and Delete Markers
Mark lines as added or removed:
```js ins={2} del={4}
const a = 1;
const b = 2; // inserted (green)
const c = 3;
const d = 4; // deleted (red)
```
MarkdownResult:
const a = 1;
const b = 2;
const c = 3;
const d = 4;
JavaScriptText Markers
Highlight specific text within code:
```js "console"
console.log('Hello');
console.error('Error');
```
MarkdownResult:
console.log('Hello');
console.error('Error');
JavaScriptInsert and Delete Text
Mark text as added or removed:
```js ins="log" del="error"
console.log('Hello');
console.error('Error');
```
MarkdownDiff Syntax
Use the diff language for diff-style code:
```diff
+ added line
- removed line
unchanged line
```
MarkdownResult:
added line
removed line
unchanged line
DiffDiff with Syntax Highlighting
Combine diff with language highlighting using lang:
```diff lang="js"
+ const x = 1;
- const x = 2;
const y = 3;
```
MarkdownResult:
const x = 1;
const x = 2;
const y = 3;
DiffCombining Features
Combine multiple features:
```js title="utils.js" showLineNumbers {3-5}
// Utility functions
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}
```
MarkdownResult:
1// Utility functions
2
3export function add(a, b) {
4 return a + b;
5}
6
7export function subtract(a, b) {
8 return a - b;
9}
Inline Code
For inline code within text, use single backticks:
Use the `console.log()` function to debug.
MarkdownResult: Use the console.log() function to debug.
Feature Summary
| Feature | Syntax | Example |
|---|---|---|
| Title | title="filename" |
title="app.js" |
| Line numbers | showLineNumbers |
showLineNumbers |
| Start line | startLineNumber=N |
startLineNumber=10 |
| Highlight lines | {lines} |
{1,3-5} |
| Insert lines | ins={lines} |
ins={2,4} |
| Delete lines | del={lines} |
del={1,3} |
| Highlight text | "text" |
"console" |
| Insert text | ins="text" |
ins="added" |
| Delete text | del="text" |
del="removed" |
| Diff mode | Language diff |
```diff |
| Diff + lang | lang="language" |
lang="js" |
Best Practices
- Always specify the language - Enables syntax highlighting
- Use titles for file references - Helps readers understand context
- Keep code concise - Show only relevant code
- Highlight important lines - Draw attention to key parts
- Use diff for changes - Show what's added/removed clearly
Related Features
- Terminal Output - Display command-line examples
- Annotations - Add explanatory notes to code