Skip to content

when using SyntaxHighlighter children is [object Object] instead of string #202

Open
@preampinbut

Description

@preampinbut

image

// react-markdown
<ReactMarkdown
  components={{
    code({ node, inline, className, children, ...props }) {
      const match = /language-(\w+)/.exec(className || "");
      return !inline && match ? (
        <SyntaxHighlighter
          language={match[1]}
          style={markdownTheme as any}
          PreTag="div"
          {...props}
        >
          {String(children).replace(/\n$/, "")} // <============
        </SyntaxHighlighter>
      ) : (
        <code
          className={className}
          {...props}
        >
          {children}
        </code>
      );
    },
  }}
>
  {markdownSource}
</ReactMarkdown>

// @uiw/react-markdown-preview
<MarkdownPreview
  source={markdownSource}
  components={{
    code({ node, inline, className, children, ...props }) {
      const match = /language-(\w+)/.exec(className || "");
      return !inline && match ? (
        <SyntaxHighlighter
          language={match[1]}
          style={markdownTheme as any}
          PreTag="div"
          {...props}
        >
          {String(children).replace(/\n$/, "")} //  <============
        </SyntaxHighlighter>
      ) : (
        <code
          className={className}
          {...props}
        >
          {children}
        </code>
      );
    },
  }}
/>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions