From dde932728a43fbc10d87a7a33482c3aeece7095e Mon Sep 17 00:00:00 2001 From: PrinOrange Date: Wed, 14 Aug 2024 11:24:25 +0800 Subject: [PATCH] [fix] format the code --- consts/consts.ts | 2 +- data/config.ts | 2 +- lib/date.ts | 28 ++++++++++++++++++++++++---- lib/post-process.ts | 7 ++++--- types/config.type.ts | 2 +- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/consts/consts.ts b/consts/consts.ts index eeb98db..6a4d899 100644 --- a/consts/consts.ts +++ b/consts/consts.ts @@ -14,4 +14,4 @@ export const PostURL = (postId: string) => `https://${Config.SiteDomain}/blog/${ export const SearchURL = (keyword: string) => `https://${Config.SiteDomain}/search/?q=${keyword}`; const year = getCurrentTime().year; -export const CopyrightAnnouncement = `COPYRIGHT © ${Config.YearStart}-${year} ${Config.AuthorName} ALL RIGHTS RESERVED`; +export const CopyrightAnnouncement = `COPYRIGHT © ${Config.YearStart === year ? year : `${Config.YearStart}-${year}`} ${Config.AuthorName} ALL RIGHTS RESERVED`; diff --git a/data/config.ts b/data/config.ts index d2909ce..06535a6 100644 --- a/data/config.ts +++ b/data/config.ts @@ -77,7 +77,7 @@ export const Config: TConfig = { }, // Website establishment year. - YearStart: 2023, + YearStart: "2023", // Please enter your legal name for use with the copyright mark. AuthorName: "JOHN DOE", }; diff --git a/lib/date.ts b/lib/date.ts index 17cb540..e51abcf 100644 --- a/lib/date.ts +++ b/lib/date.ts @@ -2,10 +2,10 @@ * Convert the date format of YYYY-MM-DD to American writing * @param date The date in format of YYYY-MM-DD. */ -export const normalizeDate = (date: string = "1970-01-01"): string => { - let [year, month, day] = date.split("-"); - let month_num = parseInt(month); - let day_num = parseInt(day); +export const normalizeDate = (date = "1970-01-01"): string => { + const [year, month, day] = date.split("-"); + const month_num = Number.parseInt(month); + const day_num = Number.parseInt(day); const month_en: { [index: number]: string; } = { @@ -43,3 +43,23 @@ export const getCurrentTime = (): { seconds: String(today.getSeconds()).padStart(2, "0"), }; }; + +export const convertDateToISO8601 = (dateString: string, timezoneOffset = 8): string => { + const date = new Date(dateString); + + const offsetHours = timezoneOffset; + const offsetMinutes = offsetHours * 60; + + date.setMinutes(date.getMinutes() + offsetMinutes); + + const isoString = date.toISOString(); + + const datePart = isoString.split("T")[0]; + const timePart = "00:00:00"; + + const offsetSign = offsetHours >= 0 ? "+" : "-"; + const absOffsetHours = Math.abs(offsetHours).toString().padStart(2, "0"); + const offsetString = `${offsetSign}${absOffsetHours}:00`; + + return `${datePart}T${timePart}${offsetString}`; +}; diff --git a/lib/post-process.ts b/lib/post-process.ts index a4e55b3..5540ec9 100644 --- a/lib/post-process.ts +++ b/lib/post-process.ts @@ -37,14 +37,15 @@ async function extractFrontmatters(filepath: string): Promise { function readPostsDirectory(): string[] { const result: string[] = []; - fs.readdirSync(PostFilesDirectory).forEach((fileName) => { + const files = fs.readdirSync(PostFilesDirectory); + + for (const fileName of files) { const filePath = path.join(PostFilesDirectory, fileName); const fileStat = fs.statSync(filePath); - if (fileStat.isFile() && fileName.endsWith(".md")) { result.push(filePath); } - }); + } return result; } diff --git a/types/config.type.ts b/types/config.type.ts index 0c1e10d..b41b998 100644 --- a/types/config.type.ts +++ b/types/config.type.ts @@ -41,6 +41,6 @@ export type TConfig = { Crypto?: { Name: string; Address: string; Blockchain: string }[]; }; - YearStart: number; + YearStart: string; AuthorName: string; };