-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
feat(netlify-cms-core) add slug customization support #1931
Conversation
Preview proposed changes to netlifycms.org in the link below: Built with commit 12cfd3d |
Preview proposed changes to the CMS demo site in the link below: Built with commit 12cfd3d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@barthc great work here, thanks for this!
I'd like for us to bring this in as a beta feature, so we can iterate based on feedback without fear of breaking changes. The fact that you made this opt-in via slug_field
enables that 👍. Would you mind adding a blurb to the top of the beta features doc on this branch?
If we consider this a developer focused feature that minimally satisfies some critical gaps in the CMS, I can forego much of the product level feedback I would otherwise give. This is a great step in the right direction and does indeed satisfy the issues mentioned in the OP.
The only thing that may be worth addressing before merge (beyond the bits I commented on in the review) is that the identifier (title) field only updates the slug field during the initial focus. So if I enter a title, blur, then edit the title a little more, the edits aren't reflected in the slug. If you're feeling pretty ready to just get this merged, I'm cool with handling that in a subsequent issue.
Sorry this took so long, and thanks again for the killer effort.
if (!state.get('entities')) return null; | ||
return state | ||
.get('entities') | ||
.filter((v, k) => k.includes(collection)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is subject to false positives where the slug happens to include the collection name.
const entryData = entry.get('data'); | ||
const unavailableSlugs = boundSelectSlugs(collection.get('name')); | ||
const availableSlugs = [entry.get('slug'), entry.getIn(['metaData', 'parentSlug'])]; | ||
const indentifierField = collection && selectIdentifier(collection); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be identifierField
.
if (!state.get('entities')) return null; | ||
return state | ||
.get('entities') | ||
.filter((v, k) => k.includes(collection)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subject to false positives.
b74ef10
to
61abc02
Compare
@erquhart Could you please take a look again, pushed some updates.
The idea is to only sync the slug field with the |
That makes sense if the identifier field is not empty on load. If it's empty on load, though, any edits between load and save should be reflected in the slug. |
Yup, I believe that's the behavior of the slug field at the moment. Try creating new entry on the demo. |
So basically for new entry, if the user has the slug field filled out first before the |
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. |
Closing this as stale, I think we can re-visit in the future as use this work as reference. |
Summary
Closes #1164 #445
Picks up where #1239 left off, but instead of throwing an error for duplicate slug, it tries to generate a unique slug by append a number just like the way WordPress does it. For example assuming we already have a slug entry
title
and a sanitize_replacement option value as-
trying to create another entry with slug stringtitle
would generate a slugtitle-1
,title-2
and so on.To enable slug customization, add a config collection setting
slug_field
with value pointing to the name of the widget field preferrable astring
widget. A couple of things to note regarding the slug field behaviour. On new entry, once the identifier field looses focus, if the slug field is blank, the normal automatic slug string is generated and set as value of the slug field. Also when the slug field looses focus, the input value is sanitized immediately and value updated. The slug field value is not saved in frontmatter.So if there is a slug change for any published or unpublished entry, a new entry is created using the new slug and the old slug entry is deleted.
Tested changes with the
test-repo
,github
,gitlab
andbitbucket
backend.