Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
prevent prepending path to absolute urls
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Jan 24, 2024
1 parent 6e15c19 commit 05ee41b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/components/RelativeLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import PropTypes from 'prop-types';
import React, {useContext} from 'react';
import {Button, Link} from '@chakra-ui/react';
import {Link as GatsbyLink, graphql, useStaticQuery} from 'gatsby';
import {PathContext, isExternalUrl} from '../utils';
import {PathContext, isAbsoluteUrl, isExternalUrl} from '../utils';
import {isAbsolute, resolve} from 'path';

export const PrimaryLink = props => (
Expand Down Expand Up @@ -44,7 +44,8 @@ function useLinkProps(href) {
};
}

const to = isAbsolute(href) ? href : resolve(path, href);
const to =
isAbsolute(href) || isAbsoluteUrl(href) ? href : resolve(path, href);
return {
as: GatsbyLink,
to: to.replace(new RegExp(`^${site.pathPrefix}`), '')
Expand Down
5 changes: 3 additions & 2 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export const NavContext = createContext();
export const PathContext = createContext();

const urlRegex = /^https?:\/\//;
export const isUrl = string => urlRegex.test(string);
export const isAbsoluteUrl = string => urlRegex.test(string);

const internalUrlRegex = /^https?:\/\/www.apollographql.com/;
export const isInternalUrl = string => internalUrlRegex.test(string);

export const isExternalUrl = string => isUrl(string) && !isInternalUrl(string);
export const isExternalUrl = string =>
isAbsoluteUrl(string) && !isInternalUrl(string);

export const isPathActive = (path, uri) =>
!relative(
Expand Down

0 comments on commit 05ee41b

Please sign in to comment.