markdown.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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.components.component import Component, CustomComponent
  8. from reflex.components.radix.themes.layout.list import (
  9. ListItem,
  10. OrderedList,
  11. UnorderedList,
  12. )
  13. from reflex.components.radix.themes.typography.heading import Heading
  14. from reflex.components.radix.themes.typography.link import Link
  15. from reflex.components.radix.themes.typography.text import Text
  16. from reflex.components.tags.tag import Tag
  17. from reflex.style import Style
  18. from reflex.utils import console, 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)
  23. _PROPS = Var.create_safe("...props", _var_is_local=False)
  24. _MOCK_ARG = Var.create_safe("")
  25. # Special remark plugins.
  26. _REMARK_MATH = Var.create_safe("remarkMath", _var_is_local=False)
  27. _REMARK_GFM = Var.create_safe("remarkGfm", _var_is_local=False)
  28. _REMARK_UNWRAP_IMAGES = Var.create_safe("remarkUnwrapImages", _var_is_local=False)
  29. _REMARK_PLUGINS = Var.create_safe([_REMARK_MATH, _REMARK_GFM, _REMARK_UNWRAP_IMAGES])
  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 (
  83. len(children) == 1 and types._isinstance(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_all_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_all_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_all_custom_components(
  120. seen=seen
  121. )
  122. return custom_components
  123. def _get_imports_list(self) -> list[imports.ImportVar]:
  124. # Import here to avoid circular imports.
  125. from reflex.components.datadisplay.code import CodeBlock
  126. from reflex.components.radix.themes.typography.code import Code
  127. _imports = super()._get_imports_list()
  128. # Special markdown imports.
  129. _imports.extend(
  130. [
  131. ImportVar(library="", tag="katex/dist/katex.min.css"),
  132. ImportVar(
  133. package="remark-math@5.1.1",
  134. tag=_REMARK_MATH._var_name,
  135. is_default=True,
  136. ),
  137. ImportVar(
  138. package="remark-gfm@3.0.1",
  139. tag=_REMARK_GFM._var_name,
  140. is_default=True,
  141. ),
  142. ImportVar(
  143. package="remark-unwrap-images@4.0.0",
  144. tag=_REMARK_UNWRAP_IMAGES._var_name,
  145. is_default=True,
  146. ),
  147. ImportVar(
  148. package="rehype-katex@6.0.3",
  149. tag=_REHYPE_KATEX._var_name,
  150. is_default=True,
  151. ),
  152. ImportVar(
  153. package="rehype-raw@6.1.1",
  154. tag=_REHYPE_RAW._var_name,
  155. is_default=True,
  156. ),
  157. ]
  158. )
  159. # Get the imports for each component.
  160. for component in self.component_map.values():
  161. _imports.extend(component(_MOCK_ARG)._get_all_imports())
  162. # Get the imports for the code components.
  163. _imports.extend(CodeBlock.create(theme="light")._get_all_imports())
  164. _imports.extend(Code.create()._get_all_imports())
  165. return _imports
  166. def get_component(self, tag: str, **props) -> Component:
  167. """Get the component for a tag and props.
  168. Args:
  169. tag: The tag of the component.
  170. **props: The props of the component.
  171. Returns:
  172. The component.
  173. Raises:
  174. ValueError: If the tag is invalid.
  175. """
  176. # Check the tag is valid.
  177. if tag not in self.component_map:
  178. raise ValueError(f"No markdown component found for tag: {tag}.")
  179. special_props = {_PROPS}
  180. children = [_CHILDREN]
  181. # For certain tags, the props from the markdown renderer are not actually valid for the component.
  182. if tag in NO_PROPS_TAGS:
  183. special_props = set()
  184. # If the children are set as a prop, don't pass them as children.
  185. children_prop = props.pop("children", None)
  186. if children_prop is not None:
  187. special_props.add(Var.create_safe(f"children={str(children_prop)}"))
  188. children = []
  189. # Get the component.
  190. component = self.component_map[tag](*children, **props).set(
  191. special_props=special_props
  192. )
  193. component._add_style(Style(self.custom_styles.get(tag, {})))
  194. return component
  195. def format_component(self, tag: str, **props) -> str:
  196. """Format a component for rendering in the component map.
  197. Args:
  198. tag: The tag of the component.
  199. **props: Extra props to pass to the component function.
  200. Returns:
  201. The formatted component.
  202. """
  203. return str(self.get_component(tag, **props)).replace("\n", " ")
  204. def format_component_map(self) -> dict[str, str]:
  205. """Format the component map for rendering.
  206. Returns:
  207. The formatted component map.
  208. """
  209. components = {
  210. tag: f"{{({{node, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {self.format_component(tag)}}}"
  211. for tag in self.component_map
  212. }
  213. # Separate out inline code and code blocks.
  214. components["code"] = f"""{{({{node, inline, className, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {{
  215. const match = (className || '').match(/language-(?<lang>.*)/);
  216. const language = match ? match[1] : '';
  217. if (language) {{
  218. (async () => {{
  219. try {{
  220. const module = await import(`react-syntax-highlighter/dist/cjs/languages/prism/${{language}}`);
  221. SyntaxHighlighter.registerLanguage(language, module.default);
  222. }} catch (error) {{
  223. console.error(`Error importing language module for ${{language}}:`, error);
  224. }}
  225. }})();
  226. }}
  227. return inline ? (
  228. {self.format_component("code")}
  229. ) : (
  230. {self.format_component("codeblock", language=Var.create_safe("language", _var_is_local=False))}
  231. );
  232. }}}}""".replace("\n", " ")
  233. return components
  234. @staticmethod
  235. def _component_map_hash(component_map) -> str:
  236. inp = str(
  237. {tag: component(_MOCK_ARG) for tag, component in component_map.items()}
  238. ).encode()
  239. return md5(inp).hexdigest()
  240. def _get_component_map_name(self) -> str:
  241. return f"ComponentMap_{self.component_map_hash}"
  242. def _get_custom_code(self) -> str | None:
  243. hooks = set()
  244. for _component in self.component_map.values():
  245. comp = _component(_MOCK_ARG)
  246. hooks.update(comp._get_all_hooks_internal())
  247. hooks.update(comp._get_all_hooks())
  248. formatted_hooks = "\n".join(hooks)
  249. return f"""
  250. function {self._get_component_map_name()} () {{
  251. {formatted_hooks}
  252. return (
  253. {str(Var.create(self.format_component_map()))}
  254. )
  255. }}
  256. """
  257. def _render(self) -> Tag:
  258. tag = (
  259. super()
  260. ._render()
  261. .add_props(
  262. remark_plugins=_REMARK_PLUGINS,
  263. rehype_plugins=_REHYPE_PLUGINS,
  264. )
  265. .remove_props("componentMap", "componentMapHash")
  266. )
  267. tag.special_props.add(
  268. Var.create_safe(
  269. f"components={{{self._get_component_map_name()}()}}",
  270. _var_is_local=True,
  271. _var_is_string=False,
  272. ),
  273. )
  274. return tag