[5.x] Improved JS slugs#9440
Merged
jasonvarga merged 47 commits intomasterfrom Mar 20, 2024
Merged
Conversation
Because we're now returning a Promise from this, I'll need to do some tweaking in all the places we use the $slugify helper.
jasonvarga
requested changes
Feb 1, 2024
Str::slug() helperStr::slug() helper
- use variable names consistent with the php slug method - return just the slug in the response rather than an array - simplify the controller by spreading the validated array into the slug method - use postJson since its posting json
…e slug, only re-enables it.
…ou click regenerate
…ore important than perfect slugs
… state on changes
jasonvarga
approved these changes
Mar 20, 2024
Str::slug() helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Edit by Jason:
The server side thing is good in theory but dealing with async stuff is a pain. So we've kept the JS version for when you need a quick slug like for handle, but you can use the server side thing when it's important language-aware ones like entry strings.
Statamic.$slugJS API that generates slug fluently:Statamic.$slug.create('Foo Bar'); // foo-barStatamic.$slug.separatedBy('_').create('Foo Bar'); // foo_barstr_slugandsnake_caseglobal helpers for these two cases. They mimic the naming of equivalent Laravel helpers.str_slug('Foo Bar'); // foo-barsnake_case('Foo Bar'); // foo_bar.create()return a promise.Statamic.$slug.async().create('Foo Bar').then(slug => ...); // foo-barStatamic.$slug.async().separatedBy('_').in('zn').create('你好').then(slug => ...); // ni_haoasync, that's what's being used.<slugify>component emits events so that you can track when slugification is happening.slugfieldtype is used, like in entries.This pull request changes the way slugs are generated in the Control Panel.
Until now, we've now used a library called
speakingurl. It worked fine for generating slugs based on English strings but it didn't work so well for other languages, like German & Chinese.Rather than implementing another JS library, we've decided to generate the slugs using Laravel's
Str::slug()helper which seems to be more reliable with other languages.We had made some modifications to our
$slugifyhelper to workaround issues like smart quotes, hiphens separated by spaces, etc. I've added tests for these cases and Laravel's string helper seems to handle them as expected.Due to slug generation now being done on the server-side & needing to perform network requests, I've had to make changes to how we use the
$slugifyhelper in various places across the codebase.Closes #2817.
Closes #9590.