From 4ce6f33755675554a72216e7c6820c7934b3afd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=98=88=EC=B0=AC?= Date: Sat, 3 Feb 2024 19:47:48 +0900 Subject: [PATCH] Update utils.ts This commit enhances the .post-template to recognize and convert user-provided date expressions in the format YYYY-MM-DD hh:mm. Now, users can include dates in this format, and the template will correctly interpret and display the specified date and time. --- src/utils.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 959a5ec..f5deb5b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -39,7 +39,9 @@ createFile(dirName: string, newFileName: string): Promise { const templateExists: boolean = templatePath !== undefined && fs.existsSync(templatePath); const fileExists: boolean = fs.existsSync(fileName); - const frontMatter = templateExists ? fs.readFileSync(templatePath) : ''; + const frontMatter = templateExists ? + Buffer.from(replaceDate(fs.readFileSync(templatePath).toString('utf-8')), 'utf-8') : ''; + setFrontMatter(!fileExists && !templateExists); if (!fileExists) { fs.appendFileSync(fileName, frontMatter); @@ -94,6 +96,27 @@ getDateTime(): string { return yyyy + '-' + mm + '-' + dd + ' ' + time; } +export function +replaceDate(data: string): string { + var formattedString = data.replace(/date:.*$/m, function(match: string) { + var today = new Date(); + var dd = String(today.getDate()).padStart(2, '0'); + var mm = String(today.getMonth() + 1).padStart(2, '0'); + var yyyy = today.getFullYear(); + var hours = String(today.getHours()).padStart(2, '0'); + var minutes = String(today.getMinutes()).padStart(2, '0'); + + return match.replace(/YYYY/g, `${yyyy}`) + .replace(/MM/g, mm) + .replace(/DD/g, dd) + .replace(/hh/g, hours) + .replace(/mm/g, minutes); + }); + + return formattedString; +} + + export function addDateToFilename(fileName: string): string { if (fileName === null) {