normalize-url 5.2.0 → 6.0.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.
package/index.d.ts CHANGED
@@ -103,6 +103,30 @@ declare namespace normalizeUrl {
103
103
  */
104
104
  readonly stripProtocol?: boolean;
105
105
 
106
+ /**
107
+ Strip the [text fragment](https://web.dev/text-fragments/) part of the URL
108
+
109
+ __Note:__ The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
110
+
111
+ @default true
112
+
113
+ @example
114
+ ```
115
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
116
+ //=> 'http://sindresorhus.com/about.html#'
117
+
118
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
119
+ //=> 'http://sindresorhus.com/about.html#section'
120
+
121
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
122
+ //=> 'http://sindresorhus.com/about.html#:~:text=hello'
123
+
124
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
125
+ //=> 'http://sindresorhus.com/about.html#section:~:text=hello'
126
+ ```
127
+ */
128
+ readonly stripTextFragment?: boolean;
129
+
106
130
  /**
107
131
  Removes `www.` from the URL.
108
132
 
package/index.js CHANGED
@@ -67,6 +67,7 @@ const normalizeUrl = (urlString, options) => {
67
67
  forceHttps: false,
68
68
  stripAuthentication: true,
69
69
  stripHash: false,
70
+ stripTextFragment: true,
70
71
  stripWWW: true,
71
72
  removeQueryParameters: [/^utm_\w+/i],
72
73
  removeTrailingSlash: true,
@@ -83,6 +84,10 @@ const normalizeUrl = (urlString, options) => {
83
84
  return normalizeDataURL(urlString, options);
84
85
  }
85
86
 
87
+ if (/^view-source:/i.test(urlString)) {
88
+ throw new Error('`view-source:` is not supported as it is a non-standard protocol');
89
+ }
90
+
86
91
  const hasRelativeProtocol = urlString.startsWith('//');
87
92
  const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
88
93
 
@@ -114,6 +119,8 @@ const normalizeUrl = (urlString, options) => {
114
119
  // Remove hash
115
120
  if (options.stripHash) {
116
121
  urlObj.hash = '';
122
+ } else if (options.stripTextFragment) {
123
+ urlObj.hash = urlObj.hash.replace(/#?:~:text.*?$/i, '');
117
124
  }
118
125
 
119
126
  // Remove duplicate slashes if not preceded by a protocol
@@ -175,9 +182,15 @@ const normalizeUrl = (urlString, options) => {
175
182
  urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
176
183
  }
177
184
 
185
+ const oldUrlString = urlString;
186
+
178
187
  // Take advantage of many of the Node `url` normalizations
179
188
  urlString = urlObj.toString();
180
189
 
190
+ if (!options.removeSingleSlash && urlObj.pathname === '/' && !oldUrlString.endsWith('/') && urlObj.hash === '') {
191
+ urlString = urlString.replace(/\/$/, '');
192
+ }
193
+
181
194
  // Remove ending `/` unless removeSingleSlash is false
182
195
  if ((options.removeTrailingSlash || urlObj.pathname === '/') && urlObj.hash === '' && options.removeSingleSlash) {
183
196
  urlString = urlString.replace(/\/$/, '');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "normalize-url",
3
- "version": "5.2.0",
3
+ "version": "6.0.0",
4
4
  "description": "Normalize a URL",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/normalize-url",
@@ -8,7 +8,7 @@
8
8
  "author": {
9
9
  "name": "Sindre Sorhus",
10
10
  "email": "sindresorhus@gmail.com",
11
- "url": "sindresorhus.com"
11
+ "url": "https://sindresorhus.com"
12
12
  },
13
13
  "engines": {
14
14
  "node": ">=10"
@@ -37,9 +37,14 @@
37
37
  ],
38
38
  "devDependencies": {
39
39
  "ava": "^2.4.0",
40
- "coveralls": "^3.0.6",
41
40
  "nyc": "^15.0.0",
42
41
  "tsd": "^0.11.0",
43
42
  "xo": "^0.25.3"
43
+ },
44
+ "nyc": {
45
+ "reporter": [
46
+ "text",
47
+ "lcov"
48
+ ]
44
49
  }
45
50
  }
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/normalize-url/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/normalize-url?branch=master)
1
+ # normalize-url [![Coverage Status](https://codecov.io/gh/sindresorhus/normalize-url/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/normalize-url)
2
2
 
3
3
  > [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL
4
4
 
@@ -135,6 +135,29 @@ normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
135
135
  //=> 'sindresorhus.com'
136
136
  ```
137
137
 
138
+ ##### stripTextFragment
139
+
140
+ Type: `boolean`\
141
+ Default: `true`
142
+
143
+ Strip the [text fragment](https://web.dev/text-fragments/) part of the URL.
144
+
145
+ **Note:** The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
146
+
147
+ ```js
148
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
149
+ //=> 'http://sindresorhus.com/about.html#'
150
+
151
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
152
+ //=> 'http://sindresorhus.com/about.html#section'
153
+
154
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
155
+ //=> 'http://sindresorhus.com/about.html#:~:text=hello'
156
+
157
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
158
+ //=> 'http://sindresorhus.com/about.html#section:~:text=hello'
159
+ ```
160
+
138
161
  ##### stripWWW
139
162
 
140
163
  Type: `boolean`\
@@ -232,7 +255,6 @@ normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
232
255
 
233
256
  - [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
234
257
 
235
-
236
258
  ---
237
259
 
238
260
  <div align="center">