Skip to content

Commit a9e1985

Browse files
mendrewsapegin
authored andcommitted
Fix: Encode section pages URLs if a section name has special symbols (styleguidist#1384)
Closes styleguidist#1332
1 parent 5ff72d8 commit a9e1985

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/client/utils/__tests__/getUrl.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ describe('getUrl', () => {
6969
expect(result).toBe('/styleguide/#/Documentation/FooBar');
7070
});
7171

72+
it('should return a route path with encoded name if name has inappropriate symbols', () => {
73+
const result = getUrl({ name: '@foo/components', slug, hashPath: ['Documentation'] }, loc);
74+
expect(result).toBe('/styleguide/#/Documentation/%40foo%2Fcomponents');
75+
});
76+
7277
it('should return a route path with a param id=foobar', () => {
7378
const result = getUrl({ name, slug, hashPath: ['Documentation'], id: true }, loc);
7479
expect(result).toBe('/styleguide/#/Documentation?id=foobar');

src/client/utils/__tests__/handleHash.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ describe('handleHash', () => {
4444
expect(result).toEqual(['FooBar', 'Component']);
4545
});
4646

47+
it('getHashAsArray should return array with an encoded component name', () => {
48+
const result = getHashAsArray('#/Documentation/Files/%40foo%2Fcomponents', routeHash);
49+
expect(result).toEqual(['Documentation', 'Files', '@foo/components']);
50+
});
51+
4752
it('getHashAsArray should return array without params', () => {
4853
const result = getHashAsArray('#/FooBar/Component?id=Example/Perfect', routeHash);
4954
expect(result).toEqual(['FooBar', 'Component']);

src/client/utils/getUrl.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ export default function getUrl(
2929
url += '?nochrome';
3030
}
3131

32+
const encodedName = encodeURIComponent(name);
33+
3234
if (anchor) {
3335
url += `#${slug}`;
3436
} else if (isolated || nochrome) {
35-
url += `#!/${name}`;
37+
url += `#!/${encodedName}`;
3638
}
3739

3840
if (hashPath) {
3941
if (!id) {
40-
hashPath = [...hashPath, name];
42+
hashPath = [...hashPath, encodedName];
4143
}
4244
url += `#/${hashPath.join('/')}`;
4345
}

src/client/utils/handleHash.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ export const getHash = (hash, prependHash) => {
5151
* @return {Array.<string>}
5252
*/
5353
export const getHashAsArray = (hash, prependHash) => {
54-
return getHash(hash, prependHash).split(separator);
54+
return trimParams(trimHash(hash, prependHash))
55+
.split(separator)
56+
.map(decodeURIComponent);
5557
};
5658

5759
/**

0 commit comments

Comments
 (0)