Transform # character to - in slug (#10896)
Co-authored-by: Bailey Pumfleet <bailey@pumfleet.co.uk>pull/10929/head^2
parent
fde3e8bea9
commit
31d4cb4eb7
|
@ -8,10 +8,12 @@ describe("slugify", () => {
|
|||
expect(slugify("HELLO")).toEqual("hello");
|
||||
});
|
||||
|
||||
it("should convert spaces, _, and any other special character to -", () => {
|
||||
it("should convert spaces, _, +, # and any other special character to -", () => {
|
||||
expect(slugify("hello there")).toEqual("hello-there");
|
||||
expect(slugify("hello_there")).toEqual("hello-there");
|
||||
expect(slugify("hello$there")).toEqual("hello-there");
|
||||
expect(slugify("hello+there")).toEqual("hello-there");
|
||||
expect(slugify("#hellothere")).toEqual("hellothere");
|
||||
});
|
||||
|
||||
it("should keep numbers as is", () => {
|
||||
|
|
|
@ -5,10 +5,9 @@ export const slugify = (str: string) => {
|
|||
.normalize("NFD") // Normalize to decomposed form for handling accents
|
||||
.replace(/\p{Diacritic}/gu, "") // Remove any diacritics (accents) from characters
|
||||
.replace(/[^\p{L}\p{N}\p{Zs}\p{Emoji}]+/gu, "-") // Replace any non-alphanumeric characters (including Unicode) with a dash
|
||||
.replace(/[\s_]+/g, "-") // Replace whitespace and underscores with a single dash
|
||||
.replace(/[\s_#]+/g, "-") // Replace whitespace, # and underscores with a single dash
|
||||
.replace(/^-+/, "") // Remove dashes from start
|
||||
.replace(/-+$/, "") // Remove dashes from end
|
||||
.replace(/\+/g, "-"); // Transform all + to -
|
||||
.replace(/-+$/, ""); // Remove dashes from end
|
||||
};
|
||||
|
||||
export default slugify;
|
||||
|
|
Loading…
Reference in New Issue