Skip to content

Commit 46528d7

Browse files
authored
fix: use slugify for path formatting (#187)
1 parent 788722c commit 46528d7

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"netlify-cms-ui-default": "^2.12.2",
3737
"react": "^16.8.4",
3838
"react-dom": "^16.8.4",
39-
"react-select": "^3.1.0"
39+
"react-select": "^3.1.0",
40+
"slugify": "^1.5.0"
4041
},
4142
"devDependencies": {
4243
"@babel/core": "^7.11.6",

src/ParentWidget.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import AsyncSelect from 'react-select/async';
33
import { reactSelectStyles } from 'netlify-cms-ui-default/dist/esm/styles';
44
import { NestedCollection } from './NestedCollection';
5+
import slugify from 'slugify';
56

67
const trimStart = (str, prefix) => {
78
return str.substring(prefix.length);
@@ -44,7 +45,7 @@ const Option = (props) => {
4445

4546
export const sanitizePath = (path) => {
4647
const replacement = '-';
47-
const sanitizedPath = path.replace(/[^a-z0-9]/gi, replacement).toLowerCase();
48+
const sanitizedPath = slugify(path.toLowerCase(), replacement);
4849

4950
// Remove any doubled or leading/trailing replacement characters (that were added in the sanitizers).
5051
const doubleReplacement = new RegExp(`(?:${replacement})+`, 'g');

src/ParentWidget.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ describe('sanitizePath', () => {
2020
it('should remove double replacer', () => {
2121
expect(sanitizePath('Who Are We')).toBe('who-are-we');
2222
});
23+
24+
it('should keep diacritis', () => {
25+
expect(sanitizePath('ăștia sunteți voi')).toBe('astia-sunteti-voi');
26+
});
27+
28+
it('should keep diacritis and remove whitespace, trailing and leading characters', () => {
29+
expect(sanitizePath('?ăștia sunteți voi ? ')).toBe('astia-sunteti-voi');
30+
});
2331
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7015,6 +7015,11 @@ slice-ansi@^4.0.0:
70157015
astral-regex "^2.0.0"
70167016
is-fullwidth-code-point "^3.0.0"
70177017

7018+
slugify@^1.5.0:
7019+
version "1.5.0"
7020+
resolved "https://registry.yarnpkg.com/slugify/-/slugify-1.5.0.tgz#5f3c8e2a84105b54eb51486db1b468a599b3c9b8"
7021+
integrity sha512-Q2UPZ2udzquy1ElHfOLILMBMqBEXkiD3wE75qtBvV+FsDdZZjUqPZ44vqLTejAVq+wLLHacOMcENnP8+ZbzmIA==
7022+
70187023
snapdragon-node@^2.0.1:
70197024
version "2.1.1"
70207025
resolved "https://registry.yarnpkg.com/snapdragon-node/-/snapdragon-node-2.1.1.tgz#6c175f86ff14bdb0724563e8f3c1b021a286853b"

0 commit comments

Comments
 (0)