Adding Simple SEO for Octopress

Adding Meta Tag, Keywords and Descriptions for your Octopress Blog

I just started using Octopress, seems it is quite simple to setup. However, the default template doesn’t provide the fields for your site, your post, or your pages. After a google search, seems it is quite easy to setup.

The main Octopress Site

Description

Meta tag Description is aleady in config file _config.yml. However, to show it in the main site. You have to modify the file source/_includes/head.html.

source/_includes/head.html
-{% capture description %}{% if page.description %}{{ page.description }}{% else %}{{ content | raw_content }}{% endif %}{% endcapture %}
+{% capture description %}{% if page.description %}{{ page.description }}{% elsif site.description %}{{ site.description }}{% else %}{{ content | raw_content }}{% endif %}{% endcapture %}
<meta name="description" content="{{ description | strip_html | condense_spaces | truncate:150 }}">
Keywords

We have to add meta tag keywords is in config file _config.yml ourselves. We also need to modify the file source/_includes/head.html again for this change.

_config.yml
description: Site/Blog description
+keywords: "siteKeywords"
source/_includes/head.html
-{% if page.keywords %}<meta name="keywords" content="{{ page.keywords }}">{% endif %}
+{% capture keywords %}{% if page.keywords %}{{ page.keywords }}{% elsif site.keywords %}{{ site.keywords }}{% endif %}{% endcapture %}
+<meta name="keywords" content="{{ keywords | strip_html | condense_spaces }}" />

New Post

The default template for creating new post or pages doesn’t provide you the meta tag Description and keywords. We can add it in the RakeFile.

Rakefile
# usage rake new_post[my-new-post] or rake new_post['my new post'] or rake new_post (defaults to "new-post")
desc "Begin a new post in #{source_dir}/#{posts_dir}"
task :new_post, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your post: ")
end
raise "### You haven't set anything up yet. First run `rake install` to set up an Octopress theme." unless File.directory?(source_dir)
mkdir_p "#{source_dir}/#{posts_dir}"
filename = "#{source_dir}/#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&amp;')}\""
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
post.puts "comments: true"
post.puts "categories: "
post.puts "description: "
post.puts "keywords: "
post.puts "---"
end

Reference