add and parse timezone in date filter
This commit is contained in:
parent
fc81ffd7fe
commit
7cc92813c6
1 changed files with 50 additions and 2 deletions
|
|
@ -8,7 +8,37 @@ const toml = require("@iarna/toml");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
|
const TIME_ZONE = "America/Santiago";
|
||||||
|
|
||||||
module.exports = function (eleventyConfig) {
|
module.exports = function (eleventyConfig) {
|
||||||
|
eleventyConfig.addDateParsing(function (dateValue) {
|
||||||
|
// i know this is a deranged solution. sorry LOL
|
||||||
|
let localDate;
|
||||||
|
try {
|
||||||
|
if (dateValue instanceof Date && !isNaN(dateValue)) {
|
||||||
|
// handle filename dates (ie 2025-10-18-post.md)
|
||||||
|
localDate = DateTime.fromJSDate(dateValue, { zone: "utc" })
|
||||||
|
.setZone(TIME_ZONE)
|
||||||
|
.startOf("day"); // Set to midnight in America/Santiago
|
||||||
|
} else if (typeof dateValue === "string" && dateValue) {
|
||||||
|
// handle string dates (ie from front matter, if used)
|
||||||
|
localDate = DateTime.fromISO(dateValue, { zone: TIME_ZONE }).startOf("day");
|
||||||
|
} else {
|
||||||
|
// handle invalid input
|
||||||
|
console.warn(`Invalid date value: ${dateValue} for ${this.page.inputPath}`);
|
||||||
|
localDate = DateTime.now().setZone(TIME_ZONE).startOf("day");
|
||||||
|
}
|
||||||
|
if (!localDate || localDate.isValid === false) {
|
||||||
|
throw new Error(`Invalid date value (${dateValue}) for ${this.page.inputPath}: ${localDate?.invalidReason || "Unknown"}`);
|
||||||
|
}
|
||||||
|
return localDate.toJSDate();
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Date parsing error for ${this.page.inputPath}:`, error.message);
|
||||||
|
// fallback to current date in TIME_ZONE
|
||||||
|
return DateTime.now().setZone(TIME_ZONE).startOf("day").toJSDate();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
eleventyConfig.setLayoutsDirectory("_layouts");
|
eleventyConfig.setLayoutsDirectory("_layouts");
|
||||||
|
|
||||||
eleventyConfig.addPassthroughCopy("img");
|
eleventyConfig.addPassthroughCopy("img");
|
||||||
|
|
@ -35,8 +65,26 @@ module.exports = function (eleventyConfig) {
|
||||||
return filtered;
|
return filtered;
|
||||||
});
|
});
|
||||||
|
|
||||||
eleventyConfig.addFilter("date", function (dateObj, format) {
|
eleventyConfig.addFilter("date", function (dateObj, format = "dd/MM/yyyy") {
|
||||||
return DateTime.fromJSDate(dateObj).toFormat(format);
|
let dt = dateObj;
|
||||||
|
// handle string dates
|
||||||
|
if (typeof dateObj === "string") {
|
||||||
|
dt = DateTime.fromISO(dateObj, { zone: TIME_ZONE }).toJSDate();
|
||||||
|
}
|
||||||
|
// handle DateTime objects (from addDateParsing)
|
||||||
|
if (dateObj instanceof DateTime) {
|
||||||
|
dt = dateObj.toJSDate();
|
||||||
|
}
|
||||||
|
// check dt as valid Date object
|
||||||
|
if (!(dt instanceof Date) || isNaN(dt)) {
|
||||||
|
console.log("Invalid date input:", dateObj);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
// format in TIME_ZONE
|
||||||
|
const formatted = DateTime.fromJSDate(dt, { zone: TIME_ZONE })
|
||||||
|
.toFormat(format);
|
||||||
|
console.log("Date input:", dt, "Formatted:", formatted, "Timezone:", TIME_ZONE);
|
||||||
|
return formatted;
|
||||||
});
|
});
|
||||||
|
|
||||||
eleventyConfig.addPlugin(eleventySass);
|
eleventyConfig.addPlugin(eleventySass);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue