-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENG-4010]Codeblock cleanup in markdown (#4233)
* Codeblock cleanup in markdown * Initial approach to getting this working with rx.memo and reflex web * abstract the map var logic * the tests are not valid + pyright fix * darglint fix * Add unit tests plus mix components * pyi run * rebase on main * fix darglint * testing different OS * revert * This should fix it. Right? * Fix tests * minor fn signature fix * use ArgsFunctionOperation * use destructured args and pass the tests * fix remaining unit tests * fix pyi files * rebase on main * move language regex on codeblock to markdown * fix tests --------- Co-authored-by: Khaleel Al-Adhami <[email protected]>
- Loading branch information
1 parent
3d85936
commit cd59ab5
Showing
27 changed files
with
565 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,14 @@ | |
from reflex.components.component import Component, ComponentNamespace | ||
from reflex.components.core.cond import color_mode_cond | ||
from reflex.components.lucide.icon import Icon | ||
from reflex.components.markdown.markdown import _LANGUAGE, MarkdownComponentMap | ||
from reflex.components.radix.themes.components.button import Button | ||
from reflex.components.radix.themes.layout.box import Box | ||
from reflex.constants.colors import Color | ||
from reflex.event import set_clipboard | ||
from reflex.style import Style | ||
from reflex.utils import console, format | ||
from reflex.utils.imports import ImportDict, ImportVar | ||
from reflex.utils.imports import ImportVar | ||
from reflex.vars.base import LiteralVar, Var, VarData | ||
|
||
LiteralCodeLanguage = Literal[ | ||
|
@@ -378,7 +379,7 @@ class Theme: | |
setattr(Theme, theme_name, getattr(Theme, theme_name)._replace(_var_type=Theme)) | ||
|
||
|
||
class CodeBlock(Component): | ||
class CodeBlock(Component, MarkdownComponentMap): | ||
"""A code block.""" | ||
|
||
library = "[email protected]" | ||
|
@@ -417,39 +418,6 @@ class CodeBlock(Component): | |
# A custom copy button to override the default one. | ||
copy_button: Optional[Union[bool, Component]] = None | ||
|
||
def add_imports(self) -> ImportDict: | ||
"""Add imports for the CodeBlock component. | ||
Returns: | ||
The import dict. | ||
""" | ||
imports_: ImportDict = {} | ||
|
||
if ( | ||
self.language is not None | ||
and (language_without_quotes := str(self.language).replace('"', "")) | ||
in LiteralCodeLanguage.__args__ # type: ignore | ||
): | ||
imports_[ | ||
f"react-syntax-highlighter/dist/cjs/languages/prism/{language_without_quotes}" | ||
] = [ | ||
ImportVar( | ||
tag=format.to_camel_case(language_without_quotes), | ||
is_default=True, | ||
install=False, | ||
) | ||
] | ||
|
||
return imports_ | ||
|
||
def _get_custom_code(self) -> Optional[str]: | ||
if ( | ||
self.language is not None | ||
and (language_without_quotes := str(self.language).replace('"', "")) | ||
in LiteralCodeLanguage.__args__ # type: ignore | ||
): | ||
return f"{self.alias}.registerLanguage('{language_without_quotes}', {format.to_camel_case(language_without_quotes)})" | ||
|
||
@classmethod | ||
def create( | ||
cls, | ||
|
@@ -534,15 +502,55 @@ def _render(self): | |
|
||
theme = self.theme | ||
|
||
out.add_props(style=theme).remove_props("theme", "code").add_props( | ||
children=self.code | ||
out.add_props(style=theme).remove_props("theme", "code", "language").add_props( | ||
children=self.code, language=_LANGUAGE | ||
) | ||
|
||
return out | ||
|
||
def _exclude_props(self) -> list[str]: | ||
return ["can_copy", "copy_button"] | ||
|
||
@classmethod | ||
def _get_language_registration_hook(cls) -> str: | ||
"""Get the hook to register the language. | ||
Returns: | ||
The hook to register the language. | ||
""" | ||
return f""" | ||
if ({str(_LANGUAGE)}) {{ | ||
(async () => {{ | ||
try {{ | ||
const module = await import(`react-syntax-highlighter/dist/cjs/languages/prism/${{{str(_LANGUAGE)}}}`); | ||
SyntaxHighlighter.registerLanguage({str(_LANGUAGE)}, module.default); | ||
}} catch (error) {{ | ||
console.error(`Error importing language module for ${{{str(_LANGUAGE)}}}:`, error); | ||
}} | ||
}})(); | ||
}} | ||
""" | ||
|
||
@classmethod | ||
def get_component_map_custom_code(cls) -> str: | ||
"""Get the custom code for the component. | ||
Returns: | ||
The custom code for the component. | ||
""" | ||
return cls._get_language_registration_hook() | ||
|
||
def add_hooks(self) -> list[str | Var]: | ||
"""Add hooks for the component. | ||
Returns: | ||
The hooks for the component. | ||
""" | ||
return [ | ||
f"const {str(_LANGUAGE)} = {str(self.language)}", | ||
self._get_language_registration_hook(), | ||
] | ||
|
||
|
||
class CodeblockNamespace(ComponentNamespace): | ||
"""Namespace for the CodeBlock component.""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.