Code

Code comes in two forms: Code is for inline code snippets, and CodeBlock for larger chunks.

You can also create code with Markdown, alongside other text: NavigationSplitView.

When using CodeBlock, you can specify a language for your code. If you also include that language in your site’s syntax highlighters, that code will be colored automatically.

For example, here's some Swift code:

CodeBlock(.swift) {
    """
    struct ContentView: View {
        var body: some View {
            Text("Hello, Swift!")
        }
    }
    """
}
struct ContentView: View {
    var body: some View {
        Text("Hello, Swift!")
    }
}

Here's some Python:

CodeBlock(.python) {
    """
    def find_primes_up_to_100():
        primes = []
        for possible_prime in range(2, 101):
            is_prime = True
            for num in range(2, int(possible_prime ** 0.5) + 1):
                if possible_prime % num == 0:
                    is_prime = False
                    break
            if is_prime:
                primes.append(possible_prime)
        return primes
    """
}
def find_primes_up_to_100():
    primes = []
    for possible_prime in range(2, 101):
        is_prime = True
        for num in range(2, int(possible_prime ** 0.5) + 1):
            if possible_prime % num == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(possible_prime)
    return primes

And here's some Ruby for good luck:

CodeBlock(.ruby) {
    """
    def print_mean_average(numbers)
      sum = numbers.sum
      count = numbers.length
      mean = count > 0 ? (sum.to_f / count) : 0
      puts "The mean average is: #{mean}"
    end
    """
}
def print_mean_average(numbers)
  sum = numbers.sum
  count = numbers.length
  mean = count > 0 ? (sum.to_f / count) : 0
  puts "The mean average is: #{mean}"
end

Important: When using syntax highlighting in articles, your site’s syntaxHighlighterConfiguration must include those languages.

Use the highlightedLines() and lineNumberVisibility() modifiers to style your code blocks:

CodeBlock(.swift) {
    """
    struct User {
        let name: String
        let age: Int
        let email: String
        let isActive: Bool
    }
    """
}
.highlightedLines(2, 4)
.lineNumberVisibility(.visible) 
struct User {
    let name: String
    let age: Int
    let email: String
    let isActive: Bool
}

Created in Swift with Ignite

Ignite v0.6.0 · Updated 11/05/26