@docusaurus/utils 2.0.0-beta.15d451942 → 2.0.0-beta.16
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 +20 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +27 -0
- package/lib/constants.js.map +1 -0
- package/lib/dataFileUtils.d.ts +24 -0
- package/lib/dataFileUtils.d.ts.map +1 -0
- package/lib/dataFileUtils.js +65 -0
- package/lib/dataFileUtils.js.map +1 -0
- package/lib/gitUtils.d.ts +17 -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 +12 -0
- package/lib/globUtils.d.ts.map +1 -0
- package/lib/globUtils.js +48 -0
- package/lib/globUtils.js.map +1 -0
- package/lib/hashUtils.d.ts +16 -0
- package/lib/hashUtils.d.ts.map +1 -0
- package/lib/hashUtils.js +41 -0
- package/lib/hashUtils.js.map +1 -0
- package/lib/index.d.ts +30 -52
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +126 -247
- package/lib/index.js.map +1 -0
- package/lib/markdownLinks.d.ts +1 -0
- package/lib/markdownLinks.d.ts.map +1 -0
- package/lib/markdownLinks.js +36 -11
- package/lib/markdownLinks.js.map +1 -0
- package/lib/markdownParser.d.ts +7 -3
- package/lib/markdownParser.d.ts.map +1 -0
- package/lib/markdownParser.js +77 -48
- package/lib/markdownParser.js.map +1 -0
- package/lib/pathUtils.d.ts +51 -0
- package/lib/pathUtils.d.ts.map +1 -0
- package/lib/pathUtils.js +106 -0
- package/lib/pathUtils.js.map +1 -0
- package/lib/slugger.d.ts +14 -0
- package/lib/slugger.d.ts.map +1 -0
- package/lib/slugger.js +19 -0
- package/lib/slugger.js.map +1 -0
- package/lib/tags.d.ts +27 -0
- package/lib/tags.d.ts.map +1 -0
- package/lib/tags.js +77 -0
- package/lib/tags.js.map +1 -0
- package/lib/urlUtils.d.ts +9 -0
- package/lib/urlUtils.d.ts.map +1 -0
- package/lib/urlUtils.js +81 -0
- package/lib/urlUtils.js.map +1 -0
- package/lib/webpackUtils.d.ts +30 -0
- package/lib/webpackUtils.d.ts.map +1 -0
- package/lib/webpackUtils.js +112 -0
- package/lib/webpackUtils.js.map +1 -0
- package/package.json +20 -10
- package/src/constants.ts +38 -0
- package/src/dataFileUtils.ts +90 -0
- package/src/deps.d.ts +10 -0
- package/src/gitUtils.ts +93 -0
- package/src/globUtils.ts +64 -0
- package/src/hashUtils.ts +37 -0
- package/src/index.ts +135 -294
- package/src/markdownLinks.ts +35 -13
- package/src/markdownParser.ts +86 -62
- package/src/pathUtils.ts +115 -0
- package/src/slugger.ts +24 -0
- package/src/tags.ts +105 -0
- package/src/urlUtils.ts +96 -0
- package/src/webpackUtils.ts +146 -0
- package/lib/.tsbuildinfo +0 -3972
- 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/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/posixPath.ts +0 -27
- package/tsconfig.json +0 -9
package/lib/urlUtils.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
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.getEditUrl = exports.normalizeUrl = void 0;
|
|
10
|
+
function normalizeUrl(rawUrls) {
|
|
11
|
+
const urls = [...rawUrls];
|
|
12
|
+
const resultArray = [];
|
|
13
|
+
let hasStartingSlash = false;
|
|
14
|
+
let hasEndingSlash = false;
|
|
15
|
+
// If the first part is a plain protocol, we combine it with the next part.
|
|
16
|
+
if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
|
|
17
|
+
const first = urls.shift();
|
|
18
|
+
if (first.startsWith('file:') && urls[0].startsWith('/')) {
|
|
19
|
+
// Force a double slash here, else we lose the information that the next
|
|
20
|
+
// segment is an absolute path
|
|
21
|
+
urls[0] = `${first}//${urls[0]}`;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
urls[0] = first + urls[0];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// There must be two or three slashes in the file protocol,
|
|
28
|
+
// two slashes in anything else.
|
|
29
|
+
const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
|
|
30
|
+
urls[0] = urls[0].replace(/^(?<protocol>[^/:]+):\/*/, replacement);
|
|
31
|
+
for (let i = 0; i < urls.length; i += 1) {
|
|
32
|
+
let component = urls[i];
|
|
33
|
+
if (typeof component !== 'string') {
|
|
34
|
+
throw new TypeError(`Url must be a string. Received ${typeof component}`);
|
|
35
|
+
}
|
|
36
|
+
if (component === '') {
|
|
37
|
+
if (i === urls.length - 1 && hasEndingSlash) {
|
|
38
|
+
resultArray.push('/');
|
|
39
|
+
}
|
|
40
|
+
// eslint-disable-next-line no-continue
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (component !== '/') {
|
|
44
|
+
if (i > 0) {
|
|
45
|
+
// Removing the starting slashes for each component but the first.
|
|
46
|
+
component = component.replace(/^[/]+/,
|
|
47
|
+
// Special case where the first element of rawUrls is empty
|
|
48
|
+
// ["", "/hello"] => /hello
|
|
49
|
+
component[0] === '/' && !hasStartingSlash ? '/' : '');
|
|
50
|
+
}
|
|
51
|
+
hasEndingSlash = component[component.length - 1] === '/';
|
|
52
|
+
// Removing the ending slashes for each component but the last. For the
|
|
53
|
+
// last component we will combine multiple slashes to a single one.
|
|
54
|
+
component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
|
|
55
|
+
}
|
|
56
|
+
hasStartingSlash = true;
|
|
57
|
+
resultArray.push(component);
|
|
58
|
+
}
|
|
59
|
+
let str = resultArray.join('/');
|
|
60
|
+
// Each input component is now separated by a single slash
|
|
61
|
+
// except the possible first plain protocol part.
|
|
62
|
+
// Remove trailing slash before parameters or hash.
|
|
63
|
+
str = str.replace(/\/(?<search>\?|&|#[^!])/g, '$1');
|
|
64
|
+
// Replace ? in parameters with &.
|
|
65
|
+
const parts = str.split('?');
|
|
66
|
+
str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
|
|
67
|
+
// Dedupe forward slashes in the entire path, avoiding protocol slashes.
|
|
68
|
+
str = str.replace(/(?<textBefore>[^:/]\/)\/+/g, '$1');
|
|
69
|
+
// Dedupe forward slashes at the beginning of the path.
|
|
70
|
+
str = str.replace(/^\/+/g, '/');
|
|
71
|
+
return str;
|
|
72
|
+
}
|
|
73
|
+
exports.normalizeUrl = normalizeUrl;
|
|
74
|
+
function getEditUrl(fileRelativePath, editUrl) {
|
|
75
|
+
return editUrl
|
|
76
|
+
? // Don't use posixPath for this: we need to force a forward slash path
|
|
77
|
+
normalizeUrl([editUrl, fileRelativePath.replace(/\\/g, '/')])
|
|
78
|
+
: undefined;
|
|
79
|
+
}
|
|
80
|
+
exports.getEditUrl = getEditUrl;
|
|
81
|
+
//# sourceMappingURL=urlUtils.js.map
|
|
@@ -0,0 +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AAK,CAAC,GAAG,CAAC,CAAC;IAC7B,GAAG,GAAG,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAEtE,wEAAwE;IACxE,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,4BAA4B,EAAE,IAAI,CAAC,CAAC;IAEtD,uDAAuD;IACvD,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;IAEhC,OAAO,GAAG,CAAC;AACb,CAAC;AA9ED,oCA8EC;AAED,SAAgB,UAAU,CACxB,gBAAwB,EACxB,OAAgB;IAEhB,OAAO,OAAO;QACZ,CAAC,CAAC,sEAAsE;YACtE,YAAY,CAAC,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;QAC/D,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC;AARD,gCAQC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
export declare function getFileLoaderUtils(): FileLoaderUtils;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=webpackUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpackUtils.d.ts","sourceRoot":"","sources":["../src/webpackUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAQzC,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;AAGF,wBAAgB,kBAAkB,IAAI,eAAe,CA+GpD"}
|
|
@@ -0,0 +1,112 @@
|
|
|
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 = (0, tslib_1.__importDefault)(require("path"));
|
|
12
|
+
const pathUtils_1 = require("./pathUtils");
|
|
13
|
+
const constants_1 = require("./constants");
|
|
14
|
+
// Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447
|
|
15
|
+
function getFileLoaderUtils() {
|
|
16
|
+
// files/images < urlLoaderLimit will be inlined as base64 strings directly in
|
|
17
|
+
// the html
|
|
18
|
+
const urlLoaderLimit = constants_1.WEBPACK_URL_LOADER_LIMIT;
|
|
19
|
+
// defines the path/pattern of the assets handled by webpack
|
|
20
|
+
const fileLoaderFileName = (folder) => `${constants_1.OUTPUT_STATIC_ASSETS_DIR_NAME}/${folder}/[name]-[contenthash].[ext]`;
|
|
21
|
+
const loaders = {
|
|
22
|
+
file: (options) => ({
|
|
23
|
+
loader: require.resolve(`file-loader`),
|
|
24
|
+
options: {
|
|
25
|
+
name: fileLoaderFileName(options.folder),
|
|
26
|
+
},
|
|
27
|
+
}),
|
|
28
|
+
url: (options) => ({
|
|
29
|
+
loader: require.resolve('url-loader'),
|
|
30
|
+
options: {
|
|
31
|
+
limit: urlLoaderLimit,
|
|
32
|
+
name: fileLoaderFileName(options.folder),
|
|
33
|
+
fallback: require.resolve('file-loader'),
|
|
34
|
+
},
|
|
35
|
+
}),
|
|
36
|
+
// TODO avoid conflicts with the ideal-image plugin
|
|
37
|
+
// TODO this may require a little breaking change for ideal-image users?
|
|
38
|
+
// Maybe with the ideal image plugin, all md images should be "ideal"?
|
|
39
|
+
// This is used to force url-loader+file-loader on markdown images
|
|
40
|
+
// https://webpack.js.org/concepts/loaders/#inline
|
|
41
|
+
inlineMarkdownImageFileLoader: `!${(0, pathUtils_1.escapePath)(require.resolve('url-loader'))}?limit=${urlLoaderLimit}&name=${fileLoaderFileName('images')}&fallback=${(0, pathUtils_1.escapePath)(require.resolve('file-loader'))}!`,
|
|
42
|
+
inlineMarkdownLinkFileLoader: `!${(0, pathUtils_1.escapePath)(require.resolve('file-loader'))}?name=${fileLoaderFileName('files')}!`,
|
|
43
|
+
};
|
|
44
|
+
const rules = {
|
|
45
|
+
/**
|
|
46
|
+
* Loads image assets, inlines images via a data URI if they are below
|
|
47
|
+
* the size threshold
|
|
48
|
+
*/
|
|
49
|
+
images: () => ({
|
|
50
|
+
use: [loaders.url({ folder: 'images' })],
|
|
51
|
+
test: /\.(?:ico|jpe?g|png|gif|webp)(?:\?.*)?$/i,
|
|
52
|
+
}),
|
|
53
|
+
fonts: () => ({
|
|
54
|
+
use: [loaders.url({ folder: 'fonts' })],
|
|
55
|
+
test: /\.(?:woff2?|eot|ttf|otf)$/i,
|
|
56
|
+
}),
|
|
57
|
+
/**
|
|
58
|
+
* Loads audio and video and inlines them via a data URI if they are below
|
|
59
|
+
* the size threshold
|
|
60
|
+
*/
|
|
61
|
+
media: () => ({
|
|
62
|
+
use: [loaders.url({ folder: 'medias' })],
|
|
63
|
+
test: /\.(?:mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/i,
|
|
64
|
+
}),
|
|
65
|
+
svg: () => ({
|
|
66
|
+
test: /\.svg$/i,
|
|
67
|
+
oneOf: [
|
|
68
|
+
{
|
|
69
|
+
use: [
|
|
70
|
+
{
|
|
71
|
+
loader: require.resolve('@svgr/webpack'),
|
|
72
|
+
options: {
|
|
73
|
+
prettier: false,
|
|
74
|
+
svgo: true,
|
|
75
|
+
svgoConfig: {
|
|
76
|
+
plugins: [
|
|
77
|
+
{
|
|
78
|
+
name: 'preset-default',
|
|
79
|
+
params: {
|
|
80
|
+
overrides: {
|
|
81
|
+
removeTitle: false,
|
|
82
|
+
removeViewBox: false,
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
titleProp: true,
|
|
89
|
+
ref: ![path_1.default],
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
// We don't want to use SVGR loader for non-React source code
|
|
94
|
+
// ie we don't want to use SVGR for CSS files...
|
|
95
|
+
issuer: {
|
|
96
|
+
and: [/\.(?:tsx?|jsx?|mdx?)$/i],
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
use: [loaders.url({ folder: 'images' })],
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
}),
|
|
104
|
+
otherAssets: () => ({
|
|
105
|
+
use: [loaders.file({ folder: 'files' })],
|
|
106
|
+
test: /\.(?:pdf|docx?|xlsx?|zip|rar)$/i,
|
|
107
|
+
}),
|
|
108
|
+
};
|
|
109
|
+
return { loaders, rules };
|
|
110
|
+
}
|
|
111
|
+
exports.getFileLoaderUtils = getFileLoaderUtils;
|
|
112
|
+
//# sourceMappingURL=webpackUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpackUtils.js","sourceRoot":"","sources":["../src/webpackUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAGH,6DAAwB;AACxB,2CAAuC;AACvC,2CAGqB;AAoBrB,+IAA+I;AAC/I,SAAgB,kBAAkB;IAChC,8EAA8E;IAC9E,WAAW;IACX,MAAM,cAAc,GAAG,oCAAwB,CAAC;IAEhD,4DAA4D;IAC5D,MAAM,kBAAkB,GAAG,CAAC,MAAmB,EAAE,EAAE,CACjD,GAAG,yCAA6B,IAAI,MAAM,6BAA6B,CAAC;IAE1E,MAAM,OAAO,GAA+B;QAC1C,IAAI,EAAE,CAAC,OAA8B,EAAE,EAAE,CAAC,CAAC;YACzC,MAAM,EAAE,OAAO,CAAC,OAAO,CAA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": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.16",
|
|
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,32 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/
|
|
22
|
-
"@
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
21
|
+
"@docusaurus/logger": "2.0.0-beta.16",
|
|
22
|
+
"@svgr/webpack": "^6.0.0",
|
|
23
|
+
"file-loader": "^6.2.0",
|
|
24
|
+
"fs-extra": "^10.0.1",
|
|
25
|
+
"github-slugger": "^1.4.0",
|
|
26
|
+
"globby": "^11.0.4",
|
|
26
27
|
"gray-matter": "^4.0.3",
|
|
27
|
-
"
|
|
28
|
+
"js-yaml": "^4.1.0",
|
|
29
|
+
"lodash": "^4.17.21",
|
|
30
|
+
"micromatch": "^4.0.4",
|
|
28
31
|
"resolve-pathname": "^3.0.0",
|
|
29
|
-
"
|
|
32
|
+
"shelljs": "^0.8.5",
|
|
33
|
+
"tslib": "^2.3.1",
|
|
34
|
+
"url-loader": "^4.1.1",
|
|
35
|
+
"webpack": "^5.69.1"
|
|
30
36
|
},
|
|
31
37
|
"engines": {
|
|
32
|
-
"node": ">=
|
|
38
|
+
"node": ">=14"
|
|
33
39
|
},
|
|
34
40
|
"devDependencies": {
|
|
41
|
+
"@docusaurus/types": "2.0.0-beta.16",
|
|
35
42
|
"@types/dedent": "^0.7.0",
|
|
43
|
+
"@types/github-slugger": "^1.3.0",
|
|
44
|
+
"@types/micromatch": "^4.0.2",
|
|
45
|
+
"@types/react-dom": "^17.0.11",
|
|
36
46
|
"dedent": "^0.7.0"
|
|
37
47
|
},
|
|
38
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "eb43c4d4f95a4fb97dc9bb9dc615413e0dc2e1e7"
|
|
39
49
|
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
export const NODE_MAJOR_VERSION = parseInt(
|
|
9
|
+
process.versions.node.split('.')[0],
|
|
10
|
+
10,
|
|
11
|
+
);
|
|
12
|
+
export const NODE_MINOR_VERSION = parseInt(
|
|
13
|
+
process.versions.node.split('.')[1],
|
|
14
|
+
10,
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
// Can be overridden with cli option --out-dir
|
|
18
|
+
export const DEFAULT_BUILD_DIR_NAME = 'build';
|
|
19
|
+
|
|
20
|
+
// Can be overridden with cli option --config
|
|
21
|
+
export const DEFAULT_CONFIG_FILE_NAME = 'docusaurus.config.js';
|
|
22
|
+
|
|
23
|
+
export const BABEL_CONFIG_FILE_NAME =
|
|
24
|
+
process.env.DOCUSAURUS_BABEL_CONFIG_FILE_NAME || 'babel.config.js';
|
|
25
|
+
|
|
26
|
+
export const GENERATED_FILES_DIR_NAME =
|
|
27
|
+
process.env.DOCUSAURUS_GENERATED_FILES_DIR_NAME || '.docusaurus';
|
|
28
|
+
|
|
29
|
+
export const SRC_DIR_NAME = 'src';
|
|
30
|
+
export const STATIC_DIR_NAME = 'static';
|
|
31
|
+
export const OUTPUT_STATIC_ASSETS_DIR_NAME = 'assets'; // files handled by webpack, hashed (can be cached aggressively)
|
|
32
|
+
export const THEME_PATH = `${SRC_DIR_NAME}/theme`;
|
|
33
|
+
export const DEFAULT_PORT = 3000;
|
|
34
|
+
export const DEFAULT_PLUGIN_ID = 'default';
|
|
35
|
+
|
|
36
|
+
// Temporary fix for https://github.com/facebook/docusaurus/issues/5493
|
|
37
|
+
export const WEBPACK_URL_LOADER_LIMIT =
|
|
38
|
+
process.env.WEBPACK_URL_LOADER_LIMIT ?? 10000;
|
|
@@ -0,0 +1,90 @@
|
|
|
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
|
+
try {
|
|
48
|
+
const contentString = await fs.readFile(filePath, {encoding: 'utf8'});
|
|
49
|
+
const unsafeContent = Yaml.load(contentString);
|
|
50
|
+
return validate(unsafeContent);
|
|
51
|
+
} catch (err) {
|
|
52
|
+
// TODO replace later by error cause, see https://v8.dev/features/error-cause
|
|
53
|
+
logger.error`The ${params.fileType} file at path=${filePath} looks invalid.`;
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Order matters: we look in priority in localized folder
|
|
59
|
+
export function getContentPathList(contentPaths: ContentPaths): string[] {
|
|
60
|
+
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// return the first folder path in which the file exists in
|
|
64
|
+
export async function findFolderContainingFile(
|
|
65
|
+
folderPaths: string[],
|
|
66
|
+
relativeFilePath: string,
|
|
67
|
+
): Promise<string | undefined> {
|
|
68
|
+
return findAsyncSequential(folderPaths, (folderPath) =>
|
|
69
|
+
fs.pathExists(path.join(folderPath, relativeFilePath)),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function getFolderContainingFile(
|
|
74
|
+
folderPaths: string[],
|
|
75
|
+
relativeFilePath: string,
|
|
76
|
+
): Promise<string> {
|
|
77
|
+
const maybeFolderPath = await findFolderContainingFile(
|
|
78
|
+
folderPaths,
|
|
79
|
+
relativeFilePath,
|
|
80
|
+
);
|
|
81
|
+
// should never happen, as the source was read from the FS anyway...
|
|
82
|
+
if (!maybeFolderPath) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`File "${relativeFilePath}" does not exist in any of these folders:\n- ${folderPaths.join(
|
|
85
|
+
'\n- ',
|
|
86
|
+
)}]`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return maybeFolderPath;
|
|
90
|
+
}
|
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/gitUtils.ts
ADDED
|
@@ -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 path from 'path';
|
|
9
|
+
import shell from 'shelljs';
|
|
10
|
+
|
|
11
|
+
export class GitNotFoundError extends Error {}
|
|
12
|
+
|
|
13
|
+
export const getFileCommitDate = (
|
|
14
|
+
file: string,
|
|
15
|
+
{
|
|
16
|
+
age = 'oldest',
|
|
17
|
+
includeAuthor = false,
|
|
18
|
+
}: {
|
|
19
|
+
age?: 'oldest' | 'newest';
|
|
20
|
+
includeAuthor?: boolean;
|
|
21
|
+
},
|
|
22
|
+
): {
|
|
23
|
+
date: Date;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
author?: string;
|
|
26
|
+
} => {
|
|
27
|
+
if (!shell.which('git')) {
|
|
28
|
+
throw new GitNotFoundError(
|
|
29
|
+
`Failed to retrieve git history for "${file}" because git is not installed.`,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!shell.test('-f', file)) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
`Failed to retrieve git history for "${file}" because the file does not exist.`,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const fileBasename = path.basename(file);
|
|
40
|
+
const fileDirname = path.dirname(file);
|
|
41
|
+
|
|
42
|
+
let formatArg = '--format=%ct';
|
|
43
|
+
if (includeAuthor) {
|
|
44
|
+
formatArg += ',%an';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let extraArgs = '--max-count=1';
|
|
48
|
+
if (age === 'oldest') {
|
|
49
|
+
// --follow is necessary to follow file renames
|
|
50
|
+
// --diff-filter=A ensures we only get the commit which (A)dded the file
|
|
51
|
+
extraArgs += ' --follow --diff-filter=A';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const result = shell.exec(
|
|
55
|
+
`git log ${extraArgs} ${formatArg} -- "${fileBasename}"`,
|
|
56
|
+
{
|
|
57
|
+
// cwd is important, see: https://github.com/facebook/docusaurus/pull/5048
|
|
58
|
+
cwd: fileDirname,
|
|
59
|
+
silent: true,
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
if (result.code !== 0) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Failed to retrieve the git history for file "${file}" with exit code ${result.code}: ${result.stderr}`,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
let regex = /^(?<timestamp>\d+)$/;
|
|
68
|
+
if (includeAuthor) {
|
|
69
|
+
regex = /^(?<timestamp>\d+),(?<author>.+)$/;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const output = result.stdout.trim();
|
|
73
|
+
const match = output.match(regex);
|
|
74
|
+
|
|
75
|
+
if (
|
|
76
|
+
!match ||
|
|
77
|
+
!match.groups ||
|
|
78
|
+
!match.groups.timestamp ||
|
|
79
|
+
(includeAuthor && !match.groups.author)
|
|
80
|
+
) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Failed to retrieve the git history for file "${file}" with unexpected output: ${output}`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const timestamp = Number(match.groups.timestamp);
|
|
87
|
+
const date = new Date(timestamp * 1000);
|
|
88
|
+
|
|
89
|
+
if (includeAuthor) {
|
|
90
|
+
return {date, timestamp, author: match.groups.author};
|
|
91
|
+
}
|
|
92
|
+
return {date, timestamp};
|
|
93
|
+
};
|
package/src/globUtils.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
// Globby/Micromatch are the 2 libs we use in Docusaurus consistently
|
|
9
|
+
|
|
10
|
+
import Micromatch from 'micromatch'; // Note: Micromatch is used by Globby
|
|
11
|
+
import path from 'path';
|
|
12
|
+
|
|
13
|
+
export {default as Globby} from 'globby';
|
|
14
|
+
|
|
15
|
+
// The default patterns we ignore when globbing
|
|
16
|
+
// using _ prefix for exclusion by convention
|
|
17
|
+
export const GlobExcludeDefault = [
|
|
18
|
+
// Ignore files starting with _
|
|
19
|
+
'**/_*.{js,jsx,ts,tsx,md,mdx}',
|
|
20
|
+
|
|
21
|
+
// Ignore folders starting with _ (including folder content)
|
|
22
|
+
'**/_*/**',
|
|
23
|
+
|
|
24
|
+
// Ignore tests
|
|
25
|
+
'**/*.test.{js,jsx,ts,tsx}',
|
|
26
|
+
'**/__tests__/**',
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
type Matcher = (str: string) => boolean;
|
|
30
|
+
|
|
31
|
+
export function createMatcher(patterns: string[]): Matcher {
|
|
32
|
+
const regexp = new RegExp(
|
|
33
|
+
patterns.map((pattern) => Micromatch.makeRe(pattern).source).join('|'),
|
|
34
|
+
);
|
|
35
|
+
return (str) => regexp.test(str);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// We use match patterns like '**/_*/**',
|
|
39
|
+
// This function permits to help to:
|
|
40
|
+
// Match /user/sebastien/website/docs/_partials/xyz.md
|
|
41
|
+
// Ignore /user/_sebastien/website/docs/partials/xyz.md
|
|
42
|
+
export function createAbsoluteFilePathMatcher(
|
|
43
|
+
patterns: string[],
|
|
44
|
+
rootFolders: string[],
|
|
45
|
+
): Matcher {
|
|
46
|
+
const matcher = createMatcher(patterns);
|
|
47
|
+
|
|
48
|
+
function getRelativeFilePath(absoluteFilePath: string) {
|
|
49
|
+
const rootFolder = rootFolders.find((folderPath) =>
|
|
50
|
+
absoluteFilePath.startsWith(folderPath),
|
|
51
|
+
);
|
|
52
|
+
if (!rootFolder) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`createAbsoluteFilePathMatcher unexpected error, absoluteFilePath=${absoluteFilePath} was not contained in any of the root folders ${JSON.stringify(
|
|
55
|
+
rootFolders,
|
|
56
|
+
)}`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return path.relative(rootFolder, absoluteFilePath);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (absoluteFilePath: string) =>
|
|
63
|
+
matcher(getRelativeFilePath(absoluteFilePath));
|
|
64
|
+
}
|
package/src/hashUtils.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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 {createHash} from 'crypto';
|
|
9
|
+
import _ from 'lodash';
|
|
10
|
+
import {shortName, isNameTooLong} from './pathUtils';
|
|
11
|
+
|
|
12
|
+
export function md5Hash(str: string): string {
|
|
13
|
+
return createHash('md5').update(str).digest('hex');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function simpleHash(str: string, length: number): string {
|
|
17
|
+
return md5Hash(str).substr(0, length);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
|
|
21
|
+
/**
|
|
22
|
+
* Given an input string, convert to kebab-case and append a hash.
|
|
23
|
+
* Avoid str collision.
|
|
24
|
+
* Also removes part of the string if its larger than the allowed
|
|
25
|
+
* filename per OS. Avoids ERRNAMETOOLONG error.
|
|
26
|
+
*/
|
|
27
|
+
export function docuHash(str: string): string {
|
|
28
|
+
if (str === '/') {
|
|
29
|
+
return 'index';
|
|
30
|
+
}
|
|
31
|
+
const shortHash = simpleHash(str, 3);
|
|
32
|
+
const parsedPath = `${_.kebabCase(str)}-${shortHash}`;
|
|
33
|
+
if (isNameTooLong(parsedPath)) {
|
|
34
|
+
return `${shortName(_.kebabCase(str))}-${shortHash}`;
|
|
35
|
+
}
|
|
36
|
+
return parsedPath;
|
|
37
|
+
}
|