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: The configuration for this site includes all three languages as highlighters.

Created in Swift with Ignite