skip to Main Content

The markdown is not working except for italic and bold. I have figured out that the problem is caused by Tailwind CSS because of how it handles text-size and other styles. If I comment out the index.css import (which defines the directives for Tailwind) in my index.jsx, all markdown types like heading, code, etc. work fine.

News.jsx

import ReactMarkdown from 'react-markdown';
import { useState } from 'react';

function News() {
  const [markdown, setMarkdown] = useState('# I am heading');
  
  return (
    <div>
      <textarea value={markdown} onChange={e => setMarkdown(e.target.value)} />
      <ReactMarkdown>{markdown}</ReactMarkdown>
    </div>
  );
}

export default News;

index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter as Router } from "react-router-dom";
import './index.css'
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <Router>
    <App />
    </Router>
  </React.StrictMode>
);

2

Answers


  1. Ah I think i know what the issue is @ayex.

    Your React markdown doesn’t have the prose classname which is what tailwind uses for the default rendering of tailwind components/text

    codesandbox

    import { useState } from "react";
    import ReactMarkdown from "react-markdown";
    
    const defaultMd = `# iam heading`;
    
    const ExampleComponent = () => {
      const [markdownSource, setMarkdownSource] = useState(defaultMd);
    
      const onChange = ({ currentTarget: { value } }) => {
        setMarkdownSource(value);
      };
    
      return (
        <>
          <textarea
            onChange={onChange}
            value={markdownSource}
            className="
              font-mono
              overflow-auto
              whitespace-pre
              border-solid
              border
              border-gray-300
              resize
              w-full
            "
          />
          <ReactMarkdown className="prose">{markdownSource}</ReactMarkdown>
        </>
      );
    };
    
    const App = () => (
      <div className="App">
        <ExampleComponent />
      </div>
    );
    
    export default App;
    
    Login or Signup to reply.
  2. You should add typography plugin in your tailwindcss

    1.install npm install -D @tailwindcss/typography

    2.add the plugin to your tailwind.config.js file:

    module.exports = {
      theme: {
        // ...
      },
      plugins: [
        require('@tailwindcss/typography'),
        // ...
      ],
    }
    
    1. use prose
    <div class="prose lg:prose-xl">
      {{ markdown }}
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search