markdown.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.ivars.base import ImmutableVar, LiteralVar
  18. from reflex.utils import types
  19. from reflex.utils.imports import ImportDict, ImportVar
  20. from reflex.vars import Var
  21. # Special vars used in the component map.
  22. _CHILDREN = ImmutableVar.create_safe("children")
  23. _PROPS = ImmutableVar.create_safe("...props")
  24. _MOCK_ARG = ImmutableVar.create_safe("")
  25. # Special remark plugins.
  26. _REMARK_MATH = ImmutableVar.create_safe("remarkMath")
  27. _REMARK_GFM = ImmutableVar.create_safe("remarkGfm")
  28. _REMARK_UNWRAP_IMAGES = ImmutableVar.create_safe("remarkUnwrapImages")
  29. _REMARK_PLUGINS = LiteralVar.create([_REMARK_MATH, _REMARK_GFM, _REMARK_UNWRAP_IMAGES])
  30. # Special rehype plugins.
  31. _REHYPE_KATEX = ImmutableVar.create_safe("rehypeKatex")
  32. _REHYPE_RAW = ImmutableVar.create_safe("rehypeRaw")
  33. _REHYPE_PLUGINS = LiteralVar.create([_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", wrap_long_lines=True, **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. # The hash of the component map, generated at create() time.
  70. component_map_hash: str = ""
  71. @classmethod
  72. def create(cls, *children, **props) -> Component:
  73. """Create a markdown component.
  74. Args:
  75. *children: The children of the component.
  76. **props: The properties of the component.
  77. Returns:
  78. The markdown component.
  79. """
  80. assert (
  81. len(children) == 1 and types._isinstance(children[0], Union[str, Var])
  82. ), "Markdown component must have exactly one child containing the markdown source."
  83. # Update the base component map with the custom component map.
  84. component_map = {**get_base_component_map(), **props.pop("component_map", {})}
  85. # Get the markdown source.
  86. src = children[0]
  87. # Dedent the source.
  88. if isinstance(src, str):
  89. src = textwrap.dedent(src)
  90. # Create the component.
  91. return super().create(
  92. src,
  93. component_map=component_map,
  94. component_map_hash=cls._component_map_hash(component_map),
  95. **props,
  96. )
  97. def _get_all_custom_components(
  98. self, seen: set[str] | None = None
  99. ) -> set[CustomComponent]:
  100. """Get all the custom components used by the component.
  101. Args:
  102. seen: The tags of the components that have already been seen.
  103. Returns:
  104. The set of custom components.
  105. """
  106. custom_components = super()._get_all_custom_components(seen=seen)
  107. # Get the custom components for each tag.
  108. for component in self.component_map.values():
  109. custom_components |= component(_MOCK_ARG)._get_all_custom_components(
  110. seen=seen
  111. )
  112. return custom_components
  113. def add_imports(self) -> ImportDict | list[ImportDict]:
  114. """Add imports for the markdown component.
  115. Returns:
  116. The imports for the markdown component.
  117. """
  118. from reflex.components.datadisplay.code import CodeBlock
  119. from reflex.components.radix.themes.typography.code import Code
  120. return [
  121. {
  122. "": "katex/dist/katex.min.css",
  123. "remark-math@5.1.1": ImportVar(
  124. tag=_REMARK_MATH._var_name, is_default=True
  125. ),
  126. "remark-gfm@3.0.1": ImportVar(
  127. tag=_REMARK_GFM._var_name, is_default=True
  128. ),
  129. "remark-unwrap-images@4.0.0": ImportVar(
  130. tag=_REMARK_UNWRAP_IMAGES._var_name, is_default=True
  131. ),
  132. "rehype-katex@6.0.3": ImportVar(
  133. tag=_REHYPE_KATEX._var_name, is_default=True
  134. ),
  135. "rehype-raw@6.1.1": ImportVar(
  136. tag=_REHYPE_RAW._var_name, is_default=True
  137. ),
  138. },
  139. *[
  140. component(_MOCK_ARG)._get_all_imports() # type: ignore
  141. for component in self.component_map.values()
  142. ],
  143. CodeBlock.create(theme="light")._get_imports(), # type: ignore,
  144. Code.create()._get_imports(), # type: ignore,
  145. ]
  146. def get_component(self, tag: str, **props) -> Component:
  147. """Get the component for a tag and props.
  148. Args:
  149. tag: The tag of the component.
  150. **props: The props of the component.
  151. Returns:
  152. The component.
  153. Raises:
  154. ValueError: If the tag is invalid.
  155. """
  156. # Check the tag is valid.
  157. if tag not in self.component_map:
  158. raise ValueError(f"No markdown component found for tag: {tag}.")
  159. special_props = {_PROPS}
  160. children = [_CHILDREN]
  161. # For certain tags, the props from the markdown renderer are not actually valid for the component.
  162. if tag in NO_PROPS_TAGS:
  163. special_props = set()
  164. # If the children are set as a prop, don't pass them as children.
  165. children_prop = props.pop("children", None)
  166. if children_prop is not None:
  167. special_props.add(
  168. Var.create_safe(
  169. f"children={{{str(children_prop)}}}", _var_is_string=False
  170. )
  171. )
  172. children = []
  173. # Get the component.
  174. component = self.component_map[tag](*children, **props).set(
  175. special_props=special_props
  176. )
  177. return component
  178. def format_component(self, tag: str, **props) -> str:
  179. """Format a component for rendering in the component map.
  180. Args:
  181. tag: The tag of the component.
  182. **props: Extra props to pass to the component function.
  183. Returns:
  184. The formatted component.
  185. """
  186. return str(self.get_component(tag, **props)).replace("\n", "")
  187. def format_component_map(self) -> dict[str, str]:
  188. """Format the component map for rendering.
  189. Returns:
  190. The formatted component map.
  191. """
  192. components = {
  193. tag: f"{{({{node, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => ({self.format_component(tag)})}}"
  194. for tag in self.component_map
  195. }
  196. # Separate out inline code and code blocks.
  197. components[
  198. "code"
  199. ] = f"""{{({{node, inline, className, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {{
  200. const match = (className || '').match(/language-(?<lang>.*)/);
  201. const language = match ? match[1] : '';
  202. if (language) {{
  203. (async () => {{
  204. try {{
  205. const module = await import(`react-syntax-highlighter/dist/cjs/languages/prism/${{language}}`);
  206. SyntaxHighlighter.registerLanguage(language, module.default);
  207. }} catch (error) {{
  208. console.error(`Error importing language module for ${{language}}:`, error);
  209. }}
  210. }})();
  211. }}
  212. return inline ? (
  213. {self.format_component("code")}
  214. ) : (
  215. {self.format_component("codeblock", language=ImmutableVar.create_safe("language"))}
  216. );
  217. }}}}""".replace("\n", " ")
  218. return components
  219. @staticmethod
  220. def _component_map_hash(component_map) -> str:
  221. inp = str(
  222. {tag: component(_MOCK_ARG) for tag, component in component_map.items()}
  223. ).encode()
  224. return md5(inp).hexdigest()
  225. def _get_component_map_name(self) -> str:
  226. return f"ComponentMap_{self.component_map_hash}"
  227. def _get_custom_code(self) -> str | None:
  228. hooks = set()
  229. for _component in self.component_map.values():
  230. comp = _component(_MOCK_ARG)
  231. hooks.update(comp._get_all_hooks_internal())
  232. hooks.update(comp._get_all_hooks())
  233. formatted_hooks = "\n".join(hooks)
  234. return f"""
  235. function {self._get_component_map_name()} () {{
  236. {formatted_hooks}
  237. return (
  238. {str(ImmutableVar.create_safe(self.format_component_map()))}
  239. )
  240. }}
  241. """
  242. def _render(self) -> Tag:
  243. tag = (
  244. super()
  245. ._render()
  246. .add_props(
  247. remark_plugins=_REMARK_PLUGINS,
  248. rehype_plugins=_REHYPE_PLUGINS,
  249. components=ImmutableVar.create_safe(
  250. f"{self._get_component_map_name()}()"
  251. ),
  252. )
  253. .remove_props("componentMap", "componentMapHash")
  254. )
  255. return tag