gpt4 book ai didi

javascript - 在 Next JS 中使用 `.mdx` 时,如何在 `public/` 文件夹之外的 `next-mdx-remote` 文件中使用图像?

转载 作者:行者123 更新时间:2023-12-03 21:10:56 24 4
gpt4 key购买 nike

我正在用 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 中使用这张图片文件,但我无法做到。
我无法使用 importrequire因为它是 next-mdx-remote 的限制.
我尝试使用名为 Image 的自定义组件使用了 img在下面并传递给 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 传递的组件.
Hello World /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 blog 将仅在 hello-world/ 中包含其图像文件夹。

最佳答案

我能够在这种情况下加载图像。我的想法是使用 webpack file-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:它就像一个魅力与下一个/图像:)

关于javascript - 在 Next JS 中使用 `.mdx` 时,如何在 `public/` 文件夹之外的 `next-mdx-remote` 文件中使用图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63957018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com