5

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>
);
Ed Lucas
  • 5,955
  • 4
  • 30
  • 42
ayex
  • 65
  • 7

3 Answers3

6

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>
Eric Tsai
  • 104
  • 3
3

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;
Denis Tsoi
  • 9,428
  • 8
  • 37
  • 56
2

For any other viewers, i actually had to both...

  1. install @tailwindcss/typography as eric suggested.
  2. set <ReactMarkdown className="prose"># Heading 1</ReactMarkdown> as denis suggested.
kevin
  • 2,707
  • 4
  • 26
  • 58