Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy button for code block disappears with rehypeSanitize #269

Open
MaxiPigna opened this issue Jul 16, 2024 · 1 comment
Open

Copy button for code block disappears with rehypeSanitize #269

MaxiPigna opened this issue Jul 16, 2024 · 1 comment

Comments

@MaxiPigna
Copy link

Given the following code

const MarkdownPresenter: FC<MarkdownPresenterProps> = ({ text }) => {
  const { theme } = useTheme();
  const rehypePlugins = [rehypeSanitize, () => rehypeExternalLinks({ target: '_blank' })];
  return (
    <MarkdownPreview
      source={text}
      rehypePlugins={rehypePlugins}
      style={{
        backgroundColor: 'transparent',
      }}
      wrapperElement={{
        'data-color-mode': theme,
      }}
      components={{
        code: ({ children = [], className, ...props }) => {
          if (typeof children === 'string' && /^\$\$(.*)\$\$/.test(children)) {
            const html = katex.renderToString(children.replace(/^\$\$(.*)\$\$/, '$1'), {
              throwOnError: false,
            });
            return <code dangerouslySetInnerHTML={{ __html: html }} style={{ background: 'transparent' }} />;
          }
          const code = props.node && props.node.children ? getCodeString(props.node.children) : children;
          if (
            typeof code === 'string' &&
            typeof className === 'string' &&
            /^language-katex/.test(className.toLocaleLowerCase())
          ) {
            const html = katex.renderToString(code, {
              throwOnError: false,
            });
            return <code style={{ fontSize: '150%' }} dangerouslySetInnerHTML={{ __html: html }} />;
          }
          return <code className={String(className)}>{children}</code>;
        },
      }}
    />
  );
};

Copy button for code block doesn't appear.
image

I found out that the if I remove rehypeSanitize from rehypePlugins, then copy button appears as expected.
image

Package version

  • "rehype-sanitize": "^6.0.0",
  • "@uiw/react-markdown-preview": "^5.1.1",

Is there any solution/workaround for this issue? Thanks

@MaxiPigna MaxiPigna changed the title Copy button for code block disappear with rehypeSanitize Copy button for code block disappears with rehypeSanitize Jul 16, 2024
jaywcjlove added a commit that referenced this issue Aug 2, 2024
@jaywcjlove
Copy link
Member

@MaxiPigna

import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
import MarkdownPreview from '@uiw/react-markdown-preview';

const source = `
## MarkdownPreview

\`\`\`bash
$ npm install @uiw/react-markdown-preview --save
\`\`\`
`;

export default function Demo() {
  return (
    <MarkdownPreview
      source={source}
      rehypePlugins={[
        [
          rehypeSanitize,
          {
            ...defaultSchema,
            attributes: {
              ...defaultSchema.attributes,
              svg: ['className', 'hidden', 'viewBox', 'fill', 'height', 'width'],
              path: ['fill-rule', 'd'],
              div: ['className', 'class', 'data-code', ...(defaultSchema.attributes?.div || [])],
            },
            tagNames: [...(defaultSchema.tagNames || []), 'svg', 'path', 'div'],
          },
        ],
      ]}
      style={{ padding: 16 }}
    />
  );
}

github-actions bot added a commit that referenced this issue Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants