Removing Footnotes from Excerpts in Jekyll
I’ve been writing a lot of Jekyll related posted lately, because I’ve recently switched all my websites over to Jekyll from Wordpress. My latest challenge has involved footnotes in excerpts. Kramdown, the default Markdown implementation in Jekyll, supports footnotes in Markdown, but they unfortunately show up when using post.excerpt
inside Liquid templates. The following is a plug-in I wrote to strip footnotes, as well as the superscript links leading to them, for use on templates with post previews, such as an index page or RSS feed.
When dealing with posts in Jekyll, post.excerpt
can be used within post iterations to display a preview of the post. The excerpt_separator
can be set in the _config.yml
to indicate where the excerpt ends. The following example shows an index page where the paginator is used to list posts and their excerpts, as well as a “Read More” link to continue on to the rest of the article.
{% for post in paginator.posts %} <section class="blogroll"> <header> <h2> <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a> </h2> <span class="blog-post-meta">{{ post.date | date: "%b %-d, %Y" }}</span> </header> <article> {{ post.excerpt }} <a class="button small right round" href="{{ post.url | prepend: site.baseurl }}"> Read More <span class="fa fa-chevron-right"></span> </a> </article> </section> {% endfor %}
The trouble with this is that if you have footnotes in the opening excerpt of your post, Kramdown will add them to the preview itself. To deal with this, we’ll need to add a Jekyll plug-in for filtering out the added footnote div
. This will require the nokogiri gem for HTML parsing.
In the following code block, nokogiri is used to read in an HTML fragment. Kramdown places all the footnotes in a div
with the class set to footnotes. It also gives all the links to the footnotes a class as well. The for loop iterates through these items and removes block elements with the appropriate classes. Since I exported my old website from Wordpress, I’m also deleting the sup
blocks as well which were generated from the Textile processor I was using in Wordpress.
After saving the above file in _plugins/stripfootnotes.rb
, the code above can be modified to include the filter like so:
... {{ post.excerpt | strip_footnotes }} ...
In the past I’ve turned little plugin-ins like this one into their own gems such as jekyll-oldcomments or jekyll-unsanitize. Since this particular plug-in is very specialized, I’ve simply places the source code in a Github Gist, free of license, for anyone to use and modify.