decap-cms icon indicating copy to clipboard operation
decap-cms copied to clipboard

Ignore files from collection (hugo _index.md)

Open panakour opened this issue 6 years ago • 19 comments

I have a hugo site in which I have a content called "project". My config file:

collections:
  - name: "project" 
    editor:
      preview: false
    label: "Project"
    folder: "site/content/project"
    create: true
    fields:
      - {label: "Featured", name: "featured", widget: "boolean", default: false}
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Publish Date", name: "date", widget: "datetime"}
      - {label: "Project Date", name: "project_date", widget: "datetime"}
      - {label: "Head Image (Recommended 1920x1080)", name: "head_image", widget: "image", required: false}
      - {label: "Thumbnail Image (Recommended 360x560)", name: "thumb_image", widget: "image", required: false}
      - {label: "Images", name: "images", widget: list, field:
        {label: "Image (Recommended 800x500)", name: "image", widget: "image"}}
      - {label: "Description", name: "body", widget: "markdown"}

The admin is working fine and I can create/edit my projects. The issue is that the admin also show the _index.md file which is used from hugo for the list page. Also this file has different front-matter (I have a file collection for this to edit from the cms).

The solution you'd like For hugo site should automatically excludes this file from the collection.

alternatives you've considered Maybe a better and more generic solution for all SSGs is to have a config option to let us give an array with some files to be ignored

panakour avatar Oct 05 '19 07:10 panakour

Should be possible, agreed.

erquhart avatar Oct 28 '19 18:10 erquhart

maybe this issue #1472 would minimize the needed of this feature

panakour avatar Oct 29 '19 08:10 panakour

Hi everyone! Have there been any updates on this issue? I've looked at #1472 but I didn't find any solution or talk related to this. I'm interested in this feature to show only the actual collection content with a Hugo site and not "collection metadata" files. I'd be happy to help implement this

GabriFila avatar Jul 01 '20 07:07 GabriFila

Hi @GabriFila, the current workaround is to use https://www.netlifycms.org/docs/collection-types#filtered-folder-collections e.g. have a show field on entries and add a filter entries with that value set to true. Also related https://github.com/netlify/netlify-cms/issues/1000

erezrokah avatar Jul 01 '20 08:07 erezrokah

Thank you @erezrokah, I'll try that. Is there an interest to implement this without a workaround but with an actual ignore field, maybe with a regex inside or a glob pattern? Would it create problems?

GabriFila avatar Jul 01 '20 08:07 GabriFila

I'm going to have a look at the suggestion to add a type: exclude/include to filter and also to support entry meta data like filename. Feels like it shouldn't be much of an effort, but I'll have to verify it. Using a regex/glob might be a bit tricky to do without a breaking change

erezrokah avatar Jul 01 '20 08:07 erezrokah

Thank you! I have little experience with React, I don't know if I could help but if you need me I'd be happy to do it!

GabriFila avatar Jul 01 '20 08:07 GabriFila

Hi @GabriFila, the current workaround is to use https://www.netlifycms.org/docs/collection-types#filtered-folder-collections e.g. have a show field on entries and add a filter entries with that value set to true. Also related #1000

I used this workaround, it's easy and clean. Just do something like this in your collection config and your md files to be ignored or included:

Front matter of a file to be included (I removed some lines from your example for clarity):

---
featured: true
title: Some title
date: 2020-07-14T22:49:10.427Z
visible: true
---

Front matter of a file to be excluded or filtered:

---
featured: true
title: A title for an _index.md
date: 2020-07-14T22:49:10.427Z
visible: false
---

Option and fields to use into your collection config:

collections:
  - name: "project" 
    editor:
       preview: false
    label: "Project"
    folder: "site/content/project"
    create: true
    filter: {field: "visible", value: true}
    fields:
      - {label: "Featured", name: "featured", widget: "boolean", default: false}
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Publish Date", name: "date", widget: "datetime"}
      - {label: "Visible", name: "visible", widget: hidden, default: true}

Pay attention to the filter option, visible label, and the visible fields in the frontmatter of the files. Now all your new files created with the CMS will include the visible label, hiding the files with visible: false for the CMS, and without any interaction with the user.

OdairTrujillo avatar Jul 14 '20 23:07 OdairTrujillo

Thank you for mentioning the hidden widget, I used with the filter but it was still visible to the end user, now it will be much better

GabriFila avatar Jul 15 '20 06:07 GabriFila

Thank you for mentioning the hidden widget, I used with the filter but it was still visible to the end user, now it will be much better

Let us know if you resolved the problem. ¿Did you notice the filter option?

OdairTrujillo avatar Jul 15 '20 13:07 OdairTrujillo

Yes yes I solved my problem, it's still a workaround but is fine for me. If and when there will be another way I'll change it. Thank you

GabriFila avatar Jul 15 '20 13:07 GabriFila

I really dislike this workaround, because it requires additional frontmatter in every page. It would be better if we could define: filter: {field: "visible", value: "!false"} or ignore: {field: "visible", value: "false"} or something similiar, so we only need to edit the hidden pages.

LorenzBischof avatar Jan 25 '21 11:01 LorenzBischof

I really dislike this workaround, because it requires additional frontmatter in every page. It would be better if we could define: filter: {field: "visible", value: "!false"} or ignore: {field: "visible", value: "false"} or something similiar, so we only need to edit the hidden pages.

Agreed. To exclude one pages, you have to define additional frontmatter for all other pages. The workaround is also not compatible with the i18n support (beta feature). Having a default value result in creating index.md for all language types even though you only edited one language..

Would really like a fix for this. All my sites are using Hugo, and im using the i18n beta feature. So the workaround is not working for me.

The problem is, as mentioned. I want to edit all my articles, but not the article list view.(since it has different fields)

andreasbalevik avatar Jan 29 '22 15:01 andreasbalevik

I use Hugo with i18n too and this workaround works fine.

The fiter setting on collection: filter: {field: visibleInCMS, value: true}

The field in collection: {name: visibleInCMS, widget: hidden, default: true, i18n: duplicate}

If you set it correctly, you should see visibleInCMS: true on every page that you save with CMS.

I noticed that adding this only to default language is enough.

martinjagodic avatar Jan 31 '22 06:01 martinjagodic

"If you set it correctly, you should see visibleInCMS: true on every page that you save with CMS."

And thats the problem. If you have configures 2 languages with i18n (nb, en). Since a default value visibleInCMS: true is provided in the config.yml, the CMS will create index.nb.md and index.en.md (even though you only added content to nb, and left en blank). In some cases you only want to create a page for a specific language.

EDIT: adding required: true to a field also does not work as intented. (Adding content to nb, leaving en blank. Allowed to save / publish content).

i18n:
  structure: multiple_files
  locales: [nb,en]
  default_locale: nb

media_folder: static/img
public_folder: /img
collections:
  - name: 'blog'
    i18n: true
    label: 'Blog'
    folder: 'content/blog'
    create: true
    path: '{{slug}}/index'
    slug: '{{year}}-{{month}}-{{day}}-{{slug}}'
    fields:
      - { label: 'Title', name: 'title', widget: 'string', i18n: true, required: true }
      - { label: 'Body', name: 'body', widget: 'markdown', i18n: true}
      - { name: visibleInCMS, widget: hidden, default: true, i18n: duplicate}

andreasbalevik avatar Jan 31 '22 07:01 andreasbalevik

I have stumbled upon the same issue. The _index.md is showing up in my collection.

Has somebody found a better solution than setting an extra field since this was last discussed?

egf-ch avatar Aug 07 '24 16:08 egf-ch

Currently, I don't follow the given workaround to add a field to every pages. It doesn't make sense to me. It would be nice if the filter could be extended to be more powerful, so that we don't need to add a field everywhere. May be adding a property default or defaultIfMissing.

Then we could do like this: filter: {field: "hidden", value: false, default: false}. It means, by default, if missing the field, we consider hidden: false. Then we include the page only if hidden: false. In _index.md, we specify hidden: true. Vice versa, it will work if you like to use visible instead.

xuanswe avatar Mar 02 '25 19:03 xuanswe

@xuanswe are you talking about something like filter: { field: hideFromCMS } and then putting hideFromCMS = true in your _index.md or whatever you want hidden?

mattdelashaw avatar Apr 10 '25 05:04 mattdelashaw

@xuanswe are you talking about something like filter: { field: hideFromCMS } and then putting hideFromCMS = true in your _index.md or whatever you want hidden?

Yes, and with the control of default value of the field if not found in _index.md.

xuanswe avatar Apr 10 '25 15:04 xuanswe