← Runnable Projects

阅读中文版本

URL Slug Generator CLI

A Node CLI that turns a title into a URL-safe slug by lowercasing, transliterating common accented characters, and collapsing separators.

beginnerjavascriptunverified

Project

node-slugify-cli

Entry point: slugify.mjs

Install

node --version

Run

node slugify.mjs "Hello, World! Café déjà vu"

Test

node --test slugify.test.mjs

Expected output

Prints a lowercase hyphenated slug with accents removed and punctuation stripped.

Dependencies

  • node >=20

What it does

Turns a title into a slug by normalising Unicode, stripping combining marks, and replacing runs of non-alphanumerics with a single hyphen.

export function slugify(text) {
  return text
    .normalize('NFKD')
    .replace(/[\u0300-\u036f]/g, '')
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '');
}

const input = process.argv.slice(2).join(' ');
if (!input) { console.error('usage: slugify <text>'); process.exit(1); }
console.log(slugify(input));

Using Unicode normalisation handles accents generically instead of a hard-coded character map, which is why the same code works across languages.

Run and verify

node slugify.mjs "Hello, World! Café déjà vu"

Pass the title as a single quoted argument; accented letters are transliterated by decomposition rather than a lookup table.

Expected Output

For the sample title the program should strip punctuation and accents and join words with hyphens. The output is:

hello-world-cafe-deja-vu

Leading and trailing hyphens are removed, runs of punctuation collapse to one hyphen, and the result is safe to use in a URL path.

Limitations

Non-Latin scripts such as CJK are stripped rather than transliterated; languages without Latin equivalents need a transliteration step or a different slug strategy.