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.
Files
outline/frontend/components/Editor/components/Link.js
Tom Moor 606dc69204 Link Toolbar Improves (#269)
* WIP

* Internal link handling

* Keyboard navigation of link select

* More constants

* Save relative urls.
Relative urls are atlas urls
2017-09-27 23:29:48 -04:00

39 lines
925 B
JavaScript

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