use ternary operator in numbers.py, move code block args to class for docs discoverability

This commit is contained in:
Elijah 2024-10-16 12:51:02 +00:00
parent 3743492b9f
commit 665179309e
3 changed files with 12 additions and 27 deletions

View File

@ -363,6 +363,7 @@ LiteralCodeTheme = Literal[
"nord",
"one-dark-pro",
"one-light",
"plain",
"plastic",
"poimandres",
"red",
@ -668,20 +669,22 @@ class ShikiHighLevelCodeBlock(ShikiCodeBlock):
# If this is enabled line numbers will be shown next to the code block.
show_line_numbers: Var[bool]
# Whether a copy button should appear.
can_copy: Var[bool] = False
# copy_button: A custom copy button to override the default one.
copy_button: Var[Optional[Union[Component | bool]]] = None
@classmethod
def create(
cls,
*children,
can_copy: bool | None = False,
copy_button: bool | Component | None = None,
**props,
) -> Component:
"""Create a code block component using [shiki syntax highlighter](https://shiki.matsu.io/).
Args:
*children: The children of the component.
can_copy: Whether a copy button should appear.
copy_button: A custom copy button to override the default one.
**props: The props to pass to the component.
Returns:
@ -690,6 +693,8 @@ class ShikiHighLevelCodeBlock(ShikiCodeBlock):
use_transformers = props.pop("use_transformers", False)
show_line_numbers = props.pop("show_line_numbers", False)
language = props.pop("language", None)
can_copy = props.pop("can_copy", False)
copy_button = props.pop("copy_button", None)
if use_transformers:
props["transformers"] = [ShikiJsTransformer()]
@ -704,7 +709,7 @@ class ShikiHighLevelCodeBlock(ShikiCodeBlock):
theme = props.pop("theme", None)
props["theme"] = props["theme"] = (
cls._map_themes(theme)
if theme
if theme is not None
else color_mode_cond( # Default color scheme responds to global color mode.
light="one-light",
dark="one-dark-pro",
@ -751,8 +756,6 @@ class ShikiHighLevelCodeBlock(ShikiCodeBlock):
),
)
)
else:
copy_button = None
if copy_button:
return ShikiCodeBlock.create(

View File

@ -20,7 +20,7 @@ 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.number import ternary_operation
from reflex.vars.function import ARRAY_ISARRAY
@ -202,7 +202,7 @@ class Markdown(Component):
raise ValueError(f"No markdown component found for tag: {tag}.")
special_props = [_PROPS_IN_TAG]
children = [_CHILDREN if not tag == "codeblock" else string_ternary_operation(ARRAY_ISARRAY.call(_CHILDREN), _CHILDREN.to(LiteralVar)[0], _CHILDREN)]
children = [_CHILDREN if not tag == "codeblock" else ternary_operation(ARRAY_ISARRAY.call(_CHILDREN), _CHILDREN.to(LiteralVar)[0], _CHILDREN).to(str)]
# For certain tags, the props from the markdown renderer are not actually valid for the component.
if tag in NO_PROPS_TAGS:

View File

@ -548,24 +548,6 @@ 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 = (