-
-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Potential issue with addDateParsing() and dates in filenames #3649
Comments
Because Yaml (via js-yaml lib as used by gray-matter) is doing its own date parsing: Line 1039 in aa713c5
You'll get real strings with |
When I look at the invocation in Lines 1022 to 1028 in aa713c5
I can see two arguments. The first one is being assigned to Can you inspect the value of import { basename } from "node:path";
/* … */
eleventyConfig.dateParsing(() => {
return DateTime.fromFormat(
basename(this.page.inputPath).slice(0, "2025-02-04".length)
)
});
/* … */ Modulo some error handling as not every post / page contains the date. (You have the |
@Ryuno-Ki apologies if you saw an utterly useless response here earlier today. I was trying to test some of the things you mentioned while doing about a dozen other things, and the end result was anything but helpful. Anyway, here's a more thoughtful response. This config...
...produces this console log:
If I add a condition to ensure it's a post file...
...we now get this console log:
So far so good-ish. If we update the config once more...
...we get this message:
I'm not very familiar with the |
I can confirm the behaviour. Looking up the documentation a slight update should yield the expected result: // eleventy.config.js
import { basename } from 'node:path';
import { DateTime } from 'luxon';
export default function (eleventyConfig) {
eleventyConfig.addDateParsing(function () {
const { inputPath } = this.page;
if (inputPath.startsWith('./src/posts/')) {
const postDate = basename(inputPath).slice(0, 10);
const newFormat = DateTime.fromISO(postDate);
console.log(newFormat);
console.log(newFormat.toFormat("MMMM dd, yyyy"));
}
});
} Resulting in
here |
Ok, so I was simply misunderstanding how this method was intended to be used! My apologies and thank you for taking the time to respond @Ryuno-Ki . |
Operating system
Windows 11
Eleventy
3.0.0
Describe the bug
Greetings,
I'm resurrecting an old Jekyll blog that has a bunch of markdown files with the date in the filename instead of in the front matter. While spinning up a new Eleventy site I was toying around with the new
addDateParsing()
method and noticed that it potentially wasn't working as it should. Or maybe I just don't understand the internal mechanics of how date is handled in Eleventy.Here's a basic reduced case:
After running
npx @11ty/eleventy
the following error appears in the console:Even though the config is copied almost verbatim from the documentation that error immediately made me feel like there was an issue with the date being passed in. So I swapped out the
return
withconsole.log(dateValue);
just to see how the date itself was being returned. From the console:So at this point we know that the date simply isn't coming through, even though I'm rendering the date in a template file with
{{ page.date }}
. I then tried adding the date in the front matter asdate: 2024-12-31
:That's more like it. Smarter people than I could explain how the extra stuff got appended there, but I'll let them explain that. Then once more I tried changing the front matter to
date: "2024-12-31"
(with double quotes):This is the end result I expected in the first place.
Reproduction steps
No response
Expected behavior
I expected the date-in-filename format to work without issue as it is stated in the documentation as a perfectly acceptable way to attach a date to a blog post. The date produced in this manner should be able to be manipulated via
addDateParsing()
.Reproduction URL
https://github.com/cmegown/eleventy-adddateparsing-reduced-case
Screenshots
No response
The text was updated successfully, but these errors were encountered: