danurbanowicz / eleventy-netlify-boilerplate

A template for building a blog with the Eleventy static site generator and Decap CMS
https://eleventy-netlify-boilerplate.netlify.app/
MIT License
528 stars 183 forks source link

'Invalid DateTime' in article #69

Closed coderkind closed 2 years ago

coderkind commented 2 years ago

Describe the bug Hi. I'm using the boilerplate to render a static version of a Wordpress site I'm wanting to mothball, but the outputted date seems to be different between the blog postslist page and an individual post.

On the postslist page, I think the boilerplate is using postlists.njk to output a post.date, whereas on an individual post/article, it uses post.njk and tries to use machineDate and readableDate (but outputs "Invalid DateTime").

date outputs a date on the individual post/article page, but not in the same format as the postslist page. I'm hoping to display the date in the same format on both types of page.

To Reproduce Steps to reproduce the behaviour:

  1. Go to https://coderkind-eleventy.netlify.app/blog/
  2. See dates are in a format similar to "06 Aug 2019"
  3. Go to individual post page: https://coderkind-eleventy.netlify.app/2019/08/06/13-npm-tricks-for-faster-javascript-development/
  4. Date displays as "Invalid DateTime"

Expected behavior The date should be in a similar format on postslists and individual post pages

Additional context Markdown format of posts: https://github.com/coderkind/coderkind-eleventy-netlify/blob/master/posts/2019/08/06/13-npm-tricks-for-faster-javascript-development/index.md

danurbanowicz commented 2 years ago

Thanks for pointing out this little discrepancy.

In the individual post template, we really ought to be using page.date instead of date. Do you mind checking if changing this in your layouts/post.njk fixes the invalid datetime?

Thanks!

coderkind commented 2 years ago

Thanks for replying!

This is what is output for each of the different dates when referenced in the layouts/post.njk file:

The last two being empty values. Neither of the first two match the date format on the postslist page.

danurbanowicz commented 2 years ago

Hmmm I've just noticed that in your post frontmatter you have your date value set as a string e.g.

date: "2019-08-06"

But the date filters are probably expecting a valid YAML date value e.g.

date: 2019-08-06

coderkind commented 2 years ago

Hmm; there's no option in Wordpress to format dates in a particular way when exporting, so presumably the dates in quotes is the default format? I was following this tutorial by the way (which links to your boilerplate).

danurbanowicz commented 2 years ago

OK, so using {{ page.date | machineDate }} and {{ page.date | readableDate }} in layouts/post.njk with the post frontmatter date: "2019-08-06" as supplied by your Wordpress export is parsing correctly for me on a copy of your repo.

Can you try this if you haven't already? Thanks

coderkind commented 2 years ago

Thanks; that's fixed it 👍

I thought I tried page. prefixing at some stage, but obviously not. Thanks once again :)