Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions _plugins/related_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ def self.normalise(tags)
def self.sort_time(post)
post[:date].respond_to?(:to_time) ? post[:date].to_time.to_i : 0
end

# The hash `rank` and `adjacent` take, from a Jekyll post.
def self.meta(doc)
{
id: doc.url,
tags: doc.data["tags"],
categories: doc.data["categories"],
lang: doc.data["lang"],
translation_id: doc.data["translation_id"],
date: doc.data["date"]
}
end
end

# --- Jekyll wiring -----------------------------------------------------------
Expand All @@ -119,16 +131,7 @@ def self.sort_time(post)
if defined?(Jekyll::Hooks)
Jekyll::Hooks.register :site, :pre_render do |site|
docs = site.posts.docs
meta = docs.map do |d|
{
id: d.url,
tags: d.data["tags"],
categories: d.data["categories"],
lang: d.data["lang"],
translation_id: d.data["translation_id"],
date: d.data["date"]
}
end
meta = docs.map { |d| RelatedPosts.meta(d) }
by_url = docs.to_h { |d| [d.url, d] }
# Sorted here rather than trusting the collection's order, and by id as well
# as date so posts with the same timestamp keep one order on every build.
Expand Down
26 changes: 22 additions & 4 deletions script/check-plugin-output.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true
#
# Checks that the _plugins/ hooks that change a post's page left their mark,
# that kramdown parsed the post's block HTML, that no listing shows a
# translation beside its original, that every cover is credited on its page,
# that no page repeats an id, and that the feed has room for every post.
# that the search index parses and holds every post, that kramdown parsed the
# post's block HTML, that no listing shows a translation beside its original,
# that every cover is credited on its page, that no page repeats an id, and
# that the feed has room for every post.
#
# test/ covers the plugins' functions, not the Jekyll hooks that apply them, and
# with a hook gone the build still passes. Jekyll itself reads the posts here,
Expand All @@ -14,6 +15,7 @@
# Prints one line per problem; exits 1 when there is any.

require "jekyll"
require "json"

site_dir = File.expand_path(ARGV.fetch(0))
root = File.expand_path("..", __dir__)
Expand All @@ -31,6 +33,7 @@
problems = []
lang = ->(post) { post.data["lang"] || site.config["lang"] }
per_lang = posts.group_by(&lang).transform_values(&:size)
metas = posts.map { |p| RelatedPosts.meta(p) }
# Where Jekyll writes a URL: a directory one to index.html, a bare one to .html.
built = lambda do |url|
file = url.end_with?("/") ? "#{url}index.html" : File.extname(url).empty? ? "#{url}.html" : url
Expand All @@ -42,6 +45,17 @@
# of an old paper never reaches it.
problems << "feed.posts_limit is #{limit} for #{posts.size} posts; raise it in _config.yml" if posts.size > limit

# search.json, with search_index.rb's plain_text: an index that does not parse
# leaves js/search.js finding nothing, and saying nothing either.
index = begin
JSON.parse(File.read(File.join(site_dir, "search.json")))
rescue SystemCallError, JSON::ParserError
nil
end
unless index.is_a?(Array) && index.size == posts.size && index.all? { |e| e["url"] && !e["content"].to_s.strip.empty? }
problems << "search.json is not a list of every post with its url and text"
end

posts.each do |post|
name = File.basename(post.path)
path = built.call(post.url)
Expand All @@ -63,10 +77,14 @@
end
end

# related_posts.rb: previous/next links, given another post in the language.
# related_posts.rb: previous/next links, given another post in the language,
# and Related reading, given a post it ranks.
if per_lang[lang.call(post)] > 1 && !html.include?(%(class="post-nav"))
problems << "#{name}: no previous/next links"
end
if RelatedPosts.rank(RelatedPosts.meta(post), metas).any? && !html.include?(%(class="related-posts"))
problems << "#{name}: no Related reading"
end

imgs = html.scan(/<img\b[^>]*>/)
# lazy_images.rb
Expand Down