Skip to content

Commit

Permalink
fix: library readme links (#299)
Browse files Browse the repository at this point in the history
* fix library readme links

* fix local link
  • Loading branch information
itwillwork authored Nov 6, 2024
1 parent ca18f08 commit cf70c66
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/components/Library/Library.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Props = {
lib: Lib;
};

const ABSOLUTE_LINK_REG_EXP = /^http/;
const HASH_REG_EXP = /^#/;

export const Library: React.FC<Props> = ({lib}) => {
const {t, i18n} = useTranslation();

Expand Down Expand Up @@ -98,6 +101,24 @@ export const Library: React.FC<Props> = ({lib}) => {
const isPrimary = lib.config.primary;
const maintainers = React.useMemo(() => getMaintainers(lib), []);

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

const githubId = lib.config?.githubId || null;
if (!githubId) {
return link;
}

const githubRepoUrl = `${GITHUB_URL}${githubId}/blob/main/`;
const absoluteUrl = new URL(link, githubRepoUrl);
return absoluteUrl.toString();
},
[lib.config?.githubId],
);

return (
<div className={b()}>
<Grid className={b('header-grid')}>
Expand Down Expand Up @@ -205,6 +226,7 @@ export const Library: React.FC<Props> = ({lib}) => {
: lib.data.changelog
}
absoluteImgPath={`https://raw.githubusercontent.com/${lib.config.githubId}/${lib.config.mainBranch}/`}
rewriteLinks={rewriteLinks}
/>
</div>
</div>
Expand Down

0 comments on commit cf70c66

Please sign in to comment.