Skip to content

Instantly share code, notes, and snippets.

@manhnguyenv
Created July 19, 2019 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manhnguyenv/8fd138d32980aa93523fecff56dd01e0 to your computer and use it in GitHub Desktop.
Save manhnguyenv/8fd138d32980aa93523fecff56dd01e0 to your computer and use it in GitHub Desktop.
Generating Slugs with JavaScript (Slug.js)
<!DOCTYPE html>
<html>
<body>
<p>Click the button to return the value of the string object.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function camelCase(data, delim = '-') {
const list = Array.isArray(data) ? data : data.split(delim);
return list.reduce((res, cur) => res + cur.charAt(0).toUpperCase() + cur.slice(1))
}
function myFunction() {
const name = "ĐẶC BIỆT QUÁ";
let newName = toSlug(name);
document.getElementById("demo").innerHTML = newName;
}
function myFunction3() {
const name = "data hello to everyone";
let newName = slugify(name);
document.getElementById("demo").innerHTML = newName;
}
function myFunction2() {
const name = "data-hello-to-everyone";
const cutStart = 0;
let newName = camelCase(name.substring(cutStart));
document.getElementById("demo").innerHTML = newName;
}
function slugify(text) {
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
function toSlug(title) {
var slug;
//Đổi chữ hoa thành chữ thường
slug = title.toLowerCase();
//Đổi ký tự có dấu thành không dấu
slug = slug.replace(/á|à|ả|ạ|ã|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ/gi, 'a');
slug = slug.replace(/é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ/gi, 'e');
slug = slug.replace(/i|í|ì|ỉ|ĩ|ị/gi, 'i');
slug = slug.replace(/ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ/gi, 'o');
slug = slug.replace(/ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự/gi, 'u');
slug = slug.replace(/ý|ỳ|ỷ|ỹ|ỵ/gi, 'y');
slug = slug.replace(/đ/gi, 'd');
//Xóa các ký tự đặt biệt
slug = slug.replace(/\`|\~|\!|\@|\#|\||\$|\%|\^|\&|\*|\(|\)|\+|\=|\,|\.|\/|\?|\>|\<|\'|\"|\:|\;|_/gi, '');
//Đổi khoảng trắng thành ký tự gạch ngang
slug = slug.replace(/ /gi, " - ");
//Đổi nhiều ký tự gạch ngang liên tiếp thành 1 ký tự gạch ngang
//Phòng trường hợp người nhập vào quá nhiều ký tự trắng
slug = slug.replace(/\-\-\-\-\-/gi, '-');
slug = slug.replace(/\-\-\-\-/gi, '-');
slug = slug.replace(/\-\-\-/gi, '-');
slug = slug.replace(/\-\-/gi, '-');
//Xóa các ký tự gạch ngang ở đầu và cuối
slug = '@' + slug + '@';
slug = slug.replace(/\@\-|\-\@|\@/gi, '');
slug = slug.replace(/\s+/g, '-'); // Replace spaces with -
slug = slug.replace(/[^\w\-]+/g, ''); // Remove all non-word chars
slug = slug.replace(/\-\-+/g, '-'); // Replace multiple - with single -
slug = slug.replace(/^-+/, ''); // Trim - from start of text
slug = slug.replace(/-+$/, ''); // Trim - from end of text
//In slug ra textbox có id “slug”
return slug;
}
</script>
</body>
</html>
function string_to_slug(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment