Renaming, Redating Blog Posts
Here's a few tips regarding renaming and redating posts and pages in Octopress.
How file names and metadata work in Octopress
- Metadata,
title
anddate
, in the post headers affects how the page displays on the website, and is used for deployment. - The actual filename is for the author's convenience and is what goes into github.
- You can change the filename any time. The generator does not care. Git will pick up the changes automatically.
- However, if you change the
title
ordate
, then you need to worry about whether one of these conditions exists where you need to create an alias for the old link.- You posted the article, such as Twitter, or you emailed somebody a link to it.
- You have other articles that reference the article being changed.
- If you don't put in an alias, you'll get a 404 error and an unhappy reader.
Blogs vs. Pages
- Blog articles are created in a directory structure based on the
date
metadata. - Pages are created where you put them.
Moving a post/page that you already published
- Follow the instructions here to use: jekyll/jekyll-redirect-from
- For any post/page that moves, simply put in this info info in the
header of the post. A couple points:
- You need to put in the
/index.html
at the end. If you don't do that, nothing happens in terms of generating an alias. - Use the path within the website, such as this example.
- You need to put in the
redirect_from: /blog/2013/05/08/saner-rails-logging/index.html
I had tweeted about the following post so I wanted to make sure the old link still worked. I added the above alias to the blog post for the old link.
- Here's the old link: http://www.railsonmaui.com/blog/2013/05/08/saner-rails-logging/
- Here's the new link: http://www.railsonmaui.com/blog/2013/05/08/strategies-for-rails-logging-and-error-handling/
You can click on the old link to see that it takes you to the new link. Ha! No 404!
Redate Rake Task
Here's an addition to an Octopress Rakefile to rename blog postings to correspond to dates. This rake task does not change the title part. You can do that manually by changing both the title metadata and the name of the file. Note, be sure to comment out line 12 if you are using org-mode.
# Based on blog post http://www.ewal.net/2012/09/08/octopress-customizations/
# Modified to option of either all or no drafts, and also to use org-mode files
desc "Redate files in the posts directory if the filename does not match the post date in the YAML front matter. Note, URLs based on 'date' metadata, so this shouldn't break any links"
task :redate_posts do
redate_posts true, source_dir, posts_dir, org_posts_dir
end
desc "Redate files in the posts directory, skipping drafts, if the filename does not match the post date in the YAML front matter. Note, URLs based on 'date' metadata, so this shouldn't break any links"
task :redate_posts_no_drafts do
redate_posts false, source_dir, posts_dir, org_posts_dir
end
def redate_posts include_drafts, source_dir, posts_dir, org_posts_dir
redate_posts_in_dir "#{source_dir}/#{posts_dir}", "markdown", include_drafts
# remove next line if you're you're not using org-mode
redate_posts_in_dir "#{source_dir}/#{org_posts_dir}", "org", include_drafts
end
def redate_posts_in_dir dir, ext, include_drafts = true
Dir.chdir(dir) do
Dir["*.#{ext}"].each do |post|
post_date = ""
File.open(post) do |f|
f.grep(/^date: /) do |line|
post_date = line.gsub(/date: /, "").gsub(/\s.*$/, "")
break
end
end
post_title = post.to_s.gsub(/\d{4}-\d{2}-\d{2}/, "") # Get the post title from the currently processed post
new_post_name = post_date + post_title # determing the correct filename
rename = post != new_post_name
next unless rename
unless include_drafts
is_draft = false
File.open(post) do |f|
f.grep(/^published: false/) do |line|
is_draft = true
break
end
end
next if is_draft
end
puts "renaming #{post} to #{new_post_name}"
FileUtils.mv(post, new_post_name)
end
end
end