Emulating Jekyll Permalinks


#21

Cool! Just make sure you are reading https://gohugo.io/content-management/urls/#override-urls-with-front-matter. Aliases are slightly different, and I don’t think they fix your particular problem (with Disqus, for instance).


#22

Ok, so what needs to be done to convert a default Jekyll 3.x site’s permalinks is:

  • Get the date from the file name.
  • Get the date from the yaml
  • Get the post name
  • Get the category name

Now add a “url: category/year/month/day/post-name.html” to the yaml

Here’s a very simple ruby script that does this. (No error checking). Pass it a directory of jekyll posts and it will add the "url: " key to the yaml in the default jekyll format.

So “ruby script.rb /path/to/posts/”

Do this on a backup! Never process files live!

I’m not a ruby programmer, I just felt like giving ruby a try today.

dir = ARGV[0]

Dir.foreach(dir) do |filename|
  next if filename == '.' or filename == '..' or (File.extname(filename) != ".md" and File.extname(filename) != ".markdown")

  f = File.open(filename, "r")
  year, month, day, post_name = File.basename(filename, ".*").match(/(\d*)-(\d*)-(\d*)-(.*)/).captures
  categories = ""
  new_file = ""
  File.open(filename, "r") do |f|  
    f.each_line { |line|
        if line =~ /^date:/
          if match = line.match(/(\d*)-(\d*)-(\d*)_/)
            year, month, day = match.captures
          end
        end
        if line =~ /^categories:/
          if match = line.match(/categories: (.*)/)
            categories = match.captures[0]
            new_file << "url: /#{categories}/#{year}/#{month}/#{day}/#{post_name}.html\n"
          end
        end
        new_file << line
    }
  end
  
  File.open(filename, 'w') do |f|
    f.write new_file
  end
end