From c71975c5e946845d2fec7a6f0c2ca6e6f6d790ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20H=C3=B6ser?= Date: Sun, 12 Dec 2021 20:54:50 +0100 Subject: [PATCH 1/6] Simplify sanitizePath Problem: If the title of an entry contains a ".", the path becomes invalid. Solution: Removing all "."s in the path using slugify resolves this problem. Furthermore the following changes got made to simplify the code of sanitizePath: - Slugify automatically removes leading, trailing or doubled replacement chars, so doing it explicitly is just a no-op. - Also slugify has an option for converting to lower case. --- src/ParentWidget.js | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/ParentWidget.js b/src/ParentWidget.js index db112885..109fb6a4 100644 --- a/src/ParentWidget.js +++ b/src/ParentWidget.js @@ -43,22 +43,7 @@ const Option = (props) => { ); }; -export const sanitizePath = (path) => { - const replacement = '-'; - 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'); - const trailingReplacement = new RegExp(`${replacement}$`); - const leadingReplacement = new RegExp(`^${replacement}`); - - const normalizedPath = sanitizedPath - .replace(doubleReplacement, replacement) - .replace(leadingReplacement, '') - .replace(trailingReplacement, ''); - - return normalizedPath; -}; +export const sanitizePath = (path) => slugify(path, { replacement: '-', lower: true, remove: /\./ }); export class ParentControl extends React.Component { constructor(props) { From 28f92cff4a901045e4688d0334d2ce6e8c035808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20H=C3=B6ser?= Date: Sun, 12 Dec 2021 20:59:04 +0100 Subject: [PATCH 2/6] Add unittest for "." removal --- src/ParentWidget.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ParentWidget.test.js b/src/ParentWidget.test.js index c9c3b596..a053bb22 100644 --- a/src/ParentWidget.test.js +++ b/src/ParentWidget.test.js @@ -28,4 +28,8 @@ describe('sanitizePath', () => { it('should keep diacritis and remove whitespace, trailing and leading characters', () => { expect(sanitizePath('?ăștia sunteți voi ? ')).toBe('astia-sunteti-voi'); }); + + it('should remove "."s', () => { + expect(sanitizePath('who are.we')).toBe('who-are-we'); + }); }); From 6819582ec7ba5586514011aacc20843cf0179073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20H=C3=B6ser?= Date: Sun, 12 Dec 2021 21:09:19 +0100 Subject: [PATCH 3/6] fix: resolve prettier warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphael Höser --- src/ParentWidget.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ParentWidget.js b/src/ParentWidget.js index 109fb6a4..84a21fe6 100644 --- a/src/ParentWidget.js +++ b/src/ParentWidget.js @@ -43,7 +43,8 @@ const Option = (props) => { ); }; -export const sanitizePath = (path) => slugify(path, { replacement: '-', lower: true, remove: /\./ }); +export const sanitizePath = (path) => + slugify(path, { replacement: '-', lower: true, remove: /\./ }); export class ParentControl extends React.Component { constructor(props) { From e794b4a071cac19a630e7cd03c4a019d9bb42407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20H=C3=B6ser?= Date: Sun, 12 Dec 2021 21:20:19 +0100 Subject: [PATCH 4/6] fix(unittest): dots get removed, not replaced MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphael Höser --- src/ParentWidget.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ParentWidget.test.js b/src/ParentWidget.test.js index a053bb22..e856cd89 100644 --- a/src/ParentWidget.test.js +++ b/src/ParentWidget.test.js @@ -30,6 +30,6 @@ describe('sanitizePath', () => { }); it('should remove "."s', () => { - expect(sanitizePath('who are.we')).toBe('who-are-we'); + expect(sanitizePath('who are.we')).toBe('who-arewe'); }); }); From e4f1cec5ea3aabbdad37884f786763ec066765f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20H=C3=B6ser?= Date: Sun, 12 Dec 2021 21:25:51 +0100 Subject: [PATCH 5/6] fix(sanitizePath): if you set slugify.remove, it doesn't remove the default char set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change the bahavior to replace dots instead of removing them Signed-off-by: Raphael Höser --- src/ParentWidget.js | 6 ++++-- src/ParentWidget.test.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ParentWidget.js b/src/ParentWidget.js index 84a21fe6..54b91beb 100644 --- a/src/ParentWidget.js +++ b/src/ParentWidget.js @@ -43,8 +43,10 @@ const Option = (props) => { ); }; -export const sanitizePath = (path) => - slugify(path, { replacement: '-', lower: true, remove: /\./ }); +export const sanitizePath = (path) => { + const replacement = '-'; + return slugify(path.replaceAll('.', replacement), { replacement, lower: true }); +}; export class ParentControl extends React.Component { constructor(props) { diff --git a/src/ParentWidget.test.js b/src/ParentWidget.test.js index e856cd89..a053bb22 100644 --- a/src/ParentWidget.test.js +++ b/src/ParentWidget.test.js @@ -30,6 +30,6 @@ describe('sanitizePath', () => { }); it('should remove "."s', () => { - expect(sanitizePath('who are.we')).toBe('who-arewe'); + expect(sanitizePath('who are.we')).toBe('who-are-we'); }); }); From d18c85421eb31afe916dbcbd38529efca8db7b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20H=C3=B6ser?= Date: Mon, 13 Dec 2021 13:08:31 +0100 Subject: [PATCH 6/6] fix(node12) node 12 doesn't support replaceAll yet My solution was using replaceAll, but that isn't supported in node 12. Therefore I switched to regex for this. --- src/ParentWidget.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ParentWidget.js b/src/ParentWidget.js index 54b91beb..3200624c 100644 --- a/src/ParentWidget.js +++ b/src/ParentWidget.js @@ -45,7 +45,7 @@ const Option = (props) => { export const sanitizePath = (path) => { const replacement = '-'; - return slugify(path.replaceAll('.', replacement), { replacement, lower: true }); + return slugify(path.replace(/\./g, replacement), { replacement, lower: true }); }; export class ParentControl extends React.Component {