@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
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
export declare function codeTranslationLocalesToTry(locale: string): string[];
|
|
8
|
-
export declare function readDefaultCodeTranslationMessages({ dirPath, locale, }: {
|
|
9
|
-
dirPath: string;
|
|
10
|
-
locale: string;
|
|
11
|
-
}): Promise<Record<string, string>>;
|
|
@@ -1,50 +0,0 @@
|
|
|
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.readDefaultCodeTranslationMessages = exports.codeTranslationLocalesToTry = void 0;
|
|
10
|
-
const tslib_1 = require("tslib");
|
|
11
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
12
|
-
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
13
|
-
// Return an ordered list of locales we should try
|
|
14
|
-
function codeTranslationLocalesToTry(locale) {
|
|
15
|
-
// @ts-expect-error: TODO until available in TS, see https://github.com/microsoft/TypeScript/issues/37326
|
|
16
|
-
const intlLocale = Intl.Locale ? new Intl.Locale(locale) : undefined;
|
|
17
|
-
if (!intlLocale) {
|
|
18
|
-
return [locale];
|
|
19
|
-
}
|
|
20
|
-
// if locale is just a simple language like "pt", we want to fallback to pt-BR (not pt-PT!)
|
|
21
|
-
// see https://github.com/facebook/docusaurus/pull/4536#issuecomment-810088783
|
|
22
|
-
if (intlLocale.language === locale) {
|
|
23
|
-
const maximizedLocale = intlLocale.maximize(); // pt-Latn-BR`
|
|
24
|
-
// ["pt","pt-BR"]
|
|
25
|
-
return [locale, `${maximizedLocale.language}-${maximizedLocale.region}`];
|
|
26
|
-
}
|
|
27
|
-
// if locale is like "pt-BR", we want to fallback to "pt"
|
|
28
|
-
else {
|
|
29
|
-
return [locale, intlLocale.language];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
exports.codeTranslationLocalesToTry = codeTranslationLocalesToTry;
|
|
33
|
-
// Useful to implement getDefaultCodeTranslationMessages() in themes
|
|
34
|
-
async function readDefaultCodeTranslationMessages({ dirPath, locale, }) {
|
|
35
|
-
const localesToTry = codeTranslationLocalesToTry(locale);
|
|
36
|
-
// Return the content of the first file that match
|
|
37
|
-
// fr_FR.json => fr.json => nothing
|
|
38
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
39
|
-
for (const fileName of localesToTry) {
|
|
40
|
-
const filePath = path_1.default.resolve(dirPath, `${fileName}.json`);
|
|
41
|
-
// eslint-disable-next-line no-await-in-loop
|
|
42
|
-
if (await fs_extra_1.default.pathExists(filePath)) {
|
|
43
|
-
// eslint-disable-next-line no-await-in-loop
|
|
44
|
-
const fileContent = await fs_extra_1.default.readFile(filePath, 'utf8');
|
|
45
|
-
return JSON.parse(fileContent);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return {};
|
|
49
|
-
}
|
|
50
|
-
exports.readDefaultCodeTranslationMessages = readDefaultCodeTranslationMessages;
|
package/lib/escapePath.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
* When you have a path like C:\X\Y
|
|
9
|
-
* It is not safe to use directly when generating code
|
|
10
|
-
* For example, this would fail due to unescaped \: `<img src={require('${filePath}')} />`
|
|
11
|
-
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
|
12
|
-
*
|
|
13
|
-
* Workaround for issue in posixPath, maybe we won't need it anymore soon?
|
|
14
|
-
* https://github.com/facebook/docusaurus/issues/4730#issuecomment-833530370
|
|
15
|
-
* https://github.com/sindresorhus/slash/pull/16#issuecomment-833528479
|
|
16
|
-
*/
|
|
17
|
-
export declare function escapePath(str: string): string;
|
package/lib/escapePath.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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.escapePath = void 0;
|
|
10
|
-
/**
|
|
11
|
-
* When you have a path like C:\X\Y
|
|
12
|
-
* It is not safe to use directly when generating code
|
|
13
|
-
* For example, this would fail due to unescaped \: `<img src={require('${filePath}')} />`
|
|
14
|
-
* But this would work: `<img src={require('${escapePath(filePath)}')} />`
|
|
15
|
-
*
|
|
16
|
-
* Workaround for issue in posixPath, maybe we won't need it anymore soon?
|
|
17
|
-
* https://github.com/facebook/docusaurus/issues/4730#issuecomment-833530370
|
|
18
|
-
* https://github.com/sindresorhus/slash/pull/16#issuecomment-833528479
|
|
19
|
-
*/
|
|
20
|
-
function escapePath(str) {
|
|
21
|
-
const escaped = JSON.stringify(str);
|
|
22
|
-
// Remove the " around the json string;
|
|
23
|
-
return escaped.substring(1, escaped.length - 1);
|
|
24
|
-
}
|
|
25
|
-
exports.escapePath = escapePath;
|
package/lib/markdownParser.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
export declare function createExcerpt(fileString: string): string | undefined;
|
|
8
|
-
export declare function parseFrontMatter(markdownFileContent: string): {
|
|
9
|
-
frontMatter: Record<string, unknown>;
|
|
10
|
-
content: string;
|
|
11
|
-
};
|
|
12
|
-
export declare function parseMarkdownContentTitle(contentUntrimmed: string, options?: {
|
|
13
|
-
keepContentTitle?: boolean;
|
|
14
|
-
}): {
|
|
15
|
-
content: string;
|
|
16
|
-
contentTitle: string | undefined;
|
|
17
|
-
};
|
|
18
|
-
declare type ParsedMarkdown = {
|
|
19
|
-
frontMatter: Record<string, unknown>;
|
|
20
|
-
content: string;
|
|
21
|
-
contentTitle: string | undefined;
|
|
22
|
-
excerpt: string | undefined;
|
|
23
|
-
};
|
|
24
|
-
export declare function parseMarkdownString(markdownFileContent: string, options?: {
|
|
25
|
-
keepContentTitle?: boolean;
|
|
26
|
-
}): ParsedMarkdown;
|
|
27
|
-
export declare function parseMarkdownFile(source: string): Promise<ParsedMarkdown>;
|
|
28
|
-
export {};
|
package/lib/markdownParser.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
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.parseMarkdownFile = exports.parseMarkdownString = exports.parseMarkdownContentTitle = exports.parseFrontMatter = exports.createExcerpt = void 0;
|
|
10
|
-
const tslib_1 = require("tslib");
|
|
11
|
-
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
12
|
-
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
13
|
-
const gray_matter_1 = tslib_1.__importDefault(require("gray-matter"));
|
|
14
|
-
// Hacky way of stripping out import statements from the excerpt
|
|
15
|
-
// TODO: Find a better way to do so, possibly by compiling the Markdown content,
|
|
16
|
-
// stripping out HTML tags and obtaining the first line.
|
|
17
|
-
function createExcerpt(fileString) {
|
|
18
|
-
const fileLines = fileString
|
|
19
|
-
.trimLeft()
|
|
20
|
-
// Remove Markdown alternate title
|
|
21
|
-
.replace(/^[^\n]*\n[=]+/g, '')
|
|
22
|
-
.split('\n');
|
|
23
|
-
/* eslint-disable no-continue */
|
|
24
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
25
|
-
for (const fileLine of fileLines) {
|
|
26
|
-
// Skip empty line.
|
|
27
|
-
if (!fileLine.trim()) {
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
// Skip import/export declaration.
|
|
31
|
-
if (/^\s*?import\s.*(from.*)?;?|export\s.*{.*};?/.test(fileLine)) {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
const cleanedLine = fileLine
|
|
35
|
-
// Remove HTML tags.
|
|
36
|
-
.replace(/<[^>]*>/g, '')
|
|
37
|
-
// Remove Title headers
|
|
38
|
-
.replace(/^\#\s*([^#]*)\s*\#?/gm, '')
|
|
39
|
-
// Remove Markdown + ATX-style headers
|
|
40
|
-
.replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1')
|
|
41
|
-
// Remove emphasis and strikethroughs.
|
|
42
|
-
.replace(/([\*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
|
|
43
|
-
// Remove images.
|
|
44
|
-
.replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
|
|
45
|
-
// Remove footnotes.
|
|
46
|
-
.replace(/\[\^.+?\](\: .*?$)?/g, '')
|
|
47
|
-
// Remove inline links.
|
|
48
|
-
.replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
|
|
49
|
-
// Remove inline code.
|
|
50
|
-
.replace(/`(.+?)`/g, '$1')
|
|
51
|
-
// Remove blockquotes.
|
|
52
|
-
.replace(/^\s{0,3}>\s?/g, '')
|
|
53
|
-
// Remove admonition definition.
|
|
54
|
-
.replace(/(:{3}.*)/, '')
|
|
55
|
-
// Remove Emoji names within colons include preceding whitespace.
|
|
56
|
-
.replace(/\s?(:(::|[^:\n])+:)/g, '')
|
|
57
|
-
.trim();
|
|
58
|
-
if (cleanedLine) {
|
|
59
|
-
return cleanedLine;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
exports.createExcerpt = createExcerpt;
|
|
65
|
-
function parseFrontMatter(markdownFileContent) {
|
|
66
|
-
var _a;
|
|
67
|
-
const { data, content } = gray_matter_1.default(markdownFileContent);
|
|
68
|
-
return {
|
|
69
|
-
frontMatter: data !== null && data !== void 0 ? data : {},
|
|
70
|
-
content: (_a = content === null || content === void 0 ? void 0 : content.trim()) !== null && _a !== void 0 ? _a : '',
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
exports.parseFrontMatter = parseFrontMatter;
|
|
74
|
-
function parseMarkdownContentTitle(contentUntrimmed, options) {
|
|
75
|
-
var _a, _b;
|
|
76
|
-
const keepContentTitleOption = (_a = options === null || options === void 0 ? void 0 : options.keepContentTitle) !== null && _a !== void 0 ? _a : false;
|
|
77
|
-
const content = contentUntrimmed.trim();
|
|
78
|
-
const IMPORT_STATEMENT = /import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/
|
|
79
|
-
.source;
|
|
80
|
-
const REGULAR_TITLE = /(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
|
|
81
|
-
.source;
|
|
82
|
-
const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
|
|
83
|
-
const regularTitleMatch = new RegExp(`^(?:${IMPORT_STATEMENT})*?${REGULAR_TITLE}`, 'g').exec(content);
|
|
84
|
-
const alternateTitleMatch = new RegExp(`^(?:${IMPORT_STATEMENT})*?${ALTERNATE_TITLE}`, 'g').exec(content);
|
|
85
|
-
const titleMatch = regularTitleMatch !== null && regularTitleMatch !== void 0 ? regularTitleMatch : alternateTitleMatch;
|
|
86
|
-
const { pattern, title } = (_b = titleMatch === null || titleMatch === void 0 ? void 0 : titleMatch.groups) !== null && _b !== void 0 ? _b : {};
|
|
87
|
-
if (!pattern || !title) {
|
|
88
|
-
return { content, contentTitle: undefined };
|
|
89
|
-
}
|
|
90
|
-
const newContent = keepContentTitleOption
|
|
91
|
-
? content
|
|
92
|
-
: content.replace(pattern, '');
|
|
93
|
-
return {
|
|
94
|
-
content: newContent.trim(),
|
|
95
|
-
contentTitle: title.trim(),
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
exports.parseMarkdownContentTitle = parseMarkdownContentTitle;
|
|
99
|
-
function parseMarkdownString(markdownFileContent, options) {
|
|
100
|
-
var _a;
|
|
101
|
-
try {
|
|
102
|
-
const keepContentTitle = (_a = options === null || options === void 0 ? void 0 : options.keepContentTitle) !== null && _a !== void 0 ? _a : false;
|
|
103
|
-
const { frontMatter, content: contentWithoutFrontMatter } = parseFrontMatter(markdownFileContent);
|
|
104
|
-
const { content, contentTitle } = parseMarkdownContentTitle(contentWithoutFrontMatter, {
|
|
105
|
-
keepContentTitle,
|
|
106
|
-
});
|
|
107
|
-
const excerpt = createExcerpt(content);
|
|
108
|
-
return {
|
|
109
|
-
frontMatter,
|
|
110
|
-
content,
|
|
111
|
-
contentTitle,
|
|
112
|
-
excerpt,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
catch (e) {
|
|
116
|
-
console.error(chalk_1.default.red(`Error while parsing markdown front matter.
|
|
117
|
-
This can happen if you use special characters like : in frontmatter values (try using "" around that value)`));
|
|
118
|
-
throw e;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
exports.parseMarkdownString = parseMarkdownString;
|
|
122
|
-
async function parseMarkdownFile(source) {
|
|
123
|
-
const markdownString = await fs_extra_1.default.readFile(source, 'utf-8');
|
|
124
|
-
try {
|
|
125
|
-
return parseMarkdownString(markdownString);
|
|
126
|
-
}
|
|
127
|
-
catch (e) {
|
|
128
|
-
throw new Error(`Error while parsing markdown file ${source}
|
|
129
|
-
${e.message}`);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
exports.parseMarkdownFile = parseMarkdownFile;
|
package/lib/posixPath.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
* Convert Windows backslash paths to posix style paths.
|
|
9
|
-
* E.g: endi\\lie -> endi/lie
|
|
10
|
-
*
|
|
11
|
-
* Looks like this code was originally copied from https://github.com/sindresorhus/slash/blob/main/index.js
|
|
12
|
-
*
|
|
13
|
-
*/
|
|
14
|
-
export declare function posixPath(str: string): string;
|
package/lib/posixPath.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
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.posixPath = void 0;
|
|
10
|
-
/**
|
|
11
|
-
* Convert Windows backslash paths to posix style paths.
|
|
12
|
-
* E.g: endi\\lie -> endi/lie
|
|
13
|
-
*
|
|
14
|
-
* Looks like this code was originally copied from https://github.com/sindresorhus/slash/blob/main/index.js
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
function posixPath(str) {
|
|
18
|
-
const isExtendedLengthPath = /^\\\\\?\\/.test(str);
|
|
19
|
-
// TODO not sure why we need this
|
|
20
|
-
// See https://github.com/sindresorhus/slash/pull/16#issuecomment-833528479
|
|
21
|
-
// See https://github.com/facebook/docusaurus/issues/4730#issuecomment-833530370
|
|
22
|
-
const hasNonAscii = /[^\u0000-\u0080]+/.test(str); // eslint-disable-line
|
|
23
|
-
if (isExtendedLengthPath || hasNonAscii) {
|
|
24
|
-
return str;
|
|
25
|
-
}
|
|
26
|
-
return str.replace(/\\/g, '/');
|
|
27
|
-
}
|
|
28
|
-
exports.posixPath = posixPath;
|
|
@@ -1,112 +0,0 @@
|
|
|
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 path from 'path';
|
|
8
|
-
import fs from 'fs-extra';
|
|
9
|
-
import {
|
|
10
|
-
codeTranslationLocalesToTry,
|
|
11
|
-
readDefaultCodeTranslationMessages,
|
|
12
|
-
} from '../codeTranslationsUtils';
|
|
13
|
-
|
|
14
|
-
describe('codeTranslationLocalesToTry', () => {
|
|
15
|
-
test('should return appropriate locale lists', () => {
|
|
16
|
-
expect(codeTranslationLocalesToTry('fr')).toEqual(['fr', 'fr-FR']);
|
|
17
|
-
expect(codeTranslationLocalesToTry('fr-FR')).toEqual(['fr-FR', 'fr']);
|
|
18
|
-
// Note: "pt" is expanded into "pt-BR", not "pt-PT", as "pt-BR" is more widely used!
|
|
19
|
-
// See https://github.com/facebook/docusaurus/pull/4536#issuecomment-810088783
|
|
20
|
-
expect(codeTranslationLocalesToTry('pt')).toEqual(['pt', 'pt-BR']);
|
|
21
|
-
expect(codeTranslationLocalesToTry('pt-BR')).toEqual(['pt-BR', 'pt']);
|
|
22
|
-
expect(codeTranslationLocalesToTry('pt-PT')).toEqual(['pt-PT', 'pt']);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe('readDefaultCodeTranslationMessages', () => {
|
|
27
|
-
const dirPath = path.resolve(
|
|
28
|
-
__dirname,
|
|
29
|
-
'__fixtures__',
|
|
30
|
-
'defaultCodeTranslations',
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
async function readAsJSON(filename: string) {
|
|
34
|
-
return JSON.parse(
|
|
35
|
-
await fs.readFile(path.resolve(dirPath, filename), 'utf8'),
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
test('for empty locale', async () => {
|
|
40
|
-
await expect(
|
|
41
|
-
readDefaultCodeTranslationMessages({
|
|
42
|
-
locale: '',
|
|
43
|
-
dirPath,
|
|
44
|
-
}),
|
|
45
|
-
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
46
|
-
`"First argument to Intl.Locale constructor can't be empty or missing"`,
|
|
47
|
-
);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('for unexisting locale', async () => {
|
|
51
|
-
await expect(
|
|
52
|
-
readDefaultCodeTranslationMessages({
|
|
53
|
-
locale: 'es',
|
|
54
|
-
dirPath,
|
|
55
|
-
}),
|
|
56
|
-
).resolves.toEqual({});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test('for fr but bad folder', async () => {
|
|
60
|
-
await expect(
|
|
61
|
-
readDefaultCodeTranslationMessages({
|
|
62
|
-
locale: 'fr',
|
|
63
|
-
dirPath: __dirname,
|
|
64
|
-
}),
|
|
65
|
-
).resolves.toEqual({});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test('for fr', async () => {
|
|
69
|
-
await expect(
|
|
70
|
-
readDefaultCodeTranslationMessages({
|
|
71
|
-
locale: 'fr',
|
|
72
|
-
dirPath,
|
|
73
|
-
}),
|
|
74
|
-
).resolves.toEqual(await readAsJSON('fr.json'));
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test('for fr-FR', async () => {
|
|
78
|
-
await expect(
|
|
79
|
-
readDefaultCodeTranslationMessages({
|
|
80
|
-
locale: 'fr-FR',
|
|
81
|
-
dirPath,
|
|
82
|
-
}),
|
|
83
|
-
).resolves.toEqual(await readAsJSON('fr-FR.json'));
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test('for en', async () => {
|
|
87
|
-
await expect(
|
|
88
|
-
readDefaultCodeTranslationMessages({
|
|
89
|
-
locale: 'en',
|
|
90
|
-
dirPath,
|
|
91
|
-
}),
|
|
92
|
-
).resolves.toEqual(await readAsJSON('en.json'));
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test('for en-US', async () => {
|
|
96
|
-
await expect(
|
|
97
|
-
readDefaultCodeTranslationMessages({
|
|
98
|
-
locale: 'en-US',
|
|
99
|
-
dirPath,
|
|
100
|
-
}),
|
|
101
|
-
).resolves.toEqual(await readAsJSON('en.json'));
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test('for en-WHATEVER', async () => {
|
|
105
|
-
await expect(
|
|
106
|
-
readDefaultCodeTranslationMessages({
|
|
107
|
-
locale: 'en-WHATEVER',
|
|
108
|
-
dirPath,
|
|
109
|
-
}),
|
|
110
|
-
).resolves.toEqual(await readAsJSON('en.json'));
|
|
111
|
-
});
|
|
112
|
-
});
|
|
@@ -1,25 +0,0 @@
|
|
|
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 {escapePath} from '../escapePath';
|
|
9
|
-
|
|
10
|
-
describe('escapePath', () => {
|
|
11
|
-
test('escapePath works', () => {
|
|
12
|
-
const asserts: Record<string, string> = {
|
|
13
|
-
'c:/aaaa\\bbbb': 'c:/aaaa\\\\bbbb',
|
|
14
|
-
'c:\\aaaa\\bbbb\\★': 'c:\\\\aaaa\\\\bbbb\\\\★',
|
|
15
|
-
'\\\\?\\c:\\aaaa\\bbbb': '\\\\\\\\?\\\\c:\\\\aaaa\\\\bbbb',
|
|
16
|
-
'c:\\aaaa\\bbbb': 'c:\\\\aaaa\\\\bbbb',
|
|
17
|
-
'foo\\bar': 'foo\\\\bar',
|
|
18
|
-
'foo\\bar/lol': 'foo\\\\bar/lol',
|
|
19
|
-
'website\\docs/**/*.{md,mdx}': 'website\\\\docs/**/*.{md,mdx}',
|
|
20
|
-
};
|
|
21
|
-
Object.keys(asserts).forEach((file) => {
|
|
22
|
-
expect(escapePath(file)).toBe(asserts[file]);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
});
|