This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/app/components/Editor/components/Link.js

52 lines
1.0 KiB
JavaScript

// @flow
import React from 'react';
import { Link as InternalLink } from 'react-router-dom';
import type { SlateNodeProps } from '../types';
function getPathFromUrl(href: string) {
if (href[0] === '/') return href;
try {
const parsed = new URL(href);
return parsed.pathname;
} catch (err) {
return '';
}
}
function isInternalUrl(href: string) {
if (href[0] === '/') return true;
try {
const outline = new URL(BASE_URL);
const parsed = new URL(href);
return parsed.hostname === outline.hostname;
} catch (err) {
return false;
}
}
export default function Link({
attributes,
node,
children,
readOnly,
}: SlateNodeProps) {
const href = node.data.get('href');
const path = getPathFromUrl(href);
if (isInternalUrl(href) && readOnly) {
return (
<InternalLink {...attributes} to={path}>
{children}
</InternalLink>
);
} else {
return (
<a {...attributes} href={readOnly ? href : undefined} target="_blank">
{children}
</a>
);
}
}