markdown.py 1.9 KB

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