diff --git a/reflex/components/markdown/markdown.py b/reflex/components/markdown/markdown.py index 1665144fd..27d8034c6 100644 --- a/reflex/components/markdown/markdown.py +++ b/reflex/components/markdown/markdown.py @@ -20,6 +20,9 @@ from reflex.components.tags.tag import Tag from reflex.utils import types from reflex.utils.imports import ImportDict, ImportVar from reflex.vars.base import LiteralVar, Var +from reflex.vars.sequence import string_ternary_operation +from reflex.vars.function import ARRAY_ISARRAY + # Special vars used in the component map. _CHILDREN = Var(_js_expr="children", _var_type=str) @@ -199,7 +202,7 @@ class Markdown(Component): raise ValueError(f"No markdown component found for tag: {tag}.") special_props = [_PROPS_IN_TAG] - children = [_CHILDREN] + children = [_CHILDREN if not tag == "codeblock" else string_ternary_operation(ARRAY_ISARRAY.call(_CHILDREN), _CHILDREN.to(LiteralVar)[0], _CHILDREN)] # For certain tags, the props from the markdown renderer are not actually valid for the component. if tag in NO_PROPS_TAGS: diff --git a/reflex/vars/function.py b/reflex/vars/function.py index a1f7fb7bd..9d734a458 100644 --- a/reflex/vars/function.py +++ b/reflex/vars/function.py @@ -180,6 +180,7 @@ class ArgsFunctionOperation(CachedVarOperation, FunctionVar): JSON_STRINGIFY = FunctionStringVar.create("JSON.stringify") +ARRAY_ISARRAY = FunctionStringVar.create("Array.isArray") PROTOTYPE_TO_STRING = FunctionStringVar.create( "((__to_string) => __to_string.toString())" ) diff --git a/reflex/vars/sequence.py b/reflex/vars/sequence.py index 39139ce3f..675d15d88 100644 --- a/reflex/vars/sequence.py +++ b/reflex/vars/sequence.py @@ -548,6 +548,24 @@ def string_replace_operation( var_type=str, ) +def string_ternary_operation(condition, true_operation, false_operation): + """ + This function generates the JavaScript ternary operation as a string. + + Parameters: + condition: The condition for the ternary operation. + true_operation: The operation if the condition is true. + false_operation: The operation if the condition is false. + + Returns: + A string representing the JavaScript ternary operation. + """ + return var_operation_return( + js_expression=f"({condition} ? {true_operation} : {false_operation})", + var_type=str, + ) + + # Compile regex for finding reflex var tags. _decode_var_pattern_re = (