Add new elements to react-markdown content in Next.js without using JSX
P粉184747536
P粉184747536 2024-01-17 09:50:24
0
1
711

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粉184747536
P粉184747536

reply all(1)
P粉311617763

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;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template