component mapping in markdown should use the first child for codeblock

This commit is contained in:
Elijah 2024-10-15 16:15:29 +00:00
parent e178cf7c0a
commit 3743492b9f
3 changed files with 23 additions and 1 deletions

View File

@ -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:

View File

@ -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())"
)

View File

@ -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 = (