Skip to content

Commit af68ae1

Browse files
Copilotdwjohnston
andcommitted
Complete Stack Overflow link implementation with documentation
Co-authored-by: dwjohnston <[email protected]>
1 parent b330e43 commit af68ae1

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Display Github permalinks as codeblocks.
44

55
Display Github issue links.
66

7+
Display Stack Overflow question links.
8+
79
![screenshot of the tool in action - dark mode ](./screenshot-permalink-dark.png)
810
![screenshot of the tool in action - light mode ](./screenshot-permalink-light.png)
911
![screenshot of the tool in action - dark mode ](./screenshot-issuelink-dark.png)
@@ -27,9 +29,9 @@ This package is compatible with Next 13+ and the components can be used as RSCs
2729

2830
Three variants of each component are exported
2931

30-
- GithubPermalink/GithubIssueLink - Client component - It fetches the data as on the client in a useEffect. ie. Data won't be retrieved until application has loaded in user's browser.
31-
- GithubPermalinkBase/GithubIssueLinkBase - this is the base component - it does no data fetching on its own.
32-
- GithubPermalinkRsc/GithubIssueLinkRsc - This is an RSC.
32+
- GithubPermalink/GithubIssueLink/StackOverflowLink - Client component - It fetches the data as on the client in a useEffect. ie. Data won't be retrieved until application has loaded in user's browser.
33+
- GithubPermalinkBase/GithubIssueLinkBase/StackOverflowLinkBase - this is the base component - it does no data fetching on its own.
34+
- GithubPermalinkRsc/GithubIssueLinkRsc/StackOverflowLinkRsc - This is an RSC.
3335

3436

3537

@@ -112,6 +114,26 @@ export function MyApp() {
112114
}
113115
```
114116

117+
## Stack Overflow Links
118+
119+
### Usage
120+
```jsx
121+
import { StackOverflowLink } from 'react-github-permalink';
122+
import "react-github-permalink/dist/github-permalink.css"; // Or provide your own styles
123+
124+
export function MyApp() {
125+
return <StackOverflowLink questionLink='https://stackoverflow.com/questions/64572466/how-to-use-react-context-in-typescript' />
126+
}
127+
```
128+
129+
StackOverflowLink also has an inline variant:
130+
131+
```jsx
132+
export function MyApp() {
133+
return <StackOverflowLink questionLink='https://stackoverflow.com/questions/64572466/how-to-use-react-context-in-typescript' variant="inline"/>
134+
}
135+
```
136+
115137
## Rate Limits and Authentication
116138

117139
By default the components make unauthenticated requests against Github's API. The rate limit for such requests is 60/hour and only publicly visible repositories are available.
@@ -126,6 +148,7 @@ The global configuration object has this signature
126148
type BaseConfiguration = {
127149
getDataFn: (permalink: string, githubToken?: string | undefined, onError?: ((err: unknown) => void) | undefined) => Promise<GithubPermalinkDataResponse>;
128150
getIssueFn: (issueLink: string, githubToken?: string | undefined, onError?: ((err: unknown) => void) | undefined) => Promise<GithubIssueLinkDataResponse>;
151+
getStackOverflowFn: (questionLink: string, onError?: ((err: unknown) => void) | undefined) => Promise<StackOverflowLinkDataResponse>;
129152
githubToken: string | undefined;
130153
onError: ((e: unknown) => void) | undefined;
131154
}

src/library/StackOverflowLink/StackOverflowLinkBase.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { PropsWithChildren } from "react";
22
import { StackOverflowSvg } from "../StackOverflowSvg/StackOverflowSvg";
33
import { StackOverflowLinkDataResponse } from "../config/GithubPermalinkContext";
44
import { ErrorMessages } from "../ErrorMessages/ErrorMessages";
5-
import { Inline } from "../common/Inline/Inline";
65

76

87
export type StackOverflowLinkBaseProps = {
@@ -12,17 +11,25 @@ export type StackOverflowLinkBaseProps = {
1211
variant?: "inline" | "block"
1312
}
1413

14+
function StackOverflowInline(props: {
15+
href: string;
16+
text: string;
17+
}) {
1518

19+
const {href, text} = props;
20+
return <a href={href} className = "rgp-base react-stackoverflow-link-inline">
21+
<StackOverflowSvg/> <span>{text}</span></a>
22+
}
1623

1724
export function StackOverflowLinkBase(props: StackOverflowLinkBaseProps) {
1825
const { data, variant ="block", questionLink} = props;
1926

2027
if (variant === "inline"){
2128
if(data.status === "ok"){
22-
return <Inline href={questionLink} text={`stackoverflow.com/q/${data.questionId}: ${data.questionTitle}`}/>
29+
return <StackOverflowInline href={questionLink} text={`stackoverflow.com/q/${data.questionId}: ${data.questionTitle}`}/>
2330
}
2431
else {
25-
return <Inline href={questionLink} text={questionLink}/>
32+
return <StackOverflowInline href={questionLink} text={questionLink}/>
2633
}
2734
}
2835

0 commit comments

Comments
 (0)