Tofu/scripts/import-recipes/recipes/slugifyRecipeName.js
2026-09-18 01:41:02 +02:00

16 lines
675 B
JavaScript

// Ligatures do not decompose under NFD, so they are spelled out first.
const LIGATURES = [[/œ/g, 'oe'], [/æ/g, 'ae']]
// Builds the file name of a recipe: "Gnocchis à la sauge" -> "gnocchis-a-la-sauge".
// The same string is the recipe id in the index, so it stays ASCII: it travels
// through a WebDAV URL and a file system.
export function slugifyRecipeName(name) {
if (typeof name !== 'string') return ''
let slug = name.toLowerCase()
for (const [pattern, replacement] of LIGATURES) slug = slug.replace(pattern, replacement)
return slug
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
}