import '@catppuccin/highlightjs/sass/catppuccin-variables.rgb.scss'; import clsx from 'clsx'; import CopyCode from 'components/post/copy-code'; import { memo } from 'react'; import * as prod from 'react/jsx-runtime'; import rehypeHighlight from 'rehype-highlight'; import rehypeReact from 'rehype-react'; import remarkGfm from 'remark-gfm'; import remarkParse from 'remark-parse'; import remarkRehype from 'remark-rehype'; import { GistsFile } from 'types'; import { unified } from 'unified'; import styles from './gists-code.module.css'; interface Props { file: GistsFile; showFileName?: boolean; } const production = { Fragment: prod.Fragment, jsx: prod.jsx, jsxs: prod.jsxs }; /** * Render GitHub gists code. * * @params file * @params showaFileName determine show full content or not */ const GistsCode = ({ file, showFileName = false }: Props) => { const fileContent = showFileName ? file.content : file.content?.split('\n').slice(0, 20).join('\n'); const code = unified() .use(remarkParse) .use(remarkRehype) .use(remarkGfm) .use(rehypeHighlight) .use(rehypeReact, production) .processSync(`\`\`\`${file.language ?? ''}\n${fileContent}`).result; return ( <> {showFileName ? (
{file.filename}
{code}
) : (
{code}
)} ); }; export default memo(GistsCode);