Renaming, Redating Blog Posts

otherMay 10, 2013Dotby Justin Gordon

Here's a few tips regarding renaming and redating posts and pages in Octopress.

How file names and metadata work in Octopress

  • Metadata, title and date, 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 or date, 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

  1. Follow the instructions here to use: jekyll/jekyll-redirect-from
  2. For any post/page that moves, simply put in this info info in the header of the post. A couple points:
    1. 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.
    2. Use the path within the website, such as this example.
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.

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
view raw Rakefile hosted with ❤ by GitHub

Closing Remark

Could your team use some help with topics like this and others covered by ShakaCode's blog and open source? We specialize in optimizing Rails applications, especially those with advanced JavaScript frontends, like React. We can also help you optimize your CI processes with lower costs and faster, more reliable tests. Scraping web data and lowering infrastructure costs are two other areas of specialization. Feel free to reach out to ShakaCode's CEO, Justin Gordon, at [email protected] or schedule an appointment to discuss how ShakaCode can help your project!
Are you looking for a software development partner who can
develop modern, high-performance web apps and sites?
See what we've doneArrow right