Managing Content

Making our code examples look nice

Summary

In this video, I demonstrate how to add syntax highlighting to a blog using the Goldmark highlighting package, which relies on Chroma - a pure Go syntax highlighter. I show how Chroma supports numerous programming languages out of the box and offers various style options through its style gallery. I walk through the installation process, configuring the highlighting options with features like line numbers, and demonstrate the result using different programming languages (Go, Rust, and JavaScript) and themes like Groovebox and Nord. I conclude by mentioning that in the next video, we'll add error pages before the blog is ready for deployment.

Transcript

Adding syntax highlighting is really straightforward. We basically just need to get this goldmark highlighting package that you see right here And this package relies on another Go package called Chroma If you go in here, you can see Chroma is a general-purpose syntax highlighter in pure Go And it has support for all of these languages. So whatever you want to write in if it's Erlang, PHP, Rust, Kotlin Java, you know, SIG, you can do it So all of these languages are supported out-of-the-box and what is also really convenient is if you jump into this Chroma style Gallery, you can see all the different styles and color schemes that you can add to your code examples on your blog So Cappuccino is a really popular one. We're gonna use the one called Groovebox But all of these you can just add and pick and choose the one you like the most So this is exactly what we need. So go back into goldmark highlighting and grab this installation line right here Jump into editor Let's go get And this one also adds Chroma, so basically we just need to add this to our extensions in our articles package Go in here You see on the new and then we just add more to the With extensions options and I'm just gonna format it so it's a little bit easier to read right and I want to just quickly Put this in so we can refer to them as high highlighting new Highlighting there we go And this one didn't take again Highlighting with style and you use groove Box, there we go And again, I misspelled this Yes This is correct. Let me just jump up and get it from here Go and there we go And final thing we need to say highlighting with format options And then say chroma HTML With line numbers set to true Give that a save Put a comma not an M and new highlighting And that is all there is to it So now if you go back into our browser and jump into our blog give this a refresh Go into read article and go down to our code example, you can see now we have line numbers We have syntax highlighting. We have groove box the groove box theme Being applied. So this is looking really really good and just to show you that if we were to jump into our content here and let's say article two and Instead of only having a go example. We will also have it's just that maybe Rust example and JavaScript example And we don't want to use let's say we don't want to use groove box anymore we want to use that was one called Nord So we just changed it here Update our development server, so I pick up the the file changes Go in here Refresh now this example is in the Nord color scheme But if we go into our second article here, you can see that we have first the Golang one then the Rust one and then the JavaScript one all being properly highlighted with With line numbers. So so now we have all the core components, right? The final thing I want to do is to add error pages so that we just show Something went wrong and and please go back to the home page or whatever. So with that in place We're gonna fix that in the next video and then basically our blog is ready to deploy if we wanted to

Early Access

$95 $65 USD

Get access

Invoices and receipts available for easy company reimbursement