33 shouldBeRandomized ,
44 randomizeArrayWithFixedElements ,
55} from "../../../utils/arrays.mjs" ;
6- import { evaluateExpression } from "../../../markdown/custom/variablesDynamic/evaluateExpression.mjs" ;
76import { processDirectiveSelect } from "../../../markdown/custom/directives/select.mjs" ;
87
98export function responseToSelectedChoiceOption ( chatbot , choiceOptionLink ) {
@@ -36,40 +35,6 @@ export function processMessageWithChoiceOptions(
3635 choiceOptions ,
3736) {
3837 // Si on a du contenu dynamique et qu'on utilise <!-- if @VARIABLE==VALEUR … --> on filtre d'abord les options si elles dépendent d'une variable
39- let dynamicVariables = chatbot . dynamicVariables ;
40- if ( yaml && yaml . dynamicContent ) {
41- if ( choiceOptions ) {
42- choiceOptions = choiceOptions . filter ( ( option ) => {
43- let condition = option . condition ;
44- if ( ! condition ) {
45- return true ;
46- } else {
47- // Remplace les variables personnalisées dans la condition
48- condition = condition . replace (
49- / @ ( [ ^ \s ( ) & | ! = < > ] + ) / g,
50- function ( match , varName ) {
51- return (
52- 'tryConvertStringToNumber(dynamicVariables["' +
53- varName . trim ( ) +
54- '"])'
55- ) ;
56- } ,
57- ) ;
58- // Gestion des valeurs si elles ne sont pas mises entre guillemets + gestion du cas undefined
59- condition = condition
60- . replaceAll (
61- / ( = = | ! = | < = | > = | < | > ) ? ( .* ?) ? ( \) | & | \| | $ ) / g,
62- function ( match , comparisonSignLeft , value , comparisonSignRight ) {
63- return `${ comparisonSignLeft } "${ value } " ${ comparisonSignRight } ` ;
64- } ,
65- )
66- . replaceAll ( '""' , '"' )
67- . replace ( '"undefined"' , "undefined" ) ;
68- return evaluateExpression ( condition , dynamicVariables ) ;
69- }
70- } ) ;
71- }
72- }
7338
7439 // S'il y a la directive !Select: x on sélectionne aléatoirement seulement x options dans l'ensemble des options disponibles
7540 [ response , choiceOptions ] = processDirectiveSelect ( response , choiceOptions ) ;
@@ -85,15 +50,17 @@ export function processMessageWithChoiceOptions(
8550 const choiceOptionsLength = choiceOptions . length ;
8651 for ( let i = 0 ; i < choiceOptionsLength ; i ++ ) {
8752 const choiceOption = choiceOptions [ i ] ;
88- const choiceOptionText = choiceOption . text ;
89- const choiceOptionLink = choiceOption . link ;
53+ const hasConditions =
54+ yaml && yaml . dynamicContent && choiceOption . condition ;
9055 messageOptions =
9156 messageOptions +
57+ ( hasConditions ? "\n`if " + choiceOption . condition + "`" : "" ) +
9258 '<li><a href="#' +
93- choiceOptionLink +
59+ choiceOption . link +
9460 '">' +
95- choiceOptionText +
96- "</a></li>\n" ;
61+ choiceOption . text +
62+ "</a></li>\n" +
63+ ( hasConditions ? "\n`endif`" : "" ) ;
9764 }
9865 messageOptions = messageOptions + "</ul>" ;
9966 response = response + messageOptions ;
0 commit comments