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);
```
Markdown

Result:

const greeting = 'Hello, World!';
console.log(greeting);
JAVASCRIPT

Supported 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);
```
Markdown

Result:

app.jsJavaScript
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;
```
Markdown

Result:

 1const a = 1;
 2const b = 2;
 3const c = 3;
JavaScript

Start from a specific line with startLineNumber:

```js showLineNumbers startLineNumber=10
// This starts at line 10
const x = 1;
```
Markdown

Line Highlighting

Highlight specific lines using curly braces:

```js {2,4}
const a = 1;
const b = 2;  // highlighted
const c = 3;
const d = 4;  // highlighted
```
Markdown

Result:

const a = 1;
const b = 2;
const c = 3;
const d = 4;
JavaScript

Line Ranges

Specify ranges with a hyphen:

```js {1-3}
// Lines 1-3 highlighted
const x = 1;
const y = 2;
const z = 3;
```
Markdown

Insert 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)
```
Markdown

Result:

const a = 1;
const b = 2;
const c = 3;
const d = 4;
JavaScript

Text Markers

Highlight specific text within code:

```js "console"
console.log('Hello');
console.error('Error');
```
Markdown

Result:

console.log('Hello');
console.error('Error');
JavaScript

Insert and Delete Text

Mark text as added or removed:

```js ins="log" del="error"
console.log('Hello');
console.error('Error');
```
Markdown

Diff Syntax

Use the diff language for diff-style code:

```diff
+ added line
- removed line
  unchanged line
```
Markdown

Result:

 added line
 removed line
 unchanged line
Diff

Diff with Syntax Highlighting

Combine diff with language highlighting using lang:

```diff lang="js"
+ const x = 1;
- const x = 2;
  const y = 3;
```
Markdown

Result:

 const x = 1;
 const x = 2;
 const y = 3;
Diff

Combining 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;
}
```
Markdown

Result:

utils.jsJavaScript
 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.
Markdown

Result: 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

  1. Always specify the language - Enables syntax highlighting
  2. Use titles for file references - Helps readers understand context
  3. Keep code concise - Show only relevant code
  4. Highlight important lines - Draw attention to key parts
  5. Use diff for changes - Show what's added/removed clearly

Back to Markdown Features