root-wrapper.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { MDXProvider } from '@mdx-js/react';
  2. import { Highlight, themes, defaultProps } from 'prism-react-renderer';
  3. import CodePen from './src/components/CodePen';
  4. import Editor from './src/components/Editor';
  5. import {
  6. Heading1,
  7. Heading2,
  8. Heading3,
  9. Heading4,
  10. Heading5,
  11. Heading6,
  12. } from './src/components/Heading';
  13. const components = {
  14. CodePen,
  15. Editor,
  16. More: () => <div style={{ display: 'none' }}>{/* more */}</div>,
  17. h1: Heading1,
  18. h2: Heading2,
  19. h3: Heading3,
  20. h4: Heading4,
  21. h5: Heading5,
  22. h6: Heading6,
  23. pre: ({ children }) => {
  24. const className = children.props.className || '';
  25. const matches = className.match(/language-(?<lang>.*)/);
  26. return (
  27. <Highlight
  28. {...defaultProps}
  29. code={children.props.children}
  30. language={
  31. matches && matches.groups && matches.groups.lang
  32. ? matches.groups.lang
  33. : ''
  34. }
  35. >
  36. {({ className, style, tokens, getLineProps, getTokenProps }) => (
  37. <pre className={className} style={style}>
  38. <code>
  39. {tokens.map((line, i) =>
  40. i === tokens.length - 1 &&
  41. line[0].empty &&
  42. line.length === 1 ? null : (
  43. <div {...getLineProps({ line, key: i })}>
  44. {line.map((token, key) => (
  45. <span {...getTokenProps({ token, key })} />
  46. ))}
  47. </div>
  48. ),
  49. )}
  50. </code>
  51. </pre>
  52. )}
  53. </Highlight>
  54. );
  55. },
  56. };
  57. export const wrapRootElement = ({ element }) => (
  58. <MDXProvider components={components}>{element}</MDXProvider>
  59. );