Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/share/proxy-input.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Alert, Input } from "antd";6import { useRouter } from "next/router";7import { useState } from "react";89import A from "components/misc/A";10import SiteName from "components/share/site-name";1112export default function ProxyInput() {13const router = useRouter();14const [error, setError] = useState<string>("");15const [show, setShow] = useState<boolean>(false);1617return (18<div style={{ margin: "15px 0" }}>19<A href="https://doc.cocalc.com/share.html">Publish what you create</A> in{" "}20<SiteName /> or{" "}21{show ? (22<>23paste a URL to a <A href="http://github.com/">GitHub</A> repository or{" "}24<A href="https://gist.github.com/">Gist</A>:25</>26) : (27<a onClick={() => setShow(true)}>28paste a URL to a GitHub repository or Gist.29</a>30)}31{show && (32<Input.Search33style={{ marginTop: "10px" }}34placeholder="URL to GitHub repository or Gist"35allowClear36enterButton="View GitHub Repository or Gist"37onSearch={(url) => {38try {39router.push(urlToProxyURL(url));40} catch (err) {41setError(`${err}`);42}43}}44/>45)}46{error && (47<Alert48style={{ marginTop: "15px" }}49type="error"50message={error}51showIcon52/>53)}54</div>55);56}5758// INPUT: a URL to something on the internet59// OUTPUT: a URL on the share serve (without the http, host stuff)60// that proxies that input URL.61// The cases we treat are:62// - gist63// - github user or repo64// - general URL fallback, if none of the above apply65//66// NOTE: we implemented general URL's. HOWEVER spammers use this to67// automate creating large numbers of links from cocalc to their bullshit68// pages to improve their SEO ranking. Thus we restrict only to github.69//70function urlToProxyURL(url: string): string {71const { host, pathname } = new URL(url);72if (host == new URL(document.URL).host) {73// URL on this very server - just go to it74return url;75} else if (host == "github.com") {76return `/github${pathname}`;77} else if (host == "gist.github.com") {78return `/gist${pathname}`;79} else {80throw Error("The URL most be to content on github.com.");81}82}838485