【问题标题】:Using syntax highlighter with tsx react markdown使用带有 tsx react markdown 的语法高亮
【发布时间】:2021-12-19 05:04:44
【问题描述】:

我正在关注 Github https://github.com/remarkjs/react-markdown 并尝试将 SyntaxHighlighter 添加到我的降价代码 sn-ps 中。尝试在用于呈现帖子的函数中使用示例代码时,出现下面提到的错误。 Markdown 使用三个反引号,这使用 NextJS。

如果我注释掉 {...props} 行,红色下划线会被移除,但我没有看到代码在我的帖子中生效

尝试添加如下任何内容,但我仍然没有在降价中看到任何效果。

code({ node, inline, className, children, ...props }: any)

错误:

No overload matches this call.
  Overload 1 of 2, '(props: SyntaxHighlighterProps | Readonly<SyntaxHighlighterProps>): SyntaxHighlighter', gave the following error.
    Type '{ ref?: LegacyRef<HTMLElement> | undefined; key?: Key | null | undefined; defaultChecked?: boolean | undefined; defaultValue?: string | number | readonly string[] | undefined; ... 255 more ...; PreTag: string; }' is not assignable to type 'IntrinsicClassAttributes<SyntaxHighlighter>'.
      Types of property 'ref' are incompatible.
        Type 'LegacyRef<HTMLElement> | undefined' is not assignable to type 'LegacyRef<SyntaxHighlighter> | undefined'.
          Type '(instance: HTMLElement | null) => void' is not assignable to type 'LegacyRef<SyntaxHighlighter> | undefined'.
            Type '(instance: HTMLElement | null) => void' is not assignable to type 

代码:

const BlogPost = ({ frontMatter, markdownBody }: BlogPostProps) => {
  if (!frontMatter) return <></>;
  return (
    <Layout>
      <ReactMarkdown
        components={{
          code({ node, inline, className, children, ...props }) {
            const match = /language-(\w+)/.exec(className || "");
            return !inline && match ? (
              <SyntaxHighlighter
                style={vscDarkPlus}
                language={match[1]}
                PreTag="div"
                // {...props}
              >
                {String(children).replace(/\n$/, "")}
              </SyntaxHighlighter>
            ) : (
              <code className={className} {...props}>
                {children}
              </code>
            );
          },
        }}
      >
        {markdownBody}
      </ReactMarkdown>
    </Layout>
  );
};

【问题讨论】:

    标签: reactjs typescript next.js react-markdown react-syntax-highlighter


    【解决方案1】:

    我今天遇到了类似的问题。看来他们的文档到处都是。我看到 SyntaxHighlighter 需要孩子,但您未能将其添加为标签的一部分。下面是我的代码 sn-p,其中的道具也被注释掉了。 This code snippet is an edit from remarkjs.

          <ReactMarkdown className="preview-markdown" 
          children={input}
          remarkPlugins={[[remarkGfm, {singleTilde: false}]]}
          components={{
            code({node, inline, className, children, ...props}) {
              const match = /language-(\w+)/.exec(className || '')
              return !inline && match ? (
                <SyntaxHighlighter
                  children={String(children).replace(/\n$/, '')}
                  style={docco}
                  language={match[1]}
                  PreTag="div"
                  // {...props}
                />
              ) : (
                <code className={className} {...props}>
                  {children}
                </code>
              )
            }
          }}
           />
    

    【讨论】:

    • 谢谢谢谢。经过数小时的故障排除后,这条线专门解决了我的问题。 children={String(children).replace(/\n$/, '')} 有趣的是,如果我只将语法高亮器传递给代码组件,我没有任何问题。
    • 我认为这仅仅是由打字稿引起的,因为它不会检查该组件中的代码是否有效。根据经验,在代码尝试运行之前我不会注意到问题。我通常创建一个单独的函数,我可以对其进行单元测试,然后在代码组件中引用该组件。
    猜你喜欢
    • 2016-06-09
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2019-11-05
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多