Browse Source

[docs] Use a generator to assign complete author data before generating site

This makes sure that `post.author` will be the actual data we want and we don't have to assign it every time, potentially messing it up.
main
Paul O’Shannessy 10 years ago
parent
commit
991fa0c7c6
  1. 7
      _includes/blog_post.html
  2. 14
      _plugins/authors.rb
  3. 3
      blog/all.html

7
_includes/blog_post.html

@ -1,5 +1,4 @@
{% assign page = include.page %}
{% assign author = site.data.authors[page.author] %}
<h1>
{% if include.isPermalink %}
@ -12,10 +11,10 @@
<p class="meta">
{{ page.date | date: "%B %e, %Y" }}
by
{% if author.url %}
<a href="{{author.url}}">{{ author.name }}</a>
{% if page.author.url %}
<a href="{{page.author.url}}">{{ page.author.name }}</a>
{% else %}
{{ author.name }}
{{ page.author.name }}
{% endif %}
</p>

14
_plugins/authors.rb

@ -0,0 +1,14 @@
# This transforms the data associated with each post, specifically the author.
# We store our author information in a yaml file and specify the keys in The
# post front matter. Instead of looking up the complete data each time we need
# it, we'll just look it up here and assign. This plays nicely with tools like
# jekyll-feed which expect post.author to be in a specific format.
module Authors
class Generator < Jekyll::Generator
def generate(site)
site.posts.each do |post|
post.data['author'] = site.data['authors'][post['author']]
end
end
end
end

3
blog/all.html

@ -9,8 +9,7 @@ id: all-posts
<div class="inner-content">
<h1>All Posts</h1>
{% for page in site.posts %}
{% assign author = site.data.authors[page.author] %}
<p><strong><a href="/react{{ page.url }}">{{ page.title }}</a></strong> on {{ page.date | date: "%B %e, %Y" }} by {{ author.name }}</p>
<p><strong><a href="/react{{ page.url }}">{{ page.title }}</a></strong> on {{ page.date | date: "%B %e, %Y" }} by {{ page.author.name }}</p>
{% endfor %}
</div>
</section>

Loading…
Cancel
Save