Skip to content

Commit

Permalink
Fix: Fix navigation to sections with numbers in the name (#1595)
Browse files Browse the repository at this point in the history
Fixes #1594
  • Loading branch information
mitsuruog committed May 26, 2020
1 parent b4c2845 commit 9d26371
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/client/utils/__tests__/getInfoFromHash.spec.ts
Expand Up @@ -35,4 +35,24 @@ describe('getInfoFromHash', () => {
targetIndex: undefined,
});
});

it('should extract target index when the URL ends with a number', () => {
const result = getInfoFromHash('#/Documentation/Files/Buttons/5');
expect(result).toEqual({
isolate: false,
hashArray: ['Documentation', 'Files', 'Buttons'],
targetName: 'Documentation',
targetIndex: 5,
});
});

it('should return a proper parsed result even though the hash starts with a number', () => {
const result = getInfoFromHash('#/1.Documentation');
expect(result).toEqual({
isolate: false,
hashArray: ['1.Documentation'],
targetName: '1.Documentation',
targetIndex: undefined,
});
});
});
11 changes: 5 additions & 6 deletions src/client/utils/getInfoFromHash.ts
@@ -1,8 +1,7 @@
import isNaN from 'lodash/isNaN';
import { hasInHash, getHashAsArray } from './handleHash';

function filterNumbers(item: string): boolean {
return isNaN(parseInt(item, 10)) && item !== '';
function hasDigitsOnly(item: string): boolean {
return item.match(/^\d+$/) !== null;
}

/**
Expand All @@ -25,12 +24,12 @@ export default function getInfoFromHash(
const shouldIsolate = hasInHash(hash, '#!/');
if (shouldIsolate || hasInHash(hash, '#/')) {
const hashArray = getHashAsArray(hash, shouldIsolate ? '#!/' : '#/');
const index = parseInt(hashArray[hashArray.length - 1], 10);
const targetHash = hashArray[hashArray.length - 1];
return {
isolate: shouldIsolate,
hashArray: hashArray.filter(filterNumbers),
hashArray: hashArray.filter(item => !hasDigitsOnly(item)),
targetName: hashArray[0],
targetIndex: isNaN(index) ? undefined : index,
targetIndex: hasDigitsOnly(targetHash) ? parseInt(targetHash, 10) : undefined,
};
}
return {};
Expand Down

0 comments on commit 9d26371

Please sign in to comment.