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/misc/A.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import Link from "next/link";
7
import { join } from "path";
8
9
import basePath from "lib/base-path";
10
11
export default function A(props: any) {
12
const { href } = props;
13
if (href == null) {
14
return <a {...copyWithout(props, new Set(["external"]))} />;
15
}
16
if (href.includes("://") || href.startsWith("mailto:")) {
17
return (
18
<a
19
{...copyWithout(props, new Set(["external"]))}
20
target={"_blank"}
21
rel={"noopener"}
22
/>
23
);
24
}
25
if (props.external) {
26
const props2 = copyWithout(props, new Set(["external"]));
27
if (!href.startsWith(basePath)) {
28
// @ts-ignore
29
props2.href = join(basePath, href);
30
}
31
return <a {...props2} target={"_blank"} rel={"noopener"} />;
32
}
33
return (
34
<Link href={href} {...copyWithout(props, new Set(["external", "href"]))} />
35
);
36
}
37
38
function copyWithout(props, without: Set<string>) {
39
const props2 = {};
40
for (const key in props) {
41
if (!without.has(key)) {
42
props2[key] = props[key];
43
}
44
}
45
return props2;
46
}
47
48