Jekyll SEO from a rake task
Posted on February 12, 2016 (Last modified on November 13, 2023) • 3 min read • 593 wordsI really like the things that the Yoast SEO plugin does for WordPress for search engine optimization (SEO). With it you can check your posts on various things in your post that a search engine keeps into consideration.
I was missing this functionality in Jekyll, until I came across the Jekyll SEO tool from Brett Hardin. The script itself was last updated in 2013, but still does its thing.
The script itself isn’t a gem that you can easily install. Since my workflow is built around tasks in a Rakefile I converted the script to it. In the gist below you find the complete task to do a test with:
rake seo['Jekyll SEO','_site/jekyll-seo/index.html']
The Rakefile, just as the original script, gives you the following results:
Is your keyword/are your keywords part of the
Like Brett I want to enhance the task so that it will do the following:
Done
If you manually installed every component you can use
rake seo['<keywords>','_site/<page>']
The tasks generates your site before it checks it, because the task runs against the generated HTML instead of the markdown file.
For the post you’re reading right now the command was:
rake seo['Jekyll SEO','_site/jekyll-seo/index.html']
If you use bundle just precede the command with ‘bundle exec’.
The output the rake task will give looks like this when testing for the keyword seo:
Analyzing page '_site/jekyll-seo/index.html' for keywords 'seo'
Article Heading: true (3)
Page title: true (1)
Content: true (20)
Meta description: true (1)
# SEO test
desc 'Check SEO values for post'
task :seo, [:keywords, :post] do |t, args|
require 'nokogiri'
heading = [];
title = [];
url = [];
content = [];
meta_description = [];
temp = [];
puts "Analyzing page '#{args.post}' for keywords '#{args.keywords}'"
sh "bundle exec jekyll build _config.yml"
post = Nokogiri::HTML(open(args.post))
post.css('h1').each do |this|
heading = this.to_s.scan(/#{args.keywords}/i)
end
post.css('title').each do |this|
title = this.to_s.scan(/#{args.keywords}/i)
end
post.css('body').each do |this|
content = this.to_s.scan(/#{args.keywords}/i)
end
post.css('meta').each do |this|
if this.attribute("name/).to_s == "description"
meta_description = this.attribute("content/).to_s.scan(/#{args.keywords}/i)
end
end
puts ""
puts "Article Heading: #{not heading.empty?} (#{heading.count})"
puts "Page title: #{not title.empty?} (#{title.count})"
# puts "Page URL: Yes (1)"
puts "Content: #{not content.empty?} (#{content.count})"
puts "Meta description: #{not meta_description.empty?} (#{meta_description.count})"
end