我试着用@next/mdx来使用Github口味的Markdown,但我似乎不知道如何在代码中使用插件。以下是我所做的:
(下面是Next.js文档中的内容:https://nextjs.org/docs/advanced-features/using-mdx)
初始化应用程序
1.我使用以下命令创建了Next.js应用程序
yarn create next-app next-gfm2.然后将所需的模块添加到
yarn add @next/mdx @mdx-js/loader3.在pages/目录中,我删除了自动生成的index.js文件,并使用index.mdx文件替换了它。
从这里开始,我对我的next.config.js文件使用了以下配置。
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命令:
yarn add remark-gfm rehype-stringify进口语法错误?
我尝试使用语法导入next.config.js中的模块
import remarkGfm from 'remark-gfm'但这给了我以下错误:
import remarkGfm from 'remark-gfm'
^^^^^^
SyntaxError: Cannot use import statement outside a module使项目成为module
我还尝试在我的package.json顶部添加以下行
"type" : "module",但这似乎与用于导入@next/mdx的需求语法相冲突。
const withMDX = require('@next/mdx')({
...这给了我一个错误:
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现在看起来像:
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包)?
参考文献
这是我完整的项目结构和(相关)文件:
next-gfm
|_ pages
|_ index.md
|_ package.json
|_ next.config.js文件
index.md
# 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] donepackage.json
{
"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
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'],
})发布于 2022-05-04 18:08:58
如果将配置文件重命名为next.config.mjs,则支持ES模块。
资料来源
在你看来,
next.config.mjs
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'],
})https://stackoverflow.com/questions/71864146
复制相似问题