Charted Data.

Command Palette

Search for a command to run...

6 min readCharted Data

Advanced MDX Syntax and Code Highlighting

Explore the advanced syntax highlighting capabilities of this blog template. Includes line numbers, line highlighting, token highlighting, and diff support.

This post explores the professional code highlighting features available in this template. Whether you're writing tutorials, documentation, or technical articles, these features ensure your code examples are clear and expressive.

What You'll Learn#

šŸ’» Multi-Language Support

Syntax highlighting for dozens of languages

šŸŽØ Line Highlighting

Draw attention to specific code lines

āœļø Word Highlighting

Highlight specific terms or phrases

šŸ“ Diff Support

Show code changes clearly

šŸŽÆ Custom Themes

Dark and light theme support

šŸ“‹ Copy Button

One-click code copying


Language Support#

The syntax highlighter supports dozens of programming languages. Use the CodeTabs component to display multiple variations side-by-side.

TypeScript, JavaScript, and Python#

interface User {
id: string;
name: string;
email: string;
createdAt: Date;
}
async function getUser(id: string): Promise<User | null> {
const response = await fetch(`/api/users/${id}`);
if (!response.ok) {
return null;
}
return response.json();
}

React/JSX#

import { useState, useEffect } from 'react';
export function UserProfile({ userId }) {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
async function fetchUser() {
try {
const response = await fetch(`/api/users/${userId}`);
const data = await response.json();
setUser(data);
} finally {
setLoading(false);
}
}
fetchUser();
}, [userId]);
if (loading) return <div>Loading...</div>;
if (!user) return <div>User not found</div>;
return (
<div className="user-profile">
<h1>{user.name}</h1>
<p>{user.email}</p>
</div>
);
}

CSS & Tailwind#

.user-profile {
display: flex;
flex-direction: column;
gap: 1rem;
padding: 2rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 12px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
}
.user-profile h1 {
font-size: 2rem;
font-weight: 700;
color: white;
margin: 0;
}
.user-profile p {
font-size: 1rem;
color: rgba(255, 255, 255, 0.8);
margin: 0;
}

SQL#

SELECT
u.id,
u.name,
u.email,
COUNT(p.id) as post_count
FROM users u
LEFT JOIN posts p ON u.id = p.author_id
WHERE u.created_at > '2024-01-01'
GROUP BY u.id, u.name, u.email
HAVING COUNT(p.id) > 5
ORDER BY post_count DESC
LIMIT 10;

JSON & YAML#

{
"name": "blog-template",
"version": "1.0.0",
"dependencies": {
"next": "^16.0.0",
"react": "^19.0.0",
"tailwindcss": "^4.0.0"
},
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}

Advanced Formatting#

Code Block Titles#

Adding a title (like a filename) to your code block provides context and makes it look like a real IDE window.

api/users.ts
export async function getUsers() {
const res = await fetch('https://api.example.com/users');
return res.json();
}
// How to write:
\```typescript title="api/users.ts"
export async function getUsers() { ... }
\```

Line Highlighting#

You can highlight single lines, ranges, or multiple non-consecutive lines using the {} syntax.

import { db } from "./database";
export async function updateUser(id: string, data: any) {
// Use a transaction for safety
return await db.transaction(async (tx) => {
await tx.update(users).set(data).where(eq(users.id, id));
});
console.log("User updated successfully");
}
// How to write:
\```typescript {1, 4-6, 9}
...
\```

Word Highlighting#

Highlight specific words or phrases. Use ins and del to mark additions/removals within a specific line.

const response = await fetch('/api/data');
const data = await response.json();
const result = process(newData); // Changed from oldData
// How to write:
\```javascript ins="newData" del="oldData"
const result = process(newData);
\```

Terminal & Shell Documentation#

The highlighter automatically detects shell prompts and provides a clean "Copy Terminal" experience.

Terminal window
# Install the package
npm install @charted-data/toolkit
# Run the development server
npm run dev

Multi-line Commands#

It also handles backslash-escaped multi-line commands gracefully.

Terminal window
docker run -d \
-p 8080:80 \
--name my-app \
my-custom-image:latest

Diff Highlighting#

Show code changes with diff syntax for a detailed view of additions and removals.

// Before
function oldApproach(data) {
return data.map(item => item.value);
}
// After
function newApproach(data) {
return data
.filter(item => item.isValid)
.map(item => item.value);
}

Configuration Comparisons#

Let's compare configuration files using our Compare component.

Next.js Config
next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
pageExtensions: ["js", "jsx", "md", "mdx", "ts", "tsx"],
images: {
remotePatterns: [
{
protocol: "https",
hostname: "**",
},
],
},
};
export default nextConfig;
Vite Config
vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
build: {
outDir: 'dist',
},
server: {
port: 3000,
},
});
// How to write:
<Compare labels={["Label 1", "Label 2"]}>
\```typescript
// Side 1
\```
---
\```typescript
// Side 2
\```
</Compare>

Common Code Patterns#

Use async/await in Server Components.


Quick Reference#


Features Summary#

1

Multi-language Support

Dozens of languages supported out of the box with beautiful syntax highlighting.
2

Line Highlighting

Draw attention to specific lines with {1-3} syntax.
3

Word Highlighting

Highlight specific words or phrases with quote syntax.
4

Diff Support

Show code changes clearly with diff blocks.
5

Copy Button

Easy one-click copying for all code blocks.

This entire post is rendered using Expressive Code, ensuring that your documentation is not only functional but also visually stunning.

Happy Coding!

Download This Boilerplate ⚔