Skip to content

Commit

Permalink
feat: react syntax highlighter (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
alarv authored Dec 19, 2024
1 parent 66ad11b commit 5812a32
Show file tree
Hide file tree
Showing 4 changed files with 445 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .idea/aws.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions mdx-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { MDXComponents } from 'mdx/types';
import { Link } from '@nextui-org/react';
import Image from 'next/image';
import { ImageProps } from 'next/image';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { materialDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';

export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
Expand Down Expand Up @@ -111,8 +113,22 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
</pre>
);
},
code({ children }) {
return <code style={{}}>{children}</code>;
code(props) {
const { children, className, ...rest } = props;
const match = /language-(\w+)/.exec(className || '');
return match ? (
<SyntaxHighlighter
PreTag="div"
// eslint-disable-next-line react/no-children-prop
children={String(children).replace(/\n$/, '')}
language={match[1]}
style={materialDark}
/>
) : (
<code {...rest} className={className}>
{children}
</code>
);
},
...components,
};
Expand Down
Loading

0 comments on commit 5812a32

Please sign in to comment.