{"size": 1280, "ext": "js", "max_stars_repo_path": "src/components/Link.js", "max_stars_repo_name": "FacePlace/sergey-gray", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/Link.js", "max_issues_repo_name": "FacePlace/sergey-gray", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/Link.js", "max_forks_repo_name": "FacePlace/sergey-gray", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 17.2972972973, "max_line_length": 91, "alphanum_fraction": 0.53828125}
import NextLink from 'next/link';
import styled from 'styled-components';

export const ExternalLink = styled.a`
  display: contents; /* to prevent this element from screwing up its children components */
`;

const LinkTemplate = ({
  children,
  href,
  target,
  rel,
  type,
  ...rest
}) =>
{
  let linkType = type || '';

  if (typeof href !== 'object' && !linkType)
  {
    if (href.match(/(^http|^mailto)/i) || target === '_blank')
    {
      linkType = 'external';
    }
    else
    {
      linkType = 'internal';
    }
  }
  else if (typeof href === 'object')
  {
    linkType = 'internal';
  }

  if (typeof target === 'undefined')
  {
    target = linkType === 'external' ? '_blank' : undefined;
  }

  const Rel = target === '_blank' ? 'noopener noreferrer' : rel;

  if (linkType === 'external')
  {
    if (typeof href === 'object') throw new Error('Wrong link type');

    return (
      <ExternalLink
        href={href}
        target={target}
        rel={Rel}
        {...rest}
      >
        {children}
      </ExternalLink>
    );
  }

  return (
    <NextLink
      href={href}
      passHref
      rel={Rel}
    >
      <ExternalLink
        {...rest}
      >
        {children}
      </ExternalLink>
    </NextLink>
  );
};

export default LinkTemplate;
