@docusaurus/utils 2.0.0-beta.ff31de0ff → 2.0.0-rc.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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urlUtils.js","sourceRoot":"","sources":["../src/urlUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,gFAAqD;AACrD,uCAAuC;AAEvC;;;;;;;;;;;GAWG;AACH,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,MAAM,eAAe,GAAG,CAAC,GAAa,EAAgC,EAAE,CACtE,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;IAEjB,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE;QAC1B,OAAO,EAAE,CAAC;KACX;IAED,2EAA2E;IAC3E,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,cAAc,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;QACpD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,EAAG,CAAC;QAC5B,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AACH,SAAgB,cAAc,CAAC,aAAqB;IAClD,IAAI;QACF,IAAI,IAAI,GAAG,CAAC,aAAa,CAAC,CAAC,QAAQ,KAAK,MAAM,EAAE;YAC9C,OAAO,IAAI,CAAC;SACb;QACD,OAAO,KAAK,CAAC;KACd;IAAC,MAAM;QACN,oCAAoC;QACpC,OAAO,+BAA+B,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;KAC5D;AACH,CAAC;AAVD,wCAUC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
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 { RuleSetRule } from 'webpack';
|
|
8
|
+
declare type AssetFolder = 'images' | 'files' | 'fonts' | 'medias';
|
|
9
|
+
declare type FileLoaderUtils = {
|
|
10
|
+
loaders: {
|
|
11
|
+
file: (options: {
|
|
12
|
+
folder: AssetFolder;
|
|
13
|
+
}) => RuleSetRule;
|
|
14
|
+
url: (options: {
|
|
15
|
+
folder: AssetFolder;
|
|
16
|
+
}) => RuleSetRule;
|
|
17
|
+
inlineMarkdownImageFileLoader: string;
|
|
18
|
+
inlineMarkdownLinkFileLoader: string;
|
|
19
|
+
};
|
|
20
|
+
rules: {
|
|
21
|
+
images: () => RuleSetRule;
|
|
22
|
+
fonts: () => RuleSetRule;
|
|
23
|
+
media: () => RuleSetRule;
|
|
24
|
+
svg: () => RuleSetRule;
|
|
25
|
+
otherAssets: () => RuleSetRule;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Returns unified loader configurations to be used for various file types.
|
|
30
|
+
*
|
|
31
|
+
* Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447
|
|
32
|
+
*/
|
|
33
|
+
export declare function getFileLoaderUtils(): FileLoaderUtils;
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=webpackUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpackUtils.d.ts","sourceRoot":"","sources":["../src/webpackUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAEzC,aAAK,WAAW,GAAG,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE3D,aAAK,eAAe,GAAG;IACrB,OAAO,EAAE;QACP,IAAI,EAAE,CAAC,OAAO,EAAE;YAAC,MAAM,EAAE,WAAW,CAAA;SAAC,KAAK,WAAW,CAAC;QACtD,GAAG,EAAE,CAAC,OAAO,EAAE;YAAC,MAAM,EAAE,WAAW,CAAA;SAAC,KAAK,WAAW,CAAC;QACrD,6BAA6B,EAAE,MAAM,CAAC;QACtC,4BAA4B,EAAE,MAAM,CAAC;KACtC,CAAC;IACF,KAAK,EAAE;QACL,MAAM,EAAE,MAAM,WAAW,CAAC;QAC1B,KAAK,EAAE,MAAM,WAAW,CAAC;QACzB,KAAK,EAAE,MAAM,WAAW,CAAC;QACzB,GAAG,EAAE,MAAM,WAAW,CAAC;QACvB,WAAW,EAAE,MAAM,WAAW,CAAC;KAChC,CAAC;CACH,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,kBAAkB,IAAI,eAAe,CAkHpD"}
|
|
@@ -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.getFileLoaderUtils = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
12
|
+
const pathUtils_1 = require("./pathUtils");
|
|
13
|
+
const constants_1 = require("./constants");
|
|
14
|
+
/**
|
|
15
|
+
* Returns unified loader configurations to be used for various file types.
|
|
16
|
+
*
|
|
17
|
+
* Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447
|
|
18
|
+
*/
|
|
19
|
+
function getFileLoaderUtils() {
|
|
20
|
+
// Files/images < urlLoaderLimit will be inlined as base64 strings directly in
|
|
21
|
+
// the html
|
|
22
|
+
const urlLoaderLimit = constants_1.WEBPACK_URL_LOADER_LIMIT;
|
|
23
|
+
const fileLoaderFileName = (folder) => path_1.default.posix.join(constants_1.OUTPUT_STATIC_ASSETS_DIR_NAME, folder, '[name]-[contenthash].[ext]');
|
|
24
|
+
const loaders = {
|
|
25
|
+
file: (options) => ({
|
|
26
|
+
loader: require.resolve(`file-loader`),
|
|
27
|
+
options: {
|
|
28
|
+
name: fileLoaderFileName(options.folder),
|
|
29
|
+
},
|
|
30
|
+
}),
|
|
31
|
+
url: (options) => ({
|
|
32
|
+
loader: require.resolve('url-loader'),
|
|
33
|
+
options: {
|
|
34
|
+
limit: urlLoaderLimit,
|
|
35
|
+
name: fileLoaderFileName(options.folder),
|
|
36
|
+
fallback: require.resolve('file-loader'),
|
|
37
|
+
},
|
|
38
|
+
}),
|
|
39
|
+
// TODO avoid conflicts with the ideal-image plugin
|
|
40
|
+
// TODO this may require a little breaking change for ideal-image users?
|
|
41
|
+
// Maybe with the ideal image plugin, all md images should be "ideal"?
|
|
42
|
+
// This is used to force url-loader+file-loader on markdown images
|
|
43
|
+
// https://webpack.js.org/concepts/loaders/#inline
|
|
44
|
+
inlineMarkdownImageFileLoader: `!${(0, pathUtils_1.escapePath)(require.resolve('url-loader'))}?limit=${urlLoaderLimit}&name=${fileLoaderFileName('images')}&fallback=${(0, pathUtils_1.escapePath)(require.resolve('file-loader'))}!`,
|
|
45
|
+
inlineMarkdownLinkFileLoader: `!${(0, pathUtils_1.escapePath)(require.resolve('file-loader'))}?name=${fileLoaderFileName('files')}!`,
|
|
46
|
+
};
|
|
47
|
+
const rules = {
|
|
48
|
+
/**
|
|
49
|
+
* Loads image assets, inlines images via a data URI if they are below
|
|
50
|
+
* the size threshold
|
|
51
|
+
*/
|
|
52
|
+
images: () => ({
|
|
53
|
+
use: [loaders.url({ folder: 'images' })],
|
|
54
|
+
test: /\.(?:ico|jpe?g|png|gif|webp)(?:\?.*)?$/i,
|
|
55
|
+
}),
|
|
56
|
+
fonts: () => ({
|
|
57
|
+
use: [loaders.url({ folder: 'fonts' })],
|
|
58
|
+
test: /\.(?:woff2?|eot|ttf|otf)$/i,
|
|
59
|
+
}),
|
|
60
|
+
/**
|
|
61
|
+
* Loads audio and video and inlines them via a data URI if they are below
|
|
62
|
+
* the size threshold
|
|
63
|
+
*/
|
|
64
|
+
media: () => ({
|
|
65
|
+
use: [loaders.url({ folder: 'medias' })],
|
|
66
|
+
test: /\.(?:mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/i,
|
|
67
|
+
}),
|
|
68
|
+
svg: () => ({
|
|
69
|
+
test: /\.svg$/i,
|
|
70
|
+
oneOf: [
|
|
71
|
+
{
|
|
72
|
+
use: [
|
|
73
|
+
{
|
|
74
|
+
loader: require.resolve('@svgr/webpack'),
|
|
75
|
+
options: {
|
|
76
|
+
prettier: false,
|
|
77
|
+
svgo: true,
|
|
78
|
+
svgoConfig: {
|
|
79
|
+
plugins: [
|
|
80
|
+
{
|
|
81
|
+
name: 'preset-default',
|
|
82
|
+
params: {
|
|
83
|
+
overrides: {
|
|
84
|
+
removeTitle: false,
|
|
85
|
+
removeViewBox: false,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
titleProp: true,
|
|
92
|
+
ref: ![path_1.default],
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
// We don't want to use SVGR loader for non-React source code
|
|
97
|
+
// ie we don't want to use SVGR for CSS files...
|
|
98
|
+
issuer: {
|
|
99
|
+
and: [/\.(?:tsx?|jsx?|mdx?)$/i],
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
use: [loaders.url({ folder: 'images' })],
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
}),
|
|
107
|
+
otherAssets: () => ({
|
|
108
|
+
use: [loaders.file({ folder: 'files' })],
|
|
109
|
+
test: /\.(?:pdf|docx?|xlsx?|zip|rar)$/i,
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
return { loaders, rules };
|
|
113
|
+
}
|
|
114
|
+
exports.getFileLoaderUtils = getFileLoaderUtils;
|
|
115
|
+
//# sourceMappingURL=webpackUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpackUtils.js","sourceRoot":"","sources":["../src/webpackUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wDAAwB;AACxB,2CAAuC;AACvC,2CAGqB;AAqBrB;;;;GAIG;AACH,SAAgB,kBAAkB;IAChC,8EAA8E;IAC9E,WAAW;IACX,MAAM,cAAc,GAAG,oCAAwB,CAAC;IAEhD,MAAM,kBAAkB,GAAG,CAAC,MAAmB,EAAE,EAAE,CACjD,cAAI,CAAC,KAAK,CAAC,IAAI,CACb,yCAA6B,EAC7B,MAAM,EACN,4BAA4B,CAC7B,CAAC;IAEJ,MAAM,OAAO,GAA+B;QAC1C,IAAI,EAAE,CAAC,OAA8B,EAAE,EAAE,CAAC,CAAC;YACzC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;YACtC,OAAO,EAAE;gBACP,IAAI,EAAE,kBAAkB,CAAC,OAAO,CAAC,MAAM,CAAC;aACzC;SACF,CAAC;QACF,GAAG,EAAE,CA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AA+C;SACtD,CAAC;QAEF,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;YACV,IAAI,EAAE,SAAS;YACf,KAAK,EAAE;gBACL;oBACE,GAAG,EAAE;wBACH;4BACE,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC;4BACxC,OAAO,EAAE;gCACP,QAAQ,EAAE,KAAK;gCACf,IAAI,EAAE,IAAI;gCACV,UAAU,EAAE;oCACV,OAAO,EAAE;wCACP;4CACE,IAAI,EAAE,gBAAgB;4CACtB,MAAM,EAAE;gDACN,SAAS,EAAE;oDACT,WAAW,EAAE,KAAK;oDAClB,aAAa,EAAE,KAAK;iDACrB;6CACF;yCACF;qCACF;iCACF;gCACD,SAAS,EAAE,IAAI;gCACf,GAAG,EAAE,CAAC,CAAC,cAAI,CAAC;6BACb;yBACF;qBACF;oBACD,6DAA6D;oBAC7D,gDAAgD;oBAChD,MAAM,EAAE;wBACN,GAAG,EAAE,CAAC,wBAAwB,CAAC;qBAChC;iBACF;gBACD;oBACE,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,EAAC,MAAM,EAAE,QAAQ,EAAC,CAAC,CAAC;iBACvC;aACF;SACF,CAAC;QAEF,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;YAClB,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,EAAC,MAAM,EAAE,OAAO,EAAC,CAAC,CAAC;YACtC,IAAI,EAAE,iCAAiC;SACxC,CAAC;KACH,CAAC;IAEF,OAAO,EAAC,OAAO,EAAE,KAAK,EAAC,CAAC;AAC1B,CAAC;AAlHD,gDAkHC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/utils",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-rc.1",
|
|
4
4
|
"description": "Node utility functions for Docusaurus packages.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -18,22 +18,40 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/
|
|
22
|
-
"@
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
21
|
+
"@docusaurus/logger": "2.0.0-rc.1",
|
|
22
|
+
"@svgr/webpack": "^6.2.1",
|
|
23
|
+
"file-loader": "^6.2.0",
|
|
24
|
+
"fs-extra": "^10.1.0",
|
|
25
|
+
"github-slugger": "^1.4.0",
|
|
26
|
+
"globby": "^11.1.0",
|
|
26
27
|
"gray-matter": "^4.0.3",
|
|
27
|
-
"
|
|
28
|
+
"js-yaml": "^4.1.0",
|
|
29
|
+
"lodash": "^4.17.21",
|
|
30
|
+
"micromatch": "^4.0.5",
|
|
28
31
|
"resolve-pathname": "^3.0.0",
|
|
29
|
-
"
|
|
32
|
+
"shelljs": "^0.8.5",
|
|
33
|
+
"tslib": "^2.4.0",
|
|
34
|
+
"url-loader": "^4.1.1",
|
|
35
|
+
"webpack": "^5.73.0"
|
|
30
36
|
},
|
|
31
37
|
"engines": {
|
|
32
|
-
"node": ">=
|
|
38
|
+
"node": ">=16.14"
|
|
33
39
|
},
|
|
34
40
|
"devDependencies": {
|
|
41
|
+
"@docusaurus/types": "2.0.0-rc.1",
|
|
35
42
|
"@types/dedent": "^0.7.0",
|
|
43
|
+
"@types/github-slugger": "^1.3.0",
|
|
44
|
+
"@types/micromatch": "^4.0.2",
|
|
45
|
+
"@types/react-dom": "^18.0.6",
|
|
36
46
|
"dedent": "^0.7.0"
|
|
37
47
|
},
|
|
38
|
-
"
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@docusaurus/types": "*"
|
|
50
|
+
},
|
|
51
|
+
"peerDependenciesMeta": {
|
|
52
|
+
"@docusaurus/types": {
|
|
53
|
+
"optional": true
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"gitHead": "c8ddd02a8e68dfaf515c20465a049a83153bd205"
|
|
39
57
|
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
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
|
+
/** Node major version, directly read from env. */
|
|
9
|
+
export const NODE_MAJOR_VERSION = parseInt(
|
|
10
|
+
process.versions.node.split('.')[0]!,
|
|
11
|
+
10,
|
|
12
|
+
);
|
|
13
|
+
/** Node minor version, directly read from env. */
|
|
14
|
+
export const NODE_MINOR_VERSION = parseInt(
|
|
15
|
+
process.versions.node.split('.')[1]!,
|
|
16
|
+
10,
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
/** Docusaurus core version. */
|
|
20
|
+
export const DOCUSAURUS_VERSION =
|
|
21
|
+
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
|
|
22
|
+
(require('../package.json') as {version: string}).version;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Can be overridden with cli option `--out-dir`. Code should generally use
|
|
26
|
+
* `context.outDir` instead (which is always absolute and localized).
|
|
27
|
+
*/
|
|
28
|
+
export const DEFAULT_BUILD_DIR_NAME = 'build';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Can be overridden with cli option `--config`. Code should generally use
|
|
32
|
+
* `context.siteConfigPath` instead (which is always absolute).
|
|
33
|
+
*
|
|
34
|
+
* This does not have extensions, so that we can substitute different ones
|
|
35
|
+
* when resolving the path.
|
|
36
|
+
*/
|
|
37
|
+
export const DEFAULT_CONFIG_FILE_NAME = 'docusaurus.config';
|
|
38
|
+
|
|
39
|
+
/** Can be absolute or relative to site directory. */
|
|
40
|
+
export const BABEL_CONFIG_FILE_NAME =
|
|
41
|
+
process.env.DOCUSAURUS_BABEL_CONFIG_FILE_NAME ?? 'babel.config.js';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Can be absolute or relative to site directory. Code should generally use
|
|
45
|
+
* `context.generatedFilesDir` instead (which is always absolute).
|
|
46
|
+
*/
|
|
47
|
+
export const GENERATED_FILES_DIR_NAME =
|
|
48
|
+
process.env.DOCUSAURUS_GENERATED_FILES_DIR_NAME ?? '.docusaurus';
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* We would assume all of the site's JS code lives in here and not outside.
|
|
52
|
+
* Relative to the site directory.
|
|
53
|
+
*/
|
|
54
|
+
export const SRC_DIR_NAME = 'src';
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Can be overridden with `config.staticDirectories`. Code should use
|
|
58
|
+
* `context.siteConfig.staticDirectories` instead (which is always absolute).
|
|
59
|
+
*/
|
|
60
|
+
export const DEFAULT_STATIC_DIR_NAME = 'static';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Files here are handled by webpack, hashed (can be cached aggressively).
|
|
64
|
+
* Relative to the build output folder.
|
|
65
|
+
*/
|
|
66
|
+
export const OUTPUT_STATIC_ASSETS_DIR_NAME = 'assets';
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Components in this directory will receive the `@theme` alias and be able to
|
|
70
|
+
* shadow default theme components.
|
|
71
|
+
*/
|
|
72
|
+
export const THEME_PATH = `${SRC_DIR_NAME}/theme`;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* All translation-related data live here, relative to site directory. Content
|
|
76
|
+
* will be namespaced by locale.
|
|
77
|
+
*/
|
|
78
|
+
export const DEFAULT_I18N_DIR_NAME = 'i18n';
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Translations for React code.
|
|
82
|
+
*/
|
|
83
|
+
export const CODE_TRANSLATIONS_FILE_NAME = 'code.json';
|
|
84
|
+
|
|
85
|
+
/** Dev server opens on this port by default. */
|
|
86
|
+
export const DEFAULT_PORT = 3000;
|
|
87
|
+
|
|
88
|
+
/** Default plugin ID. */
|
|
89
|
+
export const DEFAULT_PLUGIN_ID = 'default';
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Allow overriding the limit after which the url loader will no longer inline
|
|
93
|
+
* assets.
|
|
94
|
+
*
|
|
95
|
+
* @see https://github.com/facebook/docusaurus/issues/5493
|
|
96
|
+
*/
|
|
97
|
+
export const WEBPACK_URL_LOADER_LIMIT =
|
|
98
|
+
process.env.WEBPACK_URL_LOADER_LIMIT ?? 10000;
|
|
@@ -0,0 +1,122 @@
|
|
|
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 path from 'path';
|
|
10
|
+
import logger from '@docusaurus/logger';
|
|
11
|
+
import Yaml from 'js-yaml';
|
|
12
|
+
import {findAsyncSequential} from './index';
|
|
13
|
+
import type {ContentPaths} from './markdownLinks';
|
|
14
|
+
|
|
15
|
+
type DataFileParams = {
|
|
16
|
+
/** Path to the potential data file, relative to `contentPaths` */
|
|
17
|
+
filePath: string;
|
|
18
|
+
/**
|
|
19
|
+
* Includes the base path and localized path, both of which are eligible for
|
|
20
|
+
* sourcing data files. Both paths should be absolute.
|
|
21
|
+
*/
|
|
22
|
+
contentPaths: ContentPaths;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Looks for a data file in the potential content paths; loads a localized data
|
|
27
|
+
* file in priority.
|
|
28
|
+
*
|
|
29
|
+
* @returns An absolute path to the data file, or `undefined` if there isn't one.
|
|
30
|
+
*/
|
|
31
|
+
export async function getDataFilePath({
|
|
32
|
+
filePath,
|
|
33
|
+
contentPaths,
|
|
34
|
+
}: DataFileParams): Promise<string | undefined> {
|
|
35
|
+
const contentPath = await findFolderContainingFile(
|
|
36
|
+
getContentPathList(contentPaths),
|
|
37
|
+
filePath,
|
|
38
|
+
);
|
|
39
|
+
if (contentPath) {
|
|
40
|
+
return path.resolve(contentPath, filePath);
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Looks up for a data file in the content paths, returns the object validated
|
|
47
|
+
* and normalized according to the `validate` callback.
|
|
48
|
+
*
|
|
49
|
+
* @returns `undefined` when file not found
|
|
50
|
+
* @throws Throws when validation fails, displaying a helpful context message.
|
|
51
|
+
*/
|
|
52
|
+
export async function getDataFileData<T>(
|
|
53
|
+
params: DataFileParams & {
|
|
54
|
+
/** Used for the "The X file looks invalid" message. */
|
|
55
|
+
fileType: string;
|
|
56
|
+
},
|
|
57
|
+
validate: (content: unknown) => T,
|
|
58
|
+
): Promise<T | undefined> {
|
|
59
|
+
const filePath = await getDataFilePath(params);
|
|
60
|
+
if (!filePath) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
|
65
|
+
const unsafeContent = Yaml.load(contentString);
|
|
66
|
+
return validate(unsafeContent);
|
|
67
|
+
} catch (err) {
|
|
68
|
+
logger.error`The ${params.fileType} file at path=${filePath} looks invalid.`;
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Takes the `contentPaths` data structure and returns an ordered path list
|
|
75
|
+
* indicating their priorities. For all data, we look in the localized folder
|
|
76
|
+
* in priority.
|
|
77
|
+
*/
|
|
78
|
+
export function getContentPathList(contentPaths: ContentPaths): string[] {
|
|
79
|
+
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param folderPaths a list of absolute paths.
|
|
84
|
+
* @param relativeFilePath file path relative to each `folderPaths`.
|
|
85
|
+
* @returns the first folder path in which the file exists, or `undefined` if
|
|
86
|
+
* none is found.
|
|
87
|
+
*/
|
|
88
|
+
export async function findFolderContainingFile(
|
|
89
|
+
folderPaths: string[],
|
|
90
|
+
relativeFilePath: string,
|
|
91
|
+
): Promise<string | undefined> {
|
|
92
|
+
return findAsyncSequential(folderPaths, (folderPath) =>
|
|
93
|
+
fs.pathExists(path.join(folderPath, relativeFilePath)),
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Fail-fast alternative to `findFolderContainingFile`.
|
|
99
|
+
*
|
|
100
|
+
* @param folderPaths a list of absolute paths.
|
|
101
|
+
* @param relativeFilePath file path relative to each `folderPaths`.
|
|
102
|
+
* @returns the first folder path in which the file exists.
|
|
103
|
+
* @throws Throws if no file can be found. You should use this method only when
|
|
104
|
+
* you actually know the file exists (e.g. when the `relativeFilePath` is read
|
|
105
|
+
* with a glob and you are just trying to localize it)
|
|
106
|
+
*/
|
|
107
|
+
export async function getFolderContainingFile(
|
|
108
|
+
folderPaths: string[],
|
|
109
|
+
relativeFilePath: string,
|
|
110
|
+
): Promise<string> {
|
|
111
|
+
const maybeFolderPath = await findFolderContainingFile(
|
|
112
|
+
folderPaths,
|
|
113
|
+
relativeFilePath,
|
|
114
|
+
);
|
|
115
|
+
if (!maybeFolderPath) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
`File "${relativeFilePath}" does not exist in any of these folders:
|
|
118
|
+
- ${folderPaths.join('\n- ')}`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
return maybeFolderPath;
|
|
122
|
+
}
|
package/src/deps.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
declare module 'resolve-pathname' {
|
|
9
|
+
export default function resolvePathname(to: string, from?: string): string;
|
|
10
|
+
}
|
package/src/emitUtils.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
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 {findAsyncSequential} from './jsUtils';
|
|
12
|
+
|
|
13
|
+
const fileHash = new Map<string, string>();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Outputs a file to the generated files directory. Only writes files if content
|
|
17
|
+
* differs from cache (for hot reload performance).
|
|
18
|
+
*
|
|
19
|
+
* @param generatedFilesDir Absolute path.
|
|
20
|
+
* @param file Path relative to `generatedFilesDir`. File will always be
|
|
21
|
+
* outputted; no need to ensure directory exists.
|
|
22
|
+
* @param content String content to write.
|
|
23
|
+
* @param skipCache If `true` (defaults as `true` for production), file is
|
|
24
|
+
* force-rewritten, skipping cache.
|
|
25
|
+
*/
|
|
26
|
+
export async function generate(
|
|
27
|
+
generatedFilesDir: string,
|
|
28
|
+
file: string,
|
|
29
|
+
content: string,
|
|
30
|
+
skipCache: boolean = process.env.NODE_ENV === 'production',
|
|
31
|
+
): Promise<void> {
|
|
32
|
+
const filepath = path.resolve(generatedFilesDir, file);
|
|
33
|
+
|
|
34
|
+
if (skipCache) {
|
|
35
|
+
await fs.outputFile(filepath, content);
|
|
36
|
+
// Cache still needs to be reset, otherwise, writing "A", "B", and "A" where
|
|
37
|
+
// "B" skips cache will cause the last "A" not be able to overwrite as the
|
|
38
|
+
// first "A" remains in cache. But if the file never existed in cache, no
|
|
39
|
+
// need to register it.
|
|
40
|
+
if (fileHash.get(filepath)) {
|
|
41
|
+
fileHash.set(filepath, createHash('md5').update(content).digest('hex'));
|
|
42
|
+
}
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let lastHash = fileHash.get(filepath);
|
|
47
|
+
|
|
48
|
+
// If file already exists but it's not in runtime cache yet, we try to
|
|
49
|
+
// calculate the content hash and then compare. This is to avoid unnecessary
|
|
50
|
+
// overwriting and we can reuse old file.
|
|
51
|
+
if (!lastHash && (await fs.pathExists(filepath))) {
|
|
52
|
+
const lastContent = await fs.readFile(filepath, 'utf8');
|
|
53
|
+
lastHash = createHash('md5').update(lastContent).digest('hex');
|
|
54
|
+
fileHash.set(filepath, lastHash);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const currentHash = createHash('md5').update(content).digest('hex');
|
|
58
|
+
|
|
59
|
+
if (lastHash !== currentHash) {
|
|
60
|
+
await fs.outputFile(filepath, content);
|
|
61
|
+
fileHash.set(filepath, currentHash);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param permalink The URL that the HTML file corresponds to, without base URL
|
|
67
|
+
* @param outDir Full path to the output directory
|
|
68
|
+
* @param trailingSlash The site config option. If provided, only one path will
|
|
69
|
+
* be read.
|
|
70
|
+
* @returns This returns a buffer, which you have to decode string yourself if
|
|
71
|
+
* needed. (Not always necessary since the output isn't for human consumption
|
|
72
|
+
* anyways, and most HTML manipulation libs accept buffers)
|
|
73
|
+
* @throws Throws when the HTML file is not found at any of the potential paths.
|
|
74
|
+
* This should never happen as it would lead to a 404.
|
|
75
|
+
*/
|
|
76
|
+
export async function readOutputHTMLFile(
|
|
77
|
+
permalink: string,
|
|
78
|
+
outDir: string,
|
|
79
|
+
trailingSlash: boolean | undefined,
|
|
80
|
+
): Promise<Buffer> {
|
|
81
|
+
const withTrailingSlashPath = path.join(outDir, permalink, 'index.html');
|
|
82
|
+
const withoutTrailingSlashPath = path.join(
|
|
83
|
+
outDir,
|
|
84
|
+
`${permalink.replace(/\/$/, '')}.html`,
|
|
85
|
+
);
|
|
86
|
+
const HTMLPath = await findAsyncSequential(
|
|
87
|
+
[
|
|
88
|
+
trailingSlash !== false && withTrailingSlashPath,
|
|
89
|
+
trailingSlash !== true && withoutTrailingSlashPath,
|
|
90
|
+
].filter((p): p is string => Boolean(p)),
|
|
91
|
+
fs.pathExists,
|
|
92
|
+
);
|
|
93
|
+
if (!HTMLPath) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`Expected output HTML file to be found at ${withTrailingSlashPath}.`,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return fs.readFile(HTMLPath);
|
|
99
|
+
}
|