使用Next.js 13与remark和rehype插件。

huangapple go评论53阅读模式
英文:

Using remark and rehype plugins with nextjs-13

问题

以下是翻译的部分:

"I wanted to try out nextjs-13 so made a very simple blog."
我想尝试使用nextjs-13,所以创建了一个非常简单的博客。

"Folder structure (skip to 'The Problem' section for actual issue):"
文件夹结构(跳到'问题'部分查看实际问题):

"app/ page.jsx layout.jsx test.mdx public/ ... styles/ ... mdx-components.jsx next.config.mjs package.json"
app/ page.jsx layout.jsx test.mdx public/ ... styles/ ... mdx-components.jsx next.config.mjs package.json

"So this is a pure nextjs-13 app with all content only in the app/ directory."
这是一个纯粹的nextjs-13应用程序,所有内容都在app/目录中。

"next.config.mjs"
next.config.mjs

"import nextMDX from "@next/mdx";"
从“@next/mdx”导入nextMDX。

"import remarkGfm from "remark-gfm""
从“remark-gfm”导入remarkGfm。

"import rehypePrism from "@mapbox/rehype-prism";"
从“@mapbox/rehype-prism”导入rehypePrism。

"const nextConfig = { pageExtensions: ["ts", "tsx", "js", "jsx", "mdx"], experimental: { appDir: true, mdxRs: true, }, reactStrictMode: true };"
const nextConfig = { pageExtensions: ["ts", "tsx", "js", "jsx", "mdx"], experimental: { appDir: true, mdxRs: true, }, reactStrictMode: true };

"export default nextMDX({ extension: /.mdx?$/, options: { remarkPlugins: [remarkGfm], rehypePlugins: [rehypePrism], } })(nextConfig);"
导出nextMDX({ extension: /.mdx?$/, options: { remarkPlugins: [remarkGfm], rehypePlugins: [rehypePrism], } })(nextConfig);

"package.json"
package.json

{
"private": true,
"scripts": {
// ...
},
"dependencies": {
"@mapbox/rehype-prism": "^0.8.0",
"@next/mdx": "latest",
"next": "latest",
"react": "18.2.0",
"react-dom": "18.2.0",
"remark-gfm": "^3.0.1",
"remark-rehype": "^10.1.0",
},
}
{
"private": true,
"scripts": {
// ...
},
"dependencies": {
"@mapbox/rehype-prism": "^0.8.0",
"@next/mdx": "latest",
"next": "latest",
"react": "18.2.0",
"react-dom": "18.2.0",
"remark-gfm": "^3.0.1",
"remark-rehype": "^10.1.0",
},
}

"and finally, page.jsx"
最后,page.jsx

"import Foo from "./test.mdx";"
从“./test.mdx”导入Foo。

"export default function Page() { return <Foo /> };"
导出函数Page() { return <Foo /> }。

"and layout.jsx"
和layout.jsx

"import "./global.scss";"
导入“./global.scss”。

"import "../styles/prism.css";"
导入“../styles/prism.css”。

"export default function RootLayout({ children }) { return ( <html lang="en"> <body>{children}</body> </html> ); }"
导出函数RootLayout({ children }) { return ( <html lang="en"> <body>{children}</body> </html> ); }

"The problem"
问题

"When I run this, next certainly seems to be compiling the mdx. But it's some very basic out-of-the-box transform that does not parse code blocks (all code appears as a single string), and does not render tables correctly. That is, rehypePrism and remarkGfm are not actually being applied."
当我运行这个时,next似乎确实在编译mdx。但它是一种非常基本的开箱即用的转换,它不解析代码块(所有代码都显示为一个字符串),并且不正确地渲染表格。也就是说,rehypePrism和remarkGfm实际上并没有被应用。

英文:

I wanted to try out nextjs-13 so made a very simple blog.

Folder structure (skip to 'The Problem' section for actual issue):

app/
  page.jsx
  layout.jsx
  test.mdx
public/
 ...
styles/
 ...
mdx-components.jsx
next.config.mjs
package.json

So this is a pure nextjs-13 app with all content only in the app/ directory.

next.config.mjs

import nextMDX from &quot;@next/mdx&quot;;
import remarkGfm from &quot;remark-gfm&quot;;
import rehypePrism from &quot;@mapbox/rehype-prism&quot;;

// /** @type {import(&#39;next&#39;).NextConfig} */
const nextConfig = {
  pageExtensions: [&quot;ts&quot;, &quot;tsx&quot;, &quot;js&quot;, &quot;jsx&quot;, &quot;mdx&quot;],
  experimental: {
    appDir: true,
    mdxRs: true,
  },
  reactStrictMode: true,
};

export default nextMDX({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [remarkGfm],
    rehypePlugins: [rehypePrism],
  },
})(nextConfig);

package.json

{
  &quot;private&quot;: true,
  &quot;scripts&quot;: {
    // ...
  },
  &quot;dependencies&quot;: {
    &quot;@mapbox/rehype-prism&quot;: &quot;^0.8.0&quot;,
    &quot;@next/mdx&quot;: &quot;latest&quot;,
    &quot;next&quot;: &quot;latest&quot;,
    &quot;react&quot;: &quot;18.2.0&quot;,
    &quot;react-dom&quot;: &quot;18.2.0&quot;,
    &quot;remark-gfm&quot;: &quot;^3.0.1&quot;,
    &quot;remark-rehype&quot;: &quot;^10.1.0&quot;,
  },
}

and finally, page.jsx

import Foo from &quot;./test.mdx&quot;;

export default function Page() {
  return &lt;Foo /&gt;;
}

and layout.jsx

import &quot;./global.scss&quot;;
import &quot;../styles/prism.css&quot;;

export default function RootLayout({ children }) {
  return (
    &lt;html lang=&quot;en&quot;&gt;
      &lt;body&gt;{children}&lt;/body&gt;
    &lt;/html&gt;
  );
}

The problem

When I run this, next certainly seems to be compiling the mdx. But it's some very basic out-of-the-box transform that does not parse code blocks (all code appears as a single string), and does not render tables correctly. That is, rehypePrism and remarkGfm are not actually being applied.

Any suggestions? Thanks!

答案1

得分: 8

已解决。在 next.config.js 中禁用 mdxRs 修复了此问题。

const nextConfig = {
  pageExtensions: ["ts", "tsx", "js", "jsx", "mdx"],
  experimental: {
    appDir: true,
    mdxRs: false, // <- 禁用
  },
  reactStrictMode: true,
};
英文:

Resolved. Disabling mdxRs in next.config.js fixed the issue.

const nextConfig = {
  pageExtensions: [&quot;ts&quot;, &quot;tsx&quot;, &quot;js&quot;, &quot;jsx&quot;, &quot;mdx&quot;],
  experimental: {
    appDir: true,
    mdxRs: false, // &lt;- disabled
  },
  reactStrictMode: true,
};

huangapple
  • 本文由 发表于 2023年2月26日 19:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定