-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithubPermalinkBase.tsx
More file actions
92 lines (64 loc) · 3.04 KB
/
GithubPermalinkBase.tsx
File metadata and controls
92 lines (64 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { GithubPermalinkDataResponse, } from "../config/GithubPermalinkContext";
import { ErrorMessages } from "../ErrorMessages/ErrorMessages";
import { GithubSvg } from "../GithubSvg/GithubSvg";
import { PropsWithChildren } from "react";
import { SyntaxHighlight } from "../SyntaxHighlight/SyntaxHighlight";
import { formatForLineExclusions } from "./formatLineExclusions";
import { CopyButton } from "../common/CopyButton/CopyButton";
import { getLanguageFromPath } from "../utils/getLanguageFromPath";
import { AvailableLanguagesPrism } from "../SyntaxHighlight/availableLanguagesPrism";
export type GithubPermalinkBaseProps = {
className?: string;
permalink: string;
excludeLines?: Array<[from: number, to: number]>;
excludeText?: string;
data: GithubPermalinkDataResponse;
language?: AvailableLanguagesPrism;
}
export function GithubPermalinkBase(props: GithubPermalinkBaseProps) {
const { data, permalink, excludeLines, excludeText = "<snip>", } = props;
if (data.status === "ok") {
const formatedLineExclusions = formatForLineExclusions(data, excludeLines);
const language = props.language ?? getLanguageFromPath(data.path);
const clipboard = formatedLineExclusions.reduce((acc, cur) => {
if (cur.isExclude) {
return acc + "\n";
}
return acc + "\n" + cur.lines.join("\n");
}, '')
return <GithubPermalinkInner {...props} clipboard={clipboard} header={<>
<a href={permalink} className="file-link">{`${data.owner}/${data.repo}/${data.path}`}</a>
<p>{data.lineFrom === data.lineTo ? <>Line {data.lineFrom}</> : <>Lines {data.lineFrom} to {data.lineTo}</>} in <a className="commit-link" href={data.commitUrl}>{data.commit.slice(0, 7)}</a></p>
</>}>
{formatedLineExclusions.map((v) => {
if (v.isExclude) {
return <SyntaxHighlight className="hide-line-numbers" text={excludeText} startingLineNumber={v.from} language={language} key={v.from} />
}
return <SyntaxHighlight text={v.lines.join("\n")} startingLineNumber={v.from} language={language} key={v.from} />
})}
</GithubPermalinkInner>
}
return <GithubPermalinkInner {...props}>
<ErrorMessages data={data} />
</GithubPermalinkInner>
}
function GithubPermalinkInner(props: PropsWithChildren<{
header?: React.ReactNode
clipboard?: string;
} & GithubPermalinkBaseProps>) {
const { clipboard } = props;
return <div className={`rgp-base react-github-permalink ${props.className ?? ''}`}>
<div className="header">
<div>
<GithubSvg />
</div>
<div className="link-wrapper">
{props.header ?? <a href={props.permalink} className="file-link">{props.permalink}</a>}
</div>
{clipboard && <div className="copy-button-container">
<CopyButton clipboard={clipboard} />
</div>}
</div>
{props.children}
</div>
}