Skip to content

Commit a909c0b

Browse files
committed
fix: require label option for macro labels
1 parent 0839756 commit a909c0b

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
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:<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>}}`. |
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. |
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/utils/macroSyntax.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ describe("parseMacroToken", () => {
66
expect(parseMacroToken("")).toBeNull();
77
});
88

9-
it("parses shorthand labels", () => {
10-
const parsed = parseMacroToken("MyMacro|Pick one");
11-
expect(parsed).toEqual({ macroName: "MyMacro", label: "Pick one" });
12-
});
13-
149
it("parses label option", () => {
1510
const parsed = parseMacroToken("MyMacro|label:Pick one");
1611
expect(parsed).toEqual({ macroName: "MyMacro", label: "Pick one" });
1712
});
1813

14+
it("rejects pipe without label option", () => {
15+
const parsed = parseMacroToken("MyMacro|Pick one");
16+
expect(parsed).toBeNull();
17+
});
18+
1919
it("ignores empty label option", () => {
2020
const parsed = parseMacroToken("MyMacro|label:");
2121
expect(parsed).toEqual({ macroName: "MyMacro", label: undefined });

src/utils/macroSyntax.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ export function parseMacroToken(raw: string): ParsedMacroToken | null {
1212
}
1313

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

18-
let label = labelPart;
19-
if (labelPart.toLowerCase().startsWith("label:")) {
20-
label = labelPart.slice("label:".length).trim();
21-
}
17+
const labelPart = raw.slice(pipeIndex + 1).trim();
18+
if (!labelPart) return { macroName };
19+
if (!labelPart.toLowerCase().startsWith("label:")) return null;
20+
const label = labelPart.slice("label:".length).trim();
2221

2322
return {
2423
macroName,

0 commit comments

Comments
 (0)