Skip to content

Commit

Permalink
Fix: Encode section pages URLs if a section name has special symbols (#…
Browse files Browse the repository at this point in the history
…1384)

Closes #1332
  • Loading branch information
mendrew authored and sapegin committed Jun 15, 2019
1 parent 5ff72d8 commit a9e1985
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/client/utils/__tests__/getUrl.spec.js
Expand Up @@ -69,6 +69,11 @@ describe('getUrl', () => {
expect(result).toBe('/styleguide/#/Documentation/FooBar');
});

it('should return a route path with encoded name if name has inappropriate symbols', () => {
const result = getUrl({ name: '@foo/components', slug, hashPath: ['Documentation'] }, loc);
expect(result).toBe('/styleguide/#/Documentation/%40foo%2Fcomponents');
});

it('should return a route path with a param id=foobar', () => {
const result = getUrl({ name, slug, hashPath: ['Documentation'], id: true }, loc);
expect(result).toBe('/styleguide/#/Documentation?id=foobar');
Expand Down
5 changes: 5 additions & 0 deletions src/client/utils/__tests__/handleHash.spec.js
Expand Up @@ -44,6 +44,11 @@ describe('handleHash', () => {
expect(result).toEqual(['FooBar', 'Component']);
});

it('getHashAsArray should return array with an encoded component name', () => {
const result = getHashAsArray('#/Documentation/Files/%40foo%2Fcomponents', routeHash);
expect(result).toEqual(['Documentation', 'Files', '@foo/components']);
});

it('getHashAsArray should return array without params', () => {
const result = getHashAsArray('#/FooBar/Component?id=Example/Perfect', routeHash);
expect(result).toEqual(['FooBar', 'Component']);
Expand Down
6 changes: 4 additions & 2 deletions src/client/utils/getUrl.js
Expand Up @@ -29,15 +29,17 @@ export default function getUrl(
url += '?nochrome';
}

const encodedName = encodeURIComponent(name);

if (anchor) {
url += `#${slug}`;
} else if (isolated || nochrome) {
url += `#!/${name}`;
url += `#!/${encodedName}`;
}

if (hashPath) {
if (!id) {
hashPath = [...hashPath, name];
hashPath = [...hashPath, encodedName];
}
url += `#/${hashPath.join('/')}`;
}
Expand Down
4 changes: 3 additions & 1 deletion src/client/utils/handleHash.js
Expand Up @@ -51,7 +51,9 @@ export const getHash = (hash, prependHash) => {
* @return {Array.<string>}
*/
export const getHashAsArray = (hash, prependHash) => {
return getHash(hash, prependHash).split(separator);
return trimParams(trimHash(hash, prependHash))
.split(separator)
.map(decodeURIComponent);
};

/**
Expand Down

0 comments on commit a9e1985

Please sign in to comment.