@docusaurus/utils 2.0.0-beta.ff31de0ff → 2.0.1
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/README.md +1 -1
- package/lib/constants.d.ts +73 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +78 -0
- package/lib/constants.js.map +1 -0
- package/lib/dataFileUtils.d.ts +60 -0
- package/lib/dataFileUtils.d.ts.map +1 -0
- package/lib/dataFileUtils.js +91 -0
- package/lib/dataFileUtils.js.map +1 -0
- package/lib/emitUtils.d.ts +32 -0
- package/lib/emitUtils.d.ts.map +1 -0
- package/lib/emitUtils.js +80 -0
- package/lib/emitUtils.js.map +1 -0
- package/lib/gitUtils.d.ts +66 -0
- package/lib/gitUtils.d.ts.map +1 -0
- package/lib/gitUtils.js +63 -0
- package/lib/gitUtils.js.map +1 -0
- package/lib/globUtils.d.ts +40 -0
- package/lib/globUtils.d.ts.map +1 -0
- package/lib/globUtils.js +71 -0
- package/lib/globUtils.js.map +1 -0
- package/lib/hashUtils.d.ts +17 -0
- package/lib/hashUtils.d.ts.map +1 -0
- package/lib/hashUtils.js +42 -0
- package/lib/hashUtils.js.map +1 -0
- package/lib/i18nUtils.d.ts +53 -0
- package/lib/i18nUtils.d.ts.map +1 -0
- package/lib/i18nUtils.js +70 -0
- package/lib/i18nUtils.js.map +1 -0
- package/lib/index.d.ts +17 -82
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +91 -411
- package/lib/index.js.map +1 -0
- package/lib/jsUtils.d.ts +28 -0
- package/lib/jsUtils.d.ts.map +1 -0
- package/lib/jsUtils.js +57 -0
- package/lib/jsUtils.js.map +1 -0
- package/lib/markdownLinks.d.ts +49 -5
- package/lib/markdownLinks.d.ts.map +1 -0
- package/lib/markdownLinks.js +57 -13
- package/lib/markdownLinks.js.map +1 -0
- package/lib/markdownUtils.d.ts +112 -0
- package/lib/markdownUtils.d.ts.map +1 -0
- package/lib/markdownUtils.js +271 -0
- package/lib/markdownUtils.js.map +1 -0
- package/lib/pathUtils.d.ts +52 -0
- package/lib/pathUtils.d.ts.map +1 -0
- package/lib/pathUtils.js +115 -0
- package/lib/pathUtils.js.map +1 -0
- package/lib/shellUtils.d.ts +8 -0
- package/lib/shellUtils.d.ts.map +1 -0
- package/lib/shellUtils.js +21 -0
- package/lib/shellUtils.js.map +1 -0
- package/lib/slugger.d.ts +24 -0
- package/lib/slugger.d.ts.map +1 -0
- package/lib/slugger.js +23 -0
- package/lib/slugger.js.map +1 -0
- package/lib/tags.d.ts +59 -0
- package/lib/tags.d.ts.map +1 -0
- package/lib/tags.js +91 -0
- package/lib/tags.js.map +1 -0
- package/lib/urlUtils.d.ts +66 -0
- package/lib/urlUtils.d.ts.map +1 -0
- package/lib/urlUtils.js +207 -0
- package/lib/urlUtils.js.map +1 -0
- package/lib/webpackUtils.d.ts +35 -0
- package/lib/webpackUtils.d.ts.map +1 -0
- package/lib/webpackUtils.js +115 -0
- package/lib/webpackUtils.js.map +1 -0
- package/package.json +28 -10
- package/src/constants.ts +98 -0
- package/src/dataFileUtils.ts +122 -0
- package/src/deps.d.ts +10 -0
- package/src/emitUtils.ts +99 -0
- package/src/gitUtils.ts +146 -0
- package/src/globUtils.ts +85 -0
- package/src/hashUtils.ts +38 -0
- package/src/i18nUtils.ts +114 -0
- package/src/index.ts +98 -521
- package/src/jsUtils.ts +59 -0
- package/src/markdownLinks.ts +101 -30
- package/src/markdownUtils.ts +357 -0
- package/src/pathUtils.ts +123 -0
- package/src/shellUtils.ts +18 -0
- package/src/slugger.ts +36 -0
- package/src/tags.ts +130 -0
- package/src/urlUtils.ts +234 -0
- package/src/webpackUtils.ts +153 -0
- package/lib/.tsbuildinfo +0 -3928
- package/lib/codeTranslationsUtils.d.ts +0 -11
- package/lib/codeTranslationsUtils.js +0 -50
- package/lib/escapePath.d.ts +0 -17
- package/lib/escapePath.js +0 -25
- package/lib/markdownParser.d.ts +0 -28
- package/lib/markdownParser.js +0 -132
- package/lib/posixPath.d.ts +0 -14
- package/lib/posixPath.js +0 -28
- package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
- package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
- package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
- package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
- package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
- package/src/__tests__/escapePath.test.ts +0 -25
- package/src/__tests__/index.test.ts +0 -681
- package/src/__tests__/markdownParser.test.ts +0 -772
- package/src/__tests__/posixPath.test.ts +0 -25
- package/src/codeTranslationsUtils.ts +0 -56
- package/src/escapePath.ts +0 -23
- package/src/markdownParser.ts +0 -177
- package/src/posixPath.ts +0 -27
- package/tsconfig.json +0 -9
package/lib/pathUtils.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.addTrailingPathSeparator = exports.escapePath = exports.aliasedSitePath = exports.toMessageRelativeFilePath = exports.posixPath = exports.shortName = exports.isNameTooLong = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
12
|
+
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
13
|
+
// macOS (APFS) and Windows (NTFS) filename length limit = 255 chars,
|
|
14
|
+
// Others = 255 bytes
|
|
15
|
+
const MAX_PATH_SEGMENT_CHARS = 255;
|
|
16
|
+
const MAX_PATH_SEGMENT_BYTES = 255;
|
|
17
|
+
// Space for appending things to the string like file extensions and so on
|
|
18
|
+
const SPACE_FOR_APPENDING = 10;
|
|
19
|
+
const isMacOs = () => process.platform === 'darwin';
|
|
20
|
+
const isWindows = () => process.platform === 'win32';
|
|
21
|
+
const isNameTooLong = (str) =>
|
|
22
|
+
// Not entirely correct: we can't assume FS from OS. But good enough?
|
|
23
|
+
isMacOs() || isWindows()
|
|
24
|
+
? // Windows (NTFS) and macOS (APFS) filename length limit (255 chars)
|
|
25
|
+
str.length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_CHARS
|
|
26
|
+
: // Other (255 bytes)
|
|
27
|
+
Buffer.from(str).length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_BYTES;
|
|
28
|
+
exports.isNameTooLong = isNameTooLong;
|
|
29
|
+
function shortName(str) {
|
|
30
|
+
if (isMacOs() || isWindows()) {
|
|
31
|
+
const overflowingChars = str.length - MAX_PATH_SEGMENT_CHARS;
|
|
32
|
+
return str.slice(0, str.length - overflowingChars - SPACE_FOR_APPENDING - 1);
|
|
33
|
+
}
|
|
34
|
+
const strBuffer = Buffer.from(str);
|
|
35
|
+
const overflowingBytes = Buffer.byteLength(strBuffer) - MAX_PATH_SEGMENT_BYTES;
|
|
36
|
+
return strBuffer
|
|
37
|
+
.slice(0, Buffer.byteLength(strBuffer) - overflowingBytes - SPACE_FOR_APPENDING - 1)
|
|
38
|
+
.toString();
|
|
39
|
+
}
|
|
40
|
+
exports.shortName = shortName;
|
|
41
|
+
/**
|
|
42
|
+
* Convert Windows backslash paths to posix style paths.
|
|
43
|
+
* E.g: endi\lie -> endi/lie
|
|
44
|
+
*
|
|
45
|
+
* Returns original path if the posix counterpart is not valid Windows path.
|
|
46
|
+
* This makes the legacy code that uses posixPath safe; but also makes it less
|
|
47
|
+
* useful when you actually want a path with forward slashes (e.g. for URL)
|
|
48
|
+
*
|
|
49
|
+
* Adopted from https://github.com/sindresorhus/slash/blob/main/index.js
|
|
50
|
+
*/
|
|
51
|
+
function posixPath(str) {
|
|
52
|
+
const isExtendedLengthPath = str.startsWith('\\\\?\\');
|
|
53
|
+
// Forward slashes are only valid Windows paths when they don't contain non-
|
|
54
|
+
// ascii characters.
|
|
55
|
+
// eslint-disable-next-line no-control-regex
|
|
56
|
+
const hasNonAscii = /[^\u0000-\u0080]+/.test(str);
|
|
57
|
+
if (isExtendedLengthPath || hasNonAscii) {
|
|
58
|
+
return str;
|
|
59
|
+
}
|
|
60
|
+
return str.replace(/\\/g, '/');
|
|
61
|
+
}
|
|
62
|
+
exports.posixPath = posixPath;
|
|
63
|
+
/**
|
|
64
|
+
* When you want to display a path in a message/warning/error, it's more
|
|
65
|
+
* convenient to:
|
|
66
|
+
*
|
|
67
|
+
* - make it relative to `cwd()`
|
|
68
|
+
* - convert to posix (ie not using windows \ path separator)
|
|
69
|
+
*
|
|
70
|
+
* This way, Jest tests can run more reliably on any computer/CI on both
|
|
71
|
+
* Unix/Windows
|
|
72
|
+
* For Windows users this is not perfect (as they see / instead of \) but it's
|
|
73
|
+
* probably good enough
|
|
74
|
+
*/
|
|
75
|
+
function toMessageRelativeFilePath(filePath) {
|
|
76
|
+
return posixPath(path_1.default.relative(process.cwd(), filePath));
|
|
77
|
+
}
|
|
78
|
+
exports.toMessageRelativeFilePath = toMessageRelativeFilePath;
|
|
79
|
+
/**
|
|
80
|
+
* Alias filepath relative to site directory, very useful so that we
|
|
81
|
+
* don't expose user's site structure.
|
|
82
|
+
* Example: some/path/to/website/docs/foo.md -> @site/docs/foo.md
|
|
83
|
+
*/
|
|
84
|
+
function aliasedSitePath(filePath, siteDir) {
|
|
85
|
+
const relativePath = posixPath(path_1.default.relative(siteDir, filePath));
|
|
86
|
+
// Cannot use path.join() as it resolves '../' and removes
|
|
87
|
+
// the '@site'. Let webpack loader resolve it.
|
|
88
|
+
return `@site/${relativePath}`;
|
|
89
|
+
}
|
|
90
|
+
exports.aliasedSitePath = aliasedSitePath;
|
|
91
|
+
/**
|
|
92
|
+
* When you have a path like C:\X\Y
|
|
93
|
+
* It is not safe to use directly when generating code
|
|
94
|
+
* For example, this would fail due to unescaped \:
|
|
95
|
+
* `<img src={require('${filePath}')} />`
|
|
96
|
+
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
|
97
|
+
*
|
|
98
|
+
* posixPath can't be used in all cases, because forward slashes are only valid
|
|
99
|
+
* Windows paths when they don't contain non-ascii characters, and posixPath
|
|
100
|
+
* doesn't escape those that fail to be converted.
|
|
101
|
+
*/
|
|
102
|
+
function escapePath(str) {
|
|
103
|
+
const escaped = JSON.stringify(str);
|
|
104
|
+
// Remove the " around the json string;
|
|
105
|
+
return escaped.substring(1, escaped.length - 1);
|
|
106
|
+
}
|
|
107
|
+
exports.escapePath = escapePath;
|
|
108
|
+
function addTrailingPathSeparator(str) {
|
|
109
|
+
return str.endsWith(path_1.default.sep)
|
|
110
|
+
? str
|
|
111
|
+
: // If this is Windows, we need to change the forward slash to backward
|
|
112
|
+
`${str.replace(/\/$/, '')}${path_1.default.sep}`;
|
|
113
|
+
}
|
|
114
|
+
exports.addTrailingPathSeparator = addTrailingPathSeparator;
|
|
115
|
+
//# sourceMappingURL=pathUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pathUtils.js","sourceRoot":"","sources":["../src/pathUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wDAAwB;AAExB,+DAA+D;AAC/D,qEAAqE;AACrE,qBAAqB;AACrB,MAAM,sBAAsB,GAAG,GAAG,CAAC;AACnC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AACnC,0EAA0E;AAC1E,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,QAAQ,KAAK,QAAQ,CAAC;AACpD,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC;AAE9C,MAAM,aAAa,GAAG,CAAC,GAAW,EAAW,EAAE;AACpD,qEAAqE;AACrE,OAAO,EAAE,IAAI,SAAS,EAAE;IACtB,CAAC,CAA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,yBAAyB,CAAC,QAAgB;IACxD,OAAO,SAAS,CAAC,cAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC,CAAC;AAC3D,CAAC;AAFD,8DAEC;AAED;;;;GAIG;AACH,SAAgB,eAAe,CAAC,QAAgB,EAAE,OAAe;IAC/D,MAAM,YAAY,GAAG,SAAS,CAAC,cAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;IACjE,0DAA0D;IAC1D,8CAA8C;IAC9C,OAAO,SAAS,YAAY,EAAE,CAAC;AACjC,CAAC;AALD,0CAKC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CAAC,GAAW;IACpC,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAEpC,uCAAuC;IACvC,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAClD,CAAC;AALD,gCAKC;AAED,SAAgB,wBAAwB,CAAC,GAAW;IAClD,OAAO,GAAG,CAAC,QAAQ,CAAC,cAAI,CAAC,GAAG,CAAC;QAC3B,CAAC,CAAC,GAAG;QACL,CAAC,CAAC,sEAAsE;YACtE,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,cAAI,CAAC,GAAG,EAAE,CAAC;AAC7C,CAAC;AALD,4DAKC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
export declare function escapeShellArg(s: string): string;
|
|
8
|
+
//# sourceMappingURL=shellUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shellUtils.d.ts","sourceRoot":"","sources":["../src/shellUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH,wBAAgB,cAAc,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAIhD"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.escapeShellArg = void 0;
|
|
10
|
+
// TODO move from shelljs to execa later?
|
|
11
|
+
// Execa is well maintained and widely used
|
|
12
|
+
// Even shelljs recommends execa for security / escaping:
|
|
13
|
+
// https://github.com/shelljs/shelljs/wiki/Security-guidelines
|
|
14
|
+
// Inspired by https://github.com/xxorax/node-shell-escape/blob/master/shell-escape.js
|
|
15
|
+
function escapeShellArg(s) {
|
|
16
|
+
let res = `'${s.replace(/'/g, "'\\''")}'`;
|
|
17
|
+
res = res.replace(/^(?:'')+/g, '').replace(/\\'''/g, "\\'");
|
|
18
|
+
return res;
|
|
19
|
+
}
|
|
20
|
+
exports.escapeShellArg = escapeShellArg;
|
|
21
|
+
//# sourceMappingURL=shellUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shellUtils.js","sourceRoot":"","sources":["../src/shellUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,yCAAyC;AACzC,2CAA2C;AAC3C,yDAAyD;AACzD,8DAA8D;AAE9D,sFAAsF;AACtF,SAAgB,cAAc,CAAC,CAAS;IACtC,IAAI,GAAG,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC;IAC1C,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAC5D,OAAO,GAAG,CAAC;AACb,CAAC;AAJD,wCAIC"}
|
package/lib/slugger.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
export declare type SluggerOptions = {
|
|
8
|
+
/** Keep the headings' casing, otherwise make all lowercase. */
|
|
9
|
+
maintainCase?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare type Slugger = {
|
|
12
|
+
/**
|
|
13
|
+
* Takes a Markdown heading like "Josh Cena" and sluggifies it according to
|
|
14
|
+
* GitHub semantics (in this case `josh-cena`). Stateful, because if you try
|
|
15
|
+
* to sluggify "Josh Cena" again it would return `josh-cena-1`.
|
|
16
|
+
*/
|
|
17
|
+
slug: (value: string, options?: SluggerOptions) => string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A thin wrapper around github-slugger. This is a factory function that returns
|
|
21
|
+
* a stateful Slugger object.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createSlugger(): Slugger;
|
|
24
|
+
//# sourceMappingURL=slugger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slugger.d.ts","sourceRoot":"","sources":["../src/slugger.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,oBAAY,cAAc,GAAG;IAC3B,+DAA+D;IAC/D,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,CAAC;AAEF,oBAAY,OAAO,GAAG;IACpB;;;;OAIG;IACH,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,KAAK,MAAM,CAAC;CAC3D,CAAC;AAEF;;;GAGG;AACH,wBAAgB,aAAa,IAAI,OAAO,CAKvC"}
|
package/lib/slugger.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.createSlugger = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const github_slugger_1 = tslib_1.__importDefault(require("github-slugger"));
|
|
12
|
+
/**
|
|
13
|
+
* A thin wrapper around github-slugger. This is a factory function that returns
|
|
14
|
+
* a stateful Slugger object.
|
|
15
|
+
*/
|
|
16
|
+
function createSlugger() {
|
|
17
|
+
const githubSlugger = new github_slugger_1.default();
|
|
18
|
+
return {
|
|
19
|
+
slug: (value, options) => githubSlugger.slug(value, options?.maintainCase),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
exports.createSlugger = createSlugger;
|
|
23
|
+
//# sourceMappingURL=slugger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slugger.js","sourceRoot":"","sources":["../src/slugger.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,4EAA2C;AAmB3C;;;GAGG;AACH,SAAgB,aAAa;IAC3B,MAAM,aAAa,GAAG,IAAI,wBAAa,EAAE,CAAC;IAC1C,OAAO;QACL,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,YAAY,CAAC;KAC3E,CAAC;AACJ,CAAC;AALD,sCAKC"}
|
package/lib/tags.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
/** What the user configures. */
|
|
8
|
+
export declare type Tag = {
|
|
9
|
+
label: string;
|
|
10
|
+
/** Permalink to this tag's page, without the `/tags/` base path. */
|
|
11
|
+
permalink: string;
|
|
12
|
+
};
|
|
13
|
+
/** What the tags list page should know about each tag. */
|
|
14
|
+
export declare type TagsListItem = Tag & {
|
|
15
|
+
/** Number of posts/docs with this tag. */
|
|
16
|
+
count: number;
|
|
17
|
+
};
|
|
18
|
+
/** What the tag's own page should know about the tag. */
|
|
19
|
+
export declare type TagModule = TagsListItem & {
|
|
20
|
+
/** The tags list page's permalink. */
|
|
21
|
+
allTagsPath: string;
|
|
22
|
+
};
|
|
23
|
+
export declare type FrontMatterTag = string | Tag;
|
|
24
|
+
/**
|
|
25
|
+
* Takes tag objects as they are defined in front matter, and normalizes each
|
|
26
|
+
* into a standard tag object. The permalink is created by appending the
|
|
27
|
+
* sluggified label to `tagsPath`. Front matter tags already containing
|
|
28
|
+
* permalinks would still have `tagsPath` prepended.
|
|
29
|
+
*
|
|
30
|
+
* The result will always be unique by permalinks. The behavior with colliding
|
|
31
|
+
* permalinks is undetermined.
|
|
32
|
+
*/
|
|
33
|
+
export declare function normalizeFrontMatterTags(
|
|
34
|
+
/** Base path to append the tag permalinks to. */
|
|
35
|
+
tagsPath: string,
|
|
36
|
+
/** Can be `undefined`, so that we can directly pipe in `frontMatter.tags`. */
|
|
37
|
+
frontMatterTags?: FrontMatterTag[] | undefined): Tag[];
|
|
38
|
+
declare type TaggedItemGroup<Item> = {
|
|
39
|
+
tag: Tag;
|
|
40
|
+
items: Item[];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Permits to group docs/blog posts by tag (provided by front matter).
|
|
44
|
+
*
|
|
45
|
+
* @returns a map from tag permalink to the items and other relevant tag data.
|
|
46
|
+
* The record is indexed by permalink, because routes must be unique in the end.
|
|
47
|
+
* Labels may vary on 2 MD files but they are normalized. Docs with
|
|
48
|
+
* label='some label' and label='some-label' should end up in the same page.
|
|
49
|
+
*/
|
|
50
|
+
export declare function groupTaggedItems<Item>(items: readonly Item[],
|
|
51
|
+
/**
|
|
52
|
+
* A callback telling me how to get the tags list of the current item. Usually
|
|
53
|
+
* simply getting it from some metadata of the current item.
|
|
54
|
+
*/
|
|
55
|
+
getItemTags: (item: Item) => readonly Tag[]): {
|
|
56
|
+
[permalink: string]: TaggedItemGroup<Item>;
|
|
57
|
+
};
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=tags.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tags.d.ts","sourceRoot":"","sources":["../src/tags.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,gCAAgC;AAChC,oBAAY,GAAG,GAAG;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,0DAA0D;AAC1D,oBAAY,YAAY,GAAG,GAAG,GAAG;IAC/B,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,yDAAyD;AACzD,oBAAY,SAAS,GAAG,YAAY,GAAG;IACrC,sCAAsC;IACtC,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,oBAAY,cAAc,GAAG,MAAM,GAAG,GAAG,CAAC;AAgC1C;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB;AACtC,iDAAiD;AACjD,QAAQ,EAAE,MAAM;AAChB,8EAA8E;AAC9E,eAAe,GAAE,cAAc,EAAE,GAAG,SAAc,GACjD,GAAG,EAAE,CAMP;AAED,aAAK,eAAe,CAAC,IAAI,IAAI;IAC3B,GAAG,EAAE,GAAG,CAAC;IACT,KAAK,EAAE,IAAI,EAAE,CAAC;CACf,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EACnC,KAAK,EAAE,SAAS,IAAI,EAAE;AACtB;;;GAGG;AACH,WAAW,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,SAAS,GAAG,EAAE,GAC1C;IAAC,CAAC,SAAS,EAAE,MAAM,GAAG,eAAe,CAAC,IAAI,CAAC,CAAA;CAAC,CA0B9C"}
|
package/lib/tags.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.groupTaggedItems = exports.normalizeFrontMatterTags = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
12
|
+
const urlUtils_1 = require("./urlUtils");
|
|
13
|
+
function normalizeFrontMatterTag(tagsPath, frontMatterTag) {
|
|
14
|
+
function toTagObject(tagString) {
|
|
15
|
+
return {
|
|
16
|
+
label: tagString,
|
|
17
|
+
permalink: lodash_1.default.kebabCase(tagString),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// TODO maybe make ensure the permalink is valid url path?
|
|
21
|
+
function normalizeTagPermalink(permalink) {
|
|
22
|
+
// Note: we always apply tagsPath on purpose. For versioned docs, v1/doc.md
|
|
23
|
+
// and v2/doc.md tags with custom permalinks don't lead to the same created
|
|
24
|
+
// page. tagsPath is different for each doc version
|
|
25
|
+
return (0, urlUtils_1.normalizeUrl)([tagsPath, permalink]);
|
|
26
|
+
}
|
|
27
|
+
const tag = typeof frontMatterTag === 'string'
|
|
28
|
+
? toTagObject(frontMatterTag)
|
|
29
|
+
: frontMatterTag;
|
|
30
|
+
return {
|
|
31
|
+
label: tag.label,
|
|
32
|
+
permalink: normalizeTagPermalink(tag.permalink),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Takes tag objects as they are defined in front matter, and normalizes each
|
|
37
|
+
* into a standard tag object. The permalink is created by appending the
|
|
38
|
+
* sluggified label to `tagsPath`. Front matter tags already containing
|
|
39
|
+
* permalinks would still have `tagsPath` prepended.
|
|
40
|
+
*
|
|
41
|
+
* The result will always be unique by permalinks. The behavior with colliding
|
|
42
|
+
* permalinks is undetermined.
|
|
43
|
+
*/
|
|
44
|
+
function normalizeFrontMatterTags(
|
|
45
|
+
/** Base path to append the tag permalinks to. */
|
|
46
|
+
tagsPath,
|
|
47
|
+
/** Can be `undefined`, so that we can directly pipe in `frontMatter.tags`. */
|
|
48
|
+
frontMatterTags = []) {
|
|
49
|
+
const tags = frontMatterTags.map((tag) => normalizeFrontMatterTag(tagsPath, tag));
|
|
50
|
+
return lodash_1.default.uniqBy(tags, (tag) => tag.permalink);
|
|
51
|
+
}
|
|
52
|
+
exports.normalizeFrontMatterTags = normalizeFrontMatterTags;
|
|
53
|
+
/**
|
|
54
|
+
* Permits to group docs/blog posts by tag (provided by front matter).
|
|
55
|
+
*
|
|
56
|
+
* @returns a map from tag permalink to the items and other relevant tag data.
|
|
57
|
+
* The record is indexed by permalink, because routes must be unique in the end.
|
|
58
|
+
* Labels may vary on 2 MD files but they are normalized. Docs with
|
|
59
|
+
* label='some label' and label='some-label' should end up in the same page.
|
|
60
|
+
*/
|
|
61
|
+
function groupTaggedItems(items,
|
|
62
|
+
/**
|
|
63
|
+
* A callback telling me how to get the tags list of the current item. Usually
|
|
64
|
+
* simply getting it from some metadata of the current item.
|
|
65
|
+
*/
|
|
66
|
+
getItemTags) {
|
|
67
|
+
const result = {};
|
|
68
|
+
items.forEach((item) => {
|
|
69
|
+
getItemTags(item).forEach((tag) => {
|
|
70
|
+
var _a;
|
|
71
|
+
// Init missing tag groups
|
|
72
|
+
// TODO: it's not really clear what should be the behavior if 2 tags have
|
|
73
|
+
// the same permalink but the label is different for each
|
|
74
|
+
// For now, the first tag found wins
|
|
75
|
+
result[_a = tag.permalink] ?? (result[_a] = {
|
|
76
|
+
tag,
|
|
77
|
+
items: [],
|
|
78
|
+
});
|
|
79
|
+
// Add item to group
|
|
80
|
+
result[tag.permalink].items.push(item);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
// If user add twice the same tag to a md doc (weird but possible),
|
|
84
|
+
// we don't want the item to appear twice in the list...
|
|
85
|
+
Object.values(result).forEach((group) => {
|
|
86
|
+
group.items = lodash_1.default.uniq(group.items);
|
|
87
|
+
});
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
exports.groupTaggedItems = groupTaggedItems;
|
|
91
|
+
//# sourceMappingURL=tags.js.map
|
package/lib/tags.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tags.js","sourceRoot":"","sources":["../src/tags.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,4DAAuB;AACvB,yCAAwC;AAuBxC,SAAS,uBAAuB,CAC9B,QAAgB,EAChB,cAA8B;IAE9B,SAAS,WAAW,CAAC,SAAiB;QACpC,OAAO;YACL,KAAK,EAAE,SAAS;YAChB,SAAS,EAAE,gBAAC,CAAC,SAAS,CAAC,SAAS,CAAC;SAClC,CAAC;IACJ,CAAC;IAED,0DAA0D;IAC1D,SAAS,qBAAqB,CAAC,SAAiB;QAC9C,2EAA2E;QAC3E,2EAA2E;QAC3E,mDAAmD;QACnD,OAAO,IAAA,uBAAY,EAAC,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,GAAG,GACP,OAAO,cAAc,KAAK,QAAQ;QAChC,CAAC,CAAC,WAAW,CAAC,cAAc,CAAC;QAC7B,CAAC,CAAC,cAAc,CAAC;IAErB,OAAO;QACL,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,SAAS,EAAE,qBAAqB,CAAC,GAAG,CAAC,SAAS,CAAC;KAChD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@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Much like `path.join`, but much better. Takes an array of URL segments, and
|
|
9
|
+
* joins them into a reasonable URL.
|
|
10
|
+
*
|
|
11
|
+
* - `["file:", "/home", "/user/", "website"]` => `file:///home/user/website`
|
|
12
|
+
* - `["file://", "home", "/user/", "website"]` => `file://home/user/website` (relative!)
|
|
13
|
+
* - Remove trailing slash before parameters or hash.
|
|
14
|
+
* - Replace `?` in query parameters with `&`.
|
|
15
|
+
* - Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
16
|
+
*
|
|
17
|
+
* @throws {TypeError} If any of the URL segment is not a string, this throws.
|
|
18
|
+
*/
|
|
19
|
+
export declare function normalizeUrl(rawUrls: string[]): string;
|
|
20
|
+
/**
|
|
21
|
+
* Takes a file's path, relative to its content folder, and computes its edit
|
|
22
|
+
* URL. If `editUrl` is `undefined`, this returns `undefined`, as is the case
|
|
23
|
+
* when the user doesn't want an edit URL in her config.
|
|
24
|
+
*/
|
|
25
|
+
export declare function getEditUrl(fileRelativePath: string, editUrl?: string): string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Converts file path to a reasonable URL path, e.g. `'index.md'` -> `'/'`,
|
|
28
|
+
* `'foo/bar.js'` -> `'/foo/bar'`
|
|
29
|
+
*/
|
|
30
|
+
export declare function fileToPath(file: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Similar to `encodeURI`, but uses `encodeURIComponent` and assumes there's no
|
|
33
|
+
* query.
|
|
34
|
+
*
|
|
35
|
+
* `encodeURI("/question?/answer")` => `"/question?/answer#section"`;
|
|
36
|
+
* `encodePath("/question?/answer#section")` => `"/question%3F/answer%23foo"`
|
|
37
|
+
*/
|
|
38
|
+
export declare function encodePath(userPath: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Whether `str` is a valid pathname. It must be absolute, and not contain
|
|
41
|
+
* special characters.
|
|
42
|
+
*/
|
|
43
|
+
export declare function isValidPathname(str: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Resolve pathnames and fail-fast if resolution fails. Uses standard URL
|
|
46
|
+
* semantics (provided by `resolve-pathname` which is used internally by React
|
|
47
|
+
* router)
|
|
48
|
+
*/
|
|
49
|
+
export declare function resolvePathname(to: string, from?: string): string;
|
|
50
|
+
/** Appends a leading slash to `str`, if one doesn't exist. */
|
|
51
|
+
export declare function addLeadingSlash(str: string): string;
|
|
52
|
+
/** Appends a trailing slash to `str`, if one doesn't exist. */
|
|
53
|
+
export declare function addTrailingSlash(str: string): string;
|
|
54
|
+
/** Removes the trailing slash from `str`. */
|
|
55
|
+
export declare function removeTrailingSlash(str: string): string;
|
|
56
|
+
/** Constructs an SSH URL that can be used to push to GitHub. */
|
|
57
|
+
export declare function buildSshUrl(githubHost: string, organizationName: string, projectName: string, githubPort?: string): string;
|
|
58
|
+
/** Constructs an HTTP URL that can be used to push to GitHub. */
|
|
59
|
+
export declare function buildHttpsUrl(gitCredentials: string, githubHost: string, organizationName: string, projectName: string, githubPort?: string): string;
|
|
60
|
+
/**
|
|
61
|
+
* Whether the current URL is an SSH protocol. In addition to looking for
|
|
62
|
+
* `ssh:`, it will also allow protocol-less URLs like
|
|
63
|
+
* `git@github.com:facebook/docusaurus.git`.
|
|
64
|
+
*/
|
|
65
|
+
export declare function hasSSHProtocol(sourceRepoUrl: string): boolean;
|
|
66
|
+
//# sourceMappingURL=urlUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urlUtils.d.ts","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,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yright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.hasSSHProtocol = exports.buildHttpsUrl = exports.buildSshUrl = exports.removeTrailingSlash = exports.addTrailingSlash = exports.addLeadingSlash = exports.resolvePathname = exports.isValidPathname = exports.encodePath = exports.fileToPath = exports.getEditUrl = exports.normalizeUrl = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const resolve_pathname_1 = tslib_1.__importDefault(require("resolve-pathname"));
|
|
12
|
+
const jsUtils_1 = require("./jsUtils");
|
|
13
|
+
/**
|
|
14
|
+
* Much like `path.join`, but much better. Takes an array of URL segments, and
|
|
15
|
+
* joins them into a reasonable URL.
|
|
16
|
+
*
|
|
17
|
+
* - `["file:", "/home", "/user/", "website"]` => `file:///home/user/website`
|
|
18
|
+
* - `["file://", "home", "/user/", "website"]` => `file://home/user/website` (relative!)
|
|
19
|
+
* - Remove trailing slash before parameters or hash.
|
|
20
|
+
* - Replace `?` in query parameters with `&`.
|
|
21
|
+
* - Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
22
|
+
*
|
|
23
|
+
* @throws {TypeError} If any of the URL segment is not a string, this throws.
|
|
24
|
+
*/
|
|
25
|
+
function normalizeUrl(rawUrls) {
|
|
26
|
+
const urls = [...rawUrls];
|
|
27
|
+
const resultArray = [];
|
|
28
|
+
let hasStartingSlash = false;
|
|
29
|
+
let hasEndingSlash = false;
|
|
30
|
+
const isNonEmptyArray = (arr) => arr.length > 0;
|
|
31
|
+
if (!isNonEmptyArray(urls)) {
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
// If the first part is a plain protocol, we combine it with the next part.
|
|
35
|
+
if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
|
|
36
|
+
const first = urls.shift();
|
|
37
|
+
if (first.startsWith('file:') && urls[0].startsWith('/')) {
|
|
38
|
+
// Force a double slash here, else we lose the information that the next
|
|
39
|
+
// segment is an absolute path
|
|
40
|
+
urls[0] = `${first}//${urls[0]}`;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
urls[0] = first + urls[0];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// There must be two or three slashes in the file protocol,
|
|
47
|
+
// two slashes in anything else.
|
|
48
|
+
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
|
|
49
|
+
urls[0] = urls[0].replace(/^(?<protocol>[^/:]+):\/*/, replacement);
|
|
50
|
+
for (let i = 0; i < urls.length; i += 1) {
|
|
51
|
+
let component = urls[i];
|
|
52
|
+
if (typeof component !== 'string') {
|
|
53
|
+
throw new TypeError(`Url must be a string. Received ${typeof component}`);
|
|
54
|
+
}
|
|
55
|
+
if (component === '') {
|
|
56
|
+
if (i === urls.length - 1 && hasEndingSlash) {
|
|
57
|
+
resultArray.push('/');
|
|
58
|
+
}
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (component !== '/') {
|
|
62
|
+
if (i > 0) {
|
|
63
|
+
// Removing the starting slashes for each component but the first.
|
|
64
|
+
component = component.replace(/^\/+/,
|
|
65
|
+
// Special case where the first element of rawUrls is empty
|
|
66
|
+
// ["", "/hello"] => /hello
|
|
67
|
+
component.startsWith('/') && !hasStartingSlash ? '/' : '');
|
|
68
|
+
}
|
|
69
|
+
hasEndingSlash = component.endsWith('/');
|
|
70
|
+
// Removing the ending slashes for each component but the last. For the
|
|
71
|
+
// last component we will combine multiple slashes to a single one.
|
|
72
|
+
component = component.replace(/\/+$/, i < urls.length - 1 ? '' : '/');
|
|
73
|
+
}
|
|
74
|
+
hasStartingSlash = true;
|
|
75
|
+
resultArray.push(component);
|
|
76
|
+
}
|
|
77
|
+
let str = resultArray.join('/');
|
|
78
|
+
// Each input component is now separated by a single slash except the possible
|
|
79
|
+
// first plain protocol part.
|
|
80
|
+
// Remove trailing slash before parameters or hash.
|
|
81
|
+
str = str.replace(/\/(?<search>\?|&|#[^!])/g, '$1');
|
|
82
|
+
// Replace ? in parameters with &.
|
|
83
|
+
const parts = str.split('?');
|
|
84
|
+
str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
|
|
85
|
+
// Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
86
|
+
str = str.replace(/(?<textBefore>[^:/]\/)\/+/g, '$1');
|
|
87
|
+
// Dedupe forward slashes at the beginning of the path.
|
|
88
|
+
str = str.replace(/^\/+/g, '/');
|
|
89
|
+
return str;
|
|
90
|
+
}
|
|
91
|
+
exports.normalizeUrl = normalizeUrl;
|
|
92
|
+
/**
|
|
93
|
+
* Takes a file's path, relative to its content folder, and computes its edit
|
|
94
|
+
* URL. If `editUrl` is `undefined`, this returns `undefined`, as is the case
|
|
95
|
+
* when the user doesn't want an edit URL in her config.
|
|
96
|
+
*/
|
|
97
|
+
function getEditUrl(fileRelativePath, editUrl) {
|
|
98
|
+
return editUrl
|
|
99
|
+
? // Don't use posixPath for this: we need to force a forward slash path
|
|
100
|
+
normalizeUrl([editUrl, fileRelativePath.replace(/\\/g, '/')])
|
|
101
|
+
: undefined;
|
|
102
|
+
}
|
|
103
|
+
exports.getEditUrl = getEditUrl;
|
|
104
|
+
/**
|
|
105
|
+
* Converts file path to a reasonable URL path, e.g. `'index.md'` -> `'/'`,
|
|
106
|
+
* `'foo/bar.js'` -> `'/foo/bar'`
|
|
107
|
+
*/
|
|
108
|
+
function fileToPath(file) {
|
|
109
|
+
const indexRE = /(?<dirname>^|.*\/)index\.(?:mdx?|jsx?|tsx?)$/i;
|
|
110
|
+
const extRE = /\.(?:mdx?|jsx?|tsx?)$/;
|
|
111
|
+
if (indexRE.test(file)) {
|
|
112
|
+
return file.replace(indexRE, '/$1');
|
|
113
|
+
}
|
|
114
|
+
return `/${file.replace(extRE, '').replace(/\\/g, '/')}`;
|
|
115
|
+
}
|
|
116
|
+
exports.fileToPath = fileToPath;
|
|
117
|
+
/**
|
|
118
|
+
* Similar to `encodeURI`, but uses `encodeURIComponent` and assumes there's no
|
|
119
|
+
* query.
|
|
120
|
+
*
|
|
121
|
+
* `encodeURI("/question?/answer")` => `"/question?/answer#section"`;
|
|
122
|
+
* `encodePath("/question?/answer#section")` => `"/question%3F/answer%23foo"`
|
|
123
|
+
*/
|
|
124
|
+
function encodePath(userPath) {
|
|
125
|
+
return userPath
|
|
126
|
+
.split('/')
|
|
127
|
+
.map((item) => encodeURIComponent(item))
|
|
128
|
+
.join('/');
|
|
129
|
+
}
|
|
130
|
+
exports.encodePath = encodePath;
|
|
131
|
+
/**
|
|
132
|
+
* Whether `str` is a valid pathname. It must be absolute, and not contain
|
|
133
|
+
* special characters.
|
|
134
|
+
*/
|
|
135
|
+
function isValidPathname(str) {
|
|
136
|
+
if (!str.startsWith('/')) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const parsedPathname = new URL(str, 'https://domain.com').pathname;
|
|
141
|
+
return parsedPathname === str || parsedPathname === encodeURI(str);
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.isValidPathname = isValidPathname;
|
|
148
|
+
/**
|
|
149
|
+
* Resolve pathnames and fail-fast if resolution fails. Uses standard URL
|
|
150
|
+
* semantics (provided by `resolve-pathname` which is used internally by React
|
|
151
|
+
* router)
|
|
152
|
+
*/
|
|
153
|
+
function resolvePathname(to, from) {
|
|
154
|
+
return (0, resolve_pathname_1.default)(to, from);
|
|
155
|
+
}
|
|
156
|
+
exports.resolvePathname = resolvePathname;
|
|
157
|
+
/** Appends a leading slash to `str`, if one doesn't exist. */
|
|
158
|
+
function addLeadingSlash(str) {
|
|
159
|
+
return str.startsWith('/') ? str : `/${str}`;
|
|
160
|
+
}
|
|
161
|
+
exports.addLeadingSlash = addLeadingSlash;
|
|
162
|
+
// TODO deduplicate: also present in @docusaurus/utils-common
|
|
163
|
+
/** Appends a trailing slash to `str`, if one doesn't exist. */
|
|
164
|
+
function addTrailingSlash(str) {
|
|
165
|
+
return str.endsWith('/') ? str : `${str}/`;
|
|
166
|
+
}
|
|
167
|
+
exports.addTrailingSlash = addTrailingSlash;
|
|
168
|
+
/** Removes the trailing slash from `str`. */
|
|
169
|
+
function removeTrailingSlash(str) {
|
|
170
|
+
return (0, jsUtils_1.removeSuffix)(str, '/');
|
|
171
|
+
}
|
|
172
|
+
exports.removeTrailingSlash = removeTrailingSlash;
|
|
173
|
+
/** Constructs an SSH URL that can be used to push to GitHub. */
|
|
174
|
+
function buildSshUrl(githubHost, organizationName, projectName, githubPort) {
|
|
175
|
+
if (githubPort) {
|
|
176
|
+
return `ssh://git@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`;
|
|
177
|
+
}
|
|
178
|
+
return `git@${githubHost}:${organizationName}/${projectName}.git`;
|
|
179
|
+
}
|
|
180
|
+
exports.buildSshUrl = buildSshUrl;
|
|
181
|
+
/** Constructs an HTTP URL that can be used to push to GitHub. */
|
|
182
|
+
function buildHttpsUrl(gitCredentials, githubHost, organizationName, projectName, githubPort) {
|
|
183
|
+
if (githubPort) {
|
|
184
|
+
return `https://${gitCredentials}@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`;
|
|
185
|
+
}
|
|
186
|
+
return `https://${gitCredentials}@${githubHost}/${organizationName}/${projectName}.git`;
|
|
187
|
+
}
|
|
188
|
+
exports.buildHttpsUrl = buildHttpsUrl;
|
|
189
|
+
/**
|
|
190
|
+
* Whether the current URL is an SSH protocol. In addition to looking for
|
|
191
|
+
* `ssh:`, it will also allow protocol-less URLs like
|
|
192
|
+
* `git@github.com:facebook/docusaurus.git`.
|
|
193
|
+
*/
|
|
194
|
+
function hasSSHProtocol(sourceRepoUrl) {
|
|
195
|
+
try {
|
|
196
|
+
if (new URL(sourceRepoUrl).protocol === 'ssh:') {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
// Fails when there isn't a protocol
|
|
203
|
+
return /^(?:[\w-]+@)?[\w.-]+:[\w./-]+/.test(sourceRepoUrl);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
exports.hasSSHProtocol = hasSSHProtocol;
|
|
207
|
+
//# sourceMappingURL=urlUtils.js.map
|