markdown.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. """Markdown component."""
  2. from __future__ import annotations
  3. import dataclasses
  4. import textwrap
  5. from functools import lru_cache
  6. from hashlib import md5
  7. from typing import Any, Callable, Dict, Sequence, Union
  8. from reflex.components.component import BaseComponent, Component, CustomComponent
  9. from reflex.components.tags.tag import Tag
  10. from reflex.utils import types
  11. from reflex.utils.imports import ImportDict, ImportVar
  12. from reflex.vars.base import LiteralVar, Var
  13. from reflex.vars.function import ARRAY_ISARRAY, ArgsFunctionOperation, DestructuredArg
  14. from reflex.vars.number import ternary_operation
  15. # Special vars used in the component map.
  16. _CHILDREN = Var(_js_expr="children", _var_type=str)
  17. _PROPS = Var(_js_expr="...props")
  18. _PROPS_IN_TAG = Var(_js_expr="{...props}")
  19. _MOCK_ARG = Var(_js_expr="", _var_type=str)
  20. _LANGUAGE = Var(_js_expr="_language", _var_type=str)
  21. # Special remark plugins.
  22. _REMARK_MATH = Var(_js_expr="remarkMath")
  23. _REMARK_GFM = Var(_js_expr="remarkGfm")
  24. _REMARK_UNWRAP_IMAGES = Var(_js_expr="remarkUnwrapImages")
  25. _REMARK_PLUGINS = LiteralVar.create([_REMARK_MATH, _REMARK_GFM, _REMARK_UNWRAP_IMAGES])
  26. # Special rehype plugins.
  27. _REHYPE_KATEX = Var(_js_expr="rehypeKatex")
  28. _REHYPE_RAW = Var(_js_expr="rehypeRaw")
  29. _REHYPE_PLUGINS = LiteralVar.create([_REHYPE_KATEX, _REHYPE_RAW])
  30. # These tags do NOT get props passed to them
  31. NO_PROPS_TAGS = ("ul", "ol", "li")
  32. # Component Mapping
  33. @lru_cache
  34. def get_base_component_map() -> dict[str, Callable]:
  35. """Get the base component map.
  36. Returns:
  37. The base component map.
  38. """
  39. from reflex.components.datadisplay.code import CodeBlock
  40. from reflex.components.radix.themes.layout.list import (
  41. ListItem,
  42. OrderedList,
  43. UnorderedList,
  44. )
  45. from reflex.components.radix.themes.typography.code import Code
  46. from reflex.components.radix.themes.typography.heading import Heading
  47. from reflex.components.radix.themes.typography.link import Link
  48. from reflex.components.radix.themes.typography.text import Text
  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"),
  58. "ol": lambda value: OrderedList.create(value, margin_y="1em"),
  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", wrap_long_lines=True, **props
  64. ),
  65. }
  66. @dataclasses.dataclass()
  67. class MarkdownComponentMap:
  68. """Mixin class for handling custom component maps in Markdown components."""
  69. _explicit_return: bool = dataclasses.field(default=False)
  70. @classmethod
  71. def get_component_map_custom_code(cls) -> str:
  72. """Get the custom code for the component map.
  73. Returns:
  74. The custom code for the component map.
  75. """
  76. return ""
  77. @classmethod
  78. def create_map_fn_var(
  79. cls,
  80. fn_body: Var | None = None,
  81. fn_args: Sequence[str] | None = None,
  82. explicit_return: bool | None = None,
  83. ) -> Var:
  84. """Create a function Var for the component map.
  85. Args:
  86. fn_body: The formatted component as a string.
  87. fn_args: The function arguments.
  88. explicit_return: Whether to use explicit return syntax.
  89. Returns:
  90. The function Var for the component map.
  91. """
  92. fn_args = fn_args or cls.get_fn_args()
  93. fn_body = fn_body if fn_body is not None else cls.get_fn_body()
  94. explicit_return = explicit_return or cls._explicit_return
  95. return ArgsFunctionOperation.create(
  96. args_names=(DestructuredArg(fields=tuple(fn_args)),),
  97. return_expr=fn_body,
  98. explicit_return=explicit_return,
  99. )
  100. @classmethod
  101. def get_fn_args(cls) -> Sequence[str]:
  102. """Get the function arguments for the component map.
  103. Returns:
  104. The function arguments as a list of strings.
  105. """
  106. return ["node", _CHILDREN._js_expr, _PROPS._js_expr]
  107. @classmethod
  108. def get_fn_body(cls) -> Var:
  109. """Get the function body for the component map.
  110. Returns:
  111. The function body as a string.
  112. """
  113. return Var(_js_expr="undefined", _var_type=None)
  114. class Markdown(Component):
  115. """A markdown component."""
  116. library = "react-markdown@8.0.7"
  117. tag = "ReactMarkdown"
  118. is_default = True
  119. # The component map from a tag to a lambda that creates a component.
  120. component_map: Dict[str, Any] = {}
  121. # The hash of the component map, generated at create() time.
  122. component_map_hash: str = ""
  123. @classmethod
  124. def create(cls, *children, **props) -> Component:
  125. """Create a markdown component.
  126. Args:
  127. *children: The children of the component.
  128. **props: The properties of the component.
  129. Raises:
  130. ValueError: If the children are not valid.
  131. Returns:
  132. The markdown component.
  133. """
  134. if len(children) != 1 or not types._isinstance(children[0], Union[str, Var]):
  135. raise ValueError(
  136. "Markdown component must have exactly one child containing the markdown source."
  137. )
  138. # Update the base component map with the custom component map.
  139. component_map = {**get_base_component_map(), **props.pop("component_map", {})}
  140. # Get the markdown source.
  141. src = children[0]
  142. # Dedent the source.
  143. if isinstance(src, str):
  144. src = textwrap.dedent(src)
  145. # Create the component.
  146. return super().create(
  147. src,
  148. component_map=component_map,
  149. component_map_hash=cls._component_map_hash(component_map),
  150. **props,
  151. )
  152. def _get_all_custom_components(
  153. self, seen: set[str] | None = None
  154. ) -> set[CustomComponent]:
  155. """Get all the custom components used by the component.
  156. Args:
  157. seen: The tags of the components that have already been seen.
  158. Returns:
  159. The set of custom components.
  160. """
  161. custom_components = super()._get_all_custom_components(seen=seen)
  162. # Get the custom components for each tag.
  163. for component in self.component_map.values():
  164. custom_components |= component(_MOCK_ARG)._get_all_custom_components(
  165. seen=seen
  166. )
  167. return custom_components
  168. def add_imports(self) -> ImportDict | list[ImportDict]:
  169. """Add imports for the markdown component.
  170. Returns:
  171. The imports for the markdown component.
  172. """
  173. return [
  174. {
  175. "": "katex/dist/katex.min.css",
  176. "remark-math@5.1.1": ImportVar(
  177. tag=_REMARK_MATH._js_expr, is_default=True
  178. ),
  179. "remark-gfm@3.0.1": ImportVar(
  180. tag=_REMARK_GFM._js_expr, is_default=True
  181. ),
  182. "remark-unwrap-images@4.0.0": ImportVar(
  183. tag=_REMARK_UNWRAP_IMAGES._js_expr, is_default=True
  184. ),
  185. "rehype-katex@6.0.3": ImportVar(
  186. tag=_REHYPE_KATEX._js_expr, is_default=True
  187. ),
  188. "rehype-raw@6.1.1": ImportVar(
  189. tag=_REHYPE_RAW._js_expr, is_default=True
  190. ),
  191. },
  192. *[
  193. component(_MOCK_ARG)._get_all_imports()
  194. for component in self.component_map.values()
  195. ],
  196. ]
  197. def _get_tag_map_fn_var(self, tag: str) -> Var:
  198. return self._get_map_fn_var_from_children(self.get_component(tag), tag)
  199. def format_component_map(self) -> dict[str, Var]:
  200. """Format the component map for rendering.
  201. Returns:
  202. The formatted component map.
  203. """
  204. components = {
  205. tag: self._get_tag_map_fn_var(tag)
  206. for tag in self.component_map
  207. if tag not in ("code", "codeblock")
  208. }
  209. # Separate out inline code and code blocks.
  210. components["code"] = self._get_inline_code_fn_var()
  211. return components
  212. def _get_inline_code_fn_var(self) -> Var:
  213. """Get the function variable for inline code.
  214. This function creates a Var that represents a function to handle
  215. both inline code and code blocks in markdown.
  216. Returns:
  217. The Var for inline code.
  218. """
  219. # Get any custom code from the codeblock and code components.
  220. custom_code_list = self._get_map_fn_custom_code_from_children(
  221. self.get_component("codeblock")
  222. )
  223. custom_code_list.extend(
  224. self._get_map_fn_custom_code_from_children(self.get_component("code"))
  225. )
  226. codeblock_custom_code = "\n".join(custom_code_list)
  227. # Format the code to handle inline and block code.
  228. formatted_code = f"""
  229. const match = (className || '').match(/language-(?<lang>.*)/);
  230. const {_LANGUAGE!s} = match ? match[1] : '';
  231. {codeblock_custom_code};
  232. return inline ? (
  233. {self.format_component("code")}
  234. ) : (
  235. {self.format_component("codeblock", language=_LANGUAGE)}
  236. );
  237. """.replace("\n", " ")
  238. return MarkdownComponentMap.create_map_fn_var(
  239. fn_args=(
  240. "node",
  241. "inline",
  242. "className",
  243. _CHILDREN._js_expr,
  244. _PROPS._js_expr,
  245. ),
  246. fn_body=Var(_js_expr=formatted_code),
  247. explicit_return=True,
  248. )
  249. def get_component(self, tag: str, **props) -> Component:
  250. """Get the component for a tag and props.
  251. Args:
  252. tag: The tag of the component.
  253. **props: The props of the component.
  254. Returns:
  255. The component.
  256. Raises:
  257. ValueError: If the tag is invalid.
  258. """
  259. # Check the tag is valid.
  260. if tag not in self.component_map:
  261. raise ValueError(f"No markdown component found for tag: {tag}.")
  262. special_props = [_PROPS_IN_TAG]
  263. children = [
  264. _CHILDREN
  265. if tag != "codeblock"
  266. # For codeblock, the mapping for some cases returns an array of elements. Let's join them into a string.
  267. else ternary_operation(
  268. ARRAY_ISARRAY.call(_CHILDREN), # pyright: ignore [reportArgumentType]
  269. _CHILDREN.to(list).join("\n"),
  270. _CHILDREN,
  271. ).to(str)
  272. ]
  273. # For certain tags, the props from the markdown renderer are not actually valid for the component.
  274. if tag in NO_PROPS_TAGS:
  275. special_props = []
  276. # If the children are set as a prop, don't pass them as children.
  277. children_prop = props.pop("children", None)
  278. if children_prop is not None:
  279. special_props.append(Var(_js_expr=f"children={{{children_prop!s}}}"))
  280. children = []
  281. # Get the component.
  282. component = self.component_map[tag](*children, **props).set(
  283. special_props=special_props
  284. )
  285. return component
  286. def format_component(self, tag: str, **props) -> str:
  287. """Format a component for rendering in the component map.
  288. Args:
  289. tag: The tag of the component.
  290. **props: Extra props to pass to the component function.
  291. Returns:
  292. The formatted component.
  293. """
  294. return str(self.get_component(tag, **props)).replace("\n", "")
  295. def _get_map_fn_var_from_children(self, component: Component, tag: str) -> Var:
  296. """Create a function Var for the component map for the specified tag.
  297. Args:
  298. component: The component to check for custom code.
  299. tag: The tag of the component.
  300. Returns:
  301. The function Var for the component map.
  302. """
  303. formatted_component = Var(
  304. _js_expr=f"({self.format_component(tag)})", _var_type=str
  305. )
  306. if isinstance(component, MarkdownComponentMap):
  307. return component.create_map_fn_var(fn_body=formatted_component)
  308. # fallback to the default fn Var creation if the component is not a MarkdownComponentMap.
  309. return MarkdownComponentMap.create_map_fn_var(fn_body=formatted_component)
  310. def _get_map_fn_custom_code_from_children(
  311. self, component: BaseComponent
  312. ) -> list[str]:
  313. """Recursively get markdown custom code from children components.
  314. Args:
  315. component: The component to check for custom code.
  316. Returns:
  317. A list of markdown custom code strings.
  318. """
  319. custom_code_list = []
  320. if isinstance(component, MarkdownComponentMap):
  321. custom_code_list.append(component.get_component_map_custom_code())
  322. # If the component is a custom component(rx.memo), obtain the underlining
  323. # component and get the custom code from the children.
  324. if isinstance(component, CustomComponent):
  325. custom_code_list.extend(
  326. self._get_map_fn_custom_code_from_children(
  327. component.component_fn(*component.get_prop_vars())
  328. )
  329. )
  330. elif isinstance(component, Component):
  331. for child in component.children:
  332. custom_code_list.extend(
  333. self._get_map_fn_custom_code_from_children(child)
  334. )
  335. return custom_code_list
  336. @staticmethod
  337. def _component_map_hash(component_map: dict) -> str:
  338. inp = str(
  339. {tag: component(_MOCK_ARG) for tag, component in component_map.items()}
  340. ).encode()
  341. return md5(inp).hexdigest()
  342. def _get_component_map_name(self) -> str:
  343. return f"ComponentMap_{self.component_map_hash}"
  344. def _get_custom_code(self) -> str | None:
  345. hooks = {}
  346. from reflex.compiler.templates import MACROS
  347. for _component in self.component_map.values():
  348. comp = _component(_MOCK_ARG)
  349. hooks.update(comp._get_all_hooks())
  350. formatted_hooks = MACROS.module.renderHooks(hooks) # pyright: ignore [reportAttributeAccessIssue]
  351. return f"""
  352. function {self._get_component_map_name()} () {{
  353. {formatted_hooks}
  354. return (
  355. {LiteralVar.create(self.format_component_map())!s}
  356. )
  357. }}
  358. """
  359. def _render(self) -> Tag:
  360. tag = (
  361. super()
  362. ._render()
  363. .add_props(
  364. remark_plugins=_REMARK_PLUGINS,
  365. rehype_plugins=_REHYPE_PLUGINS,
  366. components=Var(_js_expr=f"{self._get_component_map_name()}()"),
  367. )
  368. .remove_props("componentMap", "componentMapHash")
  369. )
  370. return tag