sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.28k stars 426 forks source link

how to auto slugify the slug without using generate button click. #2110

Closed dharmveer97 closed 4 years ago

dharmveer97 commented 4 years ago

how to auto slugify the slug without using generate button click.

Aratramba commented 4 years ago

to quote @kmelve

It actually used to work like that, but we decided to move away from because it led to breakage, because people would accidentally change the slug. that’s why we have the

options: {
  source: 'title'
}

to add a generate button, which does kinda the same, but makes it a conscious decision. you can make a custom input component and by using the withDocument HoC make it automatically set a slug on updates on another field.

vicmeow commented 4 years ago

Hi and thank you for your question! As @Aratramba quote from Knut says, we moved away from it. If you'd like more help to make this component yourself, you can join our community slack and get some help with it there 👍

pastinepolenta commented 3 years ago

I am trying to achieve this behavior but using the javascript library. I need to generate the slug programmatically without interacting with the studio.

spookyuser commented 2 years ago

to add a generate button, which does kinda the same, but makes it a conscious decision. you can make a custom input component and by using the withDocument HoC make it automatically set a slug on updates on another field.

I think this is not a great idea, and I would much rather have the slug behind the scenes automatically generating than require my users to remember to click generate every time. I get that this is still technically possible to implement but I really do believe that you should have the option to set a source for something and let it automatically figure stuff out.

If the function to generate the slug is okay not sure where the errors would be coming from. Anyway that's my 2c.

rileyjshaw commented 2 years ago

Agreed, this would be very handy.

AmYre commented 1 year ago

Agreed

luciardrgz commented 1 year ago

source: 'title'

Agreed

EthanStandel commented 1 year ago

Is there a parallel in v3 for the behavior provided in withDocument in v2? If not, seems like this issue should be reopened

marcadrian-it commented 1 year ago

I believe that the CMS should have the ability to be designed with abstraction in mind, especially for non-technical users. The current behavior of requiring the user to click a “Generate” button to create a slug seems counterintuitive. I think it would be more user-friendly if the developer had the choice to automatically generate the slug based on the value of the source field, without requiring any additional user input.

der-daniel commented 1 year ago

Any news here?

My users do not know what a slug is...

Having it being generated automatically is a must :(

amcc commented 1 year ago

I thought the same thing and with a great bit of advice from Vincent Florio on the Slack I made this:

https://github.com/amcc/sanity-slugify

This is a simple sanity project with an action on publish that takes the title and automatically sets the slug if it is not already present.

The key is using Document Actions: https://www.sanity.io/docs/document-actions Vincent spotted that Sanity itself is using this package to make the slugs - so i used the same: https://github.com/pid/speakingurl here it is in action: https://github.com/sanity-io/sanity/blob/6020a46588ffd324e233b45eaf526a58652c62f2/packages/sanity/src/core/form/inputs/Slug/utils/slugify.ts#L13

It might be possible to use the slugify function directly from Sanity, but as I didn't need any of the complexity this will perform the default slugifying action exactly.

I originally was a little shocked that this wasn't the default - however as a Sanity document is not predictable (like WordPress) it may not have a title or similar to set the slug from. So doing this automatically is not guaranteed to work. You also are making the assumption that the title is a required field.

jonnystalnaker commented 1 year ago

@amcc you are an absolute legend.