Skip to content

Commit c272f54

Browse files
authored
fix internal (repo) links in README, improve author rendering (#2089)
1 parent dbef93e commit c272f54

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

components/Package/PackageAuthor.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { type NpmUser } from '~/types';
88
import tw from '~/util/tailwind';
99

1010
type Props = {
11-
author?: NpmUser;
11+
author?: NpmUser | string;
1212
compact?: boolean;
1313
};
1414

@@ -25,13 +25,37 @@ export default function PackageAuthor({ author, compact }: Props) {
2525
);
2626
}
2727

28+
if (typeof author === 'string') {
29+
if (author.includes('github.com/')) {
30+
const ghUsername = extractGitHubUsername(author);
31+
32+
return (
33+
<View>
34+
<A href={`https://github.com/${ghUsername}`} style={authorContainerStyle}>
35+
<UserAvatar src={`https://github.com/${ghUsername}.png`} alt={`${ghUsername} avatar`} />
36+
<View>
37+
<Caption style={labelStyle}>{ghUsername}</Caption>
38+
<span style={sublabelStyle}>
39+
{author.replace(/\s*\(?https?:\/\/\S+\)?\s*/g, '').trim()}
40+
</span>
41+
</View>
42+
</A>
43+
</View>
44+
);
45+
}
46+
return (
47+
<View>
48+
<Label>{author}</Label>
49+
</View>
50+
);
51+
}
52+
2853
const potentialHref = author.url ?? author.email;
2954

3055
// URL
3156
if (potentialHref && !potentialHref.includes('@')) {
3257
if (potentialHref.includes('github.com/')) {
33-
const [, potentialGHUsername] = potentialHref.split('github.com/');
34-
const ghUsername = potentialGHUsername.replace(/[<>()]/g, '');
58+
const ghUsername = extractGitHubUsername(potentialHref);
3559
const validName = getValidName(author.name);
3660

3761
return (
@@ -107,3 +131,8 @@ function getValidName(potentialName: string): string {
107131
.join(' ');
108132
return cleanName.length ? cleanName : potentialName.replace(/[<>()]/g, '');
109133
}
134+
135+
function extractGitHubUsername(author: string): string | null {
136+
const [, potentialGHUsername] = author.split('github.com/');
137+
return potentialGHUsername ? potentialGHUsername.replace(/[<>()]/g, '') : null;
138+
}

components/Package/ReadmeBox.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ export default function ReadmeBox({ packageName, githubUrl, isTemplate, loader =
102102
hr: () => null,
103103
a: (props: any) => {
104104
if (props.href && !props.href.startsWith('//')) {
105+
if (!props.href.startsWith('http')) {
106+
return <A {...props} href={`${githubUrl}/blob/HEAD/${props.href}`} />;
107+
}
105108
return <A {...props} />;
106109
}
107110
return <span>{props.children}</span>;

scenes/PackageOverviewScene.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import dynamic from 'next/dynamic';
33
import { useMemo } from 'react';
44
import { View } from 'react-native';
55

6-
import { A, H6, Label, useLayout } from '~/common/styleguide';
6+
import { A, H6, useLayout } from '~/common/styleguide';
77
import ContentContainer from '~/components/ContentContainer';
88
import MetaData from '~/components/Library/MetaData';
99
import TrendingMark from '~/components/Library/TrendingMark';
@@ -83,13 +83,7 @@ export default function PackageOverviewScene({
8383
<>
8484
<H6 style={tw`text-[16px] mt-3 text-secondary`}>Author</H6>
8585
<View style={tw`items-start`}>
86-
{typeof author === 'string' ? (
87-
<View>
88-
<Label>{author ?? 'Unknown'}</Label>
89-
</View>
90-
) : (
91-
<PackageAuthor author={author} />
92-
)}
86+
<PackageAuthor author={author} />
9387
</View>
9488
</>
9589
)}
@@ -142,13 +136,7 @@ export default function PackageOverviewScene({
142136
<>
143137
<H6 style={tw`text-[16px] text-secondary`}>Author</H6>
144138
<View style={tw`items-start`}>
145-
{typeof author === 'string' ? (
146-
<View>
147-
<Label>{author ?? 'Unknown'}</Label>
148-
</View>
149-
) : (
150-
<PackageAuthor author={author} />
151-
)}
139+
<PackageAuthor author={author} />
152140
</View>
153141
</>
154142
)}

0 commit comments

Comments
 (0)