Code Rendering Test Article

Code Rendering Test Article

This article tests the rendering of various code blocks in article pages.

Python Example

Here's a Python function that calculates factorial:

PYTHON

1
2    def factorial(n):
3        """Calculate the factorial of a number."""
4        if n < 0:
5            raise ValueError("Factorial is not defined for negative numbers")
6        elif n == 0:
7            return 1
8        else:
9            return n * factorial(n - 1)
10    
11    # Test the function
12    for i in range(6):
13        print(f"{i}! = {factorial(i)}")
    

JavaScript/TypeScript Example

Here's a TypeScript class for a simple counter:

TYPESCRIPT

1
2    class Counter {
3        private value: number = 0;
4        
5        constructor(initialValue: number = 0) {
6            this.value = initialValue;
7        }
8        
9        increment(): void {
10            this.value++;
11        }
12        
13        decrement(): void {
14            this.value--;
15        }
16        
17        getValue(): number {
18            return this.value;
19        }
20        
21        reset(): void {
22            this.value = 0;
23        }
24    }
25    
26    // Usage example
27    const counter = new Counter(10);
28    console.log(`Initial value: ${counter.getValue()}`);
29    counter.increment();
30    console.log(`After increment: ${counter.getValue()}`);
    

Rust Example

Here's a Rust implementation of a simple struct:

RUST

1
2    struct Rectangle {
3        width: u32,
4        height: u32,
5    }
6    
7    impl Rectangle {
8        fn new(width: u32, height: u32) -> Rectangle {
9            Rectangle { width, height }
10        }
11        
12        fn area(&self) -> u32 {
13            self.width * self.height
14        }
15        
16        fn can_hold(&self, other: &Rectangle) -> bool {
17            self.width > other.width && self.height > other.height
18        }
19    }
20    
21    fn main() {
22        let rect1 = Rectangle::new(30, 50);
23        let rect2 = Rectangle::new(10, 40);
24        
25        println!("Rectangle 1 area: {}", rect1.area());
26        println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
27    }
    

SQL Example

Here's a SQL query to find the top 5 users by post count:

SQL

1
2    SELECT 
3        u.id,
4        u.username,
5        u.email,
6        COUNT(p.id) as post_count
7    FROM 
8        users u
9    LEFT JOIN 
10        posts p ON u.id = p.user_id
11    WHERE 
12        u.created_at >= '2023-01-01'
13    GROUP BY 
14        u.id, u.username, u.email
15    ORDER BY 
16        post_count DESC
17    LIMIT 5;
    

Inline Code Examples

You can also use inline code like `console.log("Hello")` or `def hello(): pass`.

Math with Code

Here's a mathematical formula with code:

The formula for compound interest is:

Where:

  • A = final amount
  • P = principal amount
  • r = annual interest rate
  • t = time in years

Here's how to implement it in Python:

PYTHON

1
2    def compound_interest(principal, rate, time):
3        """Calculate compound interest."""
4        return principal * (1 + rate) ** time
5    
6    # Example: $1000 at 5% interest for 3 years
7    result = compound_interest(1000, 0.05, 3)
8    print(f"After 3 years: ${result:.2f}")
    

Conclusion

This article demonstrates that code blocks are properly rendered in articles with syntax highlighting for various programming languages.