Blog

Thoughts from my daily grind

Site Migrated to Ruby Rouge - Don't HighlightJS in Rails

Posted by Ziyan Junaideen |Published: 28 June 2020 |Category: General
Default Upload |

I have been using Highlight JS for a very long time. Since then as a habit I have used it for every version of code highlight in my personal projects. Recently I noticed some buggy highlighting issues with Highlight JS. That is when I wondered if there was a Rails side of handling code highlighting, and obviously as RubyOnRails always has a gem for it, there is!

Implementaiton Details: here

The problem

Highlight JS is a JavaScript library the executes in the client side. This kind of makes the page load a little sluggish not only in mobiles but also in computers. But that was some thing I could live with.

The issue I had was with Ruby code. I noticed that some times the code doesn't highlight at all. Some times some code blocks highlight but others don't. This doesn't happen always, but when it does, its irritating.

Processing the code server side allows the client side to execute less JavaScript. I can cache the page (partials actually) in Redis to make sure they only get rendered when necessary (like after an update). That will be more "green".

Options

I did some research this evening and noticed that there were 3 leading projects and some others that didn't seem to be worth the attention.

  • coderay
  • rouge
  • albina

Coderay

Coderay is a very old project. I'd heard about it, but I have never used it. When I looked in to its Github I found almost 100 issues and 30 pull requests. That is not too bad for me, but I kept searching.

Rouge

Then I landed on Rouge. The name sounded familiar. That is when I found out that it was the default code highlight tool in Jekyll which my other blog is based on. But it had more issues and open PRs. Open pull requests are a good thing, I mean people are contributing. But issues is a little concerning. But if its good for Jekyll guys, should be good for me right?

Albino

This is the first one I landed on. That is because I knew it was the one used by Github at least in my early Ruby days. How ever it was sad to see that the project was discontinued and there was no release in almost 3 years.

I looked for the new code-highlighter used by Github but couldn't in a short search. Its wise not to use this gem.

My setup

My final decision was based on my setup. I am using markdown to write my posts. We all can agree that markdown is cool right? Then I use redcarpet by VGM to process it.

When I did a search I found a really elegant way to integrate Rouge with Redcarpet. Half an hour later, www.jdeen.com and few of my other projects were shifted to Rouge.

Read this for its implementation details.

Tags
About the Author

Ziyan Junaideen -

Ziyan is an expert Ruby on Rails web developer with 8 years of experience specializing in SaaS applications. He spends his free time he writes blogs, drawing on his iPad, shoots photos.

Comments