From c61171d8ce2d84f1280466908d4e4cd805834d84 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 8 Mar 2019 17:21:38 +0800 Subject: [PATCH 1/3] Support unicode chars in story IDs --- .../official-storybook/stories/core/unicode.stories.js | 7 +++++++ lib/router/src/tests/id.test.js | 4 +++- lib/router/src/utils.ts | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 examples/official-storybook/stories/core/unicode.stories.js diff --git a/examples/official-storybook/stories/core/unicode.stories.js b/examples/official-storybook/stories/core/unicode.stories.js new file mode 100644 index 000000000000..dada3c9f8332 --- /dev/null +++ b/examples/official-storybook/stories/core/unicode.stories.js @@ -0,0 +1,7 @@ +import React from 'react'; +import { storiesOf } from '@storybook/react'; + +storiesOf('Core|Unicode', module) + .add('😀', () =>

❤️

) + .add('Кнопки', () =>

нормальный

) + .add('바보', () =>

🤷🏻‍♂️

); diff --git a/lib/router/src/tests/id.test.js b/lib/router/src/tests/id.test.js index f65c6c414fe7..83b2751d9d55 100644 --- a/lib/router/src/tests/id.test.js +++ b/lib/router/src/tests/id.test.js @@ -4,10 +4,12 @@ describe('toId', () => { [ // name, kind, story, output ['handles simple cases', 'kind', 'story', 'kind--story'], - ['handles basic substitution', 'a b$c?d😀e', '1-2:3', 'a-b-c-d-e--1-2-3'], + ['handles basic substitution', 'a b$c?d😀e', '1-2:3', 'a-b-c-d😀e--1-2-3'], ['handles runs of non-url chars', 'a?&*b', 'story', 'a-b--story'], ['removes non-url chars from start and end', '?ab-', 'story', 'ab--story'], ['downcases', 'KIND', 'STORY', 'kind--story'], + ['non-latin', 'Кнопки', 'нормальный', 'кнопки--нормальный'], + ['korean', 'kind', '바보 (babo)', 'kind--바보-babo'], ].forEach(([name, kind, story, output]) => { it(name, () => { expect(toId(kind, story)).toBe(output); diff --git a/lib/router/src/utils.ts b/lib/router/src/utils.ts index a0659d3b31ab..25c0fdad3bb9 100644 --- a/lib/router/src/utils.ts +++ b/lib/router/src/utils.ts @@ -12,7 +12,7 @@ const splitPath = /\/([^/]+)\/([^/]+)?/; export const sanitize = (string: string) => { return string .toLowerCase() - .replace(/[^a-z0-9-]/g, '-') + .replace(/[ '`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '-') .replace(/-+/g, '-') .replace(/^-+/, '') .replace(/-+$/, ''); From bb254c028bb75365dbf9bda0101b1630beebb74a Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 8 Mar 2019 17:47:37 +0800 Subject: [PATCH 2/3] Add more punctuation chars --- lib/router/src/tests/id.test.js | 1 + lib/router/src/utils.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/router/src/tests/id.test.js b/lib/router/src/tests/id.test.js index 83b2751d9d55..7f87b9015e43 100644 --- a/lib/router/src/tests/id.test.js +++ b/lib/router/src/tests/id.test.js @@ -10,6 +10,7 @@ describe('toId', () => { ['downcases', 'KIND', 'STORY', 'kind--story'], ['non-latin', 'Кнопки', 'нормальный', 'кнопки--нормальный'], ['korean', 'kind', '바보 (babo)', 'kind--바보-babo'], + ['all punctuation', 'kind', 'unicorns,’–—―′¿`"<>()!.!!!{}[]%^&$*#&', 'kind--unicorns'], ].forEach(([name, kind, story, output]) => { it(name, () => { expect(toId(kind, story)).toBe(output); diff --git a/lib/router/src/utils.ts b/lib/router/src/utils.ts index 25c0fdad3bb9..dffd64cff86c 100644 --- a/lib/router/src/utils.ts +++ b/lib/router/src/utils.ts @@ -9,10 +9,11 @@ interface StoryData { const knownViewModesRegex = /(story|info)/; const splitPath = /\/([^/]+)\/([^/]+)?/; +// Remove punctuation https://gist.github.com/davidjrice/9d2af51100e41c6c4b4a export const sanitize = (string: string) => { return string .toLowerCase() - .replace(/[ '`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '-') + .replace(/[ ’–—―′¿'`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '-') .replace(/-+/g, '-') .replace(/^-+/, '') .replace(/-+$/, ''); From ceb414a66080ef598a395d6c5d9818844e6e4ca2 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 8 Mar 2019 17:51:46 +0800 Subject: [PATCH 3/3] Update snapshots --- .../__snapshots__/unicode.stories.storyshot | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/official-storybook/stories/core/__snapshots__/unicode.stories.storyshot diff --git a/examples/official-storybook/stories/core/__snapshots__/unicode.stories.storyshot b/examples/official-storybook/stories/core/__snapshots__/unicode.stories.storyshot new file mode 100644 index 000000000000..8d2102535065 --- /dev/null +++ b/examples/official-storybook/stories/core/__snapshots__/unicode.stories.storyshot @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Storyshots Core|Unicode Кнопки 1`] = ` +

+ нормальный +

+`; + +exports[`Storyshots Core|Unicode 바보 1`] = ` +

+ 🤷🏻‍♂️ +

+`; + +exports[`Storyshots Core|Unicode 😀 1`] = ` +

+ ❤️ +

+`;