Philip Hendry's Blog

Using PowerShell to generate Jekyll tag pages

Sep 13, 2015 • PowerShell • 2 min read

I’m hosting this blog on GitHub using Jekyll which is a fantastic free combination. But with anything that’s free there are some drawbacks. Primarily it’s the inability to use custom Jekyll plugins in GitHub.

I want to keep this combination so I’ve been following an article that describes how to generate tag pages without having a custom plug-in.

All is well-and-good, but for every tag that’s been made, a static page must be created which lists all posts with that tag. This seemed like a pain to keep up-to-date and a perfect opportunity to test my PowerShell skills!

The first step is to iterate over the lines in the .\_data\tags.yml file which is in this format:

  slug: dot-net
  name: .Net
  slug: android
  name: Android
  slug: angularjs
  name: AngularJS

The Get-Content PowerShell command will fetch the contents then I can filter only the lines I want with Select-String and a regular expression:

get-content .\data\tags.yml | select-string "^[^ \t]"

That regular expression is simply ignoring any line that begins with either a space or a tab.

So now we have a list of lines like this:


I’m going to need to get rid of the trailing colon and then I can use each keyword to create a string:

... %{ $a = $_.Line.TrimEnd(':'); "--- `nlayout: blog_by_tag`ntag: $a`npermalink: /blog/tag/$a/`n---`n`n"

Here I’m:

The final step is to write each of these strings to a new file basing the filename on the keyword stored in the variable $a:

... | Out-File -FilePath ".\blog\tag\$" -Encoding ascii }

The annoying thing here is Jekyll wants UTF-8 encoded ]files but without a BOM but this isn’t easy with Out-File and so I’ve just used ascii instead which is fine for the moment.

So put all together this command is:

get-content .\_data\tags.yml | 
select-string "^[^ \t]" | 
    $a = $_.Line.TrimEnd(':'); 
    "---`nlayout: blog_by_tag`ntag: $a`npermalink: /blog/tag/$a/`n---`n`n" | 
    Out-File -FilePath ".\blog\tag\$" -Encoding ascii 

The next improvement would be to iterate over the posts themselves, find all the unique tags and write the tags.yml file… but that’s for another day.

Post by: Philip Hendry