问题描述
我遇到麻烦的仓库是https://github.com/agenciaglobal/web 这是我的gatsby-config.js。至少是有关的部分。
{
resolve: `gatsby-source-filesystem`,options: { path: `${__dirname}/content`,name: `content` },},{
resolve: `gatsby-source-filesystem`,options: { path: `${__dirname}/content/assets`,name: `assets` },{
resolve: "gatsby-plugin-root-import",options: {
src: path.join(__dirname,"src"),content: path.join(__dirname,"content"),pages: path.join(__dirname,"src/pages"),shared: path.join(__dirname,"src/shared"),components: path.join(__dirname,"src/components"),static: path.join(__dirname,"static"),`gatsby-plugin-typescript`,`gatsby-plugin-layout`,`gatsby-plugin-Feed-mdx`,{
resolve: `gatsby-transformer-sharp`,`gatsby-plugin-sharp`,`gatsby-remark-images`,{
resolve: `gatsby-plugin-mdx`,options: {
extensions: [".mdx",".md"],gatsbyRemarkPlugins: [
{
resolve: `gatsby-remark-images`,options: {
maxWidth: 590,],`gatsby-plugin-react-helmet`,{
resolve: `gatsby-plugin-material-ui`,options: { stylesProvider: { injectFirst: true } },{
resolve: `gatsby-plugin-netlify-cms`,options: { modulePath: `${__dirname}/src/cms.js` },{
resolve: "gatsby-plugin-netlify-cache",options: { cachePublic: true },
整个事情都在这里 https://github.com/agenciaglobal/web/blob/master/gatsby-config.js 我的网站正在运行!但我无法使图像正常工作
这是我的收藏
media_folder: content/assets
public_folder: ../assets
collections:
- name: news
label: Notícias
folder: content/news
create: true
fields:
- { name: path,label: Path }
- { name: date,label: Data,widget: date }
- { name: "body",label: "Conteúdo do blog post",widget: "markdown" }
- { name: title,label: Título }
- { name: description,label: Descrição }
- name: postType
label: Tipo de post
widget: select
options:
- { label: "Notícia",value: "news" }
- { label: "Artigo",value: "article" }
- name: type
label: Tipo do Box de exibição na lista
widget: select
options:
- { label: "Full",value: "FULL" }
- { label: "Half",value: "HALF" }
- { label: "Mirror",value: "MIRROR" }
- { label: "Left",value: "LEFT" }
- { label: "Right",value: "RIGHT" }
- { label: "Quote",value: "QUOTE" }
- name: "image"
label: "Imagem de destaque"
widget: "image"
- label: "Tags"
name: "tags"
widget: "relation"
collection: "tags"
multiple: true
searchFields: ["tag"]
valueField: "tag"
displayFields: ["tag"]
- label: "Autor do post"
name: "author"
widget: "relation"
collection: "team"
searchFields: ["name","about","image"]
valueField: "name"
displayFields: ["name"]
然后,实际新闻如下:
---
path: posteee
date: 2020-08-13T13:59:40.634Z
title: hey
description: fe
postType: news
type: HALF
image: assets/10-maria-dornelles.jpg
tags:
- yay!!
author: João Paulo Rocha
---
laõoo
![](../assets/1000x550.png)
这是有效的配置。但是cms永远不会写这样的路径。这里的问题是,通过CMS插入数据时,它将始终使用public_folder
。
因此,如果我使用assets
,它将在image字段上工作,但在markdown正文上会失败。如果我使用../assets
,它将在markdown正文上运行,但在image frontmatter字段上失败
如果我尝试../assets/whatever.png
,则图像字段将始终失败。因此,我认为我唯一的出路是让降价广告的主体识别assets/image.png
,而不仅仅是../assets/image.png
。我该如何实现?
解决方法
您media_folder
和public_folder
的路径应类似于:
media_folder: static/assets
public_folder: /assets
由于/static
is compiled within the same internal structure to the public folder中的所有内容,如果创建类似/static/assets
的文件夹结构,则您的MDX文件将可以访问资产的路径。此外,这会在不影响您的MDX文件的情况下,将所有上传的媒体输出到/static
文件夹中。
总而言之,您的资产应位于/static
文件夹下,以确保它们在公共编译文件夹中的可用性路径。此外,由于编译过程,使用相对路径(例如../assets
)可能会导致意外问题,如您所描述的。
媒体文件夹
此设置是必需的。
media_folder
选项指定上传的文件夹路径 相对于仓库的基础,应该保存文件。media_folder: "static/images/uploads"
公共文件夹
此设置为必需。
public_folder
选项指定文件夹路径,相对于构建站点的基础,该路径将访问媒体库上载的文件。对于由[file]或[image]小部件控制的字段,通过将该路径添加到所选文件的文件名之前来生成字段的值。默认值为media_folder
,带有/的开头(如果尚未包含的话)。public_folder: "/images/uploads"
基于上述设置,如果用户使用名为
avatar
的图像小部件字段上载并选择名为philosoraptor.png
的图像,则该图像将保存到/static/images/uploads/philosoraptor.png
的存储库中,并将文件的头像字段设置为/images/uploads/philosoraptor.png
。