请问 next.js 里如何好好渲染 md - V2EX
jlak

请问 next.js 里如何好好渲染 md

  •  
  •   jlak Jan 11, 2025 1958 views
    This topic created in 494 days ago, the information mentioned may be changed or developed.

    搞了好久都没搞起来,不是渲染本地 md 文件,而是从 cms 抓取 markdown 的 content 然后丢给 next-mdx-remote/rsc 渲染,需要服务器端渲染

    但是搞了很久渲染的和想要的效果差很远

    还不区分换行和空行,所有空行都没了

    最小代码

    import { MDXRemote } from "next-mdx-remote/rsc"; export default async function Render({ md }) { return ( <MDXRemote source={md} /> ); } 

    想要的效果 514e7cd8b2562d3c5f7ccfa08f9bb891.png

    实际渲染 6608d1c5e0e5915ef24b5f467071916a.png

    Supplement 1    Jan 11, 2025
    找到元凶了,是 TailwindCSS 去掉了所有样式
    Supplement 2    Jan 11, 2025
    成功解决!方案是安装 @tailwindcss/typography 插件 然后添加 prose 到 className
    11 replies    2025-01-11 09:52:33 +08:00
    scienhub
        1
    scienhub  
       Jan 11, 2025 via iPhone
    看下 mdx( https://nextjs.org/docs/pages/building-your-application/configuring/mdx)

    主要就是配合 rehype 和 remark 插件实现 md 渲染。

    有个 remark-gfm 可以实现 GitHub flavored markdown 样式。
    shakaraka
        2
    shakaraka  
    PRO
       Jan 11, 2025
    最小代码建议给一个在线跑起来的。

    目测是你的 md 没样式
    bgm004
        3
    bgm004  
       Jan 11, 2025 via Android
    样式要自己加
    jlak
        4
    jlak  
    OP
       Jan 11, 2025
    @scienhub
    我用的 next-mdx-remote/rsc 官网的文档不一样
    remark-gfm 加入区别不大,很细的变化
    jlak
        5
    jlak  
    OP
       Jan 11, 2025
    @wunonglin md 什么样式?
    https://dillinger.io 我是这里复制过来的,也复制了其他的和 ai 写的,都一样
    我 api 写入数据库 再读区数据库的

    @Track13 连空行都消失了 这也是样式关系吗
    jlak
        6
    jlak  
    OP
       Jan 11, 2025
    新弄了一套 还是丑 空行都消失了
    ```Javascript
    import { MDXRemote } from "next-mdx-remote/rsc";
    import matter from "gray-matter";
    import remarkGfm from "remark-gfm";
    export default async function Test3() {
    const data = await fetch(
    "https://raw.githubusercontent.com/onwidget/astrowind/refs/heads/main/src/data/post/landing.md"
    );
    const text = await data.text();
    const { content } = matter(text);

    const mdxOptiOns= {
    mdxOptions: {
    remarkPlugins: [remarkGfm],
    },
    };

    const compOnents= {
    h1: ({ children }: { children: React.ReactNode }) => (
    <h1 className="text-3xl font-bold text-red-500">{children}</h1>
    ),
    p: ({ children }: { children: React.ReactNode }) => (
    <p className="text-lg text-gray-700">{children}</p>
    ),
    a: ({ children, href }: { children: React.ReactNode; href: string }) => (
    <a href={href} className="text-blue-500 hover:underline">
    {children}
    </a>
    ),
    ul: ({ children }: { children: React.ReactNode }) => (
    <ul className="list-disc list-inside text-gray-700">{children}</ul>
    ),
    li: ({ children }: { children: React.ReactNode }) => (
    <li className="text-gray-700">{children}</li>
    ),
    code: ({ children }: { children: React.ReactNode }) => (
    <code className="text-gray-700">{children}</code>
    ),
    blockquote: ({ children }: { children: React.ReactNode }) => (
    <blockquote className="text-gray-700">{children}</blockquote>
    ),
    table: ({ children }: { children: React.ReactNode }) => (
    <table className="text-gray-700">{children}</table>
    ),
    tr: ({ children }: { children: React.ReactNode }) => (
    <tr className="text-gray-700">{children}</tr>
    ),
    td: ({ children }: { children: React.ReactNode }) => (
    <td className="text-gray-700">{children}</td>
    ),
    };

    return (
    <div className="container mx-auto p-4">
    <MDXRemote
    source={content}
    optiOns={mdxOptions}
    compOnents={components}
    />
    </div>
    );
    }
    ```
    jlak
        7
    jlak  
    OP
       Jan 11, 2025
    Reply 6
    jlak
    OP
    刚刚
    新弄了一套 还是丑 空行都消失了
    ```Javascript
    import { MDXRemote } from "next-mdx-remote/rsc";
    import matter from "gray-matter";
    import remarkGfm from "remark-gfm";
    export default async function Test3() {
    const data = await fetch(
    "https://raw.githubusercontent.com/onwidget/astrowind/refs/heads/main/src/data/post/landing.md"
    );
    const text = await data.text();
    const { content } = matter(text);

    const mdxOptiOns= {
    mdxOptions: {
    remarkPlugins: [remarkGfm],
    },
    };

    const compOnents= {
    h1: ({ children }: { children: React.ReactNode }) => (
    <h1 className="text-3xl font-bold text-red-500">{children}</h1>
    ),
    p: ({ children }: { children: React.ReactNode }) => (
    <p className="text-lg text-gray-700">{children}</p>
    ),
    a: ({ children, href }: { children: React.ReactNode; href: string }) => (
    <a href={href} className="text-blue-500 hover:underline">
    {children}
    </a>
    ),
    ul: ({ children }: { children: React.ReactNode }) => (
    <ul className="list-disc list-inside text-gray-700">{children}</ul>
    ),
    li: ({ children }: { children: React.ReactNode }) => (
    <li className="text-gray-700">{children}</li>
    ),
    code: ({ children }: { children: React.ReactNode }) => (
    <code className="text-gray-700">{children}</code>
    ),
    blockquote: ({ children }: { children: React.ReactNode }) => (
    <blockquote className="text-gray-700">{children}</blockquote>
    ),
    table: ({ children }: { children: React.ReactNode }) => (
    <table className="text-gray-700">{children}</table>
    ),
    tr: ({ children }: { children: React.ReactNode }) => (
    <tr className="text-gray-700">{children}</tr>
    ),
    td: ({ children }: { children: React.ReactNode }) => (
    <td className="text-gray-700">{children}</td>
    ),
    };

    return (
    <div className="container mx-auto p-4">
    <MDXRemote
    source={content}
    optiOns={mdxOptions}
    compOnents={components}
    />
    </div>
    );
    }
    jlak
        8
    jlak  
    OP
       Jan 11, 2025
    jlak
        9
    jlak  
    OP
       Jan 11, 2025
    找到元凶了,是 TailwindCSS 去掉了所有样式,删除 TailwindCSS 后正常了,但又不能不用 TailwindCSS
    scienhub
        10
    scienhub  
       Jan 11, 2025
    @jlak 如果是 tailwindcss 冲突了的话,可以在 tailwindcss.config.js 里面设置一下。
    jlak
        11
    jlak  
    OP
       Jan 11, 2025 via iPhone
    @scienhub tw 会删除所有默认样式,用 @ tailwindcss/typography 插件后好多了
    About     Help     Advertise     Blog     API     FAQ     Solana     1034 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 19:13 PVG 03:13 LAX 12:13 JFK 15:13
    Do have faith in what you're doing.
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-11940834-2', 'v2ex.com'); ga('send', 'pageview'); ga('send', 'event', 'Node', 'topic', 'qna'); ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86