From 9f4c3e9f0232048c212012a61c528725f072f7f8 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Tue, 20 Aug 2024 10:38:20 +0800 Subject: [PATCH] fix: ensure conditional blocks without matching else() do not render content --- frontend/providers/template/src/utils/json-yaml.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/frontend/providers/template/src/utils/json-yaml.ts b/frontend/providers/template/src/utils/json-yaml.ts index cc7b79774a1d..25875471ebd0 100644 --- a/frontend/providers/template/src/utils/json-yaml.ts +++ b/frontend/providers/template/src/utils/json-yaml.ts @@ -231,13 +231,14 @@ const __parseYamlIfEndif = (yamlStr: string, evaluateExpression: (exp: string) = const end = yamlStr.substring(match.index! + match[0].length); let between = ''; + let conditionMet = false; if (elifElseMatches.length === 0) { const ifResult = evaluateExpression(ifMatch[2]); if (ifResult) { between = yamlStr.substring(ifMatch.index! + ifMatch[0].length, match.index); + conditionMet = true; } } else { - let conditionMet = false; for (const clause of [ifMatch, ...elifElseMatches]) { const expression = clause[2]; if (clause[1] === 'else' || evaluateExpression(expression)) { @@ -246,10 +247,10 @@ const __parseYamlIfEndif = (yamlStr: string, evaluateExpression: (exp: string) = break; } } + } - if (!conditionMet) { - between = yamlStr.substring(elifElseMatches[elifElseMatches.length - 1].index! + elifElseMatches[elifElseMatches.length - 1][0].length, match.index); - } + if (!conditionMet && elifElseMatches.length === 0) { + between = ''; } return __parseYamlIfEndif(start + between + end, evaluateExpression);