2020-05-14 12:23:27 +02:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2020-06-19 11:50:58 +02:00
|
|
|
import React, { FC } from "react";
|
|
|
|
|
import { Link, useLocation } from "react-router-dom";
|
2020-05-19 12:59:02 +02:00
|
|
|
import ExternalLink from "./navigation/ExternalLink";
|
2020-06-19 11:50:58 +02:00
|
|
|
import { withContextPath } from "./urls";
|
2020-05-14 12:23:27 +02:00
|
|
|
|
2020-05-19 12:59:02 +02:00
|
|
|
const externalLinkRegex = new RegExp("^http(s)?://");
|
|
|
|
|
export const isExternalLink = (link: string) => {
|
|
|
|
|
return externalLinkRegex.test(link);
|
|
|
|
|
};
|
2020-05-15 09:54:20 +02:00
|
|
|
|
2020-05-19 12:59:02 +02:00
|
|
|
export const isAnchorLink = (link: string) => {
|
|
|
|
|
return link.startsWith("#");
|
|
|
|
|
};
|
2020-05-14 12:23:27 +02:00
|
|
|
|
2020-06-19 11:50:58 +02:00
|
|
|
export const isInternalScmRepoLink = (link: string) => {
|
|
|
|
|
return link.startsWith("/repo/");
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-19 12:59:02 +02:00
|
|
|
const linkWithProtcolRegex = new RegExp("^[a-z]+:");
|
|
|
|
|
export const isLinkWithProtocol = (link: string) => {
|
|
|
|
|
return linkWithProtcolRegex.test(link);
|
|
|
|
|
};
|
2020-05-14 12:23:27 +02:00
|
|
|
|
2020-05-19 15:51:33 +02:00
|
|
|
const join = (left: string, right: string) => {
|
|
|
|
|
if (left.endsWith("/") && right.startsWith("/")) {
|
|
|
|
|
return left + right.substring(1);
|
|
|
|
|
} else if (!left.endsWith("/") && !right.startsWith("/")) {
|
|
|
|
|
return left + "/" + right;
|
2020-05-14 12:23:27 +02:00
|
|
|
}
|
2020-05-19 15:51:33 +02:00
|
|
|
return left + right;
|
|
|
|
|
};
|
2020-05-14 12:23:27 +02:00
|
|
|
|
2020-05-20 08:39:06 +02:00
|
|
|
const normalizePath = (path: string) => {
|
|
|
|
|
const stack = [];
|
|
|
|
|
const parts = path.split("/");
|
|
|
|
|
for (const part of parts) {
|
|
|
|
|
if (part === "..") {
|
|
|
|
|
stack.pop();
|
|
|
|
|
} else if (part !== ".") {
|
2020-06-19 11:50:58 +02:00
|
|
|
stack.push(part);
|
2020-05-20 08:39:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-19 11:50:58 +02:00
|
|
|
const normalizedPath = stack.join("/");
|
2020-05-20 11:18:58 +02:00
|
|
|
if (normalizedPath.startsWith("/")) {
|
|
|
|
|
return normalizedPath;
|
|
|
|
|
}
|
|
|
|
|
return "/" + normalizedPath;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isAbsolute = (link: string) => {
|
|
|
|
|
return link.startsWith("/");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isSubDirectoryOf = (basePath: string, currentPath: string) => {
|
|
|
|
|
return currentPath.startsWith(basePath);
|
2020-05-20 08:39:06 +02:00
|
|
|
};
|
|
|
|
|
|
2020-05-19 15:51:33 +02:00
|
|
|
export const createLocalLink = (basePath: string, currentPath: string, link: string) => {
|
2020-06-19 11:50:58 +02:00
|
|
|
if (isInternalScmRepoLink(link)) {
|
|
|
|
|
return link;
|
|
|
|
|
}
|
2020-05-20 11:18:58 +02:00
|
|
|
if (isAbsolute(link)) {
|
2020-05-19 15:51:33 +02:00
|
|
|
return join(basePath, link);
|
|
|
|
|
}
|
2020-05-20 11:18:58 +02:00
|
|
|
if (!isSubDirectoryOf(basePath, currentPath)) {
|
2020-05-19 15:51:33 +02:00
|
|
|
return join(basePath, link);
|
|
|
|
|
}
|
|
|
|
|
let path = currentPath;
|
|
|
|
|
if (currentPath.endsWith("/")) {
|
|
|
|
|
path = currentPath.substring(0, currentPath.length - 2);
|
2020-05-14 12:23:27 +02:00
|
|
|
}
|
2020-05-19 15:51:33 +02:00
|
|
|
const lastSlash = path.lastIndexOf("/");
|
|
|
|
|
if (lastSlash < 0) {
|
|
|
|
|
path = "";
|
|
|
|
|
} else {
|
|
|
|
|
path = path.substring(0, lastSlash);
|
|
|
|
|
}
|
2020-05-20 08:39:06 +02:00
|
|
|
return normalizePath(join(path, link));
|
2020-05-19 15:51:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type LinkProps = {
|
|
|
|
|
href: string;
|
|
|
|
|
};
|
2020-05-14 12:23:27 +02:00
|
|
|
|
2020-05-19 15:51:33 +02:00
|
|
|
type Props = LinkProps & {
|
|
|
|
|
base: string;
|
2020-05-19 12:59:02 +02:00
|
|
|
};
|
2020-05-14 12:23:27 +02:00
|
|
|
|
2020-06-19 11:50:58 +02:00
|
|
|
const MarkdownLinkRenderer: FC<Props> = ({ href, base, children }) => {
|
2020-05-15 09:54:20 +02:00
|
|
|
const location = useLocation();
|
2020-05-19 12:59:02 +02:00
|
|
|
if (isExternalLink(href)) {
|
|
|
|
|
return <ExternalLink to={href}>{children}</ExternalLink>;
|
2020-05-19 15:51:33 +02:00
|
|
|
} else if (isLinkWithProtocol(href)) {
|
2020-05-19 12:59:02 +02:00
|
|
|
return <a href={href}>{children}</a>;
|
2020-05-19 15:51:33 +02:00
|
|
|
} else if (isAnchorLink(href)) {
|
|
|
|
|
return <a href={withContextPath(location.pathname) + href}>{children}</a>;
|
2020-05-19 12:59:02 +02:00
|
|
|
} else {
|
2020-05-19 15:51:33 +02:00
|
|
|
const localLink = createLocalLink(base, location.pathname, href);
|
|
|
|
|
return <Link to={localLink}>{children}</Link>;
|
2020-05-15 09:54:20 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-19 15:51:33 +02:00
|
|
|
// we use a factory method, because react-markdown does not pass
|
|
|
|
|
// base as prop down to our link component.
|
|
|
|
|
export const create = (base: string): FC<LinkProps> => {
|
|
|
|
|
return props => <MarkdownLinkRenderer base={base} {...props} />;
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-15 09:54:20 +02:00
|
|
|
export default MarkdownLinkRenderer;
|