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

feat: allow special characters to pass through slugification

Open mikestopcontinues opened this issue 3 years ago • 11 comments

Summary

Some of us would like to include / in our slug fields to build nested page structures. This PR allows arbitrary chars to pass through slugification, above and beyond what's allowed by slug.encoding. Useful for / and perhaps for other functionality in future.

Fixes #4963

Test plan

I added new tests for the new parameter I added demonstrating the true and false case. I also updated the dev config.yml with two example collections.

Checklist

Please add a x inside each checkbox:

  • [x] I have read the contribution guidelines.
  • [x] Code is formatted via running yarn format.
  • [x] Tests are passing via running yarn test.
  • [x] The status checks are successful (continuous integration). Those can be seen below.

A picture of a cute animal (not mandatory but encouraged)

Sloth pic

mikestopcontinues avatar Feb 17 '22 17:02 mikestopcontinues

@erezrokah Hey, is this solution acceptable? Should I go ahead and update the tests?

mikestopcontinues avatar Feb 24 '22 01:02 mikestopcontinues

Sorry @mikestopcontinues, haven't been able to dig into this. From a quick look at the issue, the problem seems to be scoped to nested collections and preview_path slugification. It seems that the expected behavior for nested collections is not to slugify the value of {{slug}}, so maybe we don't need an option and only change that behavior for nested collections?

erezrokah avatar Feb 24 '22 10:02 erezrokah

Hi @erezrokah thanks. I was waaay off!

I updated the PR to specifically target preview paths, under two conditions:

  1. collection.has('nested')
  2. collection.get('preview_preserve_slash')

(2) is because my use-case is a slug field that supports slashes. Nested collections are cool, but they're really hard to use. First is the GUI or lack thereof, but I also query a separate github repo for content, and the nested directories make it very difficult to do efficiently. If you push the issue, I'll remove preview_preserve_slash as I can hack my way forward via (1), though it ends up making things more buggy for my content co-founder.

You can see both the nested-method and the slug-method at work in the dev config.yml.

Two questions:

  1. I strip the first and last slash here, as that seems to be the right call. Is it?
  2. When preserving slashes, I just skip sanitizeFilename. Seemingly all of the characters it sanitize are sanitized already anyway. I would have used their {sanitize: (char) => replace} interface, except they don't pass what it would have decided, so I would have had to completely reimplement the library just to ignore slashes. Didn't seem worth it.

mikestopcontinues avatar Feb 25 '22 00:02 mikestopcontinues

Hi @mikestopcontinues, I completely forgot we did https://github.com/netlify/netlify-cms/pull/4279 a while back to support the use case described in the issue.

Can you try using {{dirname}} to see if that solves your use case?

erezrokah avatar Mar 01 '22 11:03 erezrokah

Hi @erezrokah

{{dirname}} is a blank field for me. My setup looks like this:

    slug: {{fields.slug}}
    preview_path: /{{fields.slug}}

And fields.slug is just a regex pattern that matches this-is/a-slug.

mikestopcontinues avatar Mar 01 '22 13:03 mikestopcontinues

@erezrokah Sorry to ping you on this, but it's a blocker for my current site upgrade. Any thoughts?

mikestopcontinues avatar Mar 11 '22 14:03 mikestopcontinues

@erezrokah Sorry to ping you on this, but it's a blocker for my current site upgrade. Any thoughts?

Sorry for the delay @mikestopcontinues. I might not be able to look at this for another week. I would like to separate the use cases:

  1. preview_path for nested collections - this should be handled by using {{dirname}} - I'll need to verify it works.
  2. preview_path for non nested collections. The change here is more risky as it can impact all users.

If this is blocking you should be able to use the generated CMS build from this Deploy Preview, https://deploy-preview-6220--cms-demo.netlify.app/dist/netlify-cms.js

erezrokah avatar Mar 14 '22 10:03 erezrokah

@erezrokah Thanks! For the time being, I'll try the deploy preview. Let me know what I can do to officially move this forward when you can.

mikestopcontinues avatar Mar 14 '22 15:03 mikestopcontinues

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.

stale[bot] avatar Apr 26 '23 09:04 stale[bot]

@mikestopcontinues are you still interested in moving this forward?

martinjagodic avatar Oct 16 '23 12:10 martinjagodic

Sorry @martinjagodic, I no longer use Decap.

mikestopcontinues avatar Oct 16 '23 12:10 mikestopcontinues