在 React Markdown 中渲染自定义组件

huangapple go评论53阅读模式
英文:

Render custom components in React Markdown

问题

我正在构建我的网站文档,需要渲染自定义组件和它的属性,比如<Callout>Hi! I'm a callout</Callout>

我正在使用contentlayer来管理目录,并使用react-markdown来渲染内容。

以下是我的代码。

<ReactMarkdown
  rehypePlugins={[rehypeRaw]}
  className={s.markdown}
  components={{
    code({ inline, children, ...props }) {
      if (!inline) {
        return (
          <SyntaxHighlighter
            style={coldarkDark}
            language={'typescript'}
          >
            {String(children).replace(/\n$/, '')}
          </SyntaxHighlighter>
        )
      }
      return <code {...props}>{children}</code>
    },
  }}
>
  {currentDoc.body}
</ReactMarkdown>

{currentDoc.body} 是Markdown内容。所以,通过这个例子,我将这个内容传递给了<ReactMarkdown />

Mollit nisi cillum exercitation minim officia velit laborum non Lorem
adipisicing dolore. Labore commodo consectetur commodo velit adipisicing irure
dolore dolor reprehenderit aliquip. Reprehenderit cillum mollit eiusmod
excepteur elit ipsum aute pariatur in. Cupidatat ex culpa velit culpa ad non
labore exercitation irure laborum.

<Callout>Hi! I'm a callout</Callout>

Hey! [link](https://github.com/rehypejs/rehype-react) right here

如果需要进一步帮助或有其他问题,请随时提出。

英文:

I'm building my website documentation and I need to render custom component and it's props, such as &lt;Callout&gt;Hi! I&#39;m a callout&lt;/Callout&gt;.

I'm using contentlayer for the directory and react-markdown to render the content.

Here's my code.

          &lt;ReactMarkdown
            rehypePlugins={[rehypeRaw]}
            className={s.markdown}
            components={{
              code({ inline, children, ...props }) {
                if (!inline) {
                  return (
                    &lt;SyntaxHighlighter
                      style={coldarkDark}
                      language={&#39;typescript&#39;}
                    &gt;
                      {String(children).replace(/\n$/, &#39;&#39;)}
                    &lt;/SyntaxHighlighter&gt;
                  )
                }
                return &lt;code {...props}&gt;{children}&lt;/code&gt;
              },
            }}
          &gt;
            {currentDoc.body}
          &lt;/ReactMarkdown&gt;

{currentDoc.body} is markdown. So, with this example, I'm passing this to &lt;ReactMarkdown /&gt;:

Mollit nisi cillum exercitation minim officia velit laborum non Lorem
adipisicing dolore. Labore commodo consectetur commodo velit adipisicing irure
dolore dolor reprehenderit aliquip. Reprehenderit cillum mollit eiusmod
excepteur elit ipsum aute pariatur in. Cupidatat ex culpa velit culpa ad non
labore exercitation irure laborum.

&lt;Callout&gt;Hi! I&#39;m a callout&lt;/Callout&gt;

Hey! 
(https://github.com/rehypejs/rehype-react) right here

答案1

得分: 1

以下是代码部分的翻译:

code({ inline, className, children, ...props }) {
  if (inline) return &lt;code {...props}&gt;{children}&lt;/code&gt;

  const value = String(children).replace(/\n$/, &#39;&#39;);

  if (className === &quot;language-callout&quot;) return (
    &lt;Callout&gt;{value}&lt;/Callout&gt;
  )

  return (
    &lt;SyntaxHighlighter
      style={coldarkDark}
      language={&#39;typescript&#39;}
     &gt; 
       {value}
     &lt;/SyntaxHighlighter&gt;
   )
}

如果你需要更多翻译或有其他问题,请告诉我。

英文:

The idea is to catch your component Callout from your markdown and render it?
If so, I think i would use a custom "code language" in your markdown, and then parse it in yourcomponents callback from react-markdown.

For example, your markdown would be

Mollit nisi cillum exercitation minim officia velit laborum non Lorem
adipisicing dolore. Labore commodo consectetur commodo velit adipisicing irure
dolore dolor reprehenderit aliquip. Reprehenderit cillum mollit eiusmod
excepteur elit ipsum aute pariatur in. Cupidatat ex culpa velit culpa ad non
labore exercitation irure laborum.

```callout
Hi! I&#39;m a callout
```
Something else....

React-markdown then render anything as code with a particular className using language-{code} which means in this case language-callout. So you need to catch it and render your callout depending on the className

code({ inline, className, children, ...props }) {
  if (inline) return &lt;code {...props}&gt;{children}&lt;/code&gt;

  const value = String(children).replace(/\n$/, &#39;&#39;);

  if (className === &quot;language-callout&quot;) return (
    &lt;Callout&gt;{value}&lt;/Callout&gt;
  )

  return (
    &lt;SyntaxHighlighter
      style={coldarkDark}
      language={&#39;typescript&#39;}
     &gt; 
       {value}
     &lt;/SyntaxHighlighter&gt;
   )
}

huangapple
  • 本文由 发表于 2023年2月10日 05:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定