MarkdownContent.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import ReactMarkdown from 'react-markdown';
  2. import remarkGfmNoAutolink from '../utils/remarkGfmNoAutolink';
  3. /**
  4. * Markdown written by a user, styled for the app: a folder README (#1268) and
  5. * the Connect slicer page's instructions (#3602).
  6. *
  7. * Raw HTML is not rendered (react-markdown's default) and links open in a new
  8. * tab without the opener. GFM tables, task lists and strikethrough work; bare
  9. * URLs don't turn into links (see remarkGfmNoAutolink, #2971).
  10. */
  11. export function MarkdownContent({ children }: { children: string }) {
  12. return (
  13. <ReactMarkdown
  14. remarkPlugins={[remarkGfmNoAutolink]}
  15. components={{
  16. h1: ({ children }) => <h1 className="text-lg font-semibold text-white mt-2 mb-1">{children}</h1>,
  17. h2: ({ children }) => <h2 className="text-base font-semibold text-white mt-2 mb-1">{children}</h2>,
  18. h3: ({ children }) => <h3 className="text-sm font-semibold text-white mt-2 mb-1">{children}</h3>,
  19. p: ({ children }) => <p className="my-1">{children}</p>,
  20. ul: ({ children }) => <ul className="list-disc list-inside space-y-0.5 ml-2">{children}</ul>,
  21. ol: ({ children }) => <ol className="list-decimal list-inside space-y-0.5 ml-2">{children}</ol>,
  22. li: ({ children }) => <li>{children}</li>,
  23. code: ({ children, ...props }) => {
  24. const inline = !(props as { className?: string }).className;
  25. return inline ? (
  26. <code className="px-1 py-0.5 bg-bambu-dark rounded text-xs font-mono text-bambu-green">{children}</code>
  27. ) : (
  28. <code className="block p-2 bg-bambu-dark rounded text-xs font-mono text-bambu-gray-light overflow-x-auto">{children}</code>
  29. );
  30. },
  31. pre: ({ children }) => <pre className="my-2">{children}</pre>,
  32. blockquote: ({ children }) => (
  33. <blockquote className="border-l-2 border-bambu-dark-tertiary pl-3 text-bambu-gray italic">{children}</blockquote>
  34. ),
  35. a: ({ children, href }) => (
  36. <a href={href} target="_blank" rel="noopener noreferrer" className="text-bambu-green hover:underline">
  37. {children}
  38. </a>
  39. ),
  40. table: ({ children }) => (
  41. <div className="overflow-x-auto">
  42. <table className="min-w-full text-xs border-collapse">{children}</table>
  43. </div>
  44. ),
  45. th: ({ children }) => <th className="border border-bambu-dark-tertiary px-2 py-1 text-left font-semibold text-white">{children}</th>,
  46. td: ({ children }) => <td className="border border-bambu-dark-tertiary px-2 py-1">{children}</td>,
  47. hr: () => <hr className="border-bambu-dark-tertiary my-2" />,
  48. }}
  49. >
  50. {children}
  51. </ReactMarkdown>
  52. );
  53. }