2
0

🐛 Fix inline code parsing on text with multi vars

This commit is contained in:
Baptiste Arnaud
2024-08-13 16:32:46 +02:00
parent 9218ef801d
commit d4e612a285

View File

@@ -97,7 +97,7 @@ export const getVariablesToParseInfoInText = (
takeLatestIfList, takeLatestIfList,
}: { variables: Variable[]; takeLatestIfList?: boolean } }: { variables: Variable[]; takeLatestIfList?: boolean }
): VariableToParseInformation[] => { ): VariableToParseInformation[] => {
const variablesToParseInfo: VariableToParseInformation[] = [] const inlineVarsParseInfo: VariableToParseInformation[] = []
const inlineCodeMatches = [...text.matchAll(inlineCodeRegex)] const inlineCodeMatches = [...text.matchAll(inlineCodeRegex)]
inlineCodeMatches.forEach((match) => { inlineCodeMatches.forEach((match) => {
if (isNotDefined(match.index) || !match[0].length) return if (isNotDefined(match.index) || !match[0].length) return
@@ -105,7 +105,7 @@ export const getVariablesToParseInfoInText = (
const evaluatedValue = evaluateInlineCode(inlineCodeToEvaluate, { const evaluatedValue = evaluateInlineCode(inlineCodeToEvaluate, {
variables, variables,
}) })
variablesToParseInfo.push({ inlineVarsParseInfo.push({
startIndex: match.index, startIndex: match.index,
endIndex: match.index + match[0].length, endIndex: match.index + match[0].length,
textToReplace: match[0], textToReplace: match[0],
@@ -117,19 +117,20 @@ export const getVariablesToParseInfoInText = (
) ?? '', ) ?? '',
}) })
}) })
const textWithInlineCodeParsed = text.replace( const variablesParseInfo: VariableToParseInformation[] = []
inlineCodeRegex, const variableMatches = [...text.matchAll(variableRegex)]
(_full, inlineCodeToEvaluate) =>
evaluateInlineCode(inlineCodeToEvaluate, { variables })
)
const variableMatches = [...textWithInlineCodeParsed.matchAll(variableRegex)]
variableMatches.forEach((match) => { variableMatches.forEach((match) => {
if (isNotDefined(match.index) || !match[0].length) return if (isNotDefined(match.index) || !match[0].length) return
const isPartOfInlineCode = inlineVarsParseInfo.some(
(inVar) =>
match.index >= inVar.startIndex && match.index <= inVar.endIndex
)
if (isPartOfInlineCode) return
const matchedVarName = match[1] ?? match[3] const matchedVarName = match[1] ?? match[3]
const variable = variables.find((variable) => { const variable = variables.find((variable) => {
return matchedVarName === variable.name return matchedVarName === variable.name
}) as VariableWithValue | undefined }) as VariableWithValue | undefined
variablesToParseInfo.push({ variablesParseInfo.push({
startIndex: match.index, startIndex: match.index,
endIndex: match.index + match[0].length, endIndex: match.index + match[0].length,
textToReplace: match[0], textToReplace: match[0],
@@ -142,7 +143,9 @@ export const getVariablesToParseInfoInText = (
variableId: variable?.id, variableId: variable?.id,
}) })
}) })
return variablesToParseInfo.sort((a, b) => a.startIndex - b.startIndex) return inlineVarsParseInfo
.concat(variablesParseInfo)
.sort((a, b) => a.startIndex - b.startIndex)
} }
const parseVariableValueInJson = (value: VariableWithValue['value']) => { const parseVariableValueInJson = (value: VariableWithValue['value']) => {