Data and text · Easy · 10 min

Clean slugs for Spanish titles

Accents, ñ and opening punctuation end up as broken URLs. Turn them into a readable slug.

Write slugify(title). Strip accents (á becomes a, ñ becomes n, ü becomes u), lowercase everything, replace any run of characters that are not letters a-z or digits with a single hyphen, and trim hyphens from both ends.

Example: '¿Qué es la IA local?' gives 'que-es-la-ia-local'.

Challenges 0/4

  • '¿Qué es la IA local?' gives 'que-es-la-ia-local'
  • Converts ñ and accents, keeps numbers
  • Collapses repeated separators and trims the ends
  • Removes the diaeresis and symbols

function slugify(title) {
  // 1. normalize('NFD') and drop the combining marks
  // 2. lowercase, 3. non-alphanumerics to '-', 4. trim hyphens
  return title.toLowerCase().replace(/ /g, '-');
}

console.log(slugify('¿Qué es la IA local?'));

Tab indents, Ctrl+Z undoes. To leave the editor with the keyboard: Esc, then Tab.

Console output appears here (console.log).

Why this matters

Spanish titles break slug functions written with English in mind: the ñ vanishes, accents turn into percent codes and opening punctuation leaves hyphens at the start. You notice it in blog URLs, in the file names a pipeline generates and in the document identifiers of a RAG, where two different titles can end up with the same key. normalize('NFD') solves most of it in one line. That stable identifier is what the index uses in local RAG over company docs with n8n and Ollama.

Go deeper: the MDN reference →

This in production, with your data? Let's talk for 15 minutes →