Skip to content

Commit 9d26371

Browse files
authored
Fix: Fix navigation to sections with numbers in the name (styleguidist#1595)
Fixes styleguidist#1594
1 parent b4c2845 commit 9d26371

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/client/utils/__tests__/getInfoFromHash.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,24 @@ describe('getInfoFromHash', () => {
3535
targetIndex: undefined,
3636
});
3737
});
38+
39+
it('should extract target index when the URL ends with a number', () => {
40+
const result = getInfoFromHash('#/Documentation/Files/Buttons/5');
41+
expect(result).toEqual({
42+
isolate: false,
43+
hashArray: ['Documentation', 'Files', 'Buttons'],
44+
targetName: 'Documentation',
45+
targetIndex: 5,
46+
});
47+
});
48+
49+
it('should return a proper parsed result even though the hash starts with a number', () => {
50+
const result = getInfoFromHash('#/1.Documentation');
51+
expect(result).toEqual({
52+
isolate: false,
53+
hashArray: ['1.Documentation'],
54+
targetName: '1.Documentation',
55+
targetIndex: undefined,
56+
});
57+
});
3858
});

src/client/utils/getInfoFromHash.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import isNaN from 'lodash/isNaN';
21
import { hasInHash, getHashAsArray } from './handleHash';
32

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

87
/**
@@ -25,12 +24,12 @@ export default function getInfoFromHash(
2524
const shouldIsolate = hasInHash(hash, '#!/');
2625
if (shouldIsolate || hasInHash(hash, '#/')) {
2726
const hashArray = getHashAsArray(hash, shouldIsolate ? '#!/' : '#/');
28-
const index = parseInt(hashArray[hashArray.length - 1], 10);
27+
const targetHash = hashArray[hashArray.length - 1];
2928
return {
3029
isolate: shouldIsolate,
31-
hashArray: hashArray.filter(filterNumbers),
30+
hashArray: hashArray.filter(item => !hasDigitsOnly(item)),
3231
targetName: hashArray[0],
33-
targetIndex: isNaN(index) ? undefined : index,
32+
targetIndex: hasDigitsOnly(targetHash) ? parseInt(targetHash, 10) : undefined,
3433
};
3534
}
3635
return {};

0 commit comments

Comments
 (0)