search

Home  >  Q&A  >  body text

Add new elements to react-markdown content in Next.js without using JSX

I use react-markdown to build a virtual DOM, which allows only updating the changed DOM instead of a complete rewrite. It generates content in

tags. I want to add

tag inside tag.

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

P粉184747536P粉184747536318 days ago674

reply all(1)I'll reply

  • P粉311617763

    P粉3116177632024-01-18 00:23:45

    A custom rendering function may be used for the paragraph node type. I'm not sure, but it might help.

    import React from 'react';
    import ReactMarkdown from 'react-markdown';
    
    const renderers = {
      paragraph: ({ node, ...props }) => {
        return <p {...props}><span>在此添加您的附加内容</span>{node.children}</p>;
      },
      // 根据需要使用您的自定义渲染器
    };
    
    const content = '在此添加您的markdown内容';
    
    const App = () => {
      return (
        <ReactMarkdown renderers={renderers}>
          {content}
        </ReactMarkdown>
      );
    };
    
    export default App;

    reply
    0
  • Cancelreply