Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with using dynamic media_folder name #3723

Open
smashercosmo opened this issue May 8, 2020 · 3 comments
Open

Problems with using dynamic media_folder name #3723

smashercosmo opened this issue May 8, 2020 · 3 comments
Labels
status: confirmed type: bug code to address defects in shipped code

Comments

@smashercosmo
Copy link
Contributor

Describe the bug
This is how I define my posts collection

collections:
  - label: Posts
    label_singular: post
    name: posts
    folder: content/posts
    media_folder: '/{{media_folder}}/posts/{{filename}}'
    public_folder: '{{public_folder}}/posts/{{filename}}'
    format: frontmatter
    create: true
    identifier_field: title
    fields:
      - {label: Title, name: title, widget: string}
      - {label: Cover image, name: cover, widget: image, required: false}
      - {label: Body, name: body, widget: markdown}

as you can see media_folder name is based on the filename. But the problem is that when you create post file doesn't exist, so images in markdown is written with wrong path.

How to reproduce
Here is reproduction repository
https://github.com/smashercosmo/folder-collections-media-and-public-folder-issue

  1. run npm run develop
  2. open http://localhost:8000/admin/
  3. press new post
  4. fill the form and upload an image
  5. press publish

Expected behavior
you should see correct image path in frontmatter

---
cover: /img/posts/new-post-name/image.jpg
---

Actual behavior
you will see this instead

---
cover: /img/posts/image.jpg
---

Applicable Versions:

  • Netlify CMS version: 2.12.12
@erezrokah erezrokah added the type: bug code to address defects in shipped code label May 8, 2020
@jeremyzilar
Copy link
Contributor

As a side note, thank you for adding this. I didn't realize that media_folder and public_folder could be added as additional params within a collection. 🙌

@erezrokah
Copy link
Contributor

Hi @jeremyzilar, we've added it recently so its under beta feature https://www.netlifycms.org/docs/beta-features/#folder-collections-media-and-public-folder.
We haven't documented it, but you can also configure it per field (https://github.com/netlify/netlify-cms/issues/3671).

@erezrokah
Copy link
Contributor

erezrokah commented Oct 8, 2021

We update the image paths for new entries before saving the entry here:
https://github.com/netlify/netlify-cms/blob/81ce05f3fb43594036d4753f8414e6cb51340376/packages/netlify-cms-core/src/backend.ts#L1084

That handles the location of image files in the repo.
However, we don't fix it in the markdown content, hence the issue.

We'll need to somehow update the content based on the fields configuration. I can't think of an issue way to do it (yet).
However a possible workaround is to use the preSave event to fix the markdown content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: confirmed type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants