feat: updated date parsing logic
This commit is contained in:
parent
d354c2ca37
commit
68addeb938
5 changed files with 40 additions and 30 deletions
|
|
@ -1,6 +1,7 @@
|
|||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const { execSync } = require("child_process");
|
||||
const { DateTime } = require("luxon");
|
||||
|
||||
const getGitCreatedTime = (filePath) => {
|
||||
try {
|
||||
|
|
@ -9,7 +10,7 @@ const getGitCreatedTime = (filePath) => {
|
|||
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
|
||||
).trim();
|
||||
if (result) {
|
||||
return new Date(result);
|
||||
return DateTime.fromISO(result);
|
||||
}
|
||||
} catch (e) {
|
||||
// Git command failed, return null
|
||||
|
|
@ -24,7 +25,7 @@ const getGitModifiedTime = (filePath) => {
|
|||
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
|
||||
).trim();
|
||||
if (result) {
|
||||
return new Date(result);
|
||||
return DateTime.fromISO(result);
|
||||
}
|
||||
} catch (e) {
|
||||
// Git command failed, return null
|
||||
|
|
@ -43,7 +44,8 @@ const getTitleFromFilename = (filePath) => {
|
|||
const getFileCreatedTime = (filePath) => {
|
||||
try {
|
||||
const stats = fs.statSync(filePath);
|
||||
return stats.birthtime || stats.mtime;
|
||||
const time = stats.birthtime ?? stats.mtime;
|
||||
return DateTime.fromJSDate(time);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -52,7 +54,7 @@ const getFileCreatedTime = (filePath) => {
|
|||
const getFileModifiedTime = (filePath) => {
|
||||
try {
|
||||
const stats = fs.statSync(filePath);
|
||||
return stats.mtime;
|
||||
return DateTime.fromJSDate(stats.mtime);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -60,9 +62,10 @@ const getFileModifiedTime = (filePath) => {
|
|||
|
||||
const parseDate = (value) => {
|
||||
if (!value) return null;
|
||||
if (value instanceof Date) return value;
|
||||
const parsed = new Date(value);
|
||||
return isNaN(parsed.getTime()) ? null : parsed;
|
||||
if (DateTime.isDateTime(value)) return value;
|
||||
if (value instanceof Date) return DateTime.fromJSDate(value);
|
||||
const parsed = DateTime.fromISO(value);
|
||||
return parsed.isValid ? parsed : null;
|
||||
}
|
||||
|
||||
const getPostCreatedAt = (data) => {
|
||||
|
|
@ -79,7 +82,7 @@ const getPostCreatedAt = (data) => {
|
|||
return fileDate;
|
||||
}
|
||||
|
||||
return data.page.date;
|
||||
return DateTime.fromJSDate(data.page.date);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
@ -95,19 +98,9 @@ module.exports = {
|
|||
},
|
||||
createdAt: getPostCreatedAt,
|
||||
updatedAt: (data) => {
|
||||
const modifiedDate = parseDate(data.updatedAt) ??
|
||||
return parseDate(data.updatedAt) ??
|
||||
getGitModifiedTime(data.page.inputPath) ??
|
||||
getFileModifiedTime(data.page.inputPath);
|
||||
const createdDate = getPostCreatedAt(data);
|
||||
|
||||
if (modifiedDate) {
|
||||
// Only return updatedAt if it's AFTER created date (by more than a day)
|
||||
const dayInMs = 24 * 60 * 60 * 1000;
|
||||
if (modifiedDate > createdDate && (modifiedDate - createdDate) > dayInMs) {
|
||||
return modifiedDate;
|
||||
}
|
||||
}
|
||||
return createdDate;
|
||||
},
|
||||
permalink: (data) => {
|
||||
const title = data.title || getTitleFromFilename(data.page.inputPath);
|
||||
|
|
@ -115,7 +108,7 @@ module.exports = {
|
|||
|
||||
const createdDate = getPostCreatedAt(data);
|
||||
|
||||
const isoDate = createdDate.toISOString().split('T')[0];
|
||||
const isoDate = createdDate.toISODate();
|
||||
return `/post/${slug}/${isoDate}/`;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue