decaporg / decap-cms

A Git-based CMS for Static Site Generators
https://decapcms.org
MIT License
17.93k stars 3.04k forks source link

Custom block component - Image widget's option `media_folder` not working #6595

Closed a-zurcher closed 3 months ago

a-zurcher commented 1 year ago

Hi !

I'm trying to integrate custom Hugo shortcodes in Netlify CMS, one of which includes an image coupled with text. Each post's images are stored in the "medias" sub folder, as such :

├── projects
│   ├── cv-web-lucien-cogne
│   │   ├── index.en.md
│   │   ├── index.fr.md
│   │   └── medias
│   │       └── homepage.png
│   ├── find-my-alex
│   │   ├── index.en.md
│   │   ├── index.fr.md
│   │   └── medias
│   │       └── find-my-alex.png

There's a main image field in hugo's frontmatter for the main image, for which was set media_folder: "medias" -> works at intended :

image

The images in the medias subfolder are found.

Describe the bug

My custom block component is set up as follows :

CMS.registerEditorComponent({
    id: "media_with_p-image",
    label: "Paragraph + image",
    fields: [{
            label: "Image",
            name: "img",
            widget: "image",
            choose_url: false,
            media_folder: "medias"
        },
        {
            name: "p",
            label: "Text",
            widget: "string"
        },
    ],
    pattern: /{{< media_with_p\s+img="(.+)"\s+p="(.*)"\s+>}}/,
    fromBlock: function(match) {
        return {
            img: match[1],
            p: match[2],
        };
    },
    toBlock: function(obj) {
        return `{{< media_with_p img="${obj.img}" p="${obj.p}" >}}`;
    },
    toPreview: function(obj) {
        return `{{< media_with_p img="${obj.img}" p="${obj.p}" >}}`;
    },
});

Most of it works, except the media_folder option for the image field :

image

Here, the image that should appear is find-my-alex.png, in the medias subfolder

The choose_url option wasn't ignored as it can be seen on this screenshot, so I guess there is a bug ? Or is there something else going on here ?

To Reproduce Create custom editor component including image component with the media_folder option

Expected behavior Images in the folder given in the media_folder option show up

Applicable Versions:

CMS configuration

backend:
  name: git-gateway
local_backend:
  url: http://localhost:8082/api/v1
  allowed_hosts: ['127.0.0.1']

display_url: http://localhost:1313

publish_mode: editorial_workflow
media_folder: "{{slug}}/medias"

collections:
  - name: "Home"
    label: "Home"
    folder: "content/"
    i18n: true
    fields:
      - {label: "Body", name: "body", widget: "markdown", i18n: true}

  - name: "tags"
    label: "Tags"
    label_singular: "Tag"
    folder: "/content/tags"
    path: "{{title}}/_index"
    create: true
    preview_path: "tags/{{slug}}"
    i18n: true
    identifier_field: title
    fields:
      - {label: "Title", name: "title", widget: "string", i18n: true}
      - {label: "Icon", name: "icon", widget: "image", media_folder: ".", choose_url: false, required: false}
      - {label: "Body", name: "body", widget: "markdown", i18n: true}

  - name: "projects"
    label: "Projects"
    slug: "{{title}}"
    label_singular: "Project"
    folder: "/content/projects"
    path: '{{title}}/_index'
    create: true
    preview_path: "projects/{{slug}}"
    i18n: true
    fields:
      - {label: "Title", name: "title", widget: "string", i18n: true}
      - {label: "Description", name: "description", widget: "string", i18n: true}
      - {label: "Main Image", name: "main_image", widget: "image", media_folder: "medias", choose_url: false, required: false}
      - label: "Tags"
        name: "tags"
        widget: "relation"
        collection: "tags"
        search_fields: ["title"]
        display_field: "title"
        value_field: "title"
        multiple: true
        required: false
      - {label: "Date", name: "date", widget: "datetime"}
      - {label: "Link", name: "visit", widget: "string", required: false}
      - {label: "GitHub", name: "GitHub", widget: "string", required: false}
      - {label: "GitLab", name: "GitLab", widget: "string", required: false}
      - {label: "Body", name: "body", widget: "markdown", i18n: true}

i18n:
  structure: multiple_files
  locales: [fr, en]
  default_locale: fr
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

martinjagodic commented 3 months ago

Closing as stale