CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/share/proxy-input.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Alert, Input } from "antd";
7
import { useRouter } from "next/router";
8
import { useState } from "react";
9
10
import A from "components/misc/A";
11
import SiteName from "components/share/site-name";
12
13
export default function ProxyInput() {
14
const router = useRouter();
15
const [error, setError] = useState<string>("");
16
const [show, setShow] = useState<boolean>(false);
17
18
return (
19
<div style={{ margin: "15px 0" }}>
20
<A href="https://doc.cocalc.com/share.html">Publish what you create</A> in{" "}
21
<SiteName /> or{" "}
22
{show ? (
23
<>
24
paste a URL to a <A href="http://github.com/">GitHub</A> repository or{" "}
25
<A href="https://gist.github.com/">Gist</A>:
26
</>
27
) : (
28
<a onClick={() => setShow(true)}>
29
paste a URL to a GitHub repository or Gist.
30
</a>
31
)}
32
{show && (
33
<Input.Search
34
style={{ marginTop: "10px" }}
35
placeholder="URL to GitHub repository or Gist"
36
allowClear
37
enterButton="View GitHub Repository or Gist"
38
onSearch={(url) => {
39
try {
40
router.push(urlToProxyURL(url));
41
} catch (err) {
42
setError(`${err}`);
43
}
44
}}
45
/>
46
)}
47
{error && (
48
<Alert
49
style={{ marginTop: "15px" }}
50
type="error"
51
message={error}
52
showIcon
53
/>
54
)}
55
</div>
56
);
57
}
58
59
// INPUT: a URL to something on the internet
60
// OUTPUT: a URL on the share serve (without the http, host stuff)
61
// that proxies that input URL.
62
// The cases we treat are:
63
// - gist
64
// - github user or repo
65
// - general URL fallback, if none of the above apply
66
//
67
// NOTE: we implemented general URL's. HOWEVER spammers use this to
68
// automate creating large numbers of links from cocalc to their bullshit
69
// pages to improve their SEO ranking. Thus we restrict only to github.
70
//
71
function urlToProxyURL(url: string): string {
72
const { host, pathname } = new URL(url);
73
if (host == new URL(document.URL).host) {
74
// URL on this very server - just go to it
75
return url;
76
} else if (host == "github.com") {
77
return `/github${pathname}`;
78
} else if (host == "gist.github.com") {
79
return `/gist${pathname}`;
80
} else {
81
throw Error("The URL most be to content on github.com.");
82
}
83
}
84
85