diff --git a/docs/docs/FormatSyntax.md b/docs/docs/FormatSyntax.md index 5795afd7..2cc3c15a 100644 --- a/docs/docs/FormatSyntax.md +++ b/docs/docs/FormatSyntax.md @@ -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:}}` | Replace `` with a [Moment.js date format](https://momentjs.com/docs/#/displaying/format/). You could write `{{DATE+3}}` to offset the date with 3 days. | | `{{VDATE:, }}` | 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:}}` | 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:}}` | Execute a macro and write the return value here. | diff --git a/docs/docs/QuickAddAPI.md b/docs/docs/QuickAddAPI.md index f46cbd82..f73f4ec3 100644 --- a/docs/docs/QuickAddAPI.md +++ b/docs/docs/QuickAddAPI.md @@ -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`. diff --git a/src/formatters/formatter.ts b/src/formatters/formatter.ts index 2b646699..8c84317c 100644 --- a/src/formatters/formatter.ts +++ b/src/formatters/formatter.ts @@ -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)) { @@ -66,7 +62,7 @@ export abstract class Formatter { output = this.replacer( output, DATE_REGEX_FORMATTED, - getDate({ format, offset: offset }) + getDate({ format, offset: offset }), ); } @@ -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)) { @@ -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; + protected abstract promptForValue(header?: string): Promise | string; protected async replaceValueInString(input: string): Promise { 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(); @@ -119,7 +109,6 @@ export abstract class Formatter { return output; } - protected async replaceSelectedInString(input: string): Promise { let output: string = input; @@ -134,7 +123,7 @@ export abstract class Formatter { // eslint-disable-next-line @typescript-eslint/require-await protected async replaceLinkToCurrentFileInString( - input: string + input: string, ): Promise { const currentFilePathLink = this.getCurrentFileLink(); let output = input; @@ -147,7 +136,7 @@ export abstract class Formatter { output = this.replacer( output, LINK_TO_CURRENT_FILE_REGEX, - currentFilePathLink + currentFilePathLink, ); return output; @@ -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; @@ -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; @@ -249,7 +238,7 @@ export abstract class Formatter { output = this.replacer( output, MACRO_REGEX, - macroOutput ? macroOutput.toString() : "" + macroOutput ? macroOutput.toString() : "", ); } @@ -259,7 +248,7 @@ export abstract class Formatter { protected abstract getVariableValue(variableName: string): string; protected abstract suggestForValue( - suggestedValues: string[] + suggestedValues: string[], ): Promise | string; // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -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 = ( @@ -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; @@ -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; + protected abstract getMacroValue(macroName: string): Promise | string; protected abstract promptForVariable(variableName: string): Promise; - protected abstract getTemplateContent( - templatePath: string - ): Promise; + protected abstract getTemplateContent(templatePath: string): Promise; protected abstract getSelectedText(): Promise; }