6

我正在使用next-mdx-remote制作博客,并希望在.mdx文件夹外的文件中使用图像public/

这是我的博客项目的完整代码→ https://github.com/deadcoder0904/blog-mdx-remote

我有以下文件夹结构:

.
├── package-lock.json
├── package.json
├── pages
│   ├── _app.js
│   ├── blog
│   │   └── [slug].js
│   ├── dark.css
│   ├── index.js
│   └── new.css
├── posts
│   ├── blog
│   │   ├── hello-world
│   │   │   ├── Rustin_Cohle.jpg
│   │   │   └── index.mdx
│   │   └── shit-world
│   │       └── index.mdx
│   └── tutorials
│       └── console-log-in-javascript
│           └── index.mdx
└── utils
    └── mdxUtils.js

我的所有内容都在posts/文件夹中。

我有 2 个文件夹:blog/&tutorials/

每个帖子都在它们自己的文件夹中,blog/或者tutorials/每个文件夹都包含在该特定帖子中使用的图像。

例如,在hello-world文件夹中,有 1 张名为Rustin_Cohle.jpg.

我想在hello-world/index.mdx文件中使用这个图像,但我做不到。

我不能使用import,或者require因为它是next-mdx-remote.

我尝试使用下面使用的自定义组件并将Imageimg传递给它,hydrate但它也不起作用。

组件/Image.js

export const Image = ({ src, alt }) => (
    <img style={{ backgroundColor: 'red' }} src={src} alt={alt} />
)

页面/博客/[slug].js

import hydrate from 'next-mdx-remote/hydrate'
import { Image } from '../components/Image'

const components = { Image }

const Blog = ({ source, frontMatter }) => {
  const content = hydrate(source, { components })
  return (
    <div>
      <h1>{frontMatter.title}</h1>
      {content}
    </div>
  )
}

以下 MDX 文件使用上述Image组件作为通过hydrate.

你好世界/index.mdx

---
title: Hello World
date: '2019-09-06T14:54:37.229Z'
tags: ['hello', 'world']
author: John Doe
description: This is the first post
---

Hey this is my first post

![Rustin Cohle](./Rustin_Cohle.jpg)

<img src="./Rustin_Cohle.jpg" alt="Rust Cohle" />

<Image src="./Rustin_Cohle.jpg" alt="Rust Cohle" />

This is the end of the first post

我什至尝试使用MDXProvider& 它也没有用。

页面/index.js

import { MDXProvider } from '@mdx-js/react'

const components = { Image }

const HomePage = ({ posts }) => {
    return (
        <MDXProvider components={components}>
         ...
        </MDXProvider>
    )
}

那我该如何使用图片呢?我希望它们仅位于特定帖子的文件夹中,例如hello-world博客仅在hello-world/文件夹中包含其图像。

4

3 回答 3

8

我能够在这种情况下加载图像。我的想法是使用 webpackfile-loader来完成此操作,并向组件映射添加更多信息,以告知当前帖子的放置位置(注意组件功能)。

我的页面 [slug].js 是这样的:

import renderToString from 'next-mdx-remote/render-to-string'
import hydrate from 'next-mdx-remote/hydrate'
import matter from 'gray-matter'

import { getAllPostSlugs, getPostdata } from '../lib/posts'

const components = (slug) => ({
  h1: ({ children }) => <h1>{children}</h1>,
  img: ({ src, alt }) => {
    return (
      <p>
        <img
          alt={alt}
          src={require('../content/' + slug + '/' + src).default}
        />
      </p>
    )
  }
})

const Post = ({ source, frontMatter, slug }) => {
  const content = hydrate(source, {
    components: components(slug)
  })
  return (
    <>
      <h1>Post</h1>
      <p>{content}</p>
    </>
  )
}

export async function getStaticPaths() {
  const paths = getAllPostSlugs()
  return {
    paths,
    fallback: false
  }
}

export async function getStaticProps({ params }) {
  const postContent = await getPostdata(params.slug)
  const { data, content } = matter(postContent)
  const mdxSource = await renderToString(content, {
    components: components(params.slug),
    scope: data
  })
  return {
    props: {
      slug: params.slug,
      source: mdxSource,
      frontMatter: data
    }
  }
}

export default Post

还有我的 next.config.js:

module.exports = {
  webpack: (config, options) => {
    config.module.rules.push({
      test: /\.(svg|png|jpe?g|gif|mp4)$/i,
      use: [
        {
          loader: 'file-loader',
          options: {
            publicPath: '/_next',
            name: 'static/media/[name].[hash].[ext]'
          }
        }
      ]
    })
    return config
  }
}

Ps:它就像一个魅力与下一个/图像:)

于 2021-01-12T10:08:29.410 回答
2

不幸的是,next-mdx-remote将降价内容视为数据并且根本不通过 Webpack 是不可能的 :(

于 2020-09-18T14:17:48.693 回答
1

请注意,从 Next.js v11 开始,您可以将图像从任何目录导入到 mdx 文件中,无需任何额外配置 -参考 1参考 2

于 2022-01-30T15:22:36.093 回答