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_countFROM users uLEFT JOIN posts p ON u.id = p.author_idWHERE u.created_at > '2024-01-01'GROUP BY u.id, u.name, u.emailHAVING COUNT(p.id) > 5ORDER BY post_count DESCLIMIT 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.
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.
# Install the packagenpm install @charted-data/toolkit
# Run the development servernpm run devMulti-line Commands#
It also handles backslash-escaped multi-line commands gracefully.
docker run -d \ -p 8080:80 \ --name my-app \ my-custom-image:latestDiff 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.
import type { NextConfig } from "next";
const nextConfig: NextConfig = { pageExtensions: ["js", "jsx", "md", "mdx", "ts", "tsx"], images: { remotePatterns: [ { protocol: "https", hostname: "**", }, ], },};
export default nextConfig;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#
Quick Reference#
Features Summary#
Multi-language Support
Line Highlighting
Word Highlighting
Diff Support
Copy Button
This entire post is rendered using Expressive Code, ensuring that your documentation is not only functional but also visually stunning.
Happy Coding!