How to display Swift code in your Medium article with syntax highlighting

Mark van Wijnen
2 min readAug 27, 2023

I like to write articles about Swift and the Apple frameworks and I’ve been doing so since 2020. The thing that annoyed me the most when writing those articles was that there was no easy way to insert code with syntax highlighting on Medium. So you had to use third-party solutions, such as Github gists, to accomplish that.

I choose another approach and used Carbon to create source code images. It allowed me to use my own color palette and made sure the source code was always fully visible on screen on mobile and desktop. The drawback, however, was that reader couldn’t easily copy the code.

Now we are at the end of 2023. I’ve been ignoring the plain Medium code block the whole time as it had no use to me and continued using source code images. Little did I know that as of November 10th, 2022 Medium introduced syntax highlighting in code blocks (Code blocks with syntax highlighting by Alex Benzer).

To help create more awareness of this great feature I decided to dedicated this article to it in the hopes more technical writers here on Medium will notice and hopefully are just as surprised and excited as I am right now.

How to add code blocks with highlighted syntax

--

--

Mark van Wijnen

macOS/iPadOS/iOS/watchOS/visionOS developer and SwiftUI enthousiast. “Stay Hungry, Stay Foolish!” — Steve Jobs