Skip to content

Commit 1402b1d

Browse files
Include ESM support (#3)
* Include ESM support * Add missing files and bump to v2.1.0 * rebuild to add missing files * Bump to v2.0.1
1 parent 4a001fd commit 1402b1d

18 files changed

+811
-229
lines changed

.gitattributes

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
# Remove files for archives generated using `git archive`.
88
.* export-ignore
99
appveyor.yml export-ignore
10+
fixup export-ignore
1011
package-lock.json export-ignore
1112
src export-ignore
1213
test export-ignore
1314
tsconfig.json export-ignore
14-
types/index.test-d.ts export-ignore
15+
tsconfig-cjs.json export-ignore
16+
tsconfig-esm.json export-ignore
17+
types/ export-ignore

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# detect-external-link changelog
22

3+
## 2.0.1 - Sep 27, 2023
4+
5+
### Bug
6+
7+
- Add missing files
8+
9+
## 2.0.0 - Sep 27, 2023
10+
11+
### Enhancement
12+
13+
- Include ESM support
14+
315
## 1.0.2 - Aug 25, 2023
416

517
### Bug

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
[![NPM version][npm-image]][npm-url]
44
[![Downloads][downloads-image]][npm-url]
5-
[![Build Status][travis-image]][travis-url]
65
[![AppVeyor Build Status][appveyor-image]][appveyor-url]
6+
[![Downloads][jsdelivr-image]][jsdelivr-url]
77

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.
8+
JavaScript 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.
99

1010
## Install
1111

@@ -21,10 +21,24 @@ Via Yarn
2121
yarn add detect-external-link
2222
```
2323

24+
For vanilla HTML in modern browsers, import `detect-external-link` from jsDelivr:
25+
26+
```html
27+
<script type="module">
28+
import detectExternalLink from 'https://cdn.jsdelivr.net/npm/detect-external-link@2/+esm';
29+
30+
console.log(detectExternalLink('http://test.example2.com/test/abc/', {
31+
'hosts': [
32+
'http://example.com',
33+
'http://test.example.com'
34+
]
35+
}));
36+
</script>
37+
```
2438
## Usage
2539

2640
```javascript
27-
const detectExternalLink = require('detect-external-link').default;
41+
import detectExternalLink from '/js/index.js';
2842

2943
const link = 'http://test.example2.com/test/abc/';
3044
const linkOptions = {
@@ -45,10 +59,10 @@ console.log(detectExternalLink(link, linkOptions));
4559

4660
[npm-image]: https://img.shields.io/npm/v/detect-external-link.svg
4761
[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
62+
[downloads-image]: https://img.shields.io/npm/dm/detect-external-link.svg
5263

5364
[appveyor-url]: https://ci.appveyor.com/project/trunkcode/detect-external-link
5465
[appveyor-image]: https://img.shields.io/appveyor/ci/trunkcode/detect-external-link.svg?label=appveyor
66+
67+
[jsdelivr-image]: https://img.shields.io/jsdelivr/npm/hm/detect-external-link
68+
[jsdelivr-url]: https://www.jsdelivr.com/package/npm/detect-external-link

dist/cjs/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function detectExternalLink (linkToCheck: string, options: configOptions): boolean;
2+
3+
export type configOptions = {
4+
hosts?: string[];
5+
};
File renamed without changes.

dist/cjs/index.test-d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
import detectExternalLink from './index';
4+
5+
const multipleHosts = {
6+
'hosts': [
7+
'http://example.com',
8+
'http://test.example.com',
9+
]
10+
};
11+
const singleHost = {
12+
'hosts': ['http://example.com']
13+
};
14+
const testLink = 'https://example.com';
15+
16+
if (singleHost) {
17+
console.log(detectExternalLink(testLink, singleHost));
18+
}
19+
20+
if (multipleHosts) {
21+
console.log(detectExternalLink(testLink, multipleHosts));
22+
}

dist/cjs/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

dist/esm/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function detectExternalLink (linkToCheck: string, options: configOptions): boolean;
2+
3+
export type configOptions = {
4+
hosts?: string[];
5+
};

dist/esm/index.js

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

dist/esm/index.test-d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
import detectExternalLink from './index';
4+
5+
const multipleHosts = {
6+
'hosts': [
7+
'http://example.com',
8+
'http://test.example.com',
9+
]
10+
};
11+
const singleHost = {
12+
'hosts': ['http://example.com']
13+
};
14+
const testLink = 'https://example.com';
15+
16+
if (singleHost) {
17+
console.log(detectExternalLink(testLink, singleHost));
18+
}
19+
20+
if (multipleHosts) {
21+
console.log(detectExternalLink(testLink, multipleHosts));
22+
}

0 commit comments

Comments
 (0)