87

GitHub-flavored markdown supports syntax highlighting in codeblocks. This is done by adding the name of the language next to the triple-grave codeblock markers:

```ruby
require 'redcarpet'
markdown = Redcarpet.new("Hello World!")
puts markdown.to_html
```

Standard markdown also supports inline codeblocks by wrapping text in `single graves`. Is there any way to add syntax highlighting to these inline codeblocks?

1
  • Assuming by "standard markdown," you mean CommonMark, the standard actually doesn't say anything about syntax highlighting. It just specifies how you can indicate something contains code. Commented Jun 19, 2023 at 16:55

3 Answers 3

64

GitHub comments, wikis, README.md etc. use GFM, essentially CommonMark with some extensions, where it's not possible to add syntax highlighting. (Follow the link to see whether anything has changed, but don't hold your breath because nothing has in the last decade.)

However, GitHub Pages uses Jekyll and by extension kramdown where you can use:

`x = 4`{:.ruby}

P.S. If you happen to use pandoc, the syntax is:

`x = 4`{.ruby}
0
-2

I had to do

`(inline code)`{:.language-clojure .highlihgt}

for it to work, you have to add .highlight class too. That only applies to Jekyll with kramdown.

-3

Yes, it is possible with Github Markdown:

I needed to do the same with an XML-structure inside a table row (in my case defined with markdown, nit html, so with | | delimitiers for the table structure)

just put the desired Style-class (in my case .language-xml) within the Structure {: }

`<xml attrib="someVal"></xml>`{:.language-xml}

this will set the style of the table cell to highlight the code according to the set style class

i guess for ruby it then will just be

{:.language-ruby}

took me over an hour to figure out, found the solution nowhere, so i thought i'd post it here if anyone stumbles upon this problem again.

3
  • This solution doesn't work for me, at least when commenting on github issues.
    – iFreilicht
    Commented Jun 21, 2017 at 11:31
  • 1
    to be honest, i never tried to do it in Github comments. i've done it with GuitHub flavoured Markdown and github pages, and there it works. i've tried it just now on github issues. I couldn't find a fast way to make it work for github commenting... sorry that i could not help Commented Jun 22, 2017 at 11:44
  • 3
    Doesn't work for me. I tested in both Gist and a README.md.
    – pchaigno
    Commented Aug 14, 2017 at 11:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.