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é?");
standardSlugify("toString()", { keepCase: false });
standardSlugify("toString()", { keepCase: true });
standardSlugify("Æthelflæd", { keepCase: true });
standardSlugify("ÆTHELFLÆD", { keepCase: true });
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"],
["₿", "btc"],
],
});
standardSlugify("Єгипет, Їжак, Йорданія, Югославія, Ямайка", {
replacements: [
[/(?<=^|\P{L})Є/, "YE"],
[/(?<=^|\P{L})Ї/, "YI"],
["Г", "H"],
["И", "Y"],
[/(?<=^|\P{L})Й/, "Y"],
[/(?<=^|\P{L})Ю/, "YU"],
[/(?<=^|\P{L})Я/, "YA"],
],
});
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 to true
- 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