simov / slugify

Slugifies a string
MIT License
1.47k stars 126 forks source link

change rule for certain characters #161

Closed ThePeach closed 1 year ago

ThePeach commented 2 years ago

Hello, I'm currently working with 11ty and migrating to the latest version. I've stumbled on a single instance where I can't seem to adjust the resulting permalink to the previous version. Specifically I have a tag that is "Gnu/Linux" that gets converted into "gnulinux" while before I had "gnu-linux". Is there any way to customise this behaviour?

Trott commented 2 years ago

In the slug module, you can get the desired behavior by assigning the mapping directly to the charmap property.

slug.charmap['/'] = '-';

slugify doesn't have the charmap property and trying to get this to work with extend() didn't work for me if I did it the straightforward way. I suspect this may have something to do with slugify automatically removing certain characters. I'd have to dig in more to see if there is a convenient way to override things. @simov might have the answer.

stephanbogner commented 1 year ago

I just wanted to post the same question (to have more readable results for slugified paths) but then I found out you can use extend() for this:

import slugify from 'slugify'
slugify.extend({'/': '-'})
console.log( slugify('https://github.com/simov/slugify/issues/161') )
// Result: https:-github.com-simov-slugify-issues-161
Trott commented 1 year ago

Well, if that works, then great. I don't know why I couldn't get it to work before, but happy to be wrong.