Skip to content

Commit

Permalink
Add this.page for custom date parsing callbacks #867
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jun 27, 2024
1 parent 8a79ee8 commit d81d8a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
22 changes: 15 additions & 7 deletions src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,20 +396,21 @@ class Template extends TemplateContent {
data.page = {};
}

let newDate = await this.getMappedDate(data);

// Make sure to keep these keys synchronized in src/Util/ReservedData.js
// Skip date assignment if custom date is falsy.
if (newDate) {
data.page.date = newDate;
}
data.page.inputPath = this.inputPath;
data.page.fileSlug = this.fileSlugStr;
data.page.filePathStem = this.filePathStem;
data.page.outputFileExtension = this.engine.defaultTemplateFileExtension;
data.page.templateSyntax = this.templateRender.getEnginesList(
data[this.config.keys.engineOverride],
);

let newDate = await this.getMappedDate(data);
// Skip date assignment if custom date is falsy.
if (newDate) {
data.page.date = newDate;
}

// data.page.url
// data.page.outputPath
// data.page.excerpt from gray-matter and Front Matter
Expand Down Expand Up @@ -914,8 +915,15 @@ class Template extends TemplateContent {
let dateValue = data?.date;

// These can return a Date object, or a string.
// Already type checked to be functions in UserConfig
for (let fn of this.config.customDateParsing) {
let ret = fn(dateValue);
let ret = fn.call(
{
page: data.page,
},
dateValue,
);

if (ret === false) {
// Skip out, no date will be assigned to this template!
return false;
Expand Down
44 changes: 26 additions & 18 deletions test/EleventyTest-CustomDateParsing.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import test from "ava";
import Eleventy from "../src/Eleventy.js";

test("Custom date parsing callback (one, return string), Issue #867", async (t) => {
t.plan(2);
test("Custom date parsing callback (return string), Issue #867", async (t) => {
t.plan(3);

let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/", {
config: eleventyConfig => {
eleventyConfig.addTemplate("test.html", `# Markdown`);
eleventyConfig.dataFilterSelectors.add("page.date");

eleventyConfig.addDateParsing((dateValue) => {
eleventyConfig.addDateParsing(function(dateValue) {
t.truthy(this.page.inputPath);
t.is(dateValue, undefined);
return "2001-01-01T12:00:00Z";
});
Expand All @@ -21,15 +22,16 @@ test("Custom date parsing callback (one, return string), Issue #867", async (t)
t.deepEqual(result.data.page.date, new Date(Date.UTC(2001,0,1,12)));
});

test("Custom date parsing callback (one, return Date), Issue #867", async (t) => {
t.plan(2);
test("Custom date parsing callback (return Date), Issue #867", async (t) => {
t.plan(3);

let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/", {
config: eleventyConfig => {
eleventyConfig.addTemplate("test.html", `# Markdown`);
eleventyConfig.dataFilterSelectors.add("page.date");

eleventyConfig.addDateParsing((dateValue) => {
eleventyConfig.addDateParsing(function(dateValue) {
t.truthy(this.page.inputPath);
t.is(dateValue, undefined);
return new Date(Date.UTC(2001,0,1,12));
});
Expand All @@ -41,8 +43,8 @@ test("Custom date parsing callback (one, return Date), Issue #867", async (t) =>
t.deepEqual(result.data.page.date, new Date(Date.UTC(2001,0,1,12)));
});

test("Custom date parsing callback (one, using date from data cascade, return string), Issue #867", async (t) => {
t.plan(2);
test("Custom date parsing callback (using date from data cascade, return string), Issue #867", async (t) => {
t.plan(3);

let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/", {
config: eleventyConfig => {
Expand All @@ -51,7 +53,8 @@ test("Custom date parsing callback (one, using date from data cascade, return st
});
eleventyConfig.dataFilterSelectors.add("page.date");

eleventyConfig.addDateParsing((dateValue) => {
eleventyConfig.addDateParsing(function (dateValue) {
t.truthy(this.page.inputPath);
t.true(dateValue instanceof Date);
return "2001-01-01T12:00:00Z";
});
Expand All @@ -64,7 +67,7 @@ test("Custom date parsing callback (one, using date from data cascade, return st
});

test("Custom date parsing callback (two, return undefined/falsy), Issue #867", async (t) => {
t.plan(3);
t.plan(5);

let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/", {
config: eleventyConfig => {
Expand All @@ -73,12 +76,14 @@ test("Custom date parsing callback (two, return undefined/falsy), Issue #867", a
});
eleventyConfig.dataFilterSelectors.add("page.date");

eleventyConfig.addDateParsing((dateValue) => {
eleventyConfig.addDateParsing(function (dateValue) {
t.truthy(this.page.inputPath);
t.deepEqual(dateValue, new Date(Date.UTC(2003,0,1,12)));
// return nothing
});

eleventyConfig.addDateParsing((dateValue) => {
eleventyConfig.addDateParsing(function (dateValue) {
t.truthy(this.page.inputPath);
t.deepEqual(dateValue, new Date(Date.UTC(2003,0,1,12)));
// return nothing
});
Expand All @@ -90,8 +95,8 @@ test("Custom date parsing callback (two, return undefined/falsy), Issue #867", a
t.deepEqual(result.data.page.date, new Date(Date.UTC(2003,0,1,12)));
});

test("Custom date parsing callback (two, return explicit false), Issue #867", async (t) => {
t.plan(1);
test("Custom date parsing callback (return explicit false), Issue #867", async (t) => {
t.plan(2);

let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/", {
config: eleventyConfig => {
Expand All @@ -100,7 +105,8 @@ test("Custom date parsing callback (two, return explicit false), Issue #867", as
});
eleventyConfig.dataFilterSelectors.add("page.date");

eleventyConfig.addDateParsing((dateValue) => {
eleventyConfig.addDateParsing(function (dateValue) {
t.truthy(this.page.inputPath);
return false;
});
}
Expand All @@ -113,19 +119,21 @@ test("Custom date parsing callback (two, return explicit false), Issue #867", as
});

test("Custom date parsing callbacks (two, last wins, return string), Issue #867", async (t) => {
t.plan(3);
t.plan(5);

let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/", {
config: eleventyConfig => {
eleventyConfig.addTemplate("test.html", `# Markdown`);
eleventyConfig.dataFilterSelectors.add("page.date");

eleventyConfig.addDateParsing((dateValue) => {
eleventyConfig.addDateParsing(function (dateValue) {
t.truthy(this.page.inputPath);
t.is(dateValue, undefined);
return "2010-01-01T12:00:00Z";
});

eleventyConfig.addDateParsing((dateValue) => {
eleventyConfig.addDateParsing(function (dateValue) {
t.truthy(this.page.inputPath);
t.is(dateValue, "2010-01-01T12:00:00Z");
return "2001-01-01T12:00:00Z";
});
Expand Down

0 comments on commit d81d8a7

Please sign in to comment.