Use Custom Liquid Tags and Highlight Code Blocks in Your Blog
Tags: productivity, javascript, webdev, showdev
Published: 2021-04-04 16:22:00

---
title: Use Custom Liquid Tags and Highlight Code Blocks in Your Blog
published: true
description: An instruction manual on how to add highlighting to code blocks and adding rich content with custom liquid tags to your blog's editor.
tags: productivity, javascript, webdev, showdev
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86nawllei0mi226c878b.jpg
canonical_url: https://jamesinkala.com/blog/use-custom-liquid-tags-and-highlight-code-blocks-in-your-blog
---

I know that there are plenty of automated options for websites and blogs out there, templates, plugins and the lot but I guess that part of being a developer is taking over challenges and overcoming them, in the process learning plenty of new stuff, understanding where your weaknesses lie so that you can focus on that and advance incrementally.

## An Update To My Personal Blog ##
Personally, I've always wanted to build my portfolio/personal website from the ground up, every aspect of it, from the design to everything that goes on in the back-end.

So, this week besides other things I decided to ramp up the looks of [my website's blog](https://jamesinkala.me/blog), part of a bigger overhaul of the design that has been in plan for some time.

After completing this task, more than just wanting to get a bit of feedback from those who may care to give that, I also wanted to share what this update was from a developer's scope for any other person who'd be interested to do this on their own.

I think this is a sound implementation for whoever cross-posts their articles with dev.to and would like to do that without the need to modify their article's content.

## The Challenges ##
I can treat the challenges as either ___issues___ that needed fixing or ___features___ that needed implementing, depending on how you look at them. Here on-wards I'll be primarily calling them features, thus there were 3 main features that needed implementing.

Before going into each individual feature I would like to point out the major change that happened.
Since I work with a markdown editor that I talked about in a [previous post](https://dev.to/xinnks/making-a-markdown-editor-for-your-vue-blog-with-front-matter-support-5cep) I used to store my article body in markdown format inside the database and when viewing the article I would then convert the markdown to html before displaying it to the page, this was bad implementation since that meant I used up more resources than I needed to on every page view.

So to fix that, in the current implementation while storing the article data in the database I process the markup first and create the necessary rich content embeds from the liquid tags then store the resulting html as one of the properties of the blog post alongside the raw markdown that I later use when updating my article.

Let's proceed with the features.

### 1. Code Highlighting ###
Initially the post details page as illustrated below contained code blocks that I styled manually to reflect the site's colors, but there is so much you can do with CSS and styling html code blocks, so I wasn't satisfied with that look and was looking forward to make some changes.

![Design pre-code block highlighting with highlightjs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azoj05fejcf9rmgvv9pv.png)

#### The Solution ####
I decided to use [marked](https://marked.js.org/) to parse the markdown code and [highlightJs](https://highlightjs.org) to highlight code blocks, both their documentations come quite handy when trying to accomplish this task.

Here are the instructions.

On your project start by installing highlightjs and marked
```sh
$ npm i highlightjs marked
```
Then initialize them utilizing highlightjs' core library:
```js
const hljs = require('highlight.js/lib/core')
const marked = require('marked')
```

Since you would want to work with a small footprint as possible I'd advise that you use higlightjs by loading just the libraries you need, for example when trying to highlight a Javascript code block just load the library responsible for highlighting Javascript - `hljs.registerLanguage('javascript', require('highlight.js/lib/languages/javascript'))`.

Given that you might write a blog post having a number of code blocks not necessarily of the same language the following custom marked renderer can help you cover that while still keeping a small footprint:
```js
// code higlighter
const renderer = { 
  code( code, infostring, escaped){
    let lang = hljs.highlightAuto(code); // get language
    hljs.registerLanguage(lang.language, require('highlight.js/lib/languages/'+ lang.language));
    let compiled = hljs.highlight(code, {language: lang.language}).value;
    return `<pre class="hljs"><code class="language-${lang}">${compiled}</code></pre>`
  }
}
```

Finalize by using the renderer on the initiated marked instance:
```js
marked.use({ renderer });
```

Afterwards you can proceed to parsing your markdown resulting to html containing code blocks ready to be highlighted by highlightjs' stylesheets.
Use one of the CSS flavours provided by highlightjs to highlight your code blocks [as per your setup](https://highlightjs.org/usage).

Then to get the final rendered HTML with the highlightjs CSS classes parse your markdown with markup:
```js
const finalHtml = markup(articleMarkdown);
```

And Voilà!
![Highlighted code blocks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vsj5ucw7sc1w6znyvv6.png)

### 2. Liquid Tags ###
Before implementing this feature the following is what my article page would display:
![Before processing liquid tags](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8q4jloi1hlg7wsvjyivo.png)
Naked liquid template code.

And its shameful that I took this long to work on this, leaving my blog articles this way.

Let's get beyond my shame.

I wanted to display rich content just as you have on dev.to article pages from their custom liquid tags, the dev.to rich content embeds design was my inspiration for the final design I created.

#### The Solution ####
I used [liquidjs](https://github.com/docs/liquid) - a Node.js port of the Liquid template engine to help me accomplish this.
All you need to do is create custom tag classes that extend `Liquid.Tag` from the liquidjs library. Let me show you an example custom tag that gives us a dev.to post embed.

##### The Dev.to Article Embed #####
Here, you'll be using the dev.to Community API to get the data needed to enrich the embeded content, you'll need to create a DEV Community API key.

Head over to the dev.to settings and navigate to _accounts_:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3azzo29wgp9iq5etc5w.png)

Then proceed to generating an API key:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4sdzkk4h9l32amuncuh.png)

Next, you'll need to install  the _liquid_ package, and _axios_ package which will help us perform HTTP requests to the API.
```sh
$ npm i liquid axios
```
Then, initialize liquid and it's engine:
```js
const axios = require('axios')
const Liquid = require('liquid')
const engine = new Liquid.Engine()
```

If you are going to have a liquid link of the dev.to article just as it is in the dev.to markdown editor:
```sh
{% link author/post-title-slug %}
```
Start with creating the custom Liquid class that processes your custom tag as follows:
```js
class devToLink extends Liquid.Tag {
  constructor (template, tagName, markup) {
    super(template, tagName, markup);
    this.variable = markup.trim();
  }

  async render(context) {
    const base = context.environments[0];
    if (!base[this.variable]){
      base[this.variable] = ''
    }
    let devTo = new Embeds(this.variable);
    let value = await devTo.getDevToEmbed();
    context.environments[0][this.variable] = value;
    return value.toString();
  }
}
```

Then register the custom tag class in the Liquid engine
```js
engine.registerTag('link', devToLink);
```

__Embeds()__ above is the class that fetches content from the dev.to API and constructs the markup for the embeded content. You can create your own custom design.
A brief example of the class is as follows::
```js
class Embeds {
  constructor(link){
    this.link = link;
  }

  async getDevToEmbed(){
    // fetch post's content
    const post = await axios.get(`articles/${this.link}`).then(result => result.data).catch(e => { /* handle errors here */});
    return this.devToPostMarkup({ /* utilize the content from api that you need in your embed */ })
  };

  devToPostMarkup = (contents) => {
    return `
      <style> /* custom embed styles here */ </style>
      <div> <!-- custom embed layout here containing including contents --> </div>
    `;
  }
}
```

Then process your article body to render the custom liquid tags into embeds containing the designs you made:
```js
const finalHtmlWithEmbeds = engine
  .parse(articleWithLiquidTags)
  .then(template => template.render())
  .then(result => result);
```

After completing this feature I had to update my existing posts passing their markdown through the update above and now the article content looks like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3g5kvuy8m5ukyi2oa31.png)

### 3. Article Page Layout ###
This feature was the easiest of the bunch as it only involved the changing of the layout of the article page from the last one that had only the article content laid out on the full width of the page:
![Continuous vertical page layout](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/osmyt07791g8m3zhtl42.png)

To a page layout that has a section on the right side for larger viewports that houses social buttons, links to other articles and probably more additions in the future.
![Page layout with an aside section on the right](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mle71ism0kxez7lwddj.png)

So these were the updates that went into the blog part of my website this week, more changes are going to come to the other parts of it in the coming weeks when time allows.

Hopefully in the future I'll be able to trace all the changes that went into this site and from that I will be able to see my growth as a web developer.

Lastly, I welcome all feedback regarding [my website](https://jamesinkala.me) and what I could do to make it better.