Base-Blog

Command Palette

Search for a command to run...

4 min readBase Dev

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.

Features#

  • 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.

TypeScript#

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();
}

Python#

from dataclasses import dataclass
from typing import Optional
from datetime import datetime
@dataclass
class User:
id: str
name: str
email: str
created_at: datetime
async def get_user(user_id: str) -> Optional[User]:
"""Fetch a user by their ID."""
response = await fetch(f"/api/users/${user_id}")
if not response.ok:
return None
return User(**await response.json())

JavaScript#

async function getUser(userId) {
const response = await fetch(`/api/users/${userId}`);
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;
}
<div className="flex flex-col gap-4 p-8 bg-gradient-to-br from-purple-500 to-purple-900 rounded-xl shadow-xl">
<h1 className="text-3xl font-bold text-white m-0">
User Name
</h1>
<p className="text-base text-white/80 m-0">
user@example.com
</p>
</div>

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"
}
}
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();
}

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");
}

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

Terminal & Shell Documentation#

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

Terminal window
# Install the package
npm install @base-blog/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);
}

Conclusion#

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 ⚡