Skip to content

Commit 0839756

Browse files
committed
feat: add label option to macro syntax
1 parent 612a94b commit 0839756

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

docs/docs/FormatSyntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ title: Format syntax
1717
| `{{LINKCURRENT}}` | A link to the file from which the template or capture was triggered (`[[link]]` format). When the append-link setting is set to **Enabled (skip if no active file)**, this token resolves to an empty string instead of throwing an error if no note is focused. |
1818
| `{{FILENAMECURRENT}}` | The basename (without extension) of the file from which the template or capture was triggered. Honors the same **required/optional** behavior as `{{LINKCURRENT}}` - when optional and no active file exists, resolves to an empty string. |
1919
| `{{MACRO:<MACRONAME>}}` | Execute a macro and write the return value here. |
20-
| `{{MACRO:<MACRONAME>\|<label>}}` | Executes the macro but shows the label as the placeholder when the macro prompts you to choose an export from a script object. This is helpful when multiple macro calls show similar lists. |
20+
| `{{MACRO:<MACRONAME>\|label:<label>}}` | Executes the macro but shows the label as the placeholder when the macro prompts you to choose an export from a script object. This is helpful when multiple macro calls show similar lists. You can also use the shorthand `{{MACRO:<MACRONAME>\|<label>}}`. |
2121
| `{{TEMPLATE:<TEMPLATEPATH>}}` | Include templates in your `format`. Supports Templater syntax. |
2222
| `{{GLOBAL_VAR:<name>}}` | Inserts the value of a globally defined snippet from QuickAdd settings. Snippet values can include other QuickAdd tokens (e.g., `{{VALUE:...}}`, `{{VDATE:...}}`) and are processed by the usual formatter passes. Names match case‑insensitively in the token. |
2323
| `{{MVALUE}}` | Math modal for writing LaTeX. Use CTRL + Enter to submit. |

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const FORMAT_SYNTAX: string[] = [
3838
LINKCURRENT_SYNTAX,
3939
FILENAMECURRENT_SYNTAX,
4040
"{{macro:<macroname>}}",
41-
"{{macro:<macroname>|<label>}}",
41+
"{{macro:<macroname>|label:<label>}}",
4242
"{{template:<templatepath>}}",
4343
MATH_VALUE_SYNTAX,
4444
SELECTED_SYNTAX,

src/gui/suggesters/formatSyntaxSuggester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class FormatSyntaxSuggester extends TextInputSuggest<string> {
240240
(macroName) => `{{MACRO:${macroName}}}`
241241
)
242242
);
243-
suggestions.push("{{MACRO:MyMacro|Label}}");
243+
suggestions.push("{{MACRO:MyMacro|label:Label}}");
244244
} else if (tokenDef.token === FormatSyntaxToken.VariableDate) {
245245
// Add example suggestions for VDATE with and without default values
246246
suggestions.push(

src/utils/macroSyntax.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, expect, it } from "vitest";
2+
import { parseMacroToken } from "./macroSyntax";
3+
4+
describe("parseMacroToken", () => {
5+
it("returns null for empty input", () => {
6+
expect(parseMacroToken("")).toBeNull();
7+
});
8+
9+
it("parses shorthand labels", () => {
10+
const parsed = parseMacroToken("MyMacro|Pick one");
11+
expect(parsed).toEqual({ macroName: "MyMacro", label: "Pick one" });
12+
});
13+
14+
it("parses label option", () => {
15+
const parsed = parseMacroToken("MyMacro|label:Pick one");
16+
expect(parsed).toEqual({ macroName: "MyMacro", label: "Pick one" });
17+
});
18+
19+
it("ignores empty label option", () => {
20+
const parsed = parseMacroToken("MyMacro|label:");
21+
expect(parsed).toEqual({ macroName: "MyMacro", label: undefined });
22+
});
23+
});

src/utils/macroSyntax.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ export function parseMacroToken(raw: string): ParsedMacroToken | null {
1212
}
1313

1414
const macroName = raw.slice(0, pipeIndex).trim();
15-
const label = raw.slice(pipeIndex + 1).trim();
15+
const labelPart = raw.slice(pipeIndex + 1).trim();
1616
if (!macroName) return null;
1717

18+
let label = labelPart;
19+
if (labelPart.toLowerCase().startsWith("label:")) {
20+
label = labelPart.slice("label:".length).trim();
21+
}
22+
1823
return {
1924
macroName,
2025
label: label || undefined,

0 commit comments

Comments
 (0)