Extracting date and slug from filename

I’m looking at converting jameshfisher.com from Jekyll to Hugo, and have run into a problem.

Jekyll extracts info from post filenames. Specifically, my files are at _posts/:year-:month-:day-:slug.md, and this is used to render the file _site/:year/:month/:day/:slug/index.html. This feature is a must for me, since I need exactly the same post URLs as before, and I don’t want to re-work hundreds of old posts.

It seems Hugo doesn’t do this by default. How do I turn on support for this? I can see this GitHub issue which suggests … it’s not supported? If not, I’m willing to do some work to enable it, but I’ll need some pointers.

I found hugo import jekyll. However, this works by adding ugly/redundant stuff to my post frontmatter, like this:

date: 2017-01-28T00:00:00Z
title: How can I write a file with `mmap` in C?
url: /2017/01/28/mmap-file-write/

I really want that to just look like, the following, since date/url can be generated from the filename:

title: How can I write a file with `mmap` in C?

Ah, I’ve just found there’s a PR for this :slight_smile: - but it’s not merged or released :frowning:

I’ll hold off converting to Hugo til that’s available!

Just wanted to let you know that this has been implemented somewhere before 0.38.2:
You can use the :filename option to the frontmatter > date setting in your config

I tried to use this configuration and it doesn’t seem to be working for me :frowning: It still doesn’t read the date from file name.

Me neither @klinki - did you get it working?

@klinki @apolaine if you guys would, show us your code where you’re trying to do this. Also see requesting help. :slightly_smiling_face:

Sorry, yes. An example post front-matter is:

author: playpen_admin
date: 1994-12-07 09:57:14+00:00
draft: false
title: Antirom CD-ROM (1994)
type: post
url: /1994/12/antirom/
- Portfolio
- antirom
- Interactive
- Interactivity

My config.toml has:

date  = [":filename", ":default"]

and permalinks:

posts = “:year/:month/:title/”

My blog is based on a modified version of the whisper theme, so the posts folder takes this structure:

-- blog
   -- _index.md
   -- 1994-12-07-antirom.markdown


I noticed a few posts had exported from WP with a slug, which I thought was interfering. They’re gone now. But all of them have a url param in the front matter. Without the URL param, a filename called 1994-12-07-antirom.markdown still ends up as sitename.com/antirom and I was hoping to just use the filename instead of having the param in the front matter.

Try this:

  posts = "/:filename/"
1 Like

Thanks. That just gives me sitename.com/1994-12-07-antirom as the URL though.

It may be that the only way to properly do it is either with slug/url or to have the files organised in year/month/day format (which is even more of a hassle).

I refer this in issue 7666, anyone interested can go there to track.

1 Like