Webseite als Jekyll-Projekt https://software-berater.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
2.2 KiB

# Simple related pages for the Jekyll static content generator. It will define
# links between pages when explicitely given, and additionally MAX_RELATED_CATEGORY
# entries which have the same `category` data.
#
# Usage:
#
# - Place this in your site's _plugins folder
# - add key `references` (array) to your page/post frontmatter, containing
# one or more related paths
# - Optionally: Give `category` to pages and posts
# - use something like this in your layout
#
# {% if page.related.size >= 1 %}
# <aside class="related">
# <ul class="related-posts">
# {% for post in page.related %}
# <li>
# <a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>
# </li>
# {% endfor %}
# </ul>
# </aside>
# {% endif %}
module Jekyll
class Related < Jekyll::Generator
MAX_RELATED_CATEGORY = 3
def generate(site)
# create category map
category_cache = site.posts.docs.group_by { |page| page.data['category'] }
# create forward references
related_posts_cache = Hash[site.posts.docs.map do |page|
[
page.relative_path,
(page.data['references'] || []).map { |referenced_path| find(site, referenced_path) }.compact
]
end]
# add backward references
related_posts_cache.each do |path, pages|
if origin = find(site, path)
pages.each { |page| related_posts_cache[page.relative_path] << origin }
end
end
site.posts.docs.each do |page|
page.data['related'] = []
unless page.data['category'].nil?
page.data['related'] += category_cache[page.data['category']]
.reject { |another_page| another_page == page }
.reverse
.take(MAX_RELATED_CATEGORY)
end
page.data['related'] += related_posts_cache[page.relative_path]
page.data['related'].uniq!
end
Jekyll.logger.info 'Related:', 'Completed references setup'
end
def find(site, reference)
site.posts.docs.find { |page| page.relative_path == reference }.tap do |result|
Jekyll.logger.info 'Related:', "Unresolved: #{reference.red}" if result.nil?
end
end
end
end