Last active
July 20, 2024 09:47
-
-
Save kostasx/7516158 to your computer and use it in GitHub Desktop.
Convert greek strings to URL slugs in JavaScript.
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
<!-- Demo: provided by Nicholas Andreou (https://codepen.io/DigiCodeCy) --> | |
<p> | |
<label for="title">Title:</label> | |
<input id="ctrl-title" name="title" type="text" placeholder="Enter Title" onkeyup="textToSlug(this.value)" required /> | |
</p> | |
<p> | |
<label for="slug">Slug : </label> | |
<input id="ctrl-slug" name="slug" type="text" readonly required /> | |
</p> | |
<script> | |
function textToSlug(text){ | |
var grArray = {"α": "a", "β": "b", "γ": "g", "δ": "d", "ε": "e", "ζ": "z", "η": "i", "θ": "th", "ι": "i", "κ": "k", "λ": "l", "μ": "m", "ν": "n", "ξ": "ks", "ο": "o", "π": "p", "ρ": "r", "σ": "s", "τ": "t", "υ": "u", "φ": "f", "χ": "x", "ψ": "ps", "ω": "w", "ς": "s", " ": "-","ά": "a","έ": "e","ή": "i","ί": "i","ό": "o","ύ": "u","ώ": "w" }; | |
var slug = text; | |
slug = slug.toLowerCase(); | |
for (var key in grArray) { | |
var regex = new RegExp(key, "g"); | |
slug = slug.replace(regex,grArray[key]); | |
} | |
slug = slug.replace(/[^a-z0-9 -]/g, ''); | |
slug = slug.replace(/--/g,"-"); | |
slug = slug.replace(/--/g,"-"); | |
slug = slug.replace(/^-/, ""); | |
document.getElementById("ctrl-slug").value=slug; | |
} | |
</script> |
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
function string_to_slug(str) { | |
str = str.replace(/^\s+|\s+$/g, '') // TRIM WHITESPACE AT BOTH ENDS. | |
.toLowerCase(); // CONVERT TO LOWERCASE | |
const from = [ "ου", "ΟΥ", "Ού", "ού", "αυ", "ΑΥ", "Αύ", "αύ", "ευ", "ΕΥ", "Εύ", "εύ", "α", "Α", "ά", "Ά", "β", "Β", "γ", "Γ", "δ", "Δ", "ε", "Ε", "έ", "Έ", "ζ", "Ζ", "η", "Η", "ή", "Ή", "θ", "Θ", "ι", "Ι", "ί", "Ί", "ϊ", "ΐ", "Ϊ", "κ", "Κ", "λ", "Λ", "μ", "Μ", "ν", "Ν", "ξ", "Ξ", "ο", "Ο", "ό", "Ό", "π", "Π", "ρ", "Ρ", "σ", "Σ", "ς", "τ", "Τ", "υ", "Υ", "ύ", "Ύ", "ϋ", "ΰ", "Ϋ", "φ", "Φ", "χ", "Χ", "ψ", "Ψ", "ω", "Ω", "ώ", "Ώ" ]; | |
const to = [ "ou", "ou", "ou", "ou", "au", "au", "au", "au", "eu", "eu", "eu", "eu", "a", "a", "a", "a", "b", "b", "g", "g", "d", "d", "e", "e", "e", "e", "z", "z", "i", "i", "i", "i", "th", "th", "i", "i", "i", "i", "i", "i", "i", "k", "k", "l", "l", "m", "m", "n", "n", "ks", "ks", "o", "o", "o", "o", "p", "p", "r", "r", "s", "s", "s", "t", "t", "y", "y", "y", "y", "y", "y", "y", "f", "f", "x", "x", "ps", "ps", "o", "o", "o", "o" ]; | |
for ( var i = 0; i < from.length; i++ ) { | |
while( str.indexOf( from[i]) !== -1 ){ | |
str = str.replace( from[i], to[i] ); // CONVERT GREEK CHARACTERS TO LATIN LETTERS | |
} | |
} | |
str = str.replace(/[^a-z0-9 -]/g, '') // REMOVE INVALID CHARS | |
.replace(/\s+/g, '-') // COLLAPSE WHITESPACE AND REPLACE BY DASH - | |
.replace(/-+/g, '-'); // COLLAPSE DASHES | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @GaLaTaS! Added demo to this gist.