markdown.py 15 KB

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