Skip to content
A utility to detect whether given link is internal link or external link
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Update config.yml Dec 7, 2018
.github/workflows
src fix fail test Dec 7, 2018
.gitignore initial commit Nov 2, 2018
.npmignore initial commit Nov 2, 2018
LICENSE Create LICENSE Nov 13, 2018
README.md
jest.config.js
package-lock.json
package.json
tsconfig.json Update tsconfig to exclude spec.ts Nov 2, 2018

README.md

is-internal-link

npm-badge GitHub issues GitHub license npm type definitions CircleCI Twitter

Installation

npm install --save is-internal-link

Usage

import { isInternalLink } from "is-internal-link";

or

import isInternalLink from "is-internal-link";
isInternalLink("https://www.google.com"); // false
isInternalLink("/page1"); // true

Test

npm run test

Why ?

I found my self doing this every time I started new project. That's why I decided to make this logic to abstract the logic and share it with the world

How does it fit in my App?

This is one example, if you're using React. And just want to have a single component. Instead of sometimes using <Link> and sometimes using <a>. Just create new Link component and use it everywhere.

Another pattern I usually use. Usually I add target="_blank" for external link. So every external link will be opened in new tab

import React from "react";

import { Link as ReactRouterLink } from "react-router-dom";
import { isInternalLink } from "is-internal-link";

const Link = ({ children, to, activeClassName, ...other }) => {
  if (isInternalLink(to)) {
    return (
      <ReactRouterLink to={to} activeClassName={activeClassName} {...other}>
        {children}
      </ReactRouterLink>
    );
  }
  return (
    <a href={to} target="_blank" {...other}>
      {children}
    </a>
  );
};

export default Link;

Contributors

Contributing

Please do not hesitate to submit an issue or pull request.

Question?

Submit an issue || ping me @muhajirframe on twitter

License

MIT

Enjoy

You can’t perform that action at this time.