You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Question/Bug: how do I preserve dashes and not replace them with separator? #107
I am trying to slugify a string, replacing non-matching characters with an underscore, but I don't want to consider a dash a non-matching character. This is easily achieved with regex but it looks like there is some special slugify behaviour when it comes to dashes which is preventing this working. I can't see anything in the docs or code, about how to work around this.
Please note that '-' is a reserved character in slugs. This package allows an atomic replacement of the default separator of '-' with something else towards the end of the process.
A simple preprocess of the text is recommended for special cases.
All PRs are considered provided they remain backward compatible.
Ok, I think this is definitely PR-worthy. I agree that '-' is a reserved character - but this is precisely the reason why you shouldn't remove them. I can pre-process the string; sure; but this seems a bit broken.
Will find time to raise PR but I'm not sure how this can remain backwards compatible as this would be changing (fixing) behaviour where input string contains a - character. Do you imagine this being behind a feature toggle that defaults to current behaviour perhaps?
Also, there's a logical problem with the suggested workaround. Pre-processing the string is highly problematic - what character am I going to replace - with, before I pass it to a call to slugify that is going to replace characters that don't match my regex? I have a very limited set of options and whatever I choose could legitimately appear elsewhere in the string and be erroneously substituted by the post-processing.
The only thing I could do is replace - with some string that i think is going to be reasonably unique. This is brittle and bad. This behaviour should belong to slugify.
I am facing the same issue here. I have a text like this 121232-some text here-some text here again
Trying to slugify the text with separator ' '. Expecting the output 121232-some text here-some text here again but got 121232 some text here some text here again
@PradeepTammali Slugify needs one reserved character, and since the name of the package is slugify its job is to convert some random text to a dash-delimited clean text for use in URLs. With that said, the delimiter can also be changed to whatever user wants. In short, the ask here is not doable. I encourage channelling one's energy to raising a meaningful PR instead.
Repository owner locked as resolved and limited conversation to collaborators on Dec 16, 2021
This is Dojo, posting a maintainer-authorized follow-up linking this issue to #191.
Global dash-to-separator mapping remains unchanged. No new literal-hyphen-preservation API is introduced; modern length budgeting does not resolve that separate request.
The default algorithm remains legacy; improved output rules are opt-in. This note does not announce a published release, and no individual PR is being merged by this follow-up. Thank you for the contribution and discussion.
Activity
adamcunnington-mlg commentedon Nov 1, 2021
Pretty sure it is a big, caused by https://github.com/un33k/python-slugify/blob/master/slugify/slugify.py#L179
Looks like there is a test case missing.
adamcunnington-mlg commentedon Nov 2, 2021
@un33k
un33k commentedon Nov 2, 2021
Please note that '-' is a reserved character in slugs. This package allows an atomic replacement of the default separator of '-' with something else towards the end of the process.
A simple preprocess of the text is recommended for special cases.
All PRs are considered provided they remain backward compatible.
adamcunnington-mlg commentedon Nov 2, 2021
Ok, I think this is definitely PR-worthy. I agree that '-' is a reserved character - but this is precisely the reason why you shouldn't remove them. I can pre-process the string; sure; but this seems a bit broken.
Will find time to raise PR but I'm not sure how this can remain backwards compatible as this would be changing (fixing) behaviour where input string contains a - character. Do you imagine this being behind a feature toggle that defaults to current behaviour perhaps?
adamcunnington-mlg commentedon Nov 3, 2021
Also, there's a logical problem with the suggested workaround. Pre-processing the string is highly problematic - what character am I going to replace - with, before I pass it to a call to slugify that is going to replace characters that don't match my regex? I have a very limited set of options and whatever I choose could legitimately appear elsewhere in the string and be erroneously substituted by the post-processing.
The only thing I could do is replace - with some string that i think is going to be reasonably unique. This is brittle and bad. This behaviour should belong to slugify.
un33k commentedon Nov 3, 2021
I think we have examples on how to handle this situation.
python-slugify/test.py
Line 94 in a90d967
un33k commentedon Dec 4, 2021
Closing this now, feel free to reopen if the above example is not addressing your needs and/or you have a non-breaking PR.
ideepu commentedon Dec 14, 2021
I am facing the same issue here. I have a text like this
121232-some text here-some text here againTrying to
slugifythe text with separator' '. Expecting the output121232-some text here-some text here againbut got121232 some text here some text here againIs there any work around for this.
adamcunnington-mlg commentedon Dec 14, 2021
@PradeepTammali workaround is something like this:
un33k commentedon Dec 16, 2021
@PradeepTammali Slugify needs one reserved character, and since the name of the package is
slugifyits job is to convert some random text to adash-delimited clean text for use in URLs. With that said, the delimiter can also be changed to whatever user wants. In short, theaskhere is not doable. I encourage channelling one's energy to raising a meaningful PR instead.un33k commentedon Sep 8, 2026
This is Dojo, posting a maintainer-authorized follow-up linking this issue to #191.
Global dash-to-separator mapping remains unchanged. No new literal-hyphen-preservation API is introduced; modern length budgeting does not resolve that separate request.
The default algorithm remains legacy; improved output rules are opt-in. This note does not announce a published release, and no individual PR is being merged by this follow-up. Thank you for the contribution and discussion.
🚀 Generated with Dojo ⛩️