-
-
Save kez/17638bade0382f820280dafa46277435 to your computer and use it in GitHub Desktop.
Generating Slugs in Postgres
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
CREATE EXTENSION IF NOT EXISTS "unaccent" | |
CREATE OR REPLACE FUNCTION slugify("value" TEXT) | |
RETURNS TEXT AS $$ | |
-- removes accents (diacritic signs) from a given string -- | |
WITH "unaccented" AS ( | |
SELECT unaccent("value") AS "value" | |
), | |
-- lowercases the string | |
"lowercase" AS ( | |
SELECT lower("value") AS "value" | |
FROM "unaccented" | |
), | |
-- remove single and double quotes | |
"removed_quotes" AS ( | |
SELECT regexp_replace("value", '[''"]+', '', 'gi') AS "value" | |
FROM "lowercase" | |
), | |
-- replaces anything that's not a letter, number, hyphen('-'), or underscore('_') with a hyphen('-') | |
"hyphenated" AS ( | |
SELECT regexp_replace("value", '[^a-z0-9\\-_]+', '-', 'gi') AS "value" | |
FROM "removed_quotes" | |
), | |
-- trims hyphens('-') if they exist on the head or tail of the string | |
"trimmed" AS ( | |
SELECT regexp_replace(regexp_replace("value", '\-+$', ''), '^\-', '') AS "value" | |
FROM "hyphenated" | |
) | |
SELECT "value" FROM "trimmed"; | |
$$ LANGUAGE SQL STRICT IMMUTABLE; |
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
CREATE FUNCTION public.set_slug_from_name() RETURNS trigger | |
LANGUAGE plpgsql | |
AS $$ | |
BEGIN | |
NEW.slug := slugify(NEW.name); | |
RETURN NEW; | |
END | |
$$; | |
CREATE TRIGGER "trg_slug_insert" | |
BEFORE INSERT ON "my_table_name" | |
FOR EACH ROW | |
WHEN (NEW.name IS NOT NULL AND NEW.slug IS NULL) | |
EXECUTE PROCEDURE set_slug_from_name(); |
the following chars are all not being filtered out:
\ backslash ] right square bracket ^ caret _ underscore [...]
Thank you @NielsRenard! With your fix the slugify
function is now more robust for corner cases like "the title [something]".
@kez: can you please update the code in the gist please? This is a useful utility.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the following chars are all not being filtered out:
\ backslash
] right square bracket
^ caret
_ underscore
The issue seems to be a slash too many,
\\-_
will match characters from range "\" to "_" (char code 92 to 95).Removing one of the slashes made it work as intended, i.e. :