Automatically parse markdown from variables in text bubbles

Closes #539
This commit is contained in:
Baptiste Arnaud
2023-10-05 16:50:17 +02:00
parent 9e6a1f7dc0
commit cfc5f641a6
12 changed files with 1120 additions and 56 deletions

View File

@@ -55,6 +55,35 @@ export const parseVariables =
)
}
type VariableToParseInformation = {
startIndex: number
endIndex: number
textToReplace: string
value: string
}
export const getVariablesToParseInfoInText = (
text: string,
variables: Variable[]
): VariableToParseInformation[] => {
const pattern = /\{\{([^{}]+)\}\}|(\$)\{\{([^{}]+)\}\}/g
const variablesToParseInfo: VariableToParseInformation[] = []
let match
while ((match = pattern.exec(text)) !== null) {
const matchedVarName = match[1] ?? match[3]
const variable = variables.find((variable) => {
return matchedVarName === variable.name && isDefined(variable.value)
}) as VariableWithValue | undefined
variablesToParseInfo.push({
startIndex: match.index,
endIndex: match.index + match[0].length,
textToReplace: match[0],
value: safeStringify(variable?.value) ?? '',
})
}
return variablesToParseInfo
}
const parseVariableValueInJson = (value: VariableWithValue['value']) => {
const stringifiedValue = JSON.stringify(value)
if (typeof value === 'string') return stringifiedValue.slice(1, -1)