Migrate to Hugo from Jekyll

speakers.jpg

Move static content to static

Jekyll has a rule that any directory not starting with _ will be copied as-is to the _site output. Hugo keeps all static content under static. You should therefore move it all there. With Jekyll, something that looked like

▾ <root>/
    ▾ images/
        logo.png

should become

▾ <root>/
    ▾ static/
        ▾ images/
            logo.png

Additionally, you'll want any files that should reside at the root (such as CNAME) to be moved to static.

Create your Hugo configuration file

Hugo can read your configuration as JSON, YAML or TOML. Hugo supports parameters custom configuration too. Refer to the Hugo configuration documentation for details.

Set your configuration publish folder to _site

The default is for Jekyll to publish to _site and for Hugo to publish to public. If, like me, you have _site mapped to a git submodule on the gh-pages branch, you'll want to do one of two alternatives:

  1. Change your submodule to point to map gh-pages to public instead of _site (recommended).
1  git submodule deinit _site
2  git rm _site
3  git submodule add -b gh-pages git@github.com:your-username/your-repo.git public
  1. Or, change the Hugo configuration to use _site instead of public.

     {
         ..
         "publishdir": "_site",
         ..
     }
    

Convert Jekyll templates to Hugo templates

That's the bulk of the work right here. The documentation is your friend. You should refer to Jekyll's template documentation if you need to refresh your memory on how you built your blog and Hugo's template to learn Hugo's way.

As a single reference data point, converting my templates for heyitsalex.net took me no more than a few hours.

Convert Jekyll plugins to Hugo shortcodes

Jekyll has plugins; Hugo has shortcodes. It's fairly trivial to do a port.

Implementation

As an example, I was using a custom image_tag plugin to generate figures with caption when running Jekyll. As I read about shortcodes, I found Hugo had a nice built-in shortcode that does exactly the same thing.

Jekyll's plugin:

 1    module Jekyll
 2      class ImageTag < Liquid::Tag
 3        @url = nil
 4        @caption = nil
 5        @class = nil
 6        @link = nil
 7        // Patterns
 8        IMAGE_URL_WITH_CLASS_AND_CAPTION =
 9        IMAGE_URL_WITH_CLASS_AND_CAPTION_AND_LINK = /(\w+)(\s+)((https?:\/\/|\/)(\S+))(\s+)"(.*?)"(\s+)->((https?:\/\/|\/)(\S+))(\s*)/i
10        IMAGE_URL_WITH_CAPTION = /((https?:\/\/|\/)(\S+))(\s+)"(.*?)"/i
11        IMAGE_URL_WITH_CLASS = /(\w+)(\s+)((https?:\/\/|\/)(\S+))/i
12        IMAGE_URL = /((https?:\/\/|\/)(\S+))/i
13        def initialize(tag_name, markup, tokens)
14          super
15          if markup =~ IMAGE_URL_WITH_CLASS_AND_CAPTION_AND_LINK
16            @class   = $1
17            @url     = $3
18            @caption = $7
19            @link = $9
20          elsif markup =~ IMAGE_URL_WITH_CLASS_AND_CAPTION
21            @class   = $1
22            @url     = $3
23            @caption = $7
24          elsif markup =~ IMAGE_URL_WITH_CAPTION
25            @url     = $1
26            @caption = $5
27          elsif markup =~ IMAGE_URL_WITH_CLASS
28            @class = $1
29            @url   = $3
30          elsif markup =~ IMAGE_URL
31            @url = $1
32          end
33        end
34        def render(context)
35          if @class
36            source = "<figure class='#{@class}'>"
37          else
38            source = "<figure>"
39          end
40          if @link
41            source += "<a href=\"#{@link}\">"
42          end
43          source += "<img src=\"#{@url}\">"
44          if @link
45            source += "</a>"
46          end
47          source += "<figcaption>#{@caption}</figcaption>" if @caption
48          source += "</figure>"
49          source
50        end
51      end
52    end
53    Liquid::Template.register_tag('image', Jekyll::ImageTag)

is written as this Hugo shortcode:

 1    <!-- image -->
 2    <figure {{ with .Get "class" }}class="{{.}}"{{ end }}>
 3        {{ with .Get "link"}}<a href="{{.}}">{{ end }}
 4            <img src="{{ .Get "src" }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}"{{ end }} />
 5        {{ if .Get "link"}}</a>{{ end }}
 6        {{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
 7        <figcaption>{{ if isset .Params "title" }}
 8            {{ .Get "title" }}{{ end }}
 9            {{ if or (.Get "caption") (.Get "attr")}}<p>
10            {{ .Get "caption" }}
11            {{ with .Get "attrlink"}}<a href="{{.}}"> {{ end }}
12                {{ .Get "attr" }}
13            {{ if .Get "attrlink"}}</a> {{ end }}
14            </p> {{ end }}
15        </figcaption>
16        {{ end }}
17    </figure>
18    <!-- image -->

Usage

I simply changed:

{% image full http://farm5.staticflickr.com/4136/4829260124_57712e570a_o_d.jpg "One of my favorite touristy-type photos. I secretly waited for the good light while we were "having fun" and took this. Only regret: a stupid pole in the top-left corner of the frame I had to clumsily get rid of at post-processing." ->http://www.flickr.com/photos/alexnormand/4829260124/in/set-72157624547713078/ %}

to this (this example uses a slightly extended version named fig, different than the built-in figure):

{{% fig class="full" src="http://farm5.staticflickr.com/4136/4829260124_57712e570a_o_d.jpg" title="One of my favorite touristy-type photos. I secretly waited for the good light while we were having fun and took this. Only regret: a stupid pole in the top-left corner of the frame I had to clumsily get rid of at post-processing." link="http://www.flickr.com/photos/alexnormand/4829260124/in/set-72157624547713078/" %}}

As a bonus, the shortcode named parameters are, arguably, more readable.

Finishing touches

Fix content

Depending on the amount of customization that was done with each post with Jekyll, this step will require more or less effort. There are no hard and fast rules here except that hugo server --watch is your friend. Test your changes and fix errors as needed.

Clean up

You'll want to remove the Jekyll configuration at this point. If you have anything else that isn't used, delete it.

A practical example in a diff

Hey, it's Alex was migrated in less than a father-with-kids day from Jekyll to Hugo. You can see all the changes (and screw-ups) by looking at this diff.