Slug generation

Hello,

I was wondering if there is a built-in slugify function, but it doesn’t seem so.

This is what I use to generate a slug

{{String($json.nom)
.replace(/[øØ]/g, 'o')
.replace(/[æÆ]/g, 'ae')
.replace(/[ßẞ]/g, 'ss')
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/\b\w'/g, "")
.replace(/ẞ/g, 'ß')
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '')}}

It allows to turn something like L'été, les événements débordent d'énergie créative!
into ete-les-evenements-debordent-energie-creative to create a slug.

Hope that helps,

Cheers,
Joachim

3 Likes

This may be slightly simplified now using the expression transformation function replaceSpecialChars() - Data transformation functions for strings - n8n Documentation

A slugify function would be a good feature request though

2 Likes

Thank you for the tip !

Hello everyone,

I now use a code node, here is the script:

const title = $node['name of your node'].json['field to transform to slug'];

let slug = title.normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/\/|\s/g, '-').toLowerCase().replace(/[^a-z0-9-]+/g, '');

slug = slug.replace(/-+/g, '-');

const words = slug.split('-');
const deduped = [...new Set(words)];
slug = deduped.join('-');

return [{ json: { slug } }];
  1. First, it retrieves the string from the specified field in your node (replace 'name of your node' and 'field to transform to slug' with your specific node name and field).
  2. It then uses the normalize() function in JavaScript to decompose the title into its base characters and associated accent marks. The replace(/[\u0300-\u036f]/g, "") part of the script removes any accents from characters.
  3. After that, it replaces slashes and spaces in the title with hyphens, converts the title to lowercase, and removes any characters that are not alphanumeric or hyphens.
  4. The script then replaces any occurrence of consecutive hyphens with a single hyphen.
  5. Finally, it will remove duplicate keywords in your slug. To do so, it splits the slug into individual words (separated by hyphens), removes any duplicate words and reassembles the slug.

Hope that helps some of you :slight_smile:

1 Like

This sounds interesting. I did not understand it though… What is the best user case for this?

You input an article title, a first and last name for a directory, etc. and it outputs a slug that shall be somewhat seo optimized, at scale.

François Hervé > directory.com/francois-herve
code & code snippets > website.com/code-snippets

2 Likes