@pirxpilot/nanohref 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +6 -2
  2. package/index.js +17 -17
  3. package/package.json +10 -5
package/README.md CHANGED
@@ -36,15 +36,19 @@ following conditions:
36
36
  - the link's href starts with protocol handler such as `mailto:` or `dat:`
37
37
  - the link points to a different host
38
38
  - the link has a `download` attribute
39
+ - the link's pathname does not start with the specified `prefix` (if provided)
39
40
 
40
41
  :warning: Note that we only handle `target=_blank` if they also have
41
42
  `rel="noopener"` on them. This is needed to [properly sandbox web
42
43
  pages](https://mathiasbynens.github.io/rel-noopener/).
43
44
 
44
45
  ## API
45
- ### `nanohref(handler(location))`
46
+ ### `nanohref(handler(location), { prefix })`
46
47
  Create a new anchor click handler.
47
48
 
49
+ - `handler(location)` - callback invoked with the new location on anchor click
50
+ - `prefix` - optional pathname prefix; when provided, only links whose pathname starts with the prefix are handled
51
+
48
52
  ## See Also
49
53
  - [MDN/link-types](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types)
50
54
  - [caniuse/rel=noopener](http://caniuse.com/#feat=rel-noopener)
@@ -59,7 +63,7 @@ Create a new anchor click handler.
59
63
  [npm-url]: https://npmjs.org/package/@pirxpilot/nanohref
60
64
 
61
65
  [build-url]: https://github.com/pirxpilot/nanohref/actions/workflows/check.yaml
62
- [build-image]: https://img.shields.io/github/workflow/status/pirxpilot/nanohref/check
66
+ [build-image]: https://img.shields.io/github/actions/workflow/status/pirxpilot/nanohref/check.yaml?branch=main
63
67
 
64
68
  [deps-image]: https://img.shields.io/librariesio/release/npm/@pirxpilot/nanohref
65
69
  [deps-url]: https://libraries.io/npm/@pirxpilot%2Fnanohref
package/index.js CHANGED
@@ -1,30 +1,30 @@
1
- const assert = require('assert')
1
+ import assert from 'assert';
2
2
 
3
- const safeExternalLink = /noopener/
4
- const protocolLink = /^[\w-_]+:/
3
+ const safeExternalLink = /noopener/;
4
+ const protocolLink = /^[\w-_]+:/;
5
5
 
6
- module.exports = href
7
-
8
- function href (cb) {
9
- assert(typeof cb === 'function', 'nanohref: cb should be type function')
6
+ export default function href(cb, { prefix }) {
7
+ assert(typeof cb === 'function', 'nanohref: cb should be type function');
10
8
 
11
9
  window.addEventListener('click', e => {
12
- if ((e.button && e.button !== 0) ||
13
- e.ctrlKey || e.metaKey || e.altKey || e.shiftKey ||
14
- e.defaultPrevented) return
10
+ if ((e.button && e.button !== 0) || e.ctrlKey || e.metaKey || e.altKey || e.shiftKey || e.defaultPrevented) return;
15
11
 
16
- const anchor = e.target.closest('a[href]')
17
- if (!anchor) return
12
+ const anchor = e.target.closest('a[href]');
13
+ if (!anchor) return;
18
14
 
19
- if (window.location.protocol !== anchor.protocol ||
15
+ if (
16
+ window.location.protocol !== anchor.protocol ||
20
17
  window.location.hostname !== anchor.hostname ||
21
18
  window.location.port !== anchor.port ||
19
+ (prefix && !anchor.pathname.startsWith(prefix)) ||
22
20
  anchor.hasAttribute('data-nanohref-ignore') ||
23
21
  anchor.hasAttribute('download') ||
24
22
  (anchor.getAttribute('target') === '_blank' && safeExternalLink.test(anchor.getAttribute('rel'))) ||
25
- protocolLink.test(anchor.getAttribute('href'))) return
23
+ protocolLink.test(anchor.getAttribute('href'))
24
+ )
25
+ return;
26
26
 
27
- e.preventDefault()
28
- cb(anchor)
29
- })
27
+ e.preventDefault();
28
+ cb(anchor);
29
+ });
30
30
  }
package/package.json CHANGED
@@ -1,11 +1,16 @@
1
1
  {
2
2
  "name": "@pirxpilot/nanohref",
3
3
  "description": "Tiny href click handler library",
4
- "repository": "pirxpilot/nanohref",
5
- "version": "1.0.0",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/pirxpilot/nanohref.git"
7
+ },
8
+ "version": "2.1.0",
6
9
  "scripts": {
7
- "test": "standard"
10
+ "test": "make check"
8
11
  },
12
+ "type": "module",
13
+ "exports": "./index.js",
9
14
  "browser": {
10
15
  "assert": "@pirxpilot/nanoassert"
11
16
  },
@@ -13,7 +18,7 @@
13
18
  "@pirxpilot/nanoassert": "~1"
14
19
  },
15
20
  "devDependencies": {
16
- "standard": "~17"
21
+ "@biomejs/biome": "2.4.11"
17
22
  },
18
23
  "keywords": [
19
24
  "nano",
@@ -26,4 +31,4 @@
26
31
  "files": [
27
32
  "index.js"
28
33
  ]
29
- }
34
+ }