Last active
July 12, 2021 10:18
-
-
Save langpavel/9db4b617d7c8f067b61a to your computer and use it in GitHub Desktop.
PostgreSQL URL slug usable for indices
This file contains 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
/** | |
* original unaccent function isn't usable for indices | |
*/ | |
CREATE OR REPLACE FUNCTION uaccent(text) RETURNS text AS | |
$uaccent$ | |
SELECT unaccent('unaccent',$1::text); | |
$uaccent$ LANGUAGE sql IMMUTABLE; |
This file contains 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 OR REPLACE FUNCTION url_slug(text) RETURNS text AS | |
$url_slug$ | |
SELECT trim(both '-' from | |
regexp_replace( | |
unaccent('unaccent', lower($1::text)), | |
'[^a-z0-9]+', '-', 'g' | |
) | |
); | |
$url_slug$ LANGUAGE sql IMMUTABLE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment