3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-13 21:35:02 +00:00

templateFormatter: return empty string for null/undefined variables; Tags: catch template syntax errors on tag creation; add mention() tag function

This commit is contained in:
Dragory 2019-03-16 16:59:01 +02:00
parent db5d93b5c2
commit f27da2c56f
2 changed files with 55 additions and 23 deletions

View file

@ -94,9 +94,7 @@ export function parseTemplate(str: string): ParsedTemplate {
inVar = false;
};
let i = -1;
for (const char of chars) {
i++;
for (const [i, char] of chars.entries()) {
if (inVar) {
if (currentVar) {
if (currentVar._state.inArg) {
@ -130,7 +128,7 @@ export function parseTemplate(str: string): ParsedTemplate {
} else if (char === '"') {
// A double quote can start a string argument, but only if we haven't committed to some other type of argument already
if (currentVar._state.currentArgType !== null) {
throw new TemplateParseError(`Unexpected char: ${char} at ${i}`);
throw new TemplateParseError(`Unexpected char ${char} at ${i}`);
}
currentVar._state.currentArgType = "string";
@ -138,7 +136,7 @@ export function parseTemplate(str: string): ParsedTemplate {
} else if (char.match(/\d/)) {
// A number can start a string argument, but only if we haven't committed to some other type of argument already
if (currentVar._state.currentArgType !== null) {
throw new TemplateParseError(`Unexpected char: ${char}`);
throw new TemplateParseError(`Unexpected char ${char} at ${i}`);
}
currentVar._state.currentArgType = "number";
@ -153,7 +151,7 @@ export function parseTemplate(str: string): ParsedTemplate {
currentVar._state.currentArg = newVar;
currentVar = newVar;
} else {
throw new TemplateParseError(`Unexpected char: ${char}`);
throw new TemplateParseError(`Unexpected char ${char} at ${i}`);
}
} else {
if (char === "(") {
@ -180,7 +178,7 @@ export function parseTemplate(str: string): ParsedTemplate {
if (char === "}") {
exitInjectedVar();
} else {
throw new TemplateParseError(`Unexpected char: ${char}`);
throw new TemplateParseError(`Unexpected char ${char} at ${i}`);
}
}
} else {
@ -233,10 +231,11 @@ async function evaluateTemplateVariable(theVar: ITemplateVar, values) {
}
}
return value(...args);
const result = await value(...args);
return result == null ? "" : result;
}
return value;
return value == null ? "" : value;
}
export async function renderParsedTemplate(parsedTemplate: ParsedTemplate, values: any) {