@docusaurus/utils 0.0.0-4347 → 0.0.0-4355
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/dataFileUtils.d.ts +24 -0
- package/lib/dataFileUtils.d.ts.map +1 -0
- package/lib/dataFileUtils.js +68 -0
- package/lib/dataFileUtils.js.map +1 -0
- package/lib/index.d.ts +7 -38
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +8 -128
- package/lib/index.js.map +1 -1
- package/lib/markdownLinks.js +2 -2
- package/lib/markdownLinks.js.map +1 -1
- package/lib/markdownParser.d.ts +4 -0
- package/lib/markdownParser.d.ts.map +1 -1
- package/lib/markdownParser.js +17 -1
- package/lib/markdownParser.js.map +1 -1
- package/lib/pathUtils.d.ts +29 -0
- package/lib/pathUtils.d.ts.map +1 -1
- package/lib/pathUtils.js +63 -1
- package/lib/pathUtils.js.map +1 -1
- package/lib/tags.js +2 -2
- package/lib/tags.js.map +1 -1
- package/lib/{normalizeUrl.d.ts → urlUtils.d.ts} +2 -1
- package/lib/urlUtils.d.ts.map +1 -0
- package/lib/{normalizeUrl.js → urlUtils.js} +9 -2
- package/lib/urlUtils.js.map +1 -0
- package/lib/webpackUtils.js +3 -3
- package/lib/webpackUtils.js.map +1 -1
- package/package.json +5 -5
- package/src/dataFileUtils.ts +93 -0
- package/src/index.ts +8 -154
- package/src/markdownLinks.ts +1 -1
- package/src/markdownParser.ts +18 -0
- package/src/pathUtils.ts +65 -0
- package/src/tags.ts +1 -1
- package/src/{normalizeUrl.ts → urlUtils.ts} +10 -0
- package/src/webpackUtils.ts +1 -1
- package/lib/escapePath.d.ts +0 -18
- package/lib/escapePath.d.ts.map +0 -1
- package/lib/escapePath.js +0 -26
- package/lib/escapePath.js.map +0 -1
- package/lib/normalizeUrl.d.ts.map +0 -1
- package/lib/normalizeUrl.js.map +0 -1
- package/lib/posixPath.d.ts +0 -18
- package/lib/posixPath.d.ts.map +0 -1
- package/lib/posixPath.js +0 -31
- package/lib/posixPath.js.map +0 -1
- package/src/escapePath.ts +0 -23
- package/src/posixPath.ts +0 -29
package/lib/pathUtils.js
CHANGED
|
@@ -6,8 +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.shortName = exports.isNameTooLong = void 0;
|
|
9
|
+
exports.escapePath = exports.aliasedSitePath = exports.toMessageRelativeFilePath = exports.posixPath = exports.shortName = exports.isNameTooLong = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
10
11
|
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
12
|
+
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
11
13
|
// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
|
|
12
14
|
const MAX_PATH_SEGMENT_CHARS = 255;
|
|
13
15
|
const MAX_PATH_SEGMENT_BYTES = 255;
|
|
@@ -31,4 +33,64 @@ const shortName = (str) => {
|
|
|
31
33
|
.toString();
|
|
32
34
|
};
|
|
33
35
|
exports.shortName = shortName;
|
|
36
|
+
/**
|
|
37
|
+
* Convert Windows backslash paths to posix style paths.
|
|
38
|
+
* E.g: endi\lie -> endi/lie
|
|
39
|
+
*
|
|
40
|
+
* Returns original path if the posix counterpart is not valid Windows path.
|
|
41
|
+
* This makes the legacy code that uses posixPath safe; but also makes it less
|
|
42
|
+
* useful when you actually want a path with forward slashes (e.g. for URL)
|
|
43
|
+
*
|
|
44
|
+
* Adopted from https://github.com/sindresorhus/slash/blob/main/index.js
|
|
45
|
+
*/
|
|
46
|
+
function posixPath(str) {
|
|
47
|
+
const isExtendedLengthPath = /^\\\\\?\\/.test(str);
|
|
48
|
+
// Forward slashes are only valid Windows paths when they don't contain non-ascii characters.
|
|
49
|
+
// eslint-disable-next-line no-control-regex
|
|
50
|
+
const hasNonAscii = /[^\u0000-\u0080]+/.test(str);
|
|
51
|
+
if (isExtendedLengthPath || hasNonAscii) {
|
|
52
|
+
return str;
|
|
53
|
+
}
|
|
54
|
+
return str.replace(/\\/g, '/');
|
|
55
|
+
}
|
|
56
|
+
exports.posixPath = posixPath;
|
|
57
|
+
// When you want to display a path in a message/warning/error,
|
|
58
|
+
// it's more convenient to:
|
|
59
|
+
// - make it relative to cwd()
|
|
60
|
+
// - convert to posix (ie not using windows \ path separator)
|
|
61
|
+
// This way, Jest tests can run more reliably on any computer/CI
|
|
62
|
+
// on both Unix/Windows
|
|
63
|
+
// For Windows users this is not perfect (as they see / instead of \) but it's probably good enough
|
|
64
|
+
function toMessageRelativeFilePath(filePath) {
|
|
65
|
+
return posixPath(path_1.default.relative(process.cwd(), filePath));
|
|
66
|
+
}
|
|
67
|
+
exports.toMessageRelativeFilePath = toMessageRelativeFilePath;
|
|
68
|
+
/**
|
|
69
|
+
* Alias filepath relative to site directory, very useful so that we
|
|
70
|
+
* don't expose user's site structure.
|
|
71
|
+
* Example: some/path/to/website/docs/foo.md -> @site/docs/foo.md
|
|
72
|
+
*/
|
|
73
|
+
function aliasedSitePath(filePath, siteDir) {
|
|
74
|
+
const relativePath = posixPath(path_1.default.relative(siteDir, filePath));
|
|
75
|
+
// Cannot use path.join() as it resolves '../' and removes
|
|
76
|
+
// the '@site'. Let webpack loader resolve it.
|
|
77
|
+
return `@site/${relativePath}`;
|
|
78
|
+
}
|
|
79
|
+
exports.aliasedSitePath = aliasedSitePath;
|
|
80
|
+
/**
|
|
81
|
+
* When you have a path like C:\X\Y
|
|
82
|
+
* It is not safe to use directly when generating code
|
|
83
|
+
* For example, this would fail due to unescaped \: `<img src={require('${filePath}')} />`
|
|
84
|
+
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
|
85
|
+
*
|
|
86
|
+
* posixPath can't be used in all cases, because forward slashes are only valid
|
|
87
|
+
* Windows paths when they don't contain non-ascii characters, and posixPath
|
|
88
|
+
* doesn't escape those that fail to be converted.
|
|
89
|
+
*/
|
|
90
|
+
function escapePath(str) {
|
|
91
|
+
const escaped = JSON.stringify(str);
|
|
92
|
+
// Remove the " around the json string;
|
|
93
|
+
return escaped.substring(1, escaped.length - 1);
|
|
94
|
+
}
|
|
95
|
+
exports.escapePath = escapePath;
|
|
34
96
|
//# 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
|
|
1
|
+
{"version":3,"file":"pathUtils.js","sourceRoot":"","sources":["../src/pathUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,+DAA+D;AAE/D,6DAAwB;AAExB,wFAAwF;AACxF,MAAM,sBAAsB,GAAG,GAAG,CAAC;AACnC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AACnC,0EAA0E;AAC1E,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B,MAAM,OAAO,GAAG,OAAO,CAAC,QAAQ,KAAK,QAAQ,CAAC;AAC9C,MAAM,SAAS,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC;AAExC,MAAM,aAAa,GAAG,CAAC,GAAW,EAAW,EAAE,CACpD,OAAO,IAAI,SAAS;IAClB,CAAC,CAAC,GAAG,CAAC,MAAM,GAAG,mBAAmB,GAAG,sBAAsB,CAAC,oEAAoE;IAChI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,GAAG,mBAAmB,GAAG,sBAAsB,CAAC,CAAC,oBAAoB;AAHrF,QAAA,aAAa,iBAGmD;AAEtE,MAAM,SAAS,GAAG,CAAC,GAAW,EAAU,EAAE;IAC/C,IAAI,OAAO,IAAI,SAAS,EAAE;QACxB,MAAM,gBAAgB,GAAG,GAAG,CAAC,MAAM,GAAG,sBAAsB,CAAC;QAC7D,OAAO,GAAG,CAAC,KAAK,CACd,CAAC,EACD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IACjE,0DAA0D;IAC1D,8CAA8C;IAC9C,OAAO,SAAS,YAAY,EAAE,CAAC;AACjC,CAAC;AALD,0CAKC;AAED;;;;;;;;;GASG;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"}
|
package/lib/tags.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.groupTaggedItems = exports.normalizeFrontMatterTags = exports.normalizeFrontMatterTag = void 0;
|
|
10
10
|
const lodash_1 = require("lodash");
|
|
11
|
-
const
|
|
11
|
+
const urlUtils_1 = require("./urlUtils");
|
|
12
12
|
function normalizeFrontMatterTag(tagsPath, frontMatterTag) {
|
|
13
13
|
function toTagObject(tagString) {
|
|
14
14
|
return {
|
|
@@ -21,7 +21,7 @@ function normalizeFrontMatterTag(tagsPath, frontMatterTag) {
|
|
|
21
21
|
// note: we always apply tagsPath on purpose
|
|
22
22
|
// for versioned docs, v1/doc.md and v2/doc.md tags with custom permalinks don't lead to the same created page
|
|
23
23
|
// tagsPath is different for each doc version
|
|
24
|
-
return (0,
|
|
24
|
+
return (0, urlUtils_1.normalizeUrl)([tagsPath, permalink]);
|
|
25
25
|
}
|
|
26
26
|
const tag = typeof frontMatterTag === 'string'
|
|
27
27
|
? toTagObject(frontMatterTag)
|
package/lib/tags.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tags.js","sourceRoot":"","sources":["../src/tags.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,mCAA+C;AAC/C,
|
|
1
|
+
{"version":3,"file":"tags.js","sourceRoot":"","sources":["../src/tags.ts"],"names":[],"mappings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E,GAAQ;;QACzC,0BAA0B;QAC1B,+HAA+H;QAC/H,oCAAoC;QACpC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,MAAA,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,mCAAI;YAC/C,GAAG;YACH,KAAK,EAAE,EAAE;SACV,CAAC;QAEF,oBAAoB;QACpB,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QACrB,WAAW,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAChC,aAAa,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,mEAAmE;IACnE,wDAAwD;IACxD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QACtC,KAAK,CAAC,KAAK,GAAG,IAAA,aAAI,EAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC;AAhCD,4CAgCC"}
|
|
@@ -5,4 +5,5 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
export declare function normalizeUrl(rawUrls: string[]): string;
|
|
8
|
-
|
|
8
|
+
export declare function getEditUrl(fileRelativePath: string, editUrl?: string): string | undefined;
|
|
9
|
+
//# sourceMappingURL=urlUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urlUtils.d.ts","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAwEtD;AAED,wBAAgB,UAAU,CACxB,gBAAgB,EAAE,MAAM,EACxB,OAAO,CAAC,EAAE,MAAM,GACf,MAAM,GAAG,SAAS,CAKpB"}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.normalizeUrl = void 0;
|
|
9
|
+
exports.getEditUrl = exports.normalizeUrl = void 0;
|
|
10
10
|
function normalizeUrl(rawUrls) {
|
|
11
11
|
const urls = [...rawUrls];
|
|
12
12
|
const resultArray = [];
|
|
@@ -64,4 +64,11 @@ function normalizeUrl(rawUrls) {
|
|
|
64
64
|
return str;
|
|
65
65
|
}
|
|
66
66
|
exports.normalizeUrl = normalizeUrl;
|
|
67
|
-
|
|
67
|
+
function getEditUrl(fileRelativePath, editUrl) {
|
|
68
|
+
return editUrl
|
|
69
|
+
? // Don't use posixPath for this: we need to force a forward slash path
|
|
70
|
+
normalizeUrl([editUrl, fileRelativePath.replace(/\\/g, '/')])
|
|
71
|
+
: undefined;
|
|
72
|
+
}
|
|
73
|
+
exports.getEditUrl = getEditUrl;
|
|
74
|
+
//# sourceMappingURL=urlUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urlUtils.js","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,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;IAC7B,IAAI,cAAc,GAAG,KAAK,CAAC;IAE3B,2EAA2E;IAC3E,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,cAAc,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;QACpD,MAAM,KAAK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CHANGED
|
@@ -9,7 +9,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
exports.getFileLoaderUtils = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
12
|
-
const
|
|
12
|
+
const pathUtils_1 = require("./pathUtils");
|
|
13
13
|
const constants_1 = require("./constants");
|
|
14
14
|
// Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447
|
|
15
15
|
function getFileLoaderUtils() {
|
|
@@ -37,8 +37,8 @@ function getFileLoaderUtils() {
|
|
|
37
37
|
// Maybe with the ideal image plugin, all md images should be "ideal"?
|
|
38
38
|
// This is used to force url-loader+file-loader on markdown images
|
|
39
39
|
// https://webpack.js.org/concepts/loaders/#inline
|
|
40
|
-
inlineMarkdownImageFileLoader: `!${(0,
|
|
41
|
-
inlineMarkdownLinkFileLoader: `!${(0,
|
|
40
|
+
inlineMarkdownImageFileLoader: `!${(0, pathUtils_1.escapePath)(require.resolve('url-loader'))}?limit=${urlLoaderLimit}&name=${fileLoaderFileName('images')}&fallback=${(0, pathUtils_1.escapePath)(require.resolve('file-loader'))}!`,
|
|
41
|
+
inlineMarkdownLinkFileLoader: `!${(0, pathUtils_1.escapePath)(require.resolve('file-loader'))}?name=${fileLoaderFileName('files')}!`,
|
|
42
42
|
};
|
|
43
43
|
const rules = {
|
|
44
44
|
/**
|
package/lib/webpackUtils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webpackUtils.js","sourceRoot":"","sources":["../src/webpackUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAGH,6DAAwB;AACxB,
|
|
1
|
+
{"version":3,"file":"webpackUtils.js","sourceRoot":"","sources":["../src/webpackUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAGH,6DAAwB;AACxB,2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AAC,CAAC;YAClB,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,EAAC,MAAM,EAAE,OAAO,EAAC,CAAC,CAAC;YACtC,IAAI,EAAE,oCAAoC;SAC3C,CAAC;KACH,CAAC;IAEF,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,CAAC;AAC1B,CAAC;AA7GD,gDA6GC"}
|
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-4355",
|
|
4
4
|
"description": "Node utility functions for Docusaurus packages.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -18,15 +18,15 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/logger": "0.0.0-
|
|
21
|
+
"@docusaurus/logger": "0.0.0-4355",
|
|
22
22
|
"@mdx-js/runtime": "^1.6.22",
|
|
23
23
|
"@svgr/webpack": "^6.0.0",
|
|
24
|
-
"escape-string-regexp": "^4.0.0",
|
|
25
24
|
"file-loader": "^6.2.0",
|
|
26
25
|
"fs-extra": "^10.0.0",
|
|
27
26
|
"github-slugger": "^1.4.0",
|
|
28
27
|
"globby": "^11.0.4",
|
|
29
28
|
"gray-matter": "^4.0.3",
|
|
29
|
+
"js-yaml": "^4.0.0",
|
|
30
30
|
"lodash": "^4.17.20",
|
|
31
31
|
"micromatch": "^4.0.4",
|
|
32
32
|
"remark-mdx-remove-exports": "^1.6.22",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"node": ">=14"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@docusaurus/types": "0.0.0-
|
|
42
|
+
"@docusaurus/types": "0.0.0-4355",
|
|
43
43
|
"@types/dedent": "^0.7.0",
|
|
44
44
|
"@types/github-slugger": "^1.3.0",
|
|
45
45
|
"@types/micromatch": "^4.0.2",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"react-dom": "*",
|
|
53
53
|
"webpack": "5.x"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "4ad639a600a7aa5415733d798ed53a5a99ac7da0"
|
|
56
56
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
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 fs from 'fs-extra';
|
|
9
|
+
import Yaml from 'js-yaml';
|
|
10
|
+
import path from 'path';
|
|
11
|
+
import {findAsyncSequential} from './index';
|
|
12
|
+
import type {ContentPaths} from './markdownLinks';
|
|
13
|
+
import logger from '@docusaurus/logger';
|
|
14
|
+
|
|
15
|
+
type DataFileParams = {
|
|
16
|
+
filePath: string;
|
|
17
|
+
contentPaths: ContentPaths;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export async function getDataFilePath({
|
|
21
|
+
filePath,
|
|
22
|
+
contentPaths,
|
|
23
|
+
}: DataFileParams): Promise<string | undefined> {
|
|
24
|
+
// Loads a localized data file in priority
|
|
25
|
+
const contentPath = await findFolderContainingFile(
|
|
26
|
+
getContentPathList(contentPaths),
|
|
27
|
+
filePath,
|
|
28
|
+
);
|
|
29
|
+
if (contentPath) {
|
|
30
|
+
return path.join(contentPath, filePath);
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Looks up for a data file in the content paths, returns the normalized object.
|
|
37
|
+
* Throws when validation fails; returns undefined when file not found
|
|
38
|
+
*/
|
|
39
|
+
export async function getDataFileData<T>(
|
|
40
|
+
params: DataFileParams & {fileType: string},
|
|
41
|
+
validate: (content: unknown) => T,
|
|
42
|
+
): Promise<T | undefined> {
|
|
43
|
+
const filePath = await getDataFilePath(params);
|
|
44
|
+
if (!filePath) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
if (await fs.pathExists(filePath)) {
|
|
48
|
+
try {
|
|
49
|
+
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
|
50
|
+
const unsafeContent = Yaml.load(contentString);
|
|
51
|
+
return validate(unsafeContent);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
// TODO replace later by error cause, see https://v8.dev/features/error-cause
|
|
54
|
+
logger.error`The ${params.fileType} file at path=${filePath} looks invalid.`;
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Order matters: we look in priority in localized folder
|
|
62
|
+
export function getContentPathList(contentPaths: ContentPaths): string[] {
|
|
63
|
+
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// return the first folder path in which the file exists in
|
|
67
|
+
export async function findFolderContainingFile(
|
|
68
|
+
folderPaths: string[],
|
|
69
|
+
relativeFilePath: string,
|
|
70
|
+
): Promise<string | undefined> {
|
|
71
|
+
return findAsyncSequential(folderPaths, (folderPath) =>
|
|
72
|
+
fs.pathExists(path.join(folderPath, relativeFilePath)),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function getFolderContainingFile(
|
|
77
|
+
folderPaths: string[],
|
|
78
|
+
relativeFilePath: string,
|
|
79
|
+
): Promise<string> {
|
|
80
|
+
const maybeFolderPath = await findFolderContainingFile(
|
|
81
|
+
folderPaths,
|
|
82
|
+
relativeFilePath,
|
|
83
|
+
);
|
|
84
|
+
// should never happen, as the source was read from the FS anyway...
|
|
85
|
+
if (!maybeFolderPath) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`File "${relativeFilePath}" does not exist in any of these folders:\n- ${folderPaths.join(
|
|
88
|
+
'\n- ',
|
|
89
|
+
)}]`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return maybeFolderPath;
|
|
93
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,11 +8,10 @@
|
|
|
8
8
|
import logger from '@docusaurus/logger';
|
|
9
9
|
import path from 'path';
|
|
10
10
|
import {createHash} from 'crypto';
|
|
11
|
-
import {
|
|
12
|
-
import escapeStringRegexp from 'escape-string-regexp';
|
|
11
|
+
import {mapValues} from 'lodash';
|
|
13
12
|
import fs from 'fs-extra';
|
|
14
13
|
import {URL} from 'url';
|
|
15
|
-
import {
|
|
14
|
+
import type {
|
|
16
15
|
ReportingSeverity,
|
|
17
16
|
TranslationFileContent,
|
|
18
17
|
TranslationFile,
|
|
@@ -20,30 +19,21 @@ import {
|
|
|
20
19
|
|
|
21
20
|
import resolvePathnameUnsafe from 'resolve-pathname';
|
|
22
21
|
|
|
23
|
-
import {posixPath as posixPathImport} from './posixPath';
|
|
24
22
|
import {simpleHash, docuHash} from './hashUtils';
|
|
25
|
-
import {normalizeUrl} from './normalizeUrl';
|
|
26
23
|
import {DEFAULT_PLUGIN_ID} from './constants';
|
|
27
24
|
|
|
28
25
|
export * from './constants';
|
|
29
26
|
export * from './mdxUtils';
|
|
30
|
-
export * from './
|
|
27
|
+
export * from './urlUtils';
|
|
31
28
|
export * from './tags';
|
|
32
|
-
|
|
33
|
-
export const posixPath = posixPathImport;
|
|
34
|
-
|
|
35
29
|
export * from './markdownParser';
|
|
36
30
|
export * from './markdownLinks';
|
|
37
|
-
export * from './escapePath';
|
|
38
31
|
export * from './slugger';
|
|
39
|
-
export
|
|
40
|
-
export
|
|
41
|
-
|
|
42
|
-
GlobExcludeDefault,
|
|
43
|
-
createMatcher,
|
|
44
|
-
createAbsoluteFilePathMatcher,
|
|
45
|
-
} from './globUtils';
|
|
32
|
+
export * from './pathUtils';
|
|
33
|
+
export * from './hashUtils';
|
|
34
|
+
export * from './globUtils';
|
|
46
35
|
export * from './webpackUtils';
|
|
36
|
+
export * from './dataFileUtils';
|
|
47
37
|
|
|
48
38
|
const fileHash = new Map();
|
|
49
39
|
export async function generate(
|
|
@@ -80,18 +70,6 @@ export async function generate(
|
|
|
80
70
|
}
|
|
81
71
|
}
|
|
82
72
|
|
|
83
|
-
export function objectWithKeySorted<T>(
|
|
84
|
-
obj: Record<string, T>,
|
|
85
|
-
): Record<string, T> {
|
|
86
|
-
// https://github.com/lodash/lodash/issues/1459#issuecomment-460941233
|
|
87
|
-
return Object.keys(obj)
|
|
88
|
-
.sort()
|
|
89
|
-
.reduce((acc: Record<string, T>, key: string) => {
|
|
90
|
-
acc[key] = obj[key];
|
|
91
|
-
return acc;
|
|
92
|
-
}, {});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
73
|
const indexRE = /(^|.*\/)index\.(md|mdx|js|jsx|ts|tsx)$/i;
|
|
96
74
|
const extRE = /\.(md|mdx|js|jsx|ts|tsx)$/;
|
|
97
75
|
|
|
@@ -113,37 +91,6 @@ export function encodePath(userpath: string): string {
|
|
|
113
91
|
.join('/');
|
|
114
92
|
}
|
|
115
93
|
|
|
116
|
-
/**
|
|
117
|
-
* Convert first string character to the upper case.
|
|
118
|
-
* E.g: docusaurus -> Docusaurus
|
|
119
|
-
*/
|
|
120
|
-
export function upperFirst(str: string): string {
|
|
121
|
-
return str ? str.charAt(0).toUpperCase() + str.slice(1) : '';
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Generate unique React Component Name.
|
|
126
|
-
* E.g: /foo-bar -> FooBar096
|
|
127
|
-
*/
|
|
128
|
-
export function genComponentName(pagePath: string): string {
|
|
129
|
-
if (pagePath === '/') {
|
|
130
|
-
return 'index';
|
|
131
|
-
}
|
|
132
|
-
const pageHash = docuHash(pagePath);
|
|
133
|
-
return upperFirst(camelCase(pageHash));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// When you want to display a path in a message/warning/error,
|
|
137
|
-
// it's more convenient to:
|
|
138
|
-
// - make it relative to cwd()
|
|
139
|
-
// - convert to posix (ie not using windows \ path separator)
|
|
140
|
-
// This way, Jest tests can run more reliably on any computer/CI
|
|
141
|
-
// on both Unix/Windows
|
|
142
|
-
// For Windows users this is not perfect (as they see / instead of \) but it's probably good enough
|
|
143
|
-
export function toMessageRelativeFilePath(filePath: string): string {
|
|
144
|
-
return posixPath(path.relative(process.cwd(), filePath));
|
|
145
|
-
}
|
|
146
|
-
|
|
147
94
|
const chunkNameCache = new Map();
|
|
148
95
|
/**
|
|
149
96
|
* Generate unique chunk name given a module path.
|
|
@@ -172,52 +119,6 @@ export function genChunkName(
|
|
|
172
119
|
return chunkName;
|
|
173
120
|
}
|
|
174
121
|
|
|
175
|
-
// Too dynamic
|
|
176
|
-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
|
|
177
|
-
export function idx(target: any, keyPaths?: string | (string | number)[]): any {
|
|
178
|
-
return (
|
|
179
|
-
target &&
|
|
180
|
-
keyPaths &&
|
|
181
|
-
(Array.isArray(keyPaths)
|
|
182
|
-
? keyPaths.reduce((obj, key) => obj && obj[key], target)
|
|
183
|
-
: target[keyPaths])
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Given a filepath and dirpath, get the first directory.
|
|
189
|
-
*/
|
|
190
|
-
export function getSubFolder(file: string, refDir: string): string | null {
|
|
191
|
-
const separator = escapeStringRegexp(path.sep);
|
|
192
|
-
const baseDir = escapeStringRegexp(path.basename(refDir));
|
|
193
|
-
const regexSubFolder = new RegExp(
|
|
194
|
-
`${baseDir}${separator}(.*?)${separator}.*`,
|
|
195
|
-
);
|
|
196
|
-
const match = regexSubFolder.exec(file);
|
|
197
|
-
return match && match[1];
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Alias filepath relative to site directory, very useful so that we
|
|
202
|
-
* don't expose user's site structure.
|
|
203
|
-
* Example: some/path/to/website/docs/foo.md -> @site/docs/foo.md
|
|
204
|
-
*/
|
|
205
|
-
export function aliasedSitePath(filePath: string, siteDir: string): string {
|
|
206
|
-
const relativePath = posixPath(path.relative(siteDir, filePath));
|
|
207
|
-
// Cannot use path.join() as it resolves '../' and removes
|
|
208
|
-
// the '@site'. Let webpack loader resolve it.
|
|
209
|
-
return `@site/${relativePath}`;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
export function getEditUrl(
|
|
213
|
-
fileRelativePath: string,
|
|
214
|
-
editUrl?: string,
|
|
215
|
-
): string | undefined {
|
|
216
|
-
return editUrl
|
|
217
|
-
? normalizeUrl([editUrl, posixPath(fileRelativePath)])
|
|
218
|
-
: undefined;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
122
|
export function isValidPathname(str: string): boolean {
|
|
222
123
|
if (!str.startsWith('/')) {
|
|
223
124
|
return false;
|
|
@@ -306,7 +207,7 @@ export function getPluginI18nPath({
|
|
|
306
207
|
);
|
|
307
208
|
}
|
|
308
209
|
|
|
309
|
-
export async function
|
|
210
|
+
export async function mapAsyncSequential<T, R>(
|
|
310
211
|
array: T[],
|
|
311
212
|
action: (t: T) => Promise<R>,
|
|
312
213
|
): Promise<R[]> {
|
|
@@ -332,35 +233,6 @@ export async function findAsyncSequential<T>(
|
|
|
332
233
|
return undefined;
|
|
333
234
|
}
|
|
334
235
|
|
|
335
|
-
// return the first folder path in which the file exists in
|
|
336
|
-
export async function findFolderContainingFile(
|
|
337
|
-
folderPaths: string[],
|
|
338
|
-
relativeFilePath: string,
|
|
339
|
-
): Promise<string | undefined> {
|
|
340
|
-
return findAsyncSequential(folderPaths, (folderPath) =>
|
|
341
|
-
fs.pathExists(path.join(folderPath, relativeFilePath)),
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
export async function getFolderContainingFile(
|
|
346
|
-
folderPaths: string[],
|
|
347
|
-
relativeFilePath: string,
|
|
348
|
-
): Promise<string> {
|
|
349
|
-
const maybeFolderPath = await findFolderContainingFile(
|
|
350
|
-
folderPaths,
|
|
351
|
-
relativeFilePath,
|
|
352
|
-
);
|
|
353
|
-
// should never happen, as the source was read from the FS anyway...
|
|
354
|
-
if (!maybeFolderPath) {
|
|
355
|
-
throw new Error(
|
|
356
|
-
`File "${relativeFilePath}" does not exist in any of these folders:\n- ${folderPaths.join(
|
|
357
|
-
'\n- ',
|
|
358
|
-
)}]`,
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
return maybeFolderPath;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
236
|
export function reportMessage(
|
|
365
237
|
message: string,
|
|
366
238
|
reportingSeverity: ReportingSeverity,
|
|
@@ -420,21 +292,3 @@ export function updateTranslationFileMessages(
|
|
|
420
292
|
})),
|
|
421
293
|
};
|
|
422
294
|
}
|
|
423
|
-
|
|
424
|
-
// Input: ## Some heading {#some-heading}
|
|
425
|
-
// Output: {text: "## Some heading", id: "some-heading"}
|
|
426
|
-
export function parseMarkdownHeadingId(heading: string): {
|
|
427
|
-
text: string;
|
|
428
|
-
id?: string;
|
|
429
|
-
} {
|
|
430
|
-
const customHeadingIdRegex = /^(.*?)\s*\{#([\w-]+)\}$/;
|
|
431
|
-
const matches = customHeadingIdRegex.exec(heading);
|
|
432
|
-
if (matches) {
|
|
433
|
-
return {
|
|
434
|
-
text: matches[1],
|
|
435
|
-
id: matches[2],
|
|
436
|
-
};
|
|
437
|
-
} else {
|
|
438
|
-
return {text: heading, id: undefined};
|
|
439
|
-
}
|
|
440
|
-
}
|
package/src/markdownLinks.ts
CHANGED
package/src/markdownParser.ts
CHANGED
|
@@ -9,6 +9,24 @@ import logger from '@docusaurus/logger';
|
|
|
9
9
|
import fs from 'fs-extra';
|
|
10
10
|
import matter from 'gray-matter';
|
|
11
11
|
|
|
12
|
+
// Input: ## Some heading {#some-heading}
|
|
13
|
+
// Output: {text: "## Some heading", id: "some-heading"}
|
|
14
|
+
export function parseMarkdownHeadingId(heading: string): {
|
|
15
|
+
text: string;
|
|
16
|
+
id?: string;
|
|
17
|
+
} {
|
|
18
|
+
const customHeadingIdRegex = /^(.*?)\s*\{#([\w-]+)\}$/;
|
|
19
|
+
const matches = customHeadingIdRegex.exec(heading);
|
|
20
|
+
if (matches) {
|
|
21
|
+
return {
|
|
22
|
+
text: matches[1],
|
|
23
|
+
id: matches[2],
|
|
24
|
+
};
|
|
25
|
+
} else {
|
|
26
|
+
return {text: heading, id: undefined};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
12
30
|
// Hacky way of stripping out import statements from the excerpt
|
|
13
31
|
// TODO: Find a better way to do so, possibly by compiling the Markdown content,
|
|
14
32
|
// stripping out HTML tags and obtaining the first line.
|
package/src/pathUtils.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
9
9
|
|
|
10
|
+
import path from 'path';
|
|
11
|
+
|
|
10
12
|
// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
|
|
11
13
|
const MAX_PATH_SEGMENT_CHARS = 255;
|
|
12
14
|
const MAX_PATH_SEGMENT_BYTES = 255;
|
|
@@ -39,3 +41,66 @@ export const shortName = (str: string): string => {
|
|
|
39
41
|
)
|
|
40
42
|
.toString();
|
|
41
43
|
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Convert Windows backslash paths to posix style paths.
|
|
47
|
+
* E.g: endi\lie -> endi/lie
|
|
48
|
+
*
|
|
49
|
+
* Returns original path if the posix counterpart is not valid Windows path.
|
|
50
|
+
* This makes the legacy code that uses posixPath safe; but also makes it less
|
|
51
|
+
* useful when you actually want a path with forward slashes (e.g. for URL)
|
|
52
|
+
*
|
|
53
|
+
* Adopted from https://github.com/sindresorhus/slash/blob/main/index.js
|
|
54
|
+
*/
|
|
55
|
+
export function posixPath(str: string): string {
|
|
56
|
+
const isExtendedLengthPath = /^\\\\\?\\/.test(str);
|
|
57
|
+
|
|
58
|
+
// Forward slashes are only valid Windows paths when they don't contain non-ascii characters.
|
|
59
|
+
// eslint-disable-next-line no-control-regex
|
|
60
|
+
const hasNonAscii = /[^\u0000-\u0080]+/.test(str);
|
|
61
|
+
|
|
62
|
+
if (isExtendedLengthPath || hasNonAscii) {
|
|
63
|
+
return str;
|
|
64
|
+
}
|
|
65
|
+
return str.replace(/\\/g, '/');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// When you want to display a path in a message/warning/error,
|
|
69
|
+
// it's more convenient to:
|
|
70
|
+
// - make it relative to cwd()
|
|
71
|
+
// - convert to posix (ie not using windows \ path separator)
|
|
72
|
+
// This way, Jest tests can run more reliably on any computer/CI
|
|
73
|
+
// on both Unix/Windows
|
|
74
|
+
// For Windows users this is not perfect (as they see / instead of \) but it's probably good enough
|
|
75
|
+
export function toMessageRelativeFilePath(filePath: string): string {
|
|
76
|
+
return posixPath(path.relative(process.cwd(), filePath));
|
|
77
|
+
}
|
|
78
|
+
|
|
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
|
+
export function aliasedSitePath(filePath: string, siteDir: string): string {
|
|
85
|
+
const relativePath = posixPath(path.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
|
+
|
|
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 \: `<img src={require('${filePath}')} />`
|
|
95
|
+
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
|
96
|
+
*
|
|
97
|
+
* posixPath can't be used in all cases, because forward slashes are only valid
|
|
98
|
+
* Windows paths when they don't contain non-ascii characters, and posixPath
|
|
99
|
+
* doesn't escape those that fail to be converted.
|
|
100
|
+
*/
|
|
101
|
+
export function escapePath(str: string): string {
|
|
102
|
+
const escaped = JSON.stringify(str);
|
|
103
|
+
|
|
104
|
+
// Remove the " around the json string;
|
|
105
|
+
return escaped.substring(1, escaped.length - 1);
|
|
106
|
+
}
|