markdown.py 16 KB

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