Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/FormatSyntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: Format syntax
| `{{DATE}}` | Outputs the current date in `YYYY-MM-DD` format. You could write `{{DATE+3}}` to offset the date with 3 days. You can use `+-3` to offset with `-3` days. |
| `{{DATE:<DATEFORMAT>}}` | Replace `<DATEFORMAT>` with a [Moment.js date format](https://momentjs.com/docs/#/displaying/format/). You could write `{{DATE<DATEFORMAT>+3}}` to offset the date with 3 days. |
| `{{VDATE:<variable name>, <date format>}}` | You'll get prompted to enter a date and it'll be parsed to the given date format. You could write 'today' or 'in two weeks' and it'll give you the date for that. Works like variables, so you can use the date in multiple places. **REQUIRES THE NATURAL LANGUAGE DATES PLUGIN!** |
| `{{VALUE}}` or `{{NAME}}` | Interchangeable. Represents the value given in an input prompt. If text is selected in the current editor, it will be used as the value. |
| `{{VALUE}}` or `{{NAME}}` | Interchangeable. Represents the value given in an input prompt. If text is selected in the current editor, it will be used as the value. When using the QuickAdd API, this can be passed programmatically using the reserved variable name 'value'. |
| `{{VALUE:<variable name>}}` | You can now use variable names in values. They'll get saved and inserted just like values, but the difference is that you can have as many of them as you want. Use comma separation to get a suggester rather than a prompt. |
| `{{LINKCURRENT}}` | A link to the file from which the template is activated from. `[[link]]` format. |
| `{{MACRO:<MACRONAME>}}` | Execute a macro and write the return value here. |
Expand Down
9 changes: 9 additions & 0 deletions docs/docs/QuickAddAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ massiveDataArray.forEach(async (data) => {

This would execute the choice for each item in the array, passing the data as a variable. This means you can access the variables from within your Capture with `{{VALUE:X}}` (and so on, for each key-value pair in the object).

Additionally, you can use the reserved variable name 'value' to pass a value directly to `{{VALUE}}` or `{{NAME}}` format tags:

```js
await params.quickAddApi.executeChoice('My Template Choice', {
value: "This text will be used for {{VALUE}} tags",
customVar: "This will be available as {{VALUE:customVar}}"
});
```

## Utility module
Given by `api.utility`.

Expand Down
71 changes: 26 additions & 45 deletions src/formatters/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ export abstract class Formatter {
const offsetIsInt = NUMBER_REGEX.test(offsetString);
if (offsetIsInt) offset = parseInt(offsetString);
}
output = this.replacer(
output,
DATE_REGEX,
getDate({ offset: offset })
);
output = this.replacer(output, DATE_REGEX, getDate({ offset: offset }));
}

while (DATE_REGEX_FORMATTED.test(output)) {
Expand All @@ -66,7 +62,7 @@ export abstract class Formatter {
output = this.replacer(
output,
DATE_REGEX_FORMATTED,
getDate({ format, offset: offset })
getDate({ format, offset: offset }),
);
}

Expand All @@ -80,11 +76,7 @@ export abstract class Formatter {
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" })
)
output = this.replacer(output, TIME_REGEX, getDate({ format: "HH:mm" }));
}

while (TIME_REGEX_FORMATTED.test(output)) {
Expand All @@ -93,23 +85,21 @@ export abstract class Formatter {

const format = timeMatch[1];

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

return output;
}

protected abstract promptForValue(
header?: string
): Promise<string> | string;
protected abstract promptForValue(header?: string): Promise<string> | string;

protected async replaceValueInString(input: string): Promise<string> {
let output: string = input;

if (this.variables.has("value")) {
this.value = this.variables.get("value") as string;
}

while (NAME_VALUE_REGEX.test(output)) {
if (!this.value) this.value = await this.promptForValue();

Expand All @@ -119,7 +109,6 @@ export abstract class Formatter {
return output;
}


protected async replaceSelectedInString(input: string): Promise<string> {
let output: string = input;

Expand All @@ -134,7 +123,7 @@ export abstract class Formatter {

// eslint-disable-next-line @typescript-eslint/require-await
protected async replaceLinkToCurrentFileInString(
input: string
input: string,
): Promise<string> {
const currentFilePathLink = this.getCurrentFileLink();
let output = input;
Expand All @@ -147,7 +136,7 @@ export abstract class Formatter {
output = this.replacer(
output,
LINK_TO_CURRENT_FILE_REGEX,
currentFilePathLink
currentFilePathLink,
);

return output;
Expand All @@ -171,19 +160,19 @@ export abstract class Formatter {
if (suggestedValues.length === 1)
this.variables.set(
variableName,
await this.promptForVariable(variableName)
await this.promptForVariable(variableName),
);
else
this.variables.set(
variableName,
await this.suggestForValue(suggestedValues)
await this.suggestForValue(suggestedValues),
);
}

output = this.replacer(
output,
VARIABLE_REGEX,
this.getVariableValue(variableName)
this.getVariableValue(variableName),
);
} else {
break;
Expand All @@ -206,14 +195,14 @@ export abstract class Formatter {
if (!this.getVariableValue(variableName)) {
this.variables.set(
variableName,
await this.suggestForField(variableName)
await this.suggestForField(variableName),
);
}

output = this.replacer(
output,
FIELD_VAR_REGEX,
this.getVariableValue(variableName)
this.getVariableValue(variableName),
);
} else {
break;
Expand Down Expand Up @@ -249,7 +238,7 @@ export abstract class Formatter {
output = this.replacer(
output,
MACRO_REGEX,
macroOutput ? macroOutput.toString() : ""
macroOutput ? macroOutput.toString() : "",
);
}

Expand All @@ -259,7 +248,7 @@ export abstract class Formatter {
protected abstract getVariableValue(variableName: string): string;

protected abstract suggestForValue(
suggestedValues: string[]
suggestedValues: string[],
): Promise<string> | string;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -279,16 +268,12 @@ export abstract class Formatter {
if (!this.variables.get(variableName)) {
this.variables.set(
variableName,
await this.promptForVariable(variableName)
await this.promptForVariable(variableName),
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const nld = this.getNaturalLanguageDates();
if (
!nld ||
!nld.parseDate ||
typeof nld.parseDate !== "function"
)
if (!nld || !nld.parseDate || typeof nld.parseDate !== "function")
continue;

const parseAttempt = (
Expand All @@ -300,20 +285,20 @@ export abstract class Formatter {
if (parseAttempt)
this.variables.set(
variableName,
parseAttempt.moment.format(dateFormat)
parseAttempt.moment.format(dateFormat),
);
else
throw new Error(
`unable to parse date variable ${this.variables.get(
variableName
)}`
variableName,
)}`,
);
}

output = this.replacer(
output,
DATE_VARIABLE_REGEX,
this.variables.get(variableName) as string // literally setting it above / throwing error if not set
this.variables.get(variableName) as string, // literally setting it above / throwing error if not set
);
} else {
break;
Expand Down Expand Up @@ -359,15 +344,11 @@ export abstract class Formatter {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected abstract getNaturalLanguageDates(): any;

protected abstract getMacroValue(
macroName: string
): Promise<string> | string;
protected abstract getMacroValue(macroName: string): Promise<string> | string;

protected abstract promptForVariable(variableName: string): Promise<string>;

protected abstract getTemplateContent(
templatePath: string
): Promise<string>;
protected abstract getTemplateContent(templatePath: string): Promise<string>;

protected abstract getSelectedText(): Promise<string>;
}