New syntax highlighter: PrismJS

Who would have thought that this blog would live long enough to see a third change of syntax highlighter? Incredibly enough, Blogger is also alive and kicking. This site's administration has decided to retire highlightjs, which seems to be still alive, but has lost ground to prismjs. Obviously, it's subjective, but personally I have decided to favor Prism due to the following reasons:

  • It managed to handle VB.NET code blocks better. Yes, I know it's not the most modern language, but we still hold some affection for it.
  • It looks better: it's more compact, with a better color selection. It also highlights more keywords, at least for the languages we use here.
  • It offers an extensible architecture, including a plugin system which allows, for example, adding line numbers. HighlighJS has no such feature.
  • All of these extra features, and still Prism has a very small footprint.

Without further ado, let's jump into the steps for using Prism in Blogger:

  1. In the Blogger dashboard, once the blog to modify has been selected, go to Theme, find the dropdown in the Customize button, and click Edit HTML.
  2. Get CDN links for a specific version of Prism from, for example, cdnjs. Asides from one .css file and one .js file for Prism, I suggest including a .js file for Prism's autoloader plugin. By default, Prism loads support for 6 basic languages. The autoloader plugin detects usages for other languages and automatically loads their definitions. In my case, at the time of writing, the current Prism version was 1.29.0, so I added this to html>head>meta:
    <link crossorigin='anonymous' href='https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-okaidia.min.css' integrity='sha512-mIs9kKbaw6JZFfSuo+MovjU+Ntggfoj8RwAmJbVXQ5mkAX5LlgETQEweFPI18humSPHymTb5iikEOKWF7I8ncQ==' referrerpolicy='no-referrer' rel='stylesheet'/>
    <script crossorigin='anonymous' integrity='sha512-7Z9J3l1+EYfeaPKcGXu3MS/7T+w19WtKQY/n+xzmw4hZhJ9tyYmcUS+4QqAlzhicE5LAfMQSF3iFTK9bQdTxXg==' referrerpolicy='no-referrer' src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js'/>
    <script crossorigin='anonymous' integrity='sha512-SkmBfuA2hqjzEVpmnMt/LINrjop3GKWqsuLSSB3e7iBmYK7JuWw4ldmmxwD9mdm2IRTTi0OxSAfEGvgEi0i2Kw==' referrerpolicy='no-referrer' src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js'/>
  3. The only thing that Prism can't do and HighlightJS can is automatic language detection, so the last step is to find all the pre-code blocks and inside the code tag, add the class="language-xxx" attribute. The language prefix is mandatory, per the HTML5 standard. The xxx suffix will indicate which language to use. The list of supported languages is here.

Comments

Popular posts from this blog

VB.NET: Raise base class events from a derived class

Apache Kafka - I - High level architecture and concepts

Upgrading Lodash from 3.x to 4.x