#450 - Additional date placeholders

This commit is contained in:
Elio Struyf
2022-11-04 10:34:46 +01:00
parent e864d56081
commit 5205b2d079
2 changed files with 25 additions and 4 deletions
+1
View File
@@ -14,6 +14,7 @@
- [#434](https://github.com/estruyf/vscode-front-matter/issues/434): Webview errors are logged in the extension output
- [#447](https://github.com/estruyf/vscode-front-matter/issues/447): Allow to use placeholders on git commit messages
- [#449](https://github.com/estruyf/vscode-front-matter/issues/449): Show `filename` if the `title` is not set
- [#450](https://github.com/estruyf/vscode-front-matter/issues/450): Additional time placeholders added `{{hour12}}`, `{{hour24}}`, `{{ampm}}`, and `{{minute}}`
### ⚡️ Optimizations
+24 -4
View File
@@ -8,16 +8,16 @@ import { SlugHelper } from "./SlugHelper";
* @param title
* @returns
*/
export const processKnownPlaceholders = (value: string, title: string, dateFormat: string) => {
export const processKnownPlaceholders = (value: string, title: string | undefined, dateFormat: string) => {
if (value && typeof value === "string") {
if (value.includes("{{title}}")) {
const regex = new RegExp("{{title}}", "g");
value = value.replace(regex, title);
value = value.replace(regex, title || "");
}
if (value.includes("{{slug}}")) {
const regex = new RegExp("{{slug}}", "g");
value = value.replace(regex, SlugHelper.createSlug(title) || "");
value = value.replace(regex, SlugHelper.createSlug(title || "") || "");
}
if (value.includes("{{now}}")) {
@@ -26,7 +26,7 @@ export const processKnownPlaceholders = (value: string, title: string, dateForma
if (dateFormat && typeof dateFormat === "string") {
value = value.replace(regex, format(new Date(), DateHelper.formatUpdate(dateFormat) as string));
} else {
return (new Date()).toISOString();
value = value.replace(regex, (new Date()).toISOString());
}
}
@@ -44,6 +44,26 @@ export const processKnownPlaceholders = (value: string, title: string, dateForma
const regex = new RegExp("{{day}}", "g");
value = value.replace(regex, format(new Date(), "dd"));
}
if (value.includes("{{hour12}}")) {
const regex = new RegExp("{{hour12}}", "g");
value = value.replace(regex, format(new Date(), "hh"));
}
if (value.includes("{{hour24}}")) {
const regex = new RegExp("{{hour24}}", "g");
value = value.replace(regex, format(new Date(), "HH"));
}
if (value.includes("{{ampm}}")) {
const regex = new RegExp("{{ampm}}", "g");
value = value.replace(regex, format(new Date(), "aaa"));
}
if (value.includes("{{minute}}")) {
const regex = new RegExp("{{minute}}", "g");
value = value.replace(regex, format(new Date(), "mm"));
}
}
return value;