首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Next.js中使用带有MDX的备注和Rehype插件(带@next/mdx)

在Next.js中使用带有MDX的备注和Rehype插件(带@next/mdx)
EN

Stack Overflow用户
提问于 2022-04-13 21:45:51
回答 1查看 1.8K关注 0票数 2

我试着用@next/mdx来使用Github口味的Markdown,但我似乎不知道如何在代码中使用插件。以下是我所做的:

(下面是Next.js文档中的内容:https://nextjs.org/docs/advanced-features/using-mdx)

初始化应用程序

1.我使用以下命令创建了Next.js应用程序

代码语言:javascript
复制
yarn create next-app next-gfm

2.然后将所需的模块添加到

代码语言:javascript
复制
yarn add @next/mdx @mdx-js/loader

3.pages/目录中,我删除了自动生成的index.js文件,并使用index.mdx文件替换了它。

从这里开始,我对我的next.config.js文件使用了以下配置。

代码语言:javascript
复制
const withMDX = require('@next/mdx')({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [],
    rehypePlugins: [],
  },
})

module.exports = withMDX({
  pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
})

如果我们使用yarn dev运行代码,到目前为止一切都很好。

添加GFM

这是主要问题所在。现在,我安装了以下软件包,以便使用使用Github调味的Markdown命令:

代码语言:javascript
复制
yarn add remark-gfm rehype-stringify

进口语法错误?

我尝试使用语法导入next.config.js中的模块

代码语言:javascript
复制
import remarkGfm from 'remark-gfm'

但这给了我以下错误:

代码语言:javascript
复制
import remarkGfm from 'remark-gfm'
^^^^^^

SyntaxError: Cannot use import statement outside a module

使项目成为module

我还尝试在我的package.json顶部添加以下行

代码语言:javascript
复制
"type" : "module",

但这似乎与用于导入@next/mdx的需求语法相冲突。

代码语言:javascript
复制
const withMDX = require('@next/mdx')({
...

这给了我一个错误:

代码语言:javascript
复制
Failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

使用旧的import()语法

我在网上做了一些搜索,找到了import()语法。我试图这样做,我的next.config.js现在看起来像:

代码语言:javascript
复制
const remarkGfm  = import('remark-gfm');
const remarkParse  = import('remark-parse')
const remarkRehype  = import('remark-rehype')
const rehypeStringify  = import('rehype-stringify')

const withMDX = require('@next/mdx')({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [remarkGfm, remarkParse, remarkRehype],
    rehypePlugins: [rehypeStringify],
  },
})

module.exports = withMDX({
  pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
})

我试着使用yarn dev运行这个插件,除了所有的标记插件都不能正常工作之外,一切都正常。(基本标记正在起作用,但如果我尝试使用脚注或表,则会将其呈现为纯文本)。

有谁能解释一下如何使用外部插件(例如,Github调味的Markdown)来使用MDX和Next.js (使用@next/mdx包)?

参考文献

这是我完整的项目结构和(相关)文件:

代码语言:javascript
复制
next-gfm
  |_ pages
       |_ index.md
  |_ package.json
  |_ next.config.js

文件

index.md

代码语言:javascript
复制
# GFM

## Autolink literals

www.example.com, https://example.com, and contact@example.com.

## Footnote

A note[^1]

[^1]: Big note.

## Strikethrough

~one~ or ~~two~~ tildes.

## Table

| a | b  |  c |  d  |
| - | :- | -: | :-: |

## Tasklist

* [ ] to do
* [x] done

package.json

代码语言:javascript
复制
{
  "name": "next-mdx-template",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@mdx-js/loader": "^2.1.1",
    "@next/mdx": "^12.1.5",
    "next": "12.1.5",
    "react": "18.0.0",
    "react-dom": "18.0.0",
    "rehype-katex": "^6.0.2",
    "rehype-stringify": "^9.0.3",
    "remark-gfm": "^3.0.1",
    "remark-math": "^5.1.1",
    "remark-mdx": "^2.1.1"
  },
  "devDependencies": {
    "eslint": "8.13.0",
    "eslint-config-next": "12.1.5"
  }
}

next.config.js

代码语言:javascript
复制
const remarkGfm  = import('remark-gfm');
const remarkParse  = import('remark-parse')
const remarkRehype  = import('remark-rehype')
const rehypeStringify  = import('rehype-stringify')

const withMDX = require('@next/mdx')({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [remarkGfm, remarkParse, remarkRehype],
    rehypePlugins: [rehypeStringify],
  },
})

module.exports = withMDX({
  pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-04 18:08:58

如果将配置文件重命名为next.config.mjs,则支持ES模块。

资料来源

在你看来,

next.config.mjs

代码语言:javascript
复制
import nextMDX from '@next/mdx'
import remarkGfm from 'remark-gfm'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeStringify from 'rehype-stringify'

const withMDX = nextMDX({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [remarkGfm, remarkParse, remarkRehype],
    rehypePlugins: [rehypeStringify],
  },
})

export default withMDX({
  pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
})
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71864146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档