0

Admin portal

I am trying to add an image button from the markdown editor on About page.

I have images under a folder static/img and can see those images are copied into public/img folder

Project Structure

But when open the About page, the image always broken enter image description here

so it seems that html image reference always prepend …/…/static/ before the image path. How can we solve it?

Update

Here is my gatsby-config.js

module.exports = {
  siteMetadata: {
    title: `Winston Fan's Blog`,
    description:
      "This is my personal blog websit for my hobby projects, collection of my knowledge etc.",
  },
  plugins: [
    "gatsby-plugin-react-helmet",
    "gatsby-plugin-sass",
    {
      // keep as first gatsby-source-filesystem plugin for gatsby image support
      resolve: "gatsby-source-filesystem",
      options: {
        path: `${__dirname}/static/img`,
        name: "uploads",
      },
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        path: `${__dirname}/src/pages`,
        name: "pages",
      },
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        path: `${__dirname}/src/img`,
        name: "images",
      },
    },
    {
      resolve: `gatsby-plugin-env-variables`,
      options: {
        allowList: ["NETLIFY_FUNCTION_URL_BASE", "NETLIFY_FUNCTION_ADD_SUBSCRIPTION", "NETLIFY_FUNCTION_UPDATE_SUBSCRIPTION"]
      },
    },
    "gatsby-plugin-sharp",
    "gatsby-transformer-sharp",
    {
      resolve: "gatsby-transformer-remark",
      options: {
        plugins: [
          {
            resolve: "gatsby-remark-relative-images",
            options: {
              name: "uploads",
              plugins: [
                {
                  resolve: "gatsby-remark-images",
                  options: {
                    // It's important to specify the maxWidth (in pixels) of
                    // the content container as this plugin uses this as the
                    // base for generating different widths of each image.
                    maxWidth: 2048,
                  },
                }
              ]
            },
          },
          {
            resolve: "gatsby-remark-copy-linked-files",
            options: {
              destinationDir: "static",
            },
          },
        ],
      },
    },
    {
      resolve: "gatsby-plugin-purgecss", // purges all unused/unreferenced css rules
      options: {
        develop: true, // Activates purging in npm run develop
        purgeOnly: ["/all.sass"], // applies purging only on the bulma css file
      },
    }, // must be after other CSS plugins
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `AskWinston`,
        short_name: `HeyWinston`,
        start_url: `/`,
        background_color: `#FFF`,
        theme_color: `#FAE042`,
        display: `standalone`,
        icon: `src/img/wf-logo512.png`,
      },
    },
    {
      resolve: `gatsby-plugin-offline`,
      options: {
        appendScript: `src/sw.js`,
      },
    },
    {
      resolve: "gatsby-plugin-netlify-cms",
      options: {
        modulePath: `${__dirname}/src/cms/cms.js`,
      },
    },
    "gatsby-plugin-netlify" // make sure to keep it last in the array
  ],
};

Update 2

My config.yml:

backend:
  name: git-gateway
  branch: master
  repo: franva/askwinston

  commit_messages:
    create: 'Create {{collection}} “{{slug}}”'
    update: 'Update {{collection}} “{{slug}}”'
    delete: 'Delete {{collection}} “{{slug}}”'
    uploadMedia: '[skip ci] Upload “{{path}}”'
    deleteMedia: '[skip ci] Delete “{{path}}”'

media_folder: "static/img"
public_folder: "/img"
publish_mode: editorial_workflow
local_backend: true

collections:
  - name: "blog"
    label: "Blog"
    folder: "src/pages/blog"
    create: true
    slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
    fields:
      - {label: "Template Key", name: "templateKey", widget: "hidden", default: "blog-post"}
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Publish Date", name: "date", widget: "datetime"}
      - {label: "Description", name: "description", widget: "text", required: false}
      - {label: "Featured Post", name: "featuredpost", widget: "boolean", required: false}
      - {label: "Featured Image", name: "featuredimage", widget: image, required: false}
      - {label: "Body", name: "body", widget: "markdown"}
      - {label: "Tags", name: "tags", widget: "list"}

  - name: "pages"
    label: "Pages"
    create: true
    files:
      - file: "src/pages/index.md"
        label: "Landing Page"
        name: "index"
        fields:
          - {label: "Template Key", name: "templateKey", widget: "hidden", default: "index-page"}
          - {label: Title, name: title, widget: string}
          - {label: Image, name: image, widget: image}
          - {label: Subheading, name: subheading, widget: string}
          - {label: Mainpitch, name: mainpitch, widget: object, fields: [{label: Description, name: description, widget: text}]}
          - {label: Intro, name: intro, widget: object, fields: [{label: Heading, name: heading, widget: string}, {label: Description, name: description, widget: text}, {label: Blurbs, name: blurbs, widget: list, fields: [{label: Image, name: image, widget: image}, {label: Text, name: text, widget: text}]}]}
          - {label: Main, name: main, widget: object, fields: [{label: Heading, name: heading, widget: string}, {label: Description, name: description, widget: text}, {label: Image1, name: image1, widget: object, fields: [{label: Image, name: image, widget: image}, {label: Alt, name: alt, widget: string}]}, {label: Image2, name: image2, widget: object, fields: [{label: Image, name: image, widget: image}, {label: Alt, name: alt, widget: string}]}, {label: Image3, name: image3, widget: object, fields: [{label: Image, name: image, widget: image}, {label: Alt, name: alt, widget: string}]}]}
      - file: "src/pages/about/index.md"
        label: "About"
        name: "about"
        fields:
          - {label: "Template Key", name: "templateKey", widget: "hidden", default: "about-page"}
          - {label: "Title", name: "title", widget: "string"}
          - {label: "Body", name: "body", widget: "markdown"}          
halfer
  • 19,824
  • 17
  • 99
  • 186
Franva
  • 6,565
  • 23
  • 79
  • 144
  • If all you're looking for is a reference to the image, why not pass it in the frontmatter? – Will Ward Aug 25 '20 at 09:00
  • hi @WillWard could you please elaborate your idea? I'm new to this tech stack. – Franva Aug 25 '20 at 10:38
  • this is from gatsby netlify source, right? try to look at the image code in node modules, sometimes you can find the problem in the package that is not suiting your needs.. – Muh Zulzidan Aug 25 '20 at 11:15
  • @Franva Here's a link to the Gatsby documentation that explains how to do so... https://www.gatsbyjs.com/docs/working-with-images-in-markdown/ – Will Ward Aug 25 '20 at 13:15

1 Answers1

1

Assuming that your Netlify output for public images configuration is correct and that all your images are properly copied from /static/img to /public/img folder you should point those images in Netlifiy Admin Portal with /img/name_of_image.jpg (note the initial slash).

In addition, if you are using a markdown configuration system, you should install the gatsby-transformer-remark plugin. In that case:

{
  resolve: `gatsby-transformer-remark`,
  options: {
    plugins: [
      {
        resolve: `gatsby-remark-images`,
        options: {
          maxWidth: 800,
        },
      },
    ],
  },
},

Then, you can point those images with ./name_of_image.png.


With the new information provided: your paths for media_folder and public_folder should be something like:

media_folder: static/assets
public_folder: /assets

Since everything in /static is compiled within the same internal structure to the public folder, if you create a folder structure like /static/assets your asset's path will remain accessible for you CMS. Moreover, this will output all your uploaded media in your /static folder without affecting the rest of the site.

Summarizing, your assets should be under /static folder to ensure their availability paths in the public compiled folder. Of course, you can adapt them to your needs/desired structure but they should keep the relativity of the paths and the static folder structure.

From Netlify CMS documentation:

Media Folder

This setting is required.

The media_folder option specifies the folder path where uploaded files should be saved, relative to the base of the repo.

media_folder: "static/images/uploads"

Public Folder

This setting is required.

The public_folder option specifies the folder path where the files uploaded by the media library will be accessed, relative to the base of the built site. For fields controlled by [file] or [image] widgets, the value of the field is generated by prepending this path to the filename of the selected file. Defaults to the value of media_folder, with an opening / if one is not already included.

public_folder: "/images/uploads"

Based on the settings above, if a user used an image widget field called avatar to upload and select an image called philosoraptor.png, the image would be saved to the repository at /static/images/uploads/philosoraptor.png, and the avatar field for the file would be set to /images/uploads/philosoraptor.png.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • hi Ferran, I do have that section in my config file. Please see my config and let me know if you spotted any mistakes there. thanks – Franva Aug 26 '20 at 14:04
  • Thanks. However I'm more interested in your `config.yml` to check the media paths. – Ferran Buireu Aug 26 '20 at 14:16
  • Thanks Ferran, I have updated my question with my config.yml. Help it helps. thanks – Franva Aug 27 '20 at 07:09
  • Your issue relies in the media paths. Your images are currently only accessible by your CMS but not by your application since they are being uploaded in the wrong path, that's why it throws a `404` error. I've provided a new ones that may work + a bit of documentation. Of course you can adapt the paths naming to your structure but they should keep the relativity. – Ferran Buireu Aug 27 '20 at 08:01
  • thanks @Ferran, do you mean to remove the double quote from ```public_folder``` and ```media_folder```? if that's the case, I have tried that, no luck. Also, I am following the standard approach. I set media_folder and public_folder to /img and "static/img" respectively. Also, if you take a look my gatsby-config.js I am using the ```gatsby-transformer-remark``` plugin. After all of these, it is still not working. – Franva Aug 27 '20 at 08:10
  • Also, I would like to highlight that it is not the image in markdown not working, the image reference in markdown(```![img name](abc.jpg)```) works. What is not working is `````` the html code src reference to the image. – Franva Aug 27 '20 at 08:11