@mintlify/common 1.0.1008 → 1.0.1010
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.
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type AddTrailingSlashOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Skip paths whose last segment looks like a file (e.g. `/images/logo.png`).
|
|
4
|
+
* Enabled by default for generic callers. Callers that know their input is
|
|
5
|
+
* always a page URL (canonical, sitemap) should pass `false` so valid page
|
|
6
|
+
* slugs with dots (e.g. `/release.v1`, `/api.beta`) still get a slash.
|
|
7
|
+
*/
|
|
8
|
+
skipFileExtensions?: boolean;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Appends a trailing slash to a URL's path while preserving any query string or
|
|
12
|
+
* hash fragment. Handles both absolute URLs (e.g. `https://host/docs/page`) and
|
|
13
|
+
* root-relative paths (e.g. `/docs/page`). Paths that already end in `/` are
|
|
14
|
+
* returned unchanged.
|
|
15
|
+
*
|
|
16
|
+
* By default, file-like paths (e.g. `/logo.png`) are left unchanged. Pass
|
|
17
|
+
* `skipFileExtensions: false` when the input is guaranteed to be a page URL so
|
|
18
|
+
* that page slugs containing dots are handled correctly.
|
|
19
|
+
*/
|
|
20
|
+
export declare function addTrailingSlash(url: string, options?: AddTrailingSlashOptions): string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
function looksLikeFilePath(pathname) {
|
|
2
|
+
var _a, _b;
|
|
3
|
+
const lastSegment = (_a = pathname.split('/').pop()) !== null && _a !== void 0 ? _a : '';
|
|
4
|
+
const extension = lastSegment.includes('.') ? ((_b = lastSegment.split('.').pop()) !== null && _b !== void 0 ? _b : '') : '';
|
|
5
|
+
return /[a-zA-Z]/.test(extension);
|
|
6
|
+
}
|
|
7
|
+
function ensurePathTrailingSlash(pathname, skipFileExtensions) {
|
|
8
|
+
if (pathname === '')
|
|
9
|
+
return '/';
|
|
10
|
+
if (pathname.endsWith('/'))
|
|
11
|
+
return pathname;
|
|
12
|
+
if (skipFileExtensions && looksLikeFilePath(pathname))
|
|
13
|
+
return pathname;
|
|
14
|
+
return `${pathname}/`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Appends a trailing slash to a URL's path while preserving any query string or
|
|
18
|
+
* hash fragment. Handles both absolute URLs (e.g. `https://host/docs/page`) and
|
|
19
|
+
* root-relative paths (e.g. `/docs/page`). Paths that already end in `/` are
|
|
20
|
+
* returned unchanged.
|
|
21
|
+
*
|
|
22
|
+
* By default, file-like paths (e.g. `/logo.png`) are left unchanged. Pass
|
|
23
|
+
* `skipFileExtensions: false` when the input is guaranteed to be a page URL so
|
|
24
|
+
* that page slugs containing dots are handled correctly.
|
|
25
|
+
*/
|
|
26
|
+
export function addTrailingSlash(url, options = {}) {
|
|
27
|
+
var _a, _b, _c;
|
|
28
|
+
if (typeof url !== 'string' || url === '')
|
|
29
|
+
return url;
|
|
30
|
+
const skipFileExtensions = (_a = options.skipFileExtensions) !== null && _a !== void 0 ? _a : true;
|
|
31
|
+
if (/^https?:\/\//i.test(url)) {
|
|
32
|
+
try {
|
|
33
|
+
const parsed = new URL(url);
|
|
34
|
+
parsed.pathname = ensurePathTrailingSlash(parsed.pathname, skipFileExtensions);
|
|
35
|
+
return parsed.toString();
|
|
36
|
+
}
|
|
37
|
+
catch (_d) {
|
|
38
|
+
return url;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const match = /^([^?#]*)([?#].*)?$/.exec(url);
|
|
42
|
+
const pathname = (_b = match === null || match === void 0 ? void 0 : match[1]) !== null && _b !== void 0 ? _b : url;
|
|
43
|
+
const suffix = (_c = match === null || match === void 0 ? void 0 : match[2]) !== null && _c !== void 0 ? _c : '';
|
|
44
|
+
return `${ensurePathTrailingSlash(pathname, skipFileExtensions)}${suffix}`;
|
|
45
|
+
}
|
package/dist/slug/index.d.ts
CHANGED
package/dist/slug/index.js
CHANGED