简体   繁体   English

.mdx 文件中的 React 组件和数学表达式在 Next.js 应用程序中未正确呈现

[英]React components and math expressions in .mdx file are not rendering correctly in Next.js application

My custom React components when imported in a .mdx file, are not being rendered at all.我的自定义 React 组件在导入 .mdx 文件时根本不会被渲染。 The math expressions in the .mdx file either render unformatted or throw parsing errors in spite of following the configuration instructions in the Next.js ( https://nextjs.org/docs/advanced-features/using-mdx ) and MDX documentation ( https://mdxjs.com/guides/math/ ).尽管遵循 Next.js ( https://nextjs.org/docs/advanced-features/using-mdx ) 和 MDX 文档 ( https://mdxjs.com/guides/math/ )。

Here are my configurations:这是我的配置:

//next.config.js
/** @type {import('next').NextConfig} */

const remarkMath = import('remark-math');
const rehypeKatex = import('rehype-katex');

const withMDX = require('@next/mdx')({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [remarkMath],
    rehypePlugins: [rehypeKatex],
    // If you use `MDXProvider`, uncomment the following line.
    // providerImportSource: "@mdx-js/react",
  },
})
module.exports = withMDX({
  pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
  reactStrictMode: true
})


//package.json

  "dependencies": {
    "@mdx-js/loader": "^2.1.1",
    "@next/mdx": "^12.1.5",
    "fs": "^0.0.1-security",
    "gray-matter": "^4.0.3",
    "next": "12.1.5",
    "path": "^0.12.7",
    "react": "18.0.0",
    "react-dom": "18.0.0",
    "recharts": "^2.1.9",
    "rehype-katex": "^6.0.2",
    "remark": "^14.0.2",
    "remark-html": "^15.0.1",
    "remark-math": "^5.1.1"
  },
  "devDependencies": {
    "@types/node": "17.0.25",
    "@types/react": "18.0.5",
    "@types/react-dom": "18.0.1",
    "eslint": "8.13.0",
    "eslint-config-next": "12.1.5",
    "typescript": "4.6.3"
  }

Here is my custom Document component to allow the app to fetch the katex.min.css file:这是我的自定义 Document 组件,允许应用获取 katex.min.css 文件:

//_document.tsx
import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
    return (
        <Html>
            <Head>
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.15.3/dist/katex.min.css" integrity="sha384-KiWOvVjnN8qwAZbuQyWDIbfCLFhLXNETzBQjA/92pIowpC0d2O3nppDGQVgwd2nB" crossOrigin="anonymous"/>
            </Head>
            <body>
                <Main />
                <NextScript />
            </body>
        </Html>
    )
}

What is it that I'm not doing correctly?我做错了什么?

If there are errors, probably you can't use require and so you didn't import the plugins successfully.如果有错误,可能你不能使用 require ,所以你没有成功导入插件。 You have to use import method in config file and use the MDXProvider wrap.您必须在配置文件中使用导入方法并使用 MDXProvider 包装。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用React可加载和获取组件数据(如Next.js)进行服务器端渲染? - How to do Server Side Rendering in React With React Loadable and Fetching Data for Components (Like Next.js)? 带有自定义服务器的next.js应用程序无法正确呈现 - next.js app with custom server is not rendering correctly 在 Next.js 中使用带有 MDX 的 Remark 和 Rehype 插件(使用 @next/mdx) - Using Remark and Rehype plugins with MDX in Next.js (with @next/mdx) Next.js:如何将仅外部客户端的 React 组件动态导入到开发的服务器端渲染应用程序中? - Next.js: How to dynamically import external client-side only React components into Server-Side-Rendering apps developed? 使用 Next.js 对服务器端渲染进行 React Query - React Query with server side rendering using Next.js react-table 在 next.js 中出现渲染问题 - react-table has a issue with rendering in next.js 如何在 React NEXT.JS 中的组件下方分层图像 - How to layer a image underneath components in React NEXT.JS React/Next.js Keydown 事件侦听器未正确触发 - React/Next.js Keydown Event Listener Not Triggering Correctly MDX 博客仅显示 markdown 内容,而不是在 Next JS 中使用来自 `@mdx-js/react` 的 `MDXProvider` 进行渲染 - MDX blog just displays markdown content instead of rendering it while using `MDXProvider` from `@mdx-js/react` in Next JS 如何在 react/next.js 应用程序中安全地存储 JWT 令牌? - How to securely store JWT tokens in react/next.js application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM