Chuck Grimmett

Liquid

Taxonomies

  • Improving Jekyll Build Time

    Improving Jekyll Build Time

    When I migrated all of my old posts to Jekyll, my build time skyrocketed. The average time was 39 seconds for 853 posts. Abysmal. This is especially annoying when I’m actively working on the site with jekyll serve and waiting 40 seconds for changes to reflect.

    The first step to figuring out what takes so long is running the Liquid profiler: $ jekyll build --profile

    This is what it output for me:

    Filename                                                | Count |    Bytes |  Time --------------------------------------------------------+-------+----------+------ _layouts/default.html                                   |   853 | 9652.42K | 7.462 search/feed.json                                        |     1 | 1394.16K | 4.148 _includes/header.html                                   |   853 | 1230.35K | 3.485 _layouts/post.html                                      |   755 | 5119.00K | 2.301 _includes/opengraph.html                                |   853 |  870.67K | 1.694 _includes/icons.html                                    |   810 | 1752.89K | 1.454 sitemap.xml                                             |     1 |  203.34K | 1.321 _includes/footer.html                                   |   853 |  109.12K | 0.273 til/posts.yml                                           |     1 |   14.27K | 0.147 about.md                                                |     1 |   18.02K | 0.085 _posts/2016-07-04-posts-heatmap-calendar.md             |     1 |   16.78K | 0.080 isotope.html                                            |     1 |  389.52K | 0.048 _layouts/book.html                                      |    34 |  198.47K | 0.039 til.md                                                  |     1 |   58.76K | 0.023 feed.xml                                                |     1 |  139.11K | 0.022 ... with a dozen more tiny blog index pages from paginator  done in 39.359 seconds. 

    I tried a bunch of random stuff that made tiny improvements before I started doing research: Removing extraneous files, cleaning up if blocks that I no longer used, hardcoding things in the header that relied on variables from _config.yml, etc. None of this made significant improvements. I had already moved extraneous plugins long ago and I keep Jekyll up to date, so there was nothing more to do there.

    I eventually came across this post by Mike Neumegen, which mentioned jekyll-include-cache, which caches the files you tell it to with the first instance and then serves the cached versions up for subsequent requests. For includes that don’t change based on the page content, you can replace {% include example.html %} with {% include_cached example.html %}. When I did this with my sidebar, nav bar, and footer, it cut my average first build time to 28 seconds, with subsequent regeneration times around 19 seconds. Awesome! (Neumegen’s post, which mentioned jekyll-include-cache contains a lot of other helpful advice for Jekyll build optimization. Check it out!)

    My next step was to look at other includes to see if I could shave more time off. Some files had a mix of static and dynamic assets, so I split out all of the static assets into more include_cached includes. This cut another 5-6 seconds off of my build time. Boom.

    Now that my layouts and my includes were as optimized as I could get them without major rewrites, I turned to the next major time hog: search/feed.json. 4 seconds for a single file! This file loops through everything on the site and dumps it into a single file so that I can crawl it with Javascript to power search on my site.

    I used Mat Hayward’s search project with a few tweaks. As I dug deeper on the specific functions in the feed.json generator, I noticed that everything was being converted from Markdown to HTML first, then HTML stripped out, then turned into JSON. I wondered if skipping the markdown conversion step would lead to inaccurate search, so I tried it. Not only did my search function the same, but it dropped feed.json’s build time down to around a sixth of a second.

    My build time now looks like this:

    Filename                                                | Count |    Bytes |  Time --------------------------------------------------------+-------+----------+------ _layouts/default.html                                   |   853 | 9564.71K | 2.922 _includes/head.html                                     |   853 | 1571.79K | 2.623 _includes/opengraph.html                                |   853 |  870.67K | 2.171 sitemap.xml                                             |     1 |  203.34K | 1.177 _layouts/post.html                                      |   755 | 5195.80K | 0.509 search/feed.json                                        |     1 | 1394.65K | 0.176 _posts/2016-07-04-posts-heatmap-calendar.md             |     1 |   16.78K | 0.091 about.md                                                |     1 |   18.02K | 0.076 isotope.html                                            |     1 |  389.52K | 0.050 _layouts/book.html                                      |    34 |  198.47K | 0.030 _includes/header.html                                   |     1 |    1.43K | 0.021 til.md                                                  |     1 |   58.76K | 0.019 feed.xml                                                |     1 |  139.24K | 0.018 ... with a dozen more tiny blog index pages from paginator  done in 19.594 seconds. 

    Regeneration times when running jekyll serve are hovering around 13 seconds now, which is a significant quality of life improvement for me when managing this blog.

    There is probably more work I can do with moving the highlighter to the front end with JS and simplifying the Liquid in head.html and opengraph.html, but I’m stopping here for now.

  • Link Posts with Jekyll

    Link Posts with Jekyll

    Today I learned:

    How to make link posts (or external post links) with Jekyll

    I’m an avid reader of Marco.org and Daring Fireball. They both have these nifty posts that link directly to external pages. Now that I have two blogs, a podcast, and occasionally have work published elsewhere, I want to keep a record of these things on my cagrimmett.com blog. Link posts are an excellent way to do that.

    Jekyll doesn’t have this capability out of the box, but with a little Liquid magic you can make it happen. I followed Christian-Frederik Voigt’s method. This takes advantage of the ability to create items in each post’s YAML front-matter and use them in template files:

    First, create a variable called link: in your YAML front-matter for the post you want to link elsewhere. Fill it in with the link you want to post to link to:

    --- layout: post title: Snack Time Episode 3 - Negroni Week date: 2016-06-09 feature-img: "/img/snacktime-pattern.png" excerpt: blah blah blah excerpt here link: http://snacktime.fm/episodes/2016/6/9/episode-3-negroni-week ---

    Then anywhere in the template files that generates post headlines, you’ll need to write a conditional to check if the new link: variable is present. If it is, you’ll want to write the headline’s link there (post.link) instead of the post.url. I also added an arrow inside another conditional to specify that this is an outgoing link:

    You’ll want to use this anywhere your posts are listed. For me that is in index.html and 404.html.

    See this in action on my homepage.

    This method works great if you want to have direct links to other places and don’t want a permalink page on your own site that you can reference. I don’t care about that, so I didn’t make this for my site. If you want that, you’ll need to refer to Christian-Frederik Voigt’s method. The short story is that you’ll also need to modify the post and page templates.

  • Counting and JSON output in Jekyll

    Counting and JSON output in Jekyll

    Today I learned:

    Jekyll has a built-in filter called jsonify to turn data into JSON and Liquid has a filter called json. These is pretty useful for turning standard Jekyll output arrays into JSON, but if you need something more customized, you’ll need to build it yourself because Liquid’s ability to create arrays is limited. (Not a criticism, just an observation. It is primarily a templating language.)

    For example, I wanted to store the dates I have blog posts for and the number of posts on each of those dates in order to pass it to a heatmap calendar. I couldn’t figure out a way to build that into a single array with Liquid (and I didn’t want to drop down into Ruby), so I figured out a way build JSON manually.

    This outputs my blog’s post dates in the Unix timestamp format and displays the number of posts on a given day and leaves the trailing comma off to comply with the JSON standard:

    {% assign counter = 0 %}{ {% for post in site.posts %}{% capture day %}{{ post.date | date: '%s' }}{% endcapture %}{% capture prevday %}{{ post.previous.date | date: '%s' }}{% endcapture %}{% assign counter = counter | plus: 1 %}{% if day != prevday %}"{{ post.date | date: '%s' }}": {{ counter }}{% assign counter = 0 %}{% if forloop.last == false %},{% endif %} {% endif %}{% endfor %}} 

    Output:

    { 	"1463716800": 1, 	"1463371200": 1, 	"1462593600": 2, 	"1462507200": 1, 	"1462248000": 1, 	"1462161600": 1, 	"1461643200": 1, 	"1461297600": 3, 	"1461124800": 1, 	"1460952000": 1, 	"1460606400": 1, 	"1460347200": 1, 	"1459915200": 1, 	"1458792000": 1, 	"1458705600": 1, 	"1458619200": 1, 	"1458532800": 1, 	"1458446400": 1, 	"1458273600": 2, 	"1457586000": 1, 	"1457499600": 1, 	"1457413200": 1, 	"1457326800": 1, 	"1457240400": 1, 	"1456981200": 1, 	"1456894800": 1, 	"1456808400": 1, 	"1456722000": 1, 	"1456376400": 1, 	"1456290000": 1, 	"1456203600": 1, 	"1456117200": 1, 	"1456030800": 1, 	"1455944400": 1, 	"1455858000": 1, 	"1455771600": 2, 	"1455685200": 1, 	"1455598800": 1, 	"1455512400": 1, 	"1455339600": 1, 	"1455253200": 1, 	"1455166800": 1, 	"1455080400": 1, 	"1449032400": 1, 	"1441944000": 1, 	"1441598400": 1, 	"1441166400": 1, 	"1416978000": 1, 	"1377662400": 1 }

    The key to getting the counts right was advancing the counter by 1, comparing the date of the current post to the previous post, outputting the date and the count if they did not match, and if they did match advancing the counter by 1 and comparing again. After the date and count are output, the counter is reset to 0.

    You can see the heatmap calendar on my TIL page.


    Thanks to Eric Davis for advice on using less code and pointing out how I can make the trailing comma if statement more efficient: %if forloop.last == false% , %endif%

    He said:

    If there’s one bit of “programming advice” I would give to anybody mucking around with this stuff, it’s this: Write as little code as humanly possible. The most bug-free line of code is the line not written.

    What I need to ask myself: If something is being repeated, why?

  • Hiding Categories from the Jekyll Paginator, Unless, and Insert statements

    Hiding Categories from the Jekyll Paginator, Unless, and Insert statements

    Today I learned:

    Hiding Categories from Paginator

    According to the Jekyll docs, the jekyll-paginator plugin does not support categories or tags. That means there is no way to exclude a category as a whole. You can only exclude posts individually by including hidden: true in each post’s YAML front-matter.

    I’m hiding all of my TIL posts from the front page of my site, so I did a global find and replace to accomplish it.

    Mirror of If statement in Liquid

    Liquid has a neat concept that is the inverse of an If statement. It executes if a condition is not met.

    {% unless post.categories contains 'TIL' %} 	Code here to display all the things! {% endunless %}

    Inserting via mysqli()

    Using the mysqli() function I wrote about yesterday, here is how to insert something into a table:

     $connect = new mysqli($server, $db_username, $password, $db); $connect->query("INSERT INTO `table_name` (username) VALUES ('$user');");

    Make sure your variables are sanitized first! You don’t want someone doing a SQL injection.

    Eric Davis told me that mysqli() is the old way of doing things and I should check out PDO instead.

  • Doing the Work, Flipping the Status Quo, and Alphabetizing in Liquid

    Doing the Work, Flipping the Status Quo, and Alphabetizing in Liquid

    Today I learned:

    Doing the Work

    I used to be really interested in how people worked. When they get up, what their morning rituals are, what tools they use, how they use them, etc. While these things are interesting and tell a story about a person, they miss the point.

    How someone works isn’t nearly as important as the fact that they are doing the work. They made the choice to do what it takes each day to practice their craft. The rest is minutiae.

    If using the GTD method or having morning rituals works for you, great. But don’t think that those things are what really matter. If you’ve committed yourself to doing the work, you’ll find a way. If you haven’t committed yourself, none of the rituals or methods will make a difference.

    This commitment is very difficult, but necessary.


    Flipping the Status Quo

    We all suffer from status quo bias. How do we overcome it? We can get 80% of the way there by:

    • Instead of asking, “Why should this be different?” ask “Why should this stay the same?” – This is especially powerful when confronting clutter. (By the way, the answer, “because it gives me joy” is a valid reason for keeping something. Change for change’s sake isn’t helpful.)
    • Recognize the difference between acknowledging a feeling and rationalizing that feeling to keep the status quo.

    Source: Planet Money episode #683


    Sorting tags alphabetically in Liquid

    It was starting to annoy me that my tags were not in alphabetical order on my Today I learned page. So I learned how to sort them alphabetically with Liquid and I added anchor links with counts for each tag.

    Here is my updated template code:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
      	{% assign sorted_tags = (site.tags | sort:0) %} 	 	 	{% for tag in sorted_tags %} 	  {% assign t = tag | first %} 	  {% assign posts = tag | last %} 	 	

    {{ t | downcase }}">{{ t }}

    {% endfor %}

    Sources: I cobbled this together from Michael Lanyon’s blog, Joe Kampschmidt’s blog, and the Liquid docs.

  • Jekyll Upgrades and Liquid Templates

    Jekyll Upgrades and Liquid Templates

    Today I learned:

    Upgrading Jekyll from 2.x to 3.x

    • The upgrade itself is simple: In Terminal, run gem update jekyll. Use sudo if you run into permissions issues like I did.
    • If you use pagination in any of your templates, you’ll now have to add gems: [jekyll-paginate] to the # Plugins section of your _config.yaml or else your site won’t compile. This wasn’t necessary in 2.x.

    Making an index for my TIL posts in Liquid

    I wanted to have an index for my TIL posts that was organized in two ways: reverse chronologically and by tag. Here is how I’m handling that with Liquid:

    	### Recent TIL Posts 	 	 	 	--- 	 	### TIL posts by category 	 	{% for tag in site.tags %} 	  {% assign t = tag | first %} 	  {% assign posts = tag | last %} 	 	#### {{ t }} 	 	{% endfor %}

    Notes:

    • The general structure of the tags method comes from Joe Kampschmidt. I started searching when it didn’t work the same way as the categories method I wrote earlier.
    • This method for displaying tags works because I’m only tagging my TIL posts. Everything else goes into categories. If I were to tag other kinds of posts, I’d need to first limit by posts in site.categories['TIL']
    • Category names are case sensitive.
    • You can display raw Liquid markup without it rendering by wrapping it in {%raw%} and {%endraw%}.