diff --git a/src/components/Library/Library.tsx b/src/components/Library/Library.tsx index d7fef9350590..2e8effc8f269 100644 --- a/src/components/Library/Library.tsx +++ b/src/components/Library/Library.tsx @@ -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'; @@ -103,7 +103,11 @@ export const Library: React.FC = ({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; } diff --git a/src/utils/anchor.ts b/src/utils/anchor.ts new file mode 100644 index 000000000000..7e491842d6b6 --- /dev/null +++ b/src/utils/anchor.ts @@ -0,0 +1,6 @@ +export const getAnchor = (link: string): string => { + return link + .toLowerCase() + .replace(/[^a-z0-9 ]/g, '') + .replace(/[ ]/g, '-'); +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index 2ce65f492f2c..c23c306eee82 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -6,3 +6,4 @@ export * from './components'; export * from './browser'; export * from './i18next'; export * from './locale-link'; +export * from './anchor';