uiwjs / react-markdown-preview

React component preview markdown text in web browser. The minimal amount of CSS to replicate the GitHub Markdown style. Support dark-mode/night mode.
https://uiwjs.github.io/react-markdown-preview
MIT License
271 stars 48 forks source link

[bug] render source="<div ref={} />" #252

Closed hshe closed 8 months ago

hshe commented 8 months ago

throw error

image

jaywcjlove commented 8 months ago

@hshe Provide an example that reproduces the error and I can help you take a look

hshe commented 8 months ago

"past this content

" to https://uiwjs.github.io/react-markdown-preview/

F12 show the error logs image image image add last char '>' then crash @jaywcjlove

hshe commented 8 months ago

image contains element <div ref .....

hshe commented 8 months ago

example

import MarkdownPreview from '@uiw/react-markdown-preview';

const source = `
\`\`\`js {2}
function () {
  console.log('hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello')
}
\`\`\`
\`\`\`js {2}
function () {
  console.log('hello ')
}
joker <div ref={endRef} /> 3
\`\`\`
joker <div ref/> 3
`;

export default function Demo() {
  return (
    <MarkdownPreview
      source={source}
    />
  );
}
jaywcjlove commented 8 months ago

@hshe This is a problem caused by rehype-raw, Here is a solution:

<MarkdownPreview
    pluginsFilter={(type, plugin) => {
        if (type === "rehype") {
          let data = plugin.filter((item) => {
            if (typeof item === 'function' && item.name === "rehypeRaw") {
              return false;
            }
            return true;
          });
          return data;
        }
        return plugin;
    }}
    className="editor-preview"
    source={value}
/>

https://github.com/uiwjs/react-markdown-preview/blob/5ff252b606d20d25c6cbb6811f71bfcb0d86feff/core/src/index.tsx#L7

https://github.com/uiwjs/react-markdown-preview/blob/5ff252b606d20d25c6cbb6811f71bfcb0d86feff/core/src/preview.tsx#L46-L48

hshe commented 8 months ago

Thank you @jaywcjlove