@docusaurus/utils 0.0.0-4637 → 0.0.0-4643
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/lib/emitUtils.d.ts +23 -0
- package/lib/emitUtils.d.ts.map +1 -0
- package/lib/emitUtils.js +90 -0
- package/lib/emitUtils.js.map +1 -0
- package/lib/i18nUtils.d.ts +17 -0
- package/lib/i18nUtils.d.ts.map +1 -0
- package/lib/i18nUtils.js +40 -0
- package/lib/i18nUtils.js.map +1 -0
- package/lib/index.d.ts +5 -49
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +43 -249
- package/lib/index.js.map +1 -1
- package/lib/jsUtils.d.ts +17 -0
- package/lib/jsUtils.d.ts.map +1 -0
- package/lib/jsUtils.js +72 -0
- package/lib/jsUtils.js.map +1 -0
- package/lib/pathUtils.d.ts +1 -0
- package/lib/pathUtils.d.ts.map +1 -1
- package/lib/pathUtils.js +9 -2
- package/lib/pathUtils.js.map +1 -1
- package/lib/urlUtils.d.ts +11 -0
- package/lib/urlUtils.d.ts.map +1 -1
- package/lib/urlUtils.js +56 -1
- package/lib/urlUtils.js.map +1 -1
- package/package.json +4 -4
- package/src/emitUtils.ts +113 -0
- package/src/i18nUtils.ts +58 -0
- package/src/index.ts +26 -300
- package/src/jsUtils.ts +88 -0
- package/src/pathUtils.ts +8 -2
- package/src/urlUtils.ts +53 -0
package/lib/jsUtils.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
import type { ReportingSeverity } from '@docusaurus/types';
|
|
8
|
+
export declare function removeSuffix(str: string, suffix: string): string;
|
|
9
|
+
export declare function removePrefix(str: string, prefix: string): string;
|
|
10
|
+
export declare function getElementsAround<T>(array: T[], aroundIndex: number): {
|
|
11
|
+
next: T | undefined;
|
|
12
|
+
previous: T | undefined;
|
|
13
|
+
};
|
|
14
|
+
export declare function mapAsyncSequential<T, R>(array: T[], action: (t: T) => Promise<R>): Promise<R[]>;
|
|
15
|
+
export declare function findAsyncSequential<T>(array: T[], predicate: (t: T) => Promise<boolean>): Promise<T | undefined>;
|
|
16
|
+
export declare function reportMessage(message: string, reportingSeverity: ReportingSeverity): void;
|
|
17
|
+
//# sourceMappingURL=jsUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsUtils.d.ts","sourceRoot":"","sources":["../src/jsUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,mBAAmB,CAAC;AAGzD,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAKhE;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,KAAK,EAAE,CAAC,EAAE,EACV,WAAW,EAAE,MAAM,GAClB;IACD,IAAI,EAAE,CAAC,GAAG,SAAS,CAAC;IACpB,QAAQ,EAAE,CAAC,GAAG,SAAS,CAAC;CACzB,CAWA;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EAAE,CAAC,EAC3C,KAAK,EAAE,CAAC,EAAE,EACV,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAC3B,OAAO,CAAC,CAAC,EAAE,CAAC,CAOd;AAED,wBAAsB,mBAAmB,CAAC,CAAC,EACzC,KAAK,EAAE,CAAC,EAAE,EACV,SAAS,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,GACpC,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAOxB;AAED,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,iBAAiB,EAAE,iBAAiB,GACnC,IAAI,CAoBN"}
|
package/lib/jsUtils.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
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.reportMessage = exports.findAsyncSequential = exports.mapAsyncSequential = exports.getElementsAround = exports.removePrefix = exports.removeSuffix = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
12
|
+
function removeSuffix(str, suffix) {
|
|
13
|
+
if (suffix === '') {
|
|
14
|
+
return str; // always returns "" otherwise!
|
|
15
|
+
}
|
|
16
|
+
return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
|
|
17
|
+
}
|
|
18
|
+
exports.removeSuffix = removeSuffix;
|
|
19
|
+
function removePrefix(str, prefix) {
|
|
20
|
+
return str.startsWith(prefix) ? str.slice(prefix.length) : str;
|
|
21
|
+
}
|
|
22
|
+
exports.removePrefix = removePrefix;
|
|
23
|
+
function getElementsAround(array, aroundIndex) {
|
|
24
|
+
const min = 0;
|
|
25
|
+
const max = array.length - 1;
|
|
26
|
+
if (aroundIndex < min || aroundIndex > max) {
|
|
27
|
+
throw new Error(`Valid "aroundIndex" for array (of size ${array.length}) are between ${min} and ${max}, but you provided ${aroundIndex}.`);
|
|
28
|
+
}
|
|
29
|
+
const previous = aroundIndex === min ? undefined : array[aroundIndex - 1];
|
|
30
|
+
const next = aroundIndex === max ? undefined : array[aroundIndex + 1];
|
|
31
|
+
return { previous, next };
|
|
32
|
+
}
|
|
33
|
+
exports.getElementsAround = getElementsAround;
|
|
34
|
+
async function mapAsyncSequential(array, action) {
|
|
35
|
+
const results = [];
|
|
36
|
+
for (const t of array) {
|
|
37
|
+
const result = await action(t);
|
|
38
|
+
results.push(result);
|
|
39
|
+
}
|
|
40
|
+
return results;
|
|
41
|
+
}
|
|
42
|
+
exports.mapAsyncSequential = mapAsyncSequential;
|
|
43
|
+
async function findAsyncSequential(array, predicate) {
|
|
44
|
+
for (const t of array) {
|
|
45
|
+
if (await predicate(t)) {
|
|
46
|
+
return t;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
exports.findAsyncSequential = findAsyncSequential;
|
|
52
|
+
function reportMessage(message, reportingSeverity) {
|
|
53
|
+
switch (reportingSeverity) {
|
|
54
|
+
case 'ignore':
|
|
55
|
+
break;
|
|
56
|
+
case 'log':
|
|
57
|
+
logger_1.default.info(message);
|
|
58
|
+
break;
|
|
59
|
+
case 'warn':
|
|
60
|
+
logger_1.default.warn(message);
|
|
61
|
+
break;
|
|
62
|
+
case 'error':
|
|
63
|
+
logger_1.default.error(message);
|
|
64
|
+
break;
|
|
65
|
+
case 'throw':
|
|
66
|
+
throw new Error(message);
|
|
67
|
+
default:
|
|
68
|
+
throw new Error(`Unexpected "reportingSeverity" value: ${reportingSeverity}.`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.reportMessage = reportMessage;
|
|
72
|
+
//# sourceMappingURL=jsUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsUtils.js","sourceRoot":"","sources":["../src/jsUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAGH,6EAAwC;AAExC,SAAgB,YAAY,CAAC,GAAW,EAAE,MAAc;IACtD,IAAI,MAAM,KAAK,EAAE,EAAE;QACjB,OAAO,GAAG,CAAC,CAAC,+BAA+B;KAC5C;IACD,OAAO,GAAG,CAAC,QAAQ,CAAC,MAAM,C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AC,MAAM,CAAC,CAAC;KACtB;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAVD,gDAUC;AAEM,KAAK,UAAU,mBAAmB,CACvC,KAAU,EACV,SAAqC;IAErC,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE;QACrB,IAAI,MAAM,SAAS,CAAC,CAAC,CAAC,EAAE;YACtB,OAAO,CAAC,CAAC;SACV;KACF;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAVD,kDAUC;AAED,SAAgB,aAAa,CAC3B,OAAe,EACf,iBAAoC;IAEpC,QAAQ,iBAAiB,EAAE;QACzB,KAAK,QAAQ;YACX,MAAM;QACR,KAAK,KAAK;YACR,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACrB,MAAM;QACR,KAAK,MAAM;YACT,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACrB,MAAM;QACR,KAAK,OAAO;YACV,gBAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YACtB,MAAM;QACR,KAAK,OAAO;YACV,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;QAC3B;YACE,MAAM,IAAI,KAAK,CACb,yCAAyC,iBAAiB,GAAG,CAC9D,CAAC;KACL;AACH,CAAC;AAvBD,sCAuBC"}
|
package/lib/pathUtils.d.ts
CHANGED
|
@@ -48,4 +48,5 @@ export declare function aliasedSitePath(filePath: string, siteDir: string): stri
|
|
|
48
48
|
* doesn't escape those that fail to be converted.
|
|
49
49
|
*/
|
|
50
50
|
export declare function escapePath(str: string): string;
|
|
51
|
+
export declare function addTrailingPathSeparator(str: string): string;
|
|
51
52
|
//# sourceMappingURL=pathUtils.d.ts.map
|
package/lib/pathUtils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pathUtils.d.ts","sourceRoot":"","sources":["../src/pathUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"pathUtils.d.ts","sourceRoot":"","sources":["../src/pathUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,eAAO,MAAM,aAAa,QAAS,MAAM,KAAG,OAIgC,CAAC;AAE7E,eAAO,MAAM,SAAS,QAAS,MAAM,KAAG,MAiBvC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAY7C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,yBAAyB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAKzE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAK9C;AAED,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAK5D"}
|
package/lib/pathUtils.js
CHANGED
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.escapePath = exports.aliasedSitePath = exports.toMessageRelativeFilePath = exports.posixPath = exports.shortName = exports.isNameTooLong = void 0;
|
|
9
|
+
exports.addTrailingPathSeparator = exports.escapePath = exports.aliasedSitePath = exports.toMessageRelativeFilePath = exports.posixPath = exports.shortName = exports.isNameTooLong = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
|
-
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
12
11
|
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
12
|
+
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
13
13
|
// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars,
|
|
14
14
|
// Others = 255 bytes
|
|
15
15
|
const MAX_PATH_SEGMENT_CHARS = 255;
|
|
@@ -103,4 +103,11 @@ function escapePath(str) {
|
|
|
103
103
|
return escaped.substring(1, escaped.length - 1);
|
|
104
104
|
}
|
|
105
105
|
exports.escapePath = escapePath;
|
|
106
|
+
function addTrailingPathSeparator(str) {
|
|
107
|
+
return str.endsWith(path_1.default.sep)
|
|
108
|
+
? str
|
|
109
|
+
: // If this is Windows, we need to change the forward slash to backward
|
|
110
|
+
`${str.replace(/\/$/, '')}${path_1.default.sep}`;
|
|
111
|
+
}
|
|
112
|
+
exports.addTrailingPathSeparator = addTrailingPathSeparator;
|
|
106
113
|
//# sourceMappingURL=pathUtils.js.map
|
package/lib/pathUtils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pathUtils.js","sourceRoot":"","sources":["../src/pathUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,+DAA+D;
|
|
1
|
+
{"version":3,"file":"pathUtils.js","sourceRoot":"","sources":["../src/pathUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,6DAAwB;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,CAAC,GAAG,CAAC,MAAM,GAAG,mBAAmB,GAAG,sBAAsB,CAAC,oEAAoE;IAChI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,C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,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"}
|
package/lib/urlUtils.d.ts
CHANGED
|
@@ -6,4 +6,15 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export declare function normalizeUrl(rawUrls: string[]): string;
|
|
8
8
|
export declare function getEditUrl(fileRelativePath: string, editUrl?: string): string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Convert filepath to url path.
|
|
11
|
+
* Example: 'index.md' -> '/', 'foo/bar.js' -> '/foo/bar',
|
|
12
|
+
*/
|
|
13
|
+
export declare function fileToPath(file: string): string;
|
|
14
|
+
export declare function encodePath(userPath: string): string;
|
|
15
|
+
export declare function isValidPathname(str: string): boolean;
|
|
16
|
+
export declare function resolvePathname(to: string, from?: string): string;
|
|
17
|
+
export declare function addLeadingSlash(str: string): string;
|
|
18
|
+
export declare function addTrailingSlash(str: string): string;
|
|
19
|
+
export declare function removeTrailingSlash(str: string): string;
|
|
9
20
|
//# sourceMappingURL=urlUtils.d.ts.map
|
package/lib/urlUtils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlUtils.d.ts","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"urlUtils.d.ts","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CA8EtD;AAED,wBAAgB,UAAU,CACxB,gBAAgB,EAAE,MAAM,EACxB,OAAO,CAAC,EAAE,MAAM,GACf,MAAM,GAAG,SAAS,CAKpB;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAQ/C;AAED,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAKnD;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAWpD;AAGD,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAEjE;AACD,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEnD;AAGD,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEpD;AACD,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEvD"}
|
package/lib/urlUtils.js
CHANGED
|
@@ -6,7 +6,10 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.getEditUrl = exports.normalizeUrl = void 0;
|
|
9
|
+
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 jsUtils_1 = require("./jsUtils");
|
|
12
|
+
const resolve_pathname_1 = (0, tslib_1.__importDefault)(require("resolve-pathname"));
|
|
10
13
|
function normalizeUrl(rawUrls) {
|
|
11
14
|
const urls = [...rawUrls];
|
|
12
15
|
const resultArray = [];
|
|
@@ -78,4 +81,56 @@ function getEditUrl(fileRelativePath, editUrl) {
|
|
|
78
81
|
: undefined;
|
|
79
82
|
}
|
|
80
83
|
exports.getEditUrl = getEditUrl;
|
|
84
|
+
/**
|
|
85
|
+
* Convert filepath to url path.
|
|
86
|
+
* Example: 'index.md' -> '/', 'foo/bar.js' -> '/foo/bar',
|
|
87
|
+
*/
|
|
88
|
+
function fileToPath(file) {
|
|
89
|
+
const indexRE = /(?<dirname>^|.*\/)index\.(?:mdx?|jsx?|tsx?)$/i;
|
|
90
|
+
const extRE = /\.(?:mdx?|jsx?|tsx?)$/;
|
|
91
|
+
if (indexRE.test(file)) {
|
|
92
|
+
return file.replace(indexRE, '/$1');
|
|
93
|
+
}
|
|
94
|
+
return `/${file.replace(extRE, '').replace(/\\/g, '/')}`;
|
|
95
|
+
}
|
|
96
|
+
exports.fileToPath = fileToPath;
|
|
97
|
+
function encodePath(userPath) {
|
|
98
|
+
return userPath
|
|
99
|
+
.split('/')
|
|
100
|
+
.map((item) => encodeURIComponent(item))
|
|
101
|
+
.join('/');
|
|
102
|
+
}
|
|
103
|
+
exports.encodePath = encodePath;
|
|
104
|
+
function isValidPathname(str) {
|
|
105
|
+
if (!str.startsWith('/')) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
// weird, but is there a better way?
|
|
110
|
+
const parsedPathname = new URL(str, 'https://domain.com').pathname;
|
|
111
|
+
return parsedPathname === str || parsedPathname === encodeURI(str);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.isValidPathname = isValidPathname;
|
|
118
|
+
// resolve pathname and fail fast if resolution fails
|
|
119
|
+
function resolvePathname(to, from) {
|
|
120
|
+
return (0, resolve_pathname_1.default)(to, from);
|
|
121
|
+
}
|
|
122
|
+
exports.resolvePathname = resolvePathname;
|
|
123
|
+
function addLeadingSlash(str) {
|
|
124
|
+
return str.startsWith('/') ? str : `/${str}`;
|
|
125
|
+
}
|
|
126
|
+
exports.addLeadingSlash = addLeadingSlash;
|
|
127
|
+
// TODO deduplicate: also present in @docusaurus/utils-common
|
|
128
|
+
function addTrailingSlash(str) {
|
|
129
|
+
return str.endsWith('/') ? str : `${str}/`;
|
|
130
|
+
}
|
|
131
|
+
exports.addTrailingSlash = addTrailingSlash;
|
|
132
|
+
function removeTrailingSlash(str) {
|
|
133
|
+
return (0, jsUtils_1.removeSuffix)(str, '/');
|
|
134
|
+
}
|
|
135
|
+
exports.removeTrailingSlash = removeTrailingSlash;
|
|
81
136
|
//# sourceMappingURL=urlUtils.js.map
|
package/lib/urlUtils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlUtils.js","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG
|
|
1
|
+
{"version":3,"file":"urlUtils.js","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,uCAAuC;AACvC,qFAAqD;AAErD,SAAgB,YAAY,CAAC,OAAiB;IAC5C,MAAM,IAAI,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC;IAC1B,MAAM,WAAW,GAAG,EAAE,CAAC;IAEvB,IAAI,gBAAgB,GAAG,KAAK,CAAC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BAAoB,CAAC,CAAC,QAAQ,CAAC;QACnE,OAAO,cAAc,KAAK,GAAG,IAAI,cAAc,KAAK,SAAS,CAAC,GAAG,CAAC,CAAC;KACpE;IAAC,MAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAXD,0CAWC;AAED,qDAAqD;AACrD,SAAgB,eAAe,CAAC,EAAU,EAAE,IAAa;IACvD,OAAO,IAAA,0BAAqB,EAAC,EAAE,EAAE,IAAI,CAAC,CAAC;AACzC,CAAC;AAFD,0CAEC;AACD,SAAgB,eAAe,CAAC,GAAW;IACzC,OAAO,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,CAAC;AAC/C,CAAC;AAFD,0CAEC;AAED,6DAA6D;AAC7D,SAAgB,gBAAgB,CAAC,GAAW;IAC1C,OAAO,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC;AAC7C,CAAC;AAFD,4CAEC;AACD,SAAgB,mBAAmB,CAAC,GAAW;IAC7C,OAAO,IAAA,sBAAY,EAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AAChC,CAAC;AAFD,kDAEC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/utils",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-4643",
|
|
4
4
|
"description": "Node utility functions for Docusaurus packages.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/logger": "0.0.0-
|
|
21
|
+
"@docusaurus/logger": "0.0.0-4643",
|
|
22
22
|
"@svgr/webpack": "^6.0.0",
|
|
23
23
|
"file-loader": "^6.2.0",
|
|
24
24
|
"fs-extra": "^10.0.1",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
"node": ">=14"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@docusaurus/types": "0.0.0-
|
|
41
|
+
"@docusaurus/types": "0.0.0-4643",
|
|
42
42
|
"@types/dedent": "^0.7.0",
|
|
43
43
|
"@types/github-slugger": "^1.3.0",
|
|
44
44
|
"@types/micromatch": "^4.0.2",
|
|
45
45
|
"@types/react-dom": "^17.0.11",
|
|
46
46
|
"dedent": "^0.7.0"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "b7b3ec259053eb652d5c967558115466e22f13ad"
|
|
49
49
|
}
|
package/src/emitUtils.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
import path from 'path';
|
|
9
|
+
import fs from 'fs-extra';
|
|
10
|
+
import {createHash} from 'crypto';
|
|
11
|
+
import {simpleHash, docuHash} from './hashUtils';
|
|
12
|
+
import {findAsyncSequential} from './jsUtils';
|
|
13
|
+
|
|
14
|
+
const fileHash = new Map<string, string>();
|
|
15
|
+
|
|
16
|
+
export async function generate(
|
|
17
|
+
generatedFilesDir: string,
|
|
18
|
+
file: string,
|
|
19
|
+
content: string,
|
|
20
|
+
skipCache: boolean = process.env.NODE_ENV === 'production',
|
|
21
|
+
): Promise<void> {
|
|
22
|
+
const filepath = path.join(generatedFilesDir, file);
|
|
23
|
+
|
|
24
|
+
if (skipCache) {
|
|
25
|
+
await fs.ensureDir(path.dirname(filepath));
|
|
26
|
+
await fs.writeFile(filepath, content);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let lastHash = fileHash.get(filepath);
|
|
31
|
+
|
|
32
|
+
// If file already exists but its not in runtime cache yet,
|
|
33
|
+
// we try to calculate the content hash and then compare
|
|
34
|
+
// This is to avoid unnecessary overwriting and we can reuse old file.
|
|
35
|
+
if (!lastHash && (await fs.pathExists(filepath))) {
|
|
36
|
+
const lastContent = await fs.readFile(filepath, 'utf8');
|
|
37
|
+
lastHash = createHash('md5').update(lastContent).digest('hex');
|
|
38
|
+
fileHash.set(filepath, lastHash);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const currentHash = createHash('md5').update(content).digest('hex');
|
|
42
|
+
|
|
43
|
+
if (lastHash !== currentHash) {
|
|
44
|
+
await fs.ensureDir(path.dirname(filepath));
|
|
45
|
+
await fs.writeFile(filepath, content);
|
|
46
|
+
fileHash.set(filepath, currentHash);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const chunkNameCache = new Map();
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Generate unique chunk name given a module path.
|
|
54
|
+
*/
|
|
55
|
+
export function genChunkName(
|
|
56
|
+
modulePath: string,
|
|
57
|
+
prefix?: string,
|
|
58
|
+
preferredName?: string,
|
|
59
|
+
shortId: boolean = process.env.NODE_ENV === 'production',
|
|
60
|
+
): string {
|
|
61
|
+
let chunkName: string | undefined = chunkNameCache.get(modulePath);
|
|
62
|
+
if (!chunkName) {
|
|
63
|
+
if (shortId) {
|
|
64
|
+
chunkName = simpleHash(modulePath, 8);
|
|
65
|
+
} else {
|
|
66
|
+
let str = modulePath;
|
|
67
|
+
if (preferredName) {
|
|
68
|
+
const shortHash = simpleHash(modulePath, 3);
|
|
69
|
+
str = `${preferredName}${shortHash}`;
|
|
70
|
+
}
|
|
71
|
+
const name = str === '/' ? 'index' : docuHash(str);
|
|
72
|
+
chunkName = prefix ? `${prefix}---${name}` : name;
|
|
73
|
+
}
|
|
74
|
+
chunkNameCache.set(modulePath, chunkName);
|
|
75
|
+
}
|
|
76
|
+
return chunkName;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param permalink The URL that the HTML file corresponds to, without base URL
|
|
81
|
+
* @param outDir Full path to the output directory
|
|
82
|
+
* @param trailingSlash The site config option. If provided, only one path will
|
|
83
|
+
* be read.
|
|
84
|
+
* @returns This returns a buffer, which you have to decode string yourself if
|
|
85
|
+
* needed. (Not always necessary since the output isn't for human consumption
|
|
86
|
+
* anyways, and most HTML manipulation libs accept buffers)
|
|
87
|
+
*/
|
|
88
|
+
export async function readOutputHTMLFile(
|
|
89
|
+
permalink: string,
|
|
90
|
+
outDir: string,
|
|
91
|
+
trailingSlash: boolean | undefined,
|
|
92
|
+
): Promise<Buffer> {
|
|
93
|
+
const withTrailingSlashPath = path.join(outDir, permalink, 'index.html');
|
|
94
|
+
const withoutTrailingSlashPath = path.join(
|
|
95
|
+
outDir,
|
|
96
|
+
`${permalink.replace(/\/$/, '')}.html`,
|
|
97
|
+
);
|
|
98
|
+
if (trailingSlash) {
|
|
99
|
+
return fs.readFile(withTrailingSlashPath);
|
|
100
|
+
} else if (trailingSlash === false) {
|
|
101
|
+
return fs.readFile(withoutTrailingSlashPath);
|
|
102
|
+
}
|
|
103
|
+
const HTMLPath = await findAsyncSequential(
|
|
104
|
+
[withTrailingSlashPath, withoutTrailingSlashPath],
|
|
105
|
+
fs.pathExists,
|
|
106
|
+
);
|
|
107
|
+
if (!HTMLPath) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`Expected output HTML file to be found at ${withTrailingSlashPath}`,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return fs.readFile(HTMLPath);
|
|
113
|
+
}
|
package/src/i18nUtils.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
import path from 'path';
|
|
9
|
+
import _ from 'lodash';
|
|
10
|
+
import type {TranslationFileContent, TranslationFile} from '@docusaurus/types';
|
|
11
|
+
import {DEFAULT_PLUGIN_ID} from './constants';
|
|
12
|
+
|
|
13
|
+
export function mergeTranslations(
|
|
14
|
+
contents: TranslationFileContent[],
|
|
15
|
+
): TranslationFileContent {
|
|
16
|
+
return contents.reduce((acc, content) => ({...acc, ...content}), {});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Useful to update all the messages of a translation file
|
|
20
|
+
// Used in tests to simulate translations
|
|
21
|
+
export function updateTranslationFileMessages(
|
|
22
|
+
translationFile: TranslationFile,
|
|
23
|
+
updateMessage: (message: string) => string,
|
|
24
|
+
): TranslationFile {
|
|
25
|
+
return {
|
|
26
|
+
...translationFile,
|
|
27
|
+
content: _.mapValues(translationFile.content, (translation) => ({
|
|
28
|
+
...translation,
|
|
29
|
+
message: updateMessage(translation.message),
|
|
30
|
+
})),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getPluginI18nPath({
|
|
35
|
+
siteDir,
|
|
36
|
+
locale,
|
|
37
|
+
pluginName,
|
|
38
|
+
pluginId = DEFAULT_PLUGIN_ID,
|
|
39
|
+
subPaths = [],
|
|
40
|
+
}: {
|
|
41
|
+
siteDir: string;
|
|
42
|
+
locale: string;
|
|
43
|
+
pluginName: string;
|
|
44
|
+
pluginId?: string | undefined;
|
|
45
|
+
subPaths?: string[];
|
|
46
|
+
}): string {
|
|
47
|
+
return path.join(
|
|
48
|
+
siteDir,
|
|
49
|
+
'i18n',
|
|
50
|
+
// namespace first by locale: convenient to work in a single folder for a
|
|
51
|
+
// translator
|
|
52
|
+
locale,
|
|
53
|
+
// Make it convenient to use for single-instance
|
|
54
|
+
// ie: return "docs", not "docs-default" nor "docs/default"
|
|
55
|
+
`${pluginName}${pluginId === DEFAULT_PLUGIN_ID ? '' : `-${pluginId}`}`,
|
|
56
|
+
...subPaths,
|
|
57
|
+
);
|
|
58
|
+
}
|