loading...
Cover image for How to add code highlighting to your Dev.to posts.

How to add code highlighting to your Dev.to posts.

hoverbaum profile image Hendrik ・1 min read

The simple truth of the matter is that:

const turorialFunction = (name) => {
  console.log(`Hello ${name}`)
}

does look way nicer than:

const turorialFunction = (name) => {
  console.log(`Hello ${name}`)
}

when writing a post here on Dev.to.


How to do it

Dev.tos posts are based on Markdown. Within Markdown we can use identation or so called Code Blocks to specify sections of code. The later ones are indicated using ```. Read more about this in this cheatsheet.

Using the three ` variant we can also specify a language for the code block. A lot of tooling build on top of Markdown utilized this characteristic to implement richer features. But the simplest of them is code highlighting. The above nicely colored code snippet is achieved by starting the code block with ```javascript.

The full example for the above would be:

```javascript
const turorialFunction = (name) => {
  console.log(`Hello ${name}`)
}
```

And if you are now wondering how the hell I got that to display:

<pre>
```javascript
const turorialFunction = (name) => {
  console.log(`Hello ${name}`)
}
```
</pre>

and the inline code is: <code>&#96;&#96;&#96;</code>.


Here is where my explanations stop and your colorful posts start.

The list of supported languages is impressive, though not all encompassing (check comments).

Posted on by:

hoverbaum profile

Hendrik

@hoverbaum

JavaScript enthusiast and developer for fun 👨‍💻

Discussion

markdown guide
 

Does anyone know if dev.to supports highlighting lines of code or combining the diff highlighter with a language highlighter?

My use case is to draw attention to new or update lines of code when I write tutorials.

Something like this:

Javascript syntax highlighting

I know we can use diff but I can't find a way to combine that with code highlighting:

function hello() {
-  alert("Hello!");
+  alert("Hi!");
}
 

I am also trying to figure out how to do this - did you ever find an answer?

 
 

I did a quick search in the code: dev.to uses the Redcarpet Ruby library to parse Markdown with rouge which does the highlighting part.

The list of lexers is impressive:

Dart:

void main() {
  print('Hello, World!');
}

Julia:

println("hello world")

No Cobol though :D

You can find the code in devto here and here and here

 

Also shoutout for the "how I got this to display" part because I sure WAS wondering how you got the literal ```javascript to display

 

yeah, that's great, I would have used screenshots :D

 

Glad that part is helping 😁

 

In theory, yes (although I try to resist using non-standard Markdown features).

In practice, the list of languages seems to be arbitrarily chosen:

DISPLAY 'COBOL code does not work.' WITH NO ADVANCING.
 

Ahh interesting aspect. I will make sure to mention that above. Haven't looked into the fundamentals of dev.to but I guess they are using a code highlighter somewhere and are only including a limited amount of plugins, as in language support, to keep bundle size down.

Maybe a good candidate for improvement 👍

 

Thanks mate! Exactly what I was looking for!

 

Hi @hendrik ,
Is there a way that I can change the background colour of the code block from black to white? Like theming or something?

 

I don't think there is. The code highlight is basically a markdown feature and how it looks is defined by dev.to globally. But providing a theme to use for code blocks could be a cool feature for the Frontmatter, I agree.

 
 

shell and console don't seem to add much flavor. I was hoping at least for some #comment coloring...

 

post was clutch. do you know which Markdown lib dev.to is using?

 

Thank you very much, the <pre> part was a just want I needed.

 
 

Any clue how to add a [copy code] to the code block? I recognize this isn't standard markup, but hoping they added it somewhere and/or will add it. wink wink

 

Haven't heard anything about a feature like this. But sounds useful 👍

 
 

I've found that "pre" doesn't color the code and it should instead be "code" now.

 

Thank you, mister! 🤓

 

Is there a way to pick a different set of colours?
Just wondering. It looks quite ugly some times.

 

I remember someone made their hyperlink or a tag on the blog to be pink ish with yellow background. I've been trying to figure it out. Does anyone has idea how to do it?