markdown-to-jsx icon indicating copy to clipboard operation
markdown-to-jsx copied to clipboard

Option to provide a custom component as wrapper / pass className to wrapper?

Open WillNeve opened this issue 1 year ago • 1 comments

Option to provide a custom component as wrapper / pass className to wrapper?

I apologise in advance if this is covered in the docs but I could not find it

  • Is there an ability to provide a custom component as the wrapper instead of a base element type like options={{ wrapper: 'div' }}?
  • ... Or to provide a className for the existing wrapper element?

WillNeve avatar Jul 27 '24 18:07 WillNeve

@WillNeve

  • Yes, you can provide a custom component as a wrapper.
  • Since you are defining the custom component, you have complete control over the wrapper. You can add all the attributes you want, such as className, id, etc.

Example:

import Markdown from 'markdown-to-jsx';

type CustomElementProps = {
    children: React.ReactNode;
};

const CustomElement = ({ children }: CustomElementProps) => {
    return <section className="section-class"> {children} </section>;
};

const answer = `# A first-level heading 
## A second-level heading 
### A third-level heading`;

function App() {
    return (
        <Markdown
            options={{
                wrapper: CustomElement,
            }}
        >
            {answer}
        </Markdown>
    );
}

export default App;

Output:

image

YogiDhingani avatar Jul 30 '24 17:07 YogiDhingani