Code Highlighting Test
This article is used to test the new code highlighting feature, including syntax highlighting, copy button, language display, etc.
JavaScript
JAVASCRIPT
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
const result = fibonacci(10);
console.log(`The 10th Fibonacci number is: ${result}`);
// Async/Await
const asyncFunction = async () => {
try {
const response = await fetch('/api/data');
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching data:', error);
}
};
Codeblock with Line Numbers
PYTHON
1# Python with line numbers
2import asyncio
3from typing import List, Optional
4
5class DataProcessor:
6 def __init__(self, data: List[dict]):
7 self.data = data
8
9 def process(self) -> Optional[dict]:
10 """Process the data and return the result"""
11 if not self.data:
12 return None
13
14 result = {
15 'total': len(self.data),
16 'processed': []
17 }
18
19 for item in self.data:
20 if self.validate_item(item):
21 result['processed'].append(item)
22
23 return result
Highlighting Specific Lines
GO
1package main
2
3import "fmt" // This line will be highlighted
4
5func main() {
6 message := "Hello, World!" // This line will also be highlighted
7
8 fmt.Println(message) // This line will also be highlighted
9
10 for i := 0; i < 3; i++ {
11 fmt.Printf("Count: %d\n", i)
12 }
13}
Codeblock with Filename
api.ts
// TypeScript API
interface ApiResponse<T> {
data: T;
status: number;
message: string;
}
interface User {
id: number;
name: string;
email: string;
avatar?: string;
}
class ApiClient {
private baseURL: string;
private headers: Record<string, string>;
constructor(baseURL: string, apiKey?: string) {
this.baseURL = baseURL;
this.headers = {
'Content-Type': 'application/json',
...(apiKey && { 'Authorization': `Bearer ${apiKey}` })
};
}
async get<T>(endpoint: string): Promise<ApiResponse<T>> {
const response = await fetch(`${this.baseURL}${endpoint}`, {
method: 'GET',
headers: this.headers,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
async post<T>(endpoint: string, data: any): Promise<ApiResponse<T>> {
const response = await fetch(`${this.baseURL}${endpoint}`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(data),
});
return response.json();
}
}
const client = new ApiClient('https://api.example.com', 'your-api-key');
async function getUsers(): Promise<User[]> {
try {
const response = await client.get<User[]>('/users');
return response.data;
} catch (error) {
console.error('Error fetching users:', error);
return [];
}
}
Plain Text Codeblock
PLAINTEXT
This is a plain text codeblock.
It should not have syntax highlighting.
You can test the copy functionality here.
function test() {
console.log("This is a test.");
}
Inline Code
This is an inline code example:const x = 42;
and npm install
and git commit -m "update"
.
Comments