Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Slugify support for international characters #75

Open
kmturley opened this issue Oct 9, 2018 · 0 comments
Open

Slugify support for international characters #75

kmturley opened this issue Oct 9, 2018 · 0 comments

Comments

@kmturley
Copy link

kmturley commented Oct 9, 2018

[ ] bug report
[x] feature request

Current behavior
I am trying to slugify a city name: Malmö
{{ 'Malmö' | slugify }} = malm

Expected behavior
I would expect it to replace the ö for an o
{{ 'Malmö' | slugify }} = malmo

Minimal reproduction of the problem with instructions
plnkr isn't loading right now, but simple to replicate. I have previously been using a function which does the replacement for you:

slugify(text) {
    if (text) {
      const specialChars = {'à': 'a', 'ä': 'a', 'á': 'a', 'â': 'a', 'æ': 'a', 'å': 'a', 'ë': 'e', 'è': 'e', 'é': 'e',
      'ê': 'e', 'î': 'i', 'ï': 'i', 'ì': 'i', 'í': 'i', 'ò': 'o', 'ó': 'o', 'ö': 'o', 'ô': 'o', 'ø': 'o', 'ù': 'o',
      'ú': 'u', 'ü': 'u', 'û': 'u', 'ñ': 'n', 'ç': 'c', 'ß': 's', 'ÿ': 'y', 'œ': 'o', 'ŕ': 'r', 'ś': 's', 'ń': 'n',
      'ṕ': 'p', 'ẃ': 'w', 'ǵ': 'g', 'ǹ': 'n', 'ḿ': 'm', 'ǘ': 'u', 'ẍ': 'x', 'ź': 'z', 'ḧ': 'h', '·': '-', '/': '-',
      '_': '-', ', ': '-', ': ': '-', ';': '-'};

      return text.toString().toLowerCase()
        .replace(/\s+/g, '-')           // Replace spaces with -
        .replace(/./g, (target, index, str) => specialChars[target] || target) // Replace special characters using the hash map
        .replace(/&/g, '-and-')         // Replace & with 'and'
        .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
        .replace(/\-\-+/g, '-')         // Replace multiple - with single -
        .replace(/^-+/, '')             // Trim - from start of text
        .replace(/-+$/, '');             // Trim - from end of text
    } else {
      return false;
    }
  }

What is the motivation / use case for changing the behavior?
nicer slugs for urls, easier to read as they are closer to the originals

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant