remarkjs / react-markdown

Markdown component for React
https://remarkjs.github.io/react-markdown/
MIT License
13.26k stars 876 forks source link

Headings and subheadings aren't getting rendered (NextJS 14/ChakraUI) #832

Closed FranticSoft closed 5 months ago

FranticSoft commented 5 months ago

Initial checklist

Affected packages and versions

react-markdown

Link to runnable example

Video: https://youtu.be/NglCta2XuCA

Steps to reproduce

Steps are shown in the video. My stack is using NextJS 14 (I forget what Node version this is) with ChakraUI/SaaS UI

Using NPM 10.2.3

Expected behavior

Anything using a # Should be rendered as a heading in Markdown

Actual behavior

The # gets ignored and not rendered. Text that should be heading is plaintext.

Runtime

Other (please specify in steps to reproduce)

Package manager

Other (please specify in steps to reproduce)

OS

Windows

Build and bundle tools

Next.js

tomfuller71 commented 5 months ago

I'm not familiar with ChakraUI, but I had a similar issue using using Next, react-markdown and tailwind, where because by default tailwind normalizes css for heading and lists the rendered markdown appears as plaintext . In my case my options were to either opt out of the default preflight normalizing, or to use the components prop to explicitly create styling for the elements I was interested in.

    <Markdown
      components={{
        p({ children }) {
          return <p className="mb-2 last:mb-0">{children}</p>;
        },
        h1({ children }) {
          return <h1 className="text-bold text-2xl">{children}</h1>;
        },
        h2({ children }) {
          return <h2 className="text-bold text-xl">{children}</h2>;
        },
        h3({ children }) {
          return <h3 className="text-bold text-lg">{children}</h3>;
        },
        ol({ children }) {
          return <ol className="list-inside list-decimal">{children}</ol>;
        },
        ul({ children }) {
          return <ul className="list-inside list-disc">{children}</ul>;
        },
        li({ children }) {
          return <li className="mb-2 list-item list-inside">{children}</li>;
        },
        blockquote({ children }) {
          return (
            <blockquote className="relative border-s-4 border-gray-800 bg-slate-200 pl-2 ps-4 sm:ps-6">
              {children}
            </blockquote>
          );
        },
      }}
    >
ChristianMurphy commented 5 months ago

Welcome @FranticSoft! 👋 Sorry you ran into some confusion.

react-markdown renders plain HTML, when you inspect the HTML on your page you'll see this. The issue is with the CSS reset on your page making headers no look like headers.

As @tomfuller71 alludes to this is most often due to tailwind CSS, but can happen in other frameworks too. Style the HTML elements to look how you want them to.

If you use plain CSS you can apply you can use https://github.com/sindresorhus/github-markdown-css With tailwind use prose https://dev.to/adamof/introduction-to-the-prose-class-that-instantly-beautifies-articles-tailwindcss--bn7 Other frameworks will also have similar equivalents, reach out to the community or team that maintains the CSS you are using for recommendations.

github-actions[bot] commented 5 months ago

Hi! This was closed. Team: If this was fixed, please add phase/solved. Otherwise, please add one of the no/* labels.