Skip to content

Commit

Permalink
#450 - Additional date placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Nov 4, 2022
1 parent e864d56 commit 5205b2d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 24 additions & 4 deletions src/helpers/PlaceholderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}")) {
Expand All @@ -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());
}
}

Expand All @@ -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;
Expand Down

0 comments on commit 5205b2d

Please sign in to comment.