docusaurus/lib/core/toSlug.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-07-07 13:28:29 -04:00
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
2017-07-07 13:28:29 -04:00
*/
module.exports = string => {
// var accents = "àáäâèéëêìíïîòóöôùúüûñç";
2017-07-10 19:38:35 -04:00
const accents =
"\u00e0\u00e1\u00e4\u00e2\u00e8" +
"\u00e9\u00eb\u00ea\u00ec\u00ed\u00ef" +
"\u00ee\u00f2\u00f3\u00f6\u00f4\u00f9" +
"\u00fa\u00fc\u00fb\u00f1\u00e7";
2017-07-07 13:28:29 -04:00
2017-07-10 19:38:35 -04:00
const without = "aaaaeeeeiiiioooouuuunc";
2017-07-07 13:28:29 -04:00
let slug = string
.toString()
// Handle uppercase characters
.toLowerCase()
// Handle accentuated characters
2017-07-10 19:38:35 -04:00
.replace(new RegExp("[" + accents + "]", "g"), c => {
return without.charAt(accents.indexOf(c));
})
2017-07-07 13:28:29 -04:00
// Replace `.`, `(` and `?` with blank string like Github does
2017-07-10 19:38:35 -04:00
.replace(/\.|\(|\?/g, "")
2017-07-07 13:28:29 -04:00
// Dash special characters
2017-07-10 19:38:35 -04:00
.replace(/[^a-z0-9]/g, "-")
2017-07-07 13:28:29 -04:00
// Compress multiple dash
2017-07-10 19:38:35 -04:00
.replace(/-+/g, "-")
2017-07-07 13:28:29 -04:00
// Trim dashes
2017-07-10 19:38:35 -04:00
.replace(/^-|-$/g, "");
2017-07-07 13:28:29 -04:00
// Add trailing `-` if string contains ` ...` in the end like Github does
if (/\s[.]{1,}/.test(string)) {
2017-07-10 19:38:35 -04:00
slug += "-";
2017-07-07 13:28:29 -04:00
}
return slug;
};