Skip to content
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

[5.x] Fix hyphens in JS slugs #10541

Merged
merged 12 commits into from
Aug 2, 2024
Merged

[5.x] Fix hyphens in JS slugs #10541

merged 12 commits into from
Aug 2, 2024

Conversation

duncanmcclean
Copy link
Member

This pull request fixes an issue where hyphens weren't being handled correctly in JS slugs.

Fixes #10533.

@jasonvarga
Copy link
Member

This would make dashes never work. It's perfectly fine for slugs in general to have dashes. Usually that's only what should be used.

What's happening in the referenced issue is that the validation wants a handle which doesn't allow dashes.

But we are only telling it to use _ as a separator.

'separator' => '_',

The JS still allows you to manually type dashes. They're valid in PHP too:

Str::snake('foo bar-baz'); // foo_bar-baz

What we probably need is a new method that enforces changing dashes to underscores. Maybe something like:

Statamic.$slug.separatedBy('_').withReplacements({'-': '_'}).create(string);
replacements:
  '-': '_'

The withReplacements could pass that object into the custom like you've done in this PR.

@jasonvarga jasonvarga closed this Aug 1, 2024
@duncanmcclean duncanmcclean reopened this Aug 1, 2024
@duncanmcclean
Copy link
Member Author

Good suggestion!

I've reverted my previous fix and instead went ahead with the replacements approach, which I've implemented on both the PHP & JS sides.

let custom = charmap[this.#language] ?? {};
custom["'"] = ""; // Remove apostrophes in all languages
custom["’"] = ""; // Remove smart single quotes
custom[" - "] = " "; // Prevent `Block - Hero` turning into `block_-_hero`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why'd this need to be removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was thinking it might not be needed after these changes...

However, on second thought, I'll add it back since not all slug/handle fields specify replacements. 👍

@jasonvarga
Copy link
Member

jasonvarga commented Aug 2, 2024

I realized (after you made all my requested changes 😬) that in PHP's Str::slug() if you were to use _ as a separator, JSON-LD would be converted to json-ld automatically.

So, I reverted the withReplacements stuff and instead just make dashes convert to the separator, when the separator isn't a dash. It matches PHP.

Everything you did was great and worked well, but I don't think we need it. Thank you and sorry!

@jasonvarga jasonvarga merged commit 8efcedc into 5.x Aug 2, 2024
17 checks passed
@jasonvarga jasonvarga deleted the fix/hyphens-in-js-slugs branch August 2, 2024 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Field automatic handle generation doesn't slugify dash characters
2 participants