First I needed to redirect my old /YYYY/MM/TITLE
structure to the new structure without any date prefix. Jekyll by default will let you do that easily with permalink: /:title
. But that does not solve the problem of having inbound links redirect to the right place on the new blog. It is pretty easy to solve with the redirect_from “native” jekyll plugin. The only thing that remains now is to add the redirect_from:/old_uri_with_date
to each new post.
For that I whipped the following short ruby script and to run over all the files I just used some bash.
for f in *; do ruby process.rb $f; done
Where process.rb
is:
ARGV.each do |a|
if (a[/^\d+/])
puts a
text = File.read(a)
new_contents = text.gsub(/^redirect_from:(.*)/, "redirect_from: #{$1}#{a}")
# To merely print the contents of the file, use:
puts new_contents
# To write changes to the file, use:
File.open(a, "w") {|file| file.puts new_contents }
end
end
That gets us about 80% of the transition covered. If you heavily customized your WP content with various plugins to pull this may create issues because the imported would probably not be able to convert them in an intelligent way.
The things that still remained for me were
So…
/media
folder and used regex magic to update the paths.Sublime Editor made it very easy to test and apply the regex. Using Git to track the changes and revert if I blew it proved useful too.
Below you can find some of the regex I used:
S: ^redirect_from: (\d+)-(\d+)-(\d+)-(.*).md$
R: redirect_from: /$1/$2/$4/
S: <pre>([\s\S]*?)\[cc lang="(.*?)"(.*)\]
(greedy match)
R: \n
``$3\n`
S: \[cc lang="(.*?)" (.*)"\]
R: \n
``$1\n`
S: \[/cc\]([\s\S]*?)</pre>
R: \n
``\n`