markdown.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.utils import imports, types
  19. from reflex.utils.imports import ImportVar
  20. from reflex.vars import Var
  21. # Special vars used in the component map.
  22. _CHILDREN = Var.create_safe("children", _var_is_local=False, _var_is_string=False)
  23. _PROPS = Var.create_safe("...props", _var_is_local=False, _var_is_string=False)
  24. _MOCK_ARG = Var.create_safe("", _var_is_string=False)
  25. # Special remark plugins.
  26. _REMARK_MATH = Var.create_safe("remarkMath", _var_is_local=False, _var_is_string=False)
  27. _REMARK_GFM = Var.create_safe("remarkGfm", _var_is_local=False, _var_is_string=False)
  28. _REMARK_UNWRAP_IMAGES = Var.create_safe(
  29. "remarkUnwrapImages", _var_is_local=False, _var_is_string=False
  30. )
  31. _REMARK_PLUGINS = Var.create_safe([_REMARK_MATH, _REMARK_GFM, _REMARK_UNWRAP_IMAGES])
  32. # Special rehype plugins.
  33. _REHYPE_KATEX = Var.create_safe(
  34. "rehypeKatex", _var_is_local=False, _var_is_string=False
  35. )
  36. _REHYPE_RAW = Var.create_safe("rehypeRaw", _var_is_local=False, _var_is_string=False)
  37. _REHYPE_PLUGINS = Var.create_safe([_REHYPE_KATEX, _REHYPE_RAW])
  38. # These tags do NOT get props passed to them
  39. NO_PROPS_TAGS = ("ul", "ol", "li")
  40. # Component Mapping
  41. @lru_cache
  42. def get_base_component_map() -> dict[str, Callable]:
  43. """Get the base component map.
  44. Returns:
  45. The base component map.
  46. """
  47. from reflex.components.datadisplay.code import CodeBlock
  48. from reflex.components.radix.themes.typography.code import Code
  49. return {
  50. "h1": lambda value: Heading.create(value, as_="h1", size="6", margin_y="0.5em"),
  51. "h2": lambda value: Heading.create(value, as_="h2", size="5", margin_y="0.5em"),
  52. "h3": lambda value: Heading.create(value, as_="h3", size="4", margin_y="0.5em"),
  53. "h4": lambda value: Heading.create(value, as_="h4", size="3", margin_y="0.5em"),
  54. "h5": lambda value: Heading.create(value, as_="h5", size="2", margin_y="0.5em"),
  55. "h6": lambda value: Heading.create(value, as_="h6", size="1", margin_y="0.5em"),
  56. "p": lambda value: Text.create(value, margin_y="1em"),
  57. "ul": lambda value: UnorderedList.create(value, margin_y="1em"), # type: ignore
  58. "ol": lambda value: OrderedList.create(value, margin_y="1em"), # type: ignore
  59. "li": lambda value: ListItem.create(value, margin_y="0.5em"),
  60. "a": lambda value: Link.create(value),
  61. "code": lambda value: Code.create(value),
  62. "codeblock": lambda value, **props: CodeBlock.create(
  63. value, margin_y="1em", **props
  64. ),
  65. }
  66. class Markdown(Component):
  67. """A markdown component."""
  68. library = "react-markdown@8.0.7"
  69. tag = "ReactMarkdown"
  70. is_default = True
  71. # The component map from a tag to a lambda that creates a component.
  72. component_map: Dict[str, Any] = {}
  73. # The hash of the component map, generated at create() time.
  74. component_map_hash: str = ""
  75. @classmethod
  76. def create(cls, *children, **props) -> Component:
  77. """Create a markdown component.
  78. Args:
  79. *children: The children of the component.
  80. **props: The properties of the component.
  81. Returns:
  82. The markdown component.
  83. """
  84. assert (
  85. len(children) == 1 and types._isinstance(children[0], Union[str, Var])
  86. ), "Markdown component must have exactly one child containing the markdown source."
  87. # Update the base component map with the custom component map.
  88. component_map = {**get_base_component_map(), **props.pop("component_map", {})}
  89. # Get the markdown source.
  90. src = children[0]
  91. # Dedent the source.
  92. if isinstance(src, str):
  93. src = textwrap.dedent(src)
  94. # Create the component.
  95. return super().create(
  96. src,
  97. component_map=component_map,
  98. component_map_hash=cls._component_map_hash(component_map),
  99. **props,
  100. )
  101. def _get_all_custom_components(
  102. self, seen: set[str] | None = None
  103. ) -> set[CustomComponent]:
  104. """Get all the custom components used by the component.
  105. Args:
  106. seen: The tags of the components that have already been seen.
  107. Returns:
  108. The set of custom components.
  109. """
  110. custom_components = super()._get_all_custom_components(seen=seen)
  111. # Get the custom components for each tag.
  112. for component in self.component_map.values():
  113. custom_components |= component(_MOCK_ARG)._get_all_custom_components(
  114. seen=seen
  115. )
  116. return custom_components
  117. def _get_imports(self) -> imports.ImportDict:
  118. # Import here to avoid circular imports.
  119. from reflex.components.datadisplay.code import CodeBlock
  120. from reflex.components.radix.themes.typography.code import Code
  121. imports = super()._get_imports()
  122. # Special markdown imports.
  123. imports.update(
  124. {
  125. "": [ImportVar(tag="katex/dist/katex.min.css")],
  126. "remark-math@5.1.1": [
  127. ImportVar(tag=_REMARK_MATH._var_name, is_default=True)
  128. ],
  129. "remark-gfm@3.0.1": [
  130. ImportVar(tag=_REMARK_GFM._var_name, is_default=True)
  131. ],
  132. "remark-unwrap-images@4.0.0": [
  133. ImportVar(tag=_REMARK_UNWRAP_IMAGES._var_name, is_default=True)
  134. ],
  135. "rehype-katex@6.0.3": [
  136. ImportVar(tag=_REHYPE_KATEX._var_name, is_default=True)
  137. ],
  138. "rehype-raw@6.1.1": [
  139. ImportVar(tag=_REHYPE_RAW._var_name, is_default=True)
  140. ],
  141. }
  142. )
  143. # Get the imports for each component.
  144. for component in self.component_map.values():
  145. imports = utils.merge_imports(
  146. imports, component(_MOCK_ARG)._get_all_imports()
  147. )
  148. # Get the imports for the code components.
  149. imports = utils.merge_imports(
  150. imports, CodeBlock.create(theme="light")._get_imports()
  151. )
  152. imports = utils.merge_imports(imports, Code.create()._get_imports())
  153. return imports
  154. def get_component(self, tag: str, **props) -> Component:
  155. """Get the component for a tag and props.
  156. Args:
  157. tag: The tag of the component.
  158. **props: The props of the component.
  159. Returns:
  160. The component.
  161. Raises:
  162. ValueError: If the tag is invalid.
  163. """
  164. # Check the tag is valid.
  165. if tag not in self.component_map:
  166. raise ValueError(f"No markdown component found for tag: {tag}.")
  167. special_props = {_PROPS}
  168. children = [_CHILDREN]
  169. # For certain tags, the props from the markdown renderer are not actually valid for the component.
  170. if tag in NO_PROPS_TAGS:
  171. special_props = set()
  172. # If the children are set as a prop, don't pass them as children.
  173. children_prop = props.pop("children", None)
  174. if children_prop is not None:
  175. special_props.add(
  176. Var.create_safe(f"children={str(children_prop)}", _var_is_string=False)
  177. )
  178. children = []
  179. # Get the component.
  180. component = self.component_map[tag](*children, **props).set(
  181. special_props=special_props
  182. )
  183. return component
  184. def format_component(self, tag: str, **props) -> str:
  185. """Format a component for rendering in the component map.
  186. Args:
  187. tag: The tag of the component.
  188. **props: Extra props to pass to the component function.
  189. Returns:
  190. The formatted component.
  191. """
  192. return str(self.get_component(tag, **props)).replace("\n", "")
  193. def format_component_map(self) -> dict[str, str]:
  194. """Format the component map for rendering.
  195. Returns:
  196. The formatted component map.
  197. """
  198. components = {
  199. tag: f"{{({{node, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {self.format_component(tag)}}}"
  200. for tag in self.component_map
  201. }
  202. # Separate out inline code and code blocks.
  203. components["code"] = 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, _var_is_string=False))}
  220. );
  221. }}}}""".replace("\n", " ")
  222. return components
  223. @staticmethod
  224. def _component_map_hash(component_map) -> str:
  225. inp = str(
  226. {tag: component(_MOCK_ARG) for tag, component in component_map.items()}
  227. ).encode()
  228. return md5(inp).hexdigest()
  229. def _get_component_map_name(self) -> str:
  230. return f"ComponentMap_{self.component_map_hash}"
  231. def _get_custom_code(self) -> str | None:
  232. hooks = set()
  233. for _component in self.component_map.values():
  234. comp = _component(_MOCK_ARG)
  235. hooks.update(comp._get_all_hooks_internal())
  236. hooks.update(comp._get_all_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