diff --git a/package.json b/package.json index 2678be1d..1fcd58f0 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "netlify-cms-ui-default": "^2.12.2", "react": "^16.8.4", "react-dom": "^16.8.4", - "react-select": "^3.1.0" + "react-select": "^3.1.0", + "slugify": "^1.5.0" }, "devDependencies": { "@babel/core": "^7.11.6", diff --git a/src/ParentWidget.js b/src/ParentWidget.js index 51225cfc..e37bfc99 100644 --- a/src/ParentWidget.js +++ b/src/ParentWidget.js @@ -2,6 +2,7 @@ import React from 'react'; import AsyncSelect from 'react-select/async'; import { reactSelectStyles } from 'netlify-cms-ui-default/dist/esm/styles'; import { NestedCollection } from './NestedCollection'; +import slugify from 'slugify'; const trimStart = (str, prefix) => { return str.substring(prefix.length); @@ -44,7 +45,7 @@ const Option = (props) => { export const sanitizePath = (path) => { const replacement = '-'; - const sanitizedPath = path.replace(/[^a-z0-9]/gi, replacement).toLowerCase(); + const sanitizedPath = slugify(path.toLowerCase(), replacement); // Remove any doubled or leading/trailing replacement characters (that were added in the sanitizers). const doubleReplacement = new RegExp(`(?:${replacement})+`, 'g'); diff --git a/src/ParentWidget.test.js b/src/ParentWidget.test.js index 829c62d7..c9c3b596 100644 --- a/src/ParentWidget.test.js +++ b/src/ParentWidget.test.js @@ -20,4 +20,12 @@ describe('sanitizePath', () => { it('should remove double replacer', () => { expect(sanitizePath('Who Are We')).toBe('who-are-we'); }); + + it('should keep diacritis', () => { + expect(sanitizePath('ăștia sunteți voi')).toBe('astia-sunteti-voi'); + }); + + it('should keep diacritis and remove whitespace, trailing and leading characters', () => { + expect(sanitizePath('?ăștia sunteți voi ? ')).toBe('astia-sunteti-voi'); + }); }); diff --git a/yarn.lock b/yarn.lock index 94f530e3..4890bdef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7060,6 +7060,11 @@ slice-ansi@^4.0.0: astral-regex "^2.0.0" is-fullwidth-code-point "^3.0.0" +slugify@^1.5.0: + version "1.5.0" + resolved "https://registry.yarnpkg.com/slugify/-/slugify-1.5.0.tgz#5f3c8e2a84105b54eb51486db1b468a599b3c9b8" + integrity sha512-Q2UPZ2udzquy1ElHfOLILMBMqBEXkiD3wE75qtBvV+FsDdZZjUqPZ44vqLTejAVq+wLLHacOMcENnP8+ZbzmIA== + snapdragon-node@^2.0.1: version "2.1.1" resolved "https://registry.yarnpkg.com/snapdragon-node/-/snapdragon-node-2.1.1.tgz#6c175f86ff14bdb0724563e8f3c1b021a286853b"