Skip to content

Commit 4fe0087

Browse files
Initial Release v1.0.0
0 parents  commit 4fe0087

17 files changed

+5376
-0
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
indent_style = space
8+
quote_type = single
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.eslintrc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root": true,
3+
"env": {
4+
"es6": true,
5+
"node": true,
6+
"mocha": true
7+
},
8+
"extends": [
9+
"eslint:recommended"
10+
],
11+
"overrides": [
12+
{
13+
"files": [
14+
"*.ts",
15+
"*.tsx"
16+
],
17+
"parser": "@typescript-eslint/parser",
18+
"plugins": [
19+
"@typescript-eslint"
20+
],
21+
"extends": [
22+
"plugin:@typescript-eslint/recommended"
23+
]
24+
}
25+
]
26+
}

.gitattributes

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Set the default behavior, in case people don't have `core.autocrlf` set.
2+
* text=auto
3+
4+
# Declare files that will always have LF line endings on checkout.
5+
*.ts text eol=lf
6+
7+
# Remove files for archives generated using `git archive`.
8+
.* export-ignore
9+
appveyor.yml export-ignore
10+
package-lock.json export-ignore
11+
src export-ignore
12+
test export-ignore
13+
tsconfig.json export-ignore
14+
types/index.test-d.ts export-ignore

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
node_modules
3+
npm-debug.log
4+
yarn-error.log

.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
branches:
2+
only:
3+
- main
4+
dist: xenial
5+
language: node_js
6+
node_js:
7+
- 12
8+
- 13
9+
- 14
10+
- node
11+
os:
12+
- linux
13+
- osx
14+
install:
15+
npm install --dev
16+
script:
17+
- npm run lint
18+
- npm run test

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 TrunkCode
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# detect-external-link
2+
3+
[![NPM version][npm-image]][npm-url]
4+
[![Downloads][downloads-image]][npm-url]
5+
[![Build Status][travis-image]][travis-url]
6+
[![AppVeyor Build Status][appveyor-image]][appveyor-url]
7+
8+
Node.js plugin that analyzes the given link and determines if the URL is internal or external. To ensure accuracy, please include a list of specified hosts that should be marked as internal. This way, the plugin can effectively differentiate between internal and external links based on the provided hosts list.
9+
10+
## Install
11+
12+
Via `npm`
13+
14+
```bash
15+
npm install detect-external-link
16+
```
17+
18+
Via Yarn
19+
20+
```bash
21+
yarn add detect-external-link
22+
```
23+
24+
## Usage
25+
26+
```javascript
27+
const detectExternalLink = require('detect-external-link').default;
28+
29+
const link = 'http://test.example2.com/test/abc/';
30+
const linkOptions = {
31+
'hosts': [
32+
'http://example.com',
33+
'http://test.example.com'
34+
]
35+
};
36+
37+
console.log(detectExternalLink(link, linkOptions));
38+
```
39+
40+
## Parameters
41+
42+
| Attributes | Type | Required | Default | Description |
43+
|:----------:|:-----:|:--------:|:-------:|:------------------------------------------:|
44+
| hosts | Array | Yes | [] | Site hostname(s) to detect external links. |
45+
46+
[npm-image]: https://img.shields.io/npm/v/detect-external-link.svg
47+
[npm-url]: https://www.npmjs.com/package/detect-external-link
48+
[downloads-image]: https://img.shields.io/npm/dt/detect-external-link.svg
49+
50+
[travis-image]: https://api.travis-ci.com/trunkcode/detect-external-link.svg?branch=main
51+
[travis-url]: https://travis-ci.com/trunkcode/detect-external-link
52+
53+
[appveyor-url]: https://ci.appveyor.com/project/trunkcode/detect-external-link
54+
[appveyor-image]: https://img.shields.io/appveyor/ci/trunkcode/detect-external-link.svg?label=appveyor

appveyor.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: "{build}"
2+
3+
# branches to build
4+
branches:
5+
only:
6+
- main
7+
8+
environment:
9+
matrix:
10+
# node.js
11+
- nodejs_version: "12"
12+
- nodejs_version: "13"
13+
- nodejs_version: "14"
14+
- nodejs_version: ""
15+
16+
install:
17+
- ps: Install-Product node $env:nodejs_version
18+
- npm install --dev
19+
20+
test_script:
21+
- npm run lint
22+
- npm run test
23+
24+
build: off

index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
function detectExternalLink(linkToCheck, options) {
4+
const configHosts = [];
5+
const finalConfig = options;
6+
let isExternal = false;
7+
let linkOrigin = '';
8+
if (finalConfig.hosts) {
9+
let singleHost;
10+
for (singleHost of finalConfig.hosts) {
11+
if (singleHost.indexOf('http://') === 0 || singleHost.indexOf('https://') === 0) {
12+
configHosts.push(new URL(singleHost).hostname);
13+
}
14+
else {
15+
configHosts.push(singleHost);
16+
}
17+
}
18+
}
19+
if (linkToCheck.indexOf('http://') === 0 || linkToCheck.indexOf('https://') === 0) {
20+
linkOrigin = new URL(linkToCheck).hostname;
21+
}
22+
else if (linkToCheck.indexOf('://') === 0) {
23+
linkOrigin = new URL('https' + linkToCheck).hostname;
24+
}
25+
if (configHosts.length === 0 || configHosts.indexOf(linkOrigin) === -1) {
26+
isExternal = true;
27+
}
28+
return isExternal;
29+
}
30+
exports.default = detectExternalLink;

0 commit comments

Comments
 (0)