standard-slugify
v4.0.0
Published
Converts a string into a slug safe for URLs and filenames. Supports Latin, Greek and Cyrillic scripts.
Downloads
113
Maintainers
Readme
standard-slugify
Converts a string into a slug safe for URLs and filenames. Supports Latin, Greek and Cyrillic scripts.
Installing
npm install standard-slugify
API
standardSlugify(string, { keepCase, replacements })
Returns a slug of the given string
.
The slug is in lowercase, unless the keepCase
option is set to true
.
import standardSlugify from "standard-slugify";
standardSlugify("Where is your résumé?");
// => "where-is-your-resume"
standardSlugify("toString()", { keepCase: false });
// => "tostring"
standardSlugify("toString()", { keepCase: true });
// => "toString"
standardSlugify("Æthelflæd", { keepCase: true });
// => "Aethelflaed"
standardSlugify("ÆTHELFLÆD", { keepCase: true });
// => "AETHELFLAED"
To specify custom replacements, pass as the replacements
option an array of [regexp, replacement]
pairs.
import standardSlugify from "standard-slugify";
standardSlugify("₿ raising, € falling", {
replacements: [
["€", "eur"], // EURO SIGN
["₿", "btc"], // BITCOIN SIGN
],
});
// => "btc-raising-eur-falling"
// Replacements can be matched with regular expressions,
// e.g., transliterating Ukrainian according to ISO/IEC 7501-3
standardSlugify("Єгипет, Їжак, Йорданія, Югославія, Ямайка", {
replacements: [
[/(?<=^|\P{L})Є/, "YE"], // Є as the first letter of a word
[/(?<=^|\P{L})Ї/, "YI"], // Ї as the first letter of a word
["Г", "H"], // Г in any position
["И", "Y"], // И in any position
[/(?<=^|\P{L})Й/, "Y"], // Й as the first letter of a word
[/(?<=^|\P{L})Ю/, "YU"], // Ю as the first letter of a word
[/(?<=^|\P{L})Я/, "YA"], // Я as the first letter of a word
],
});
// => "yehypet-yizhak-yordaniia-yuhoslaviia-yamaika"
Details
The slug is created by replacing characters according to the following rules and standards:
- If any custom
replacements
are given, they are applied before the default replacements - Letters from ISO-8859-1,2,3,4,5,7,9,10,13,14,15,16, MES-1 and WGL4 (every Latin, Greek and Cyrillic letter actually used in keyboard layouts or fonts) are transliterated to ASCII:
- Latin or Cyrillic letters are transliterated according to ISO/IEC 7501-3
- Greek letters are transliterated according to ISO 843
- Letters are lowercased, unless the
keepCase
option is set totrue
- Characters with the White_Space property or in the Dash_Punctuation (Pd) General Category of Unicode and Control Codes with semantics in Unicode (§ 23.1), such as HT (
\t
) and LF (\n
), are converted to HYPHEN-MINUS (-
) - Characters than are not an ASCII letter (
[A-Za-z]
), a number ([0-9]
), a LOWLINE (_
) or a HYPHEN-MINUS (-
) are removed - Leading, trailing and duplicate HYPHEN-MINUS (
-
) characters are removed