From f11b884beddc119d8ffdcabcc5e3bd6a1073e25b Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 28 Jun 2021 14:58:08 +0200 Subject: [PATCH] #34 - Fix for last modified time on template creation --- src/commands/Article.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/commands/Article.ts b/src/commands/Article.ts index 2885cbf8..51d48e3e 100644 --- a/src/commands/Article.ts +++ b/src/commands/Article.ts @@ -101,14 +101,11 @@ export class Article { const config = vscode.workspace.getConfiguration(CONFIG_KEY); const dateFormat = config.get(SETTING_DATE_FORMAT) as string; const dateField = config.get(SETTING_DATE_FIELD) as string || "date"; + const modField = config.get(SETTING_MODIFIED_FIELD) as string || "date"; + + article = this.articleDate(article, dateFormat, dateField, forceCreate); + article = this.articleDate(article, dateFormat, modField, false); - if (typeof article.data[dateField] !== "undefined" || forceCreate) { - if (dateFormat && typeof dateFormat === "string") { - article.data[dateField] = format(new Date(), dateFormat); - } else { - article.data[dateField] = new Date(); - } - } return article; } @@ -185,4 +182,22 @@ export class Article { article.data["draft"] = newDraftStatus; ArticleHelper.update(editor, article); } + + /** + * Update the article date and return it + * @param article + * @param dateFormat + * @param field + * @param forceCreate + */ + private static articleDate(article: matter.GrayMatterFile, dateFormat: string, field: string, forceCreate: boolean) { + if (typeof article.data[field] !== "undefined" || forceCreate) { + if (dateFormat && typeof dateFormat === "string") { + article.data[field] = format(new Date(), dateFormat); + } else { + article.data[field] = new Date(); + } + } + return article; + } }