Skip to content

Commit 20b71b1

Browse files
authored
fix: improve error messages in case of invalid variable names (#32)
* fix: improve error messages in case of invalid variable names Fixes #29. * fix: update error message and readme
1 parent c698d33 commit 20b71b1

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ The currently supported custom variable types are:
9494
| `boolean` | show_summary: boolean |
9595
| `enum` (dropdown list) | color: enum(Red, Yellow) |
9696

97+
You can't use special characters ("@", ",", "#", "+", "(", etc.) or spaces in variable names. However, you can use "_" in variable names.
98+
9799
> **NOTE**: If you declare a custom variable with same name as the built-in variables, the custom variable value will be used.
98100
99101
Internally, [Handlebars.Js](https://handlebarsjs.com/) is used to compile the templates. You can write templates to be compatible with `Handlebars`.

src/parser.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,24 @@ export class Parser {
5959
return variableValues;
6060
}
6161

62+
private checkVariableNames(variableNames: string[]) {
63+
for (const variable of variableNames) {
64+
try {
65+
const compiledBody = Handlebars.compile(`{{ ${variable} }}`);
66+
compiledBody({});
67+
} catch {
68+
throw new Error(`Variable name "${variable}" is invalid.\n\nPlease avoid using special characters ("@", ",", "#", "+", "(", etc.) or spaces in variable names. However, you can use "_" in variable names.`);
69+
}
70+
}
71+
}
72+
6273
private async getVariableInputs(title: string, variables: Record<string, string>) {
6374
if (Object.keys(variables).length == 0) {
6475
return {};
6576
}
6677

78+
this.checkVariableNames(Object.keys(variables));
79+
6780
await setTemplateVariablesView(this.dialog, title, variables);
6881
const dialogResponse = (await joplin.views.dialogs.open(this.dialog));
6982

0 commit comments

Comments
 (0)