markdown.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. """Markdown component."""
  2. from __future__ import annotations
  3. import textwrap
  4. from functools import lru_cache
  5. from hashlib import md5
  6. from typing import Any, Callable, Dict, Union
  7. from reflex.compiler import utils
  8. from reflex.components.component import Component, CustomComponent
  9. from reflex.components.radix.themes.layout.list import (
  10. ListItem,
  11. OrderedList,
  12. UnorderedList,
  13. )
  14. from reflex.components.radix.themes.typography.heading import Heading
  15. from reflex.components.radix.themes.typography.link import Link
  16. from reflex.components.radix.themes.typography.text import Text
  17. from reflex.components.tags.tag import Tag
  18. from reflex.style import Style
  19. from reflex.utils import console, imports, types
  20. from reflex.utils.imports import ImportVar
  21. from reflex.vars import Var
  22. # Special vars used in the component map.
  23. _CHILDREN = Var.create_safe("children", _var_is_local=False)
  24. _PROPS = Var.create_safe("...props", _var_is_local=False)
  25. _MOCK_ARG = Var.create_safe("")
  26. # Special remark plugins.
  27. _REMARK_MATH = Var.create_safe("remarkMath", _var_is_local=False)
  28. _REMARK_GFM = Var.create_safe("remarkGfm", _var_is_local=False)
  29. _REMARK_PLUGINS = Var.create_safe([_REMARK_MATH, _REMARK_GFM])
  30. # Special rehype plugins.
  31. _REHYPE_KATEX = Var.create_safe("rehypeKatex", _var_is_local=False)
  32. _REHYPE_RAW = Var.create_safe("rehypeRaw", _var_is_local=False)
  33. _REHYPE_PLUGINS = Var.create_safe([_REHYPE_KATEX, _REHYPE_RAW])
  34. # These tags do NOT get props passed to them
  35. NO_PROPS_TAGS = ("ul", "ol", "li")
  36. # Component Mapping
  37. @lru_cache
  38. def get_base_component_map() -> dict[str, Callable]:
  39. """Get the base component map.
  40. Returns:
  41. The base component map.
  42. """
  43. from reflex.components.datadisplay.code import CodeBlock
  44. from reflex.components.radix.themes.typography.code import Code
  45. return {
  46. "h1": lambda value: Heading.create(value, as_="h1", size="6", margin_y="0.5em"),
  47. "h2": lambda value: Heading.create(value, as_="h2", size="5", margin_y="0.5em"),
  48. "h3": lambda value: Heading.create(value, as_="h3", size="4", margin_y="0.5em"),
  49. "h4": lambda value: Heading.create(value, as_="h4", size="3", margin_y="0.5em"),
  50. "h5": lambda value: Heading.create(value, as_="h5", size="2", margin_y="0.5em"),
  51. "h6": lambda value: Heading.create(value, as_="h6", size="1", margin_y="0.5em"),
  52. "p": lambda value: Text.create(value, margin_y="1em"),
  53. "ul": lambda value: UnorderedList.create(value, margin_y="1em"), # type: ignore
  54. "ol": lambda value: OrderedList.create(value, margin_y="1em"), # type: ignore
  55. "li": lambda value: ListItem.create(value, margin_y="0.5em"),
  56. "a": lambda value: Link.create(value),
  57. "code": lambda value: Code.create(value),
  58. "codeblock": lambda value, **props: CodeBlock.create(
  59. value, margin_y="1em", **props
  60. ),
  61. }
  62. class Markdown(Component):
  63. """A markdown component."""
  64. library = "react-markdown@8.0.7"
  65. tag = "ReactMarkdown"
  66. is_default = True
  67. # The component map from a tag to a lambda that creates a component.
  68. component_map: Dict[str, Any] = {}
  69. # Custom styles for the markdown (deprecated in v0.2.9).
  70. custom_styles: Dict[str, Any] = {}
  71. # The hash of the component map, generated at create() time.
  72. component_map_hash: str = ""
  73. @classmethod
  74. def create(cls, *children, **props) -> Component:
  75. """Create a markdown component.
  76. Args:
  77. *children: The children of the component.
  78. **props: The properties of the component.
  79. Returns:
  80. The markdown component.
  81. """
  82. assert len(children) == 1 and types._isinstance(
  83. children[0], Union[str, Var]
  84. ), "Markdown component must have exactly one child containing the markdown source."
  85. # Custom styles are deprecated.
  86. if "custom_styles" in props:
  87. console.deprecate(
  88. feature_name="rx.markdown custom_styles",
  89. reason="Use the component_map prop instead.",
  90. deprecation_version="0.2.9",
  91. removal_version="0.5.0",
  92. )
  93. # Update the base component map with the custom component map.
  94. component_map = {**get_base_component_map(), **props.pop("component_map", {})}
  95. # Get the markdown source.
  96. src = children[0]
  97. # Dedent the source.
  98. if isinstance(src, str):
  99. src = textwrap.dedent(src)
  100. # Create the component.
  101. return super().create(
  102. src,
  103. component_map=component_map,
  104. component_map_hash=cls._component_map_hash(component_map),
  105. **props,
  106. )
  107. def get_custom_components(
  108. self, seen: set[str] | None = None
  109. ) -> set[CustomComponent]:
  110. """Get all the custom components used by the component.
  111. Args:
  112. seen: The tags of the components that have already been seen.
  113. Returns:
  114. The set of custom components.
  115. """
  116. custom_components = super().get_custom_components(seen=seen)
  117. # Get the custom components for each tag.
  118. for component in self.component_map.values():
  119. custom_components |= component(_MOCK_ARG).get_custom_components(seen=seen)
  120. return custom_components
  121. def _get_imports(self) -> imports.ImportDict:
  122. # Import here to avoid circular imports.
  123. from reflex.components.datadisplay.code import CodeBlock
  124. from reflex.components.radix.themes.typography.code import Code
  125. imports = super()._get_imports()
  126. # Special markdown imports.
  127. imports.update(
  128. {
  129. "": [ImportVar(tag="katex/dist/katex.min.css")],
  130. "remark-math@5.1.1": [
  131. ImportVar(tag=_REMARK_MATH._var_name, is_default=True)
  132. ],
  133. "remark-gfm@3.0.1": [
  134. ImportVar(tag=_REMARK_GFM._var_name, is_default=True)
  135. ],
  136. "rehype-katex@6.0.3": [
  137. ImportVar(tag=_REHYPE_KATEX._var_name, is_default=True)
  138. ],
  139. "rehype-raw@6.1.1": [
  140. ImportVar(tag=_REHYPE_RAW._var_name, is_default=True)
  141. ],
  142. }
  143. )
  144. # Get the imports for each component.
  145. for component in self.component_map.values():
  146. imports = utils.merge_imports(imports, component(_MOCK_ARG).get_imports())
  147. # Get the imports for the code components.
  148. imports = utils.merge_imports(
  149. imports, CodeBlock.create(theme="light")._get_imports()
  150. )
  151. imports = utils.merge_imports(imports, Code.create()._get_imports())
  152. return imports
  153. def get_component(self, tag: str, **props) -> Component:
  154. """Get the component for a tag and props.
  155. Args:
  156. tag: The tag of the component.
  157. **props: The props of the component.
  158. Returns:
  159. The component.
  160. Raises:
  161. ValueError: If the tag is invalid.
  162. """
  163. # Check the tag is valid.
  164. if tag not in self.component_map:
  165. raise ValueError(f"No markdown component found for tag: {tag}.")
  166. special_props = {_PROPS}
  167. children = [_CHILDREN]
  168. # For certain tags, the props from the markdown renderer are not actually valid for the component.
  169. if tag in NO_PROPS_TAGS:
  170. special_props = set()
  171. # If the children are set as a prop, don't pass them as children.
  172. children_prop = props.pop("children", None)
  173. if children_prop is not None:
  174. special_props.add(Var.create_safe(f"children={str(children_prop)}"))
  175. children = []
  176. # Get the component.
  177. component = self.component_map[tag](*children, **props).set(
  178. special_props=special_props
  179. )
  180. component._add_style(Style(self.custom_styles.get(tag, {})))
  181. return component
  182. def format_component(self, tag: str, **props) -> str:
  183. """Format a component for rendering in the component map.
  184. Args:
  185. tag: The tag of the component.
  186. **props: Extra props to pass to the component function.
  187. Returns:
  188. The formatted component.
  189. """
  190. return str(self.get_component(tag, **props)).replace("\n", " ")
  191. def format_component_map(self) -> dict[str, str]:
  192. """Format the component map for rendering.
  193. Returns:
  194. The formatted component map.
  195. """
  196. components = {
  197. tag: f"{{({{node, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {self.format_component(tag)}}}"
  198. for tag in self.component_map
  199. }
  200. # Separate out inline code and code blocks.
  201. components[
  202. "code"
  203. ] = f"""{{({{node, inline, className, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {{
  204. const match = (className || '').match(/language-(?<lang>.*)/);
  205. const language = match ? match[1] : '';
  206. if (language) {{
  207. (async () => {{
  208. try {{
  209. const module = await import(`react-syntax-highlighter/dist/cjs/languages/prism/${{language}}`);
  210. SyntaxHighlighter.registerLanguage(language, module.default);
  211. }} catch (error) {{
  212. console.error(`Error importing language module for ${{language}}:`, error);
  213. }}
  214. }})();
  215. }}
  216. return inline ? (
  217. {self.format_component("code")}
  218. ) : (
  219. {self.format_component("codeblock", language=Var.create_safe("language", _var_is_local=False))}
  220. );
  221. }}}}""".replace(
  222. "\n", " "
  223. )
  224. return components
  225. @staticmethod
  226. def _component_map_hash(component_map) -> str:
  227. inp = str(
  228. {tag: component(_MOCK_ARG) for tag, component in component_map.items()}
  229. ).encode()
  230. return md5(inp).hexdigest()
  231. def _get_component_map_name(self) -> str:
  232. return f"ComponentMap_{self.component_map_hash}"
  233. def _get_custom_code(self) -> str | None:
  234. hooks = set()
  235. for component in self.component_map.values():
  236. hooks |= component(_MOCK_ARG).get_hooks()
  237. formatted_hooks = "\n".join(hooks)
  238. return f"""
  239. function {self._get_component_map_name()} () {{
  240. {formatted_hooks}
  241. return (
  242. {str(Var.create(self.format_component_map()))}
  243. )
  244. }}
  245. """
  246. def _render(self) -> Tag:
  247. tag = (
  248. super()
  249. ._render()
  250. .add_props(
  251. remark_plugins=_REMARK_PLUGINS,
  252. rehype_plugins=_REHYPE_PLUGINS,
  253. )
  254. .remove_props("componentMap", "componentMapHash")
  255. )
  256. tag.special_props.add(
  257. Var.create_safe(
  258. f"components={{{self._get_component_map_name()}()}}",
  259. _var_is_local=True,
  260. _var_is_string=False,
  261. ),
  262. )
  263. return tag