Skip to content

Commit

Permalink
feat: add {{time}} support
Browse files Browse the repository at this point in the history
re #364 and #464
  • Loading branch information
chhoumann committed Apr 21, 2023
1 parent c536135 commit 9e322b6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const VALUE_SYNTAX = "{{value}}";
export const DATE_SYNTAX = "{{date}}";
export const TIME_SYNTAX = "{{time}}";
export const NAME_SYNTAX = "{{name}}";
export const VARIABLE_SYNTAX = "{{value:<variable name>}}";
export const FIELD_VAR_SYNTAX = "{{field:<field name>}}";
Expand Down Expand Up @@ -46,6 +47,8 @@ export const DATE_REGEX = new RegExp(/{{DATE(\+-?[0-9]+)?}}/i);
export const DATE_REGEX_FORMATTED = new RegExp(
/{{DATE:([^}\n\r+]*)(\+-?[0-9]+)?}}/i
);
export const TIME_REGEX = new RegExp(/{{TIME}}/i);
export const TIME_REGEX_FORMATTED = new RegExp(/{{TIME:([^}\n\r+]*)}}/i);
export const NAME_VALUE_REGEX = new RegExp(/{{NAME}}|{{VALUE}}/i);
export const VARIABLE_REGEX = new RegExp(/{{VALUE:([^\n\r}]*)}}/i);
export const FIELD_VAR_REGEX = new RegExp(/{{FIELD:([^\n\r}]*)}}/i);
Expand Down Expand Up @@ -111,6 +114,10 @@ export const TITLE_SYNTAX_SUGGEST_REGEX = new RegExp(
export const SELECTED_SYNTAX_SUGGEST_REGEX = new RegExp(
/{{[S]?[E]?[L]?[E]?[C]?[T]?[E]?[D]?[}]?[}]?/i
);
export const TIME_SYNTAX_SUGGEST_REGEX = new RegExp(/{{[T]?[I]?[M]?[E]?[}]?[}]?/i);
export const TIME_FORMAT_SYNTAX_SUGGEST_REGEX = new RegExp(
/{{[T]?[I]?[M]?[E]?[:]?$|{{TIME:[^\n\r}]*}}$/i
)

// == File Exists (Template Choice) == //
export const fileExistsIncrement = "Increment the file name" as const;
Expand Down
1 change: 1 addition & 0 deletions src/formatters/completeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class CompleteFormatter extends Formatter {
output = await this.replaceMacrosInString(output);
output = await this.replaceTemplateInString(output);
output = this.replaceDateInString(output);
output = this.replaceTimeInString(output);
output = await this.replaceValueInString(output);
output = await this.replaceSelectedInString(output);
output = await this.replaceDateVariableInString(output);
Expand Down
1 change: 1 addition & 0 deletions src/formatters/fileNameDisplayFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class FileNameDisplayFormatter extends Formatter {

output = await this.replaceMacrosInString(output);
output = this.replaceDateInString(output);
output = this.replaceTimeInString(output);
output = await this.replaceValueInString(output);
output = await this.replaceDateVariableInString(output);
output = await this.replaceVariableInString(output);
Expand Down
1 change: 1 addition & 0 deletions src/formatters/formatDisplayFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class FormatDisplayFormatter extends Formatter {
let output: string = input;

output = this.replaceDateInString(output);
output = this.replaceTimeInString(output);
output = await this.replaceValueInString(output);
output = await this.replaceDateVariableInString(output);
output = await this.replaceVariableInString(output);
Expand Down
32 changes: 32 additions & 0 deletions src/formatters/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
VARIABLE_REGEX,
FIELD_VAR_REGEX,
SELECTED_REGEX,
TIME_REGEX,
TIME_REGEX_FORMATTED,
} from "../constants";
import { getDate } from "../utilityObsidian";

Expand Down Expand Up @@ -71,6 +73,36 @@ export abstract class Formatter {
return output;
}

protected replaceTimeInString(input: string): string {
let output: string = input;

while (TIME_REGEX.test(output)) {
const timeMatch = TIME_REGEX.exec(output);
if (!timeMatch) throw new Error("unable to parse time");

output = this.replacer(
output,
TIME_REGEX,
getDate({ format: "HH:mm" })
)
}

while (TIME_REGEX_FORMATTED.test(output)) {
const timeMatch = TIME_REGEX_FORMATTED.exec(output);
if (!timeMatch) throw new Error("unable to parse time");

const format = timeMatch[1];

output = this.replacer(
output,
TIME_REGEX_FORMATTED,
getDate({ format })
)
}

return output;
}

protected abstract promptForValue(
header?: string
): Promise<string> | string;
Expand Down
9 changes: 8 additions & 1 deletion src/gui/suggesters/formatSyntaxSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DATE_FORMAT_SYNTAX_SUGGEST_REGEX,
DATE_SYNTAX,
DATE_SYNTAX_SUGGEST_REGEX,
TIME_SYNTAX,
LINKCURRENT_SYNTAX,
LINKCURRENT_SYNTAX_SUGGEST_REGEX,
MACRO_SYNTAX_SUGGEST_REGEX,
Expand All @@ -18,6 +19,7 @@ import {
VARIABLE_SYNTAX_SUGGEST_REGEX,
SELECTED_SYNTAX_SUGGEST_REGEX,
SELECTED_SYNTAX,
TIME_SYNTAX_SUGGEST_REGEX,
} from "../../constants";
import type QuickAdd from "../../main";

Expand All @@ -32,6 +34,8 @@ enum FormatSyntaxToken {
Macro,
Template,
MathValue,
Time,
Selected
}

export class FormatSyntaxSuggester extends TextInputSuggest<string> {
Expand Down Expand Up @@ -160,6 +164,9 @@ export class FormatSyntaxSuggester extends TextInputSuggest<string> {
const dateMatch = DATE_SYNTAX_SUGGEST_REGEX.exec(input);
if (dateMatch) callback(dateMatch, FormatSyntaxToken.Date, DATE_SYNTAX);

const timeMatch = TIME_SYNTAX_SUGGEST_REGEX.exec(input);
if (timeMatch) callback(timeMatch, FormatSyntaxToken.Time, TIME_SYNTAX);

const nameMatch = NAME_SYNTAX_SUGGEST_REGEX.exec(input);
if (nameMatch) callback(nameMatch, FormatSyntaxToken.Name, NAME_SYNTAX);

Expand All @@ -177,7 +184,7 @@ export class FormatSyntaxSuggester extends TextInputSuggest<string> {

const selectedMatch = SELECTED_SYNTAX_SUGGEST_REGEX.exec(input);
if (selectedMatch)
callback(selectedMatch, FormatSyntaxToken.Macro, SELECTED_SYNTAX);
callback(selectedMatch, FormatSyntaxToken.Selected, SELECTED_SYNTAX);

const variableMatch = VARIABLE_SYNTAX_SUGGEST_REGEX.exec(input);
if (variableMatch)
Expand Down

1 comment on commit 9e322b6

@vercel
Copy link

@vercel vercel bot commented on 9e322b6 Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

quickadd – ./

quickadd-chrisbbh.vercel.app
quickadd-git-master-chrisbbh.vercel.app
quickadd.obsidian.guide

Please sign in to comment.