Skip to content

Commit

Permalink
fix library page local anchors (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
itwillwork authored Nov 6, 2024
1 parent cf70c66 commit da064f3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/components/Library/Library.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import storybookIcon from '../../assets/icons/storybook.svg';
import versionIcon from '../../assets/icons/version.svg';
import {ContributorList} from '../../components/ContributorList';
import {Link} from '../../components/Link';
import {Lib, block, getLocaleLink, getMaintainers} from '../../utils';
import {Lib, block, getAnchor, getLocaleLink, getMaintainers} from '../../utils';
import {MDXRenderer} from '../MDXRenderer/MDXRenderer';

import './Library.scss';
Expand Down Expand Up @@ -103,7 +103,11 @@ export const Library: React.FC<Props> = ({lib}) => {

const rewriteLinks = React.useCallback(
(link: string) => {
if (ABSOLUTE_LINK_REG_EXP.test(link) || HASH_REG_EXP.test(link)) {
if (HASH_REG_EXP.test(link)) {
return `#${getAnchor(link)}`;
}

if (ABSOLUTE_LINK_REG_EXP.test(link)) {
return link;
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/anchor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const getAnchor = (link: string): string => {
return link
.toLowerCase()
.replace(/[^a-z0-9 ]/g, '')
.replace(/[ ]/g, '-');
};
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './components';
export * from './browser';
export * from './i18next';
export * from './locale-link';
export * from './anchor';

0 comments on commit da064f3

Please sign in to comment.