markdown.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Table components."""
  2. from typing import List
  3. from pynecone import utils
  4. from pynecone.components.component import Component
  5. from pynecone.var import BaseVar, Var
  6. class Markdown(Component):
  7. """A markdown component."""
  8. library = "react-markdown"
  9. tag = "ReactMarkdown"
  10. # The source of the markdown.
  11. src: Var[str]
  12. def _get_imports(self):
  13. imports = super()._get_imports()
  14. imports["@chakra-ui/react"] = {"Heading", "Code", "Text", "Link"}
  15. imports["react-syntax-highlighter"] = {"Prism"}
  16. imports["remark-math"] = {"remarkMath"}
  17. imports["remark-gfm"] = {"remarkGfm"}
  18. imports["rehype-katex"] = {"rehypeKatex"}
  19. imports[""] = {"katex/dist/katex.min.css"}
  20. return imports
  21. def _render(self):
  22. tag = super()._render()
  23. return tag.add_props(
  24. children=utils.wrap(str(self.src).strip(), "`"),
  25. components={
  26. "h1": "{({node, ...props}) => <Heading size='2xl' {...props} />}",
  27. "h2": "{({node, ...props}) => <Heading size='xl' {...props} />}",
  28. "h3": "{({node, ...props}) => <Heading size='lg' {...props} />}",
  29. "p": "{Text}",
  30. "a": "{Link}",
  31. # "code": "{Code}"
  32. "code": """{({node, inline, className, children, ...props}) =>
  33. {
  34. const match = (className || '').match(/language-(?<lang>.*)/);
  35. return !inline ? (
  36. <Prism
  37. children={String(children).replace(/\n$/, '')}
  38. language={match ? match[1] : ''}
  39. {...props}
  40. />
  41. ) : (
  42. <Code {...props}>
  43. {children}
  44. </Code>
  45. );
  46. }}""".replace(
  47. "\n", " "
  48. ),
  49. },
  50. remark_plugins=BaseVar(name="[remarkMath, remarkGfm]", type_=List[str]),
  51. rehype_plugins=BaseVar(name="[rehypeKatex]", type_=List[str]),
  52. src="",
  53. )