@docusaurus/utils 2.0.0-beta.2 → 2.0.0-beta.20

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.
Files changed (112) hide show
  1. package/lib/constants.d.ts +70 -0
  2. package/lib/constants.d.ts.map +1 -0
  3. package/lib/constants.js +74 -0
  4. package/lib/constants.js.map +1 -0
  5. package/lib/dataFileUtils.d.ts +60 -0
  6. package/lib/dataFileUtils.d.ts.map +1 -0
  7. package/lib/dataFileUtils.js +91 -0
  8. package/lib/dataFileUtils.js.map +1 -0
  9. package/lib/emitUtils.d.ts +32 -0
  10. package/lib/emitUtils.d.ts.map +1 -0
  11. package/lib/emitUtils.js +80 -0
  12. package/lib/emitUtils.js.map +1 -0
  13. package/lib/gitUtils.d.ts +66 -0
  14. package/lib/gitUtils.d.ts.map +1 -0
  15. package/lib/gitUtils.js +66 -0
  16. package/lib/gitUtils.js.map +1 -0
  17. package/lib/globUtils.d.ts +40 -0
  18. package/lib/globUtils.d.ts.map +1 -0
  19. package/lib/globUtils.js +71 -0
  20. package/lib/globUtils.js.map +1 -0
  21. package/lib/hashUtils.d.ts +6 -4
  22. package/lib/hashUtils.d.ts.map +1 -0
  23. package/lib/hashUtils.js +13 -10
  24. package/lib/hashUtils.js.map +1 -0
  25. package/lib/i18nUtils.d.ts +51 -0
  26. package/lib/i18nUtils.d.ts.map +1 -0
  27. package/lib/i18nUtils.js +69 -0
  28. package/lib/i18nUtils.js.map +1 -0
  29. package/lib/index.d.ts +15 -76
  30. package/lib/index.d.ts.map +1 -0
  31. package/lib/index.js +90 -396
  32. package/lib/index.js.map +1 -0
  33. package/lib/jsUtils.d.ts +45 -0
  34. package/lib/jsUtils.d.ts.map +1 -0
  35. package/lib/jsUtils.js +94 -0
  36. package/lib/jsUtils.js.map +1 -0
  37. package/lib/markdownLinks.d.ts +49 -5
  38. package/lib/markdownLinks.d.ts.map +1 -0
  39. package/lib/markdownLinks.js +54 -13
  40. package/lib/markdownLinks.js.map +1 -0
  41. package/lib/markdownUtils.d.ts +112 -0
  42. package/lib/markdownUtils.d.ts.map +1 -0
  43. package/lib/markdownUtils.js +271 -0
  44. package/lib/markdownUtils.js.map +1 -0
  45. package/lib/pathUtils.d.ts +45 -1
  46. package/lib/pathUtils.d.ts.map +1 -0
  47. package/lib/pathUtils.js +92 -12
  48. package/lib/pathUtils.js.map +1 -0
  49. package/lib/slugger.d.ts +24 -0
  50. package/lib/slugger.d.ts.map +1 -0
  51. package/lib/slugger.js +23 -0
  52. package/lib/slugger.js.map +1 -0
  53. package/lib/tags.d.ts +59 -0
  54. package/lib/tags.d.ts.map +1 -0
  55. package/lib/tags.js +91 -0
  56. package/lib/tags.js.map +1 -0
  57. package/lib/urlUtils.d.ts +66 -0
  58. package/lib/urlUtils.d.ts.map +1 -0
  59. package/lib/urlUtils.js +207 -0
  60. package/lib/urlUtils.js.map +1 -0
  61. package/lib/webpackUtils.d.ts +35 -0
  62. package/lib/webpackUtils.d.ts.map +1 -0
  63. package/lib/webpackUtils.js +115 -0
  64. package/lib/webpackUtils.js.map +1 -0
  65. package/package.json +20 -10
  66. package/src/constants.ts +94 -0
  67. package/src/dataFileUtils.ts +122 -0
  68. package/{lib/getFilePathForRoutePath.d.ts → src/deps.d.ts} +4 -1
  69. package/src/emitUtils.ts +99 -0
  70. package/src/gitUtils.ts +153 -0
  71. package/src/globUtils.ts +85 -0
  72. package/src/hashUtils.ts +9 -8
  73. package/src/i18nUtils.ts +115 -0
  74. package/src/index.ts +97 -503
  75. package/src/jsUtils.ts +102 -0
  76. package/src/markdownLinks.ts +95 -30
  77. package/src/markdownUtils.ts +354 -0
  78. package/src/pathUtils.ts +93 -12
  79. package/src/slugger.ts +36 -0
  80. package/src/tags.ts +130 -0
  81. package/src/urlUtils.ts +234 -0
  82. package/src/webpackUtils.ts +153 -0
  83. package/lib/.tsbuildinfo +0 -1
  84. package/lib/codeTranslationsUtils.d.ts +0 -11
  85. package/lib/codeTranslationsUtils.js +0 -50
  86. package/lib/docuHash.d.ts +0 -13
  87. package/lib/docuHash.js +0 -29
  88. package/lib/escapePath.d.ts +0 -17
  89. package/lib/escapePath.js +0 -25
  90. package/lib/getFilePathForRoutePath.js +0 -40
  91. package/lib/markdownParser.d.ts +0 -30
  92. package/lib/markdownParser.js +0 -140
  93. package/lib/posixPath.d.ts +0 -14
  94. package/lib/posixPath.js +0 -28
  95. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  96. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  97. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  98. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
  99. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  100. package/src/__tests__/escapePath.test.ts +0 -25
  101. package/src/__tests__/getFilePathForRoutePath.test.ts +0 -87
  102. package/src/__tests__/hashUtils.test.ts +0 -51
  103. package/src/__tests__/index.test.ts +0 -631
  104. package/src/__tests__/markdownParser.test.ts +0 -817
  105. package/src/__tests__/pathUtils.test.ts +0 -63
  106. package/src/__tests__/posixPath.test.ts +0 -25
  107. package/src/codeTranslationsUtils.ts +0 -56
  108. package/src/escapePath.ts +0 -23
  109. package/src/getFilePathForRoutePath.ts +0 -43
  110. package/src/markdownParser.ts +0 -182
  111. package/src/posixPath.ts +0 -27
  112. 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/docuHash.d.ts DELETED
@@ -1,13 +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
- * Given an input string, convert to kebab-case and append a hash.
9
- * Avoid str collision.
10
- * Also removes part of the string if its larger than the allowed
11
- * filename per OS. Avoids ERRNAMETOOLONG error.
12
- */
13
- export declare function docuHash(str: string): string;
package/lib/docuHash.js DELETED
@@ -1,29 +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.docuHash = void 0;
10
- const lodash_1 = require("lodash");
11
- const pathUtils_1 = require("./pathUtils");
12
- /**
13
- * Given an input string, convert to kebab-case and append a hash.
14
- * Avoid str collision.
15
- * Also removes part of the string if its larger than the allowed
16
- * filename per OS. Avoids ERRNAMETOOLONG error.
17
- */
18
- function docuHash(str) {
19
- if (str === '/') {
20
- return 'index';
21
- }
22
- const shortHash = pathUtils_1.simpleHash(str, 3);
23
- const parsedPath = `${lodash_1.kebabCase(str)}-${shortHash}`;
24
- if (pathUtils_1.isNameTooLong(parsedPath)) {
25
- return `${pathUtils_1.shortName(lodash_1.kebabCase(str))}-${shortHash}`;
26
- }
27
- return parsedPath;
28
- }
29
- exports.docuHash = docuHash;
@@ -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;
@@ -1,40 +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.getFilePathForRoutePath = void 0;
10
- const tslib_1 = require("tslib");
11
- const path_1 = tslib_1.__importDefault(require("path"));
12
- /*
13
- export function getFilePathForRoutePath(routePath: string): string {
14
- const fileName = path.basename(routePath);
15
- const filePath = path.dirname(routePath);
16
- return path.join(filePath, `${fileName}/index.html`);
17
- }
18
- */
19
- // Almost exact copy of the behavior we implemented in our Docusaurus fork of the webpack static gen plugin
20
- // See https://github.com/slorber/static-site-generator-webpack-plugin/blob/master/index.js#L167
21
- function getFilePathForRoutePath(routePath, trailingSlash) {
22
- // const outputFileName = routePath.replace(/^(\/|\\)/, ''); // Remove leading slashes for webpack-dev-server
23
- // Paths ending with .html are left untouched
24
- if (/\.(html?)$/i.test(routePath)) {
25
- return routePath;
26
- }
27
- // Legacy retro-compatible behavior
28
- if (typeof trailingSlash === 'undefined') {
29
- return path_1.default.join(routePath, 'index.html');
30
- }
31
- // New behavior: we can say if we prefer file/folder output
32
- // Useful resource: https://github.com/slorber/trailing-slash-guide
33
- if (routePath === '' || routePath.endsWith('/') || trailingSlash) {
34
- return path_1.default.join(routePath, 'index.html');
35
- }
36
- else {
37
- return `${routePath}.html`;
38
- }
39
- }
40
- exports.getFilePathForRoutePath = getFilePathForRoutePath;
@@ -1,30 +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
- removeContentTitle?: 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
- removeContentTitle?: boolean;
26
- }): ParsedMarkdown;
27
- export declare function parseMarkdownFile(source: string, options?: {
28
- removeContentTitle?: boolean;
29
- }): Promise<ParsedMarkdown>;
30
- export {};
@@ -1,140 +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
- // Remove custom Markdown heading id.
58
- .replace(/{#*[\w-]+}/, '')
59
- .trim();
60
- if (cleanedLine) {
61
- return cleanedLine;
62
- }
63
- }
64
- return undefined;
65
- }
66
- exports.createExcerpt = createExcerpt;
67
- function parseFrontMatter(markdownFileContent) {
68
- var _a;
69
- const { data, content } = gray_matter_1.default(markdownFileContent);
70
- return {
71
- frontMatter: data !== null && data !== void 0 ? data : {},
72
- content: (_a = content === null || content === void 0 ? void 0 : content.trim()) !== null && _a !== void 0 ? _a : '',
73
- };
74
- }
75
- exports.parseFrontMatter = parseFrontMatter;
76
- // Try to convert markdown heading as text
77
- // Does not need to be perfect, it is only used as a fallback when frontMatter.title is not provided
78
- // For now, we just unwrap possible inline code blocks (# `config.js`)
79
- function toTextContentTitle(contentTitle) {
80
- if (contentTitle.startsWith('`') && contentTitle.endsWith('`')) {
81
- return contentTitle.substring(1, contentTitle.length - 1);
82
- }
83
- return contentTitle;
84
- }
85
- function parseMarkdownContentTitle(contentUntrimmed, options) {
86
- var _a, _b;
87
- const removeContentTitleOption = (_a = options === null || options === void 0 ? void 0 : options.removeContentTitle) !== null && _a !== void 0 ? _a : false;
88
- const content = contentUntrimmed.trim();
89
- const IMPORT_STATEMENT = /import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/
90
- .source;
91
- const REGULAR_TITLE = /(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
92
- .source;
93
- const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
94
- const regularTitleMatch = new RegExp(`^(?:${IMPORT_STATEMENT})*?${REGULAR_TITLE}`, 'g').exec(content);
95
- const alternateTitleMatch = new RegExp(`^(?:${IMPORT_STATEMENT})*?${ALTERNATE_TITLE}`, 'g').exec(content);
96
- const titleMatch = regularTitleMatch !== null && regularTitleMatch !== void 0 ? regularTitleMatch : alternateTitleMatch;
97
- const { pattern, title } = (_b = titleMatch === null || titleMatch === void 0 ? void 0 : titleMatch.groups) !== null && _b !== void 0 ? _b : {};
98
- if (!pattern || !title) {
99
- return { content, contentTitle: undefined };
100
- }
101
- else {
102
- const newContent = removeContentTitleOption
103
- ? content.replace(pattern, '')
104
- : content;
105
- return {
106
- content: newContent.trim(),
107
- contentTitle: toTextContentTitle(title.trim()).trim(),
108
- };
109
- }
110
- }
111
- exports.parseMarkdownContentTitle = parseMarkdownContentTitle;
112
- function parseMarkdownString(markdownFileContent, options) {
113
- try {
114
- const { frontMatter, content: contentWithoutFrontMatter } = parseFrontMatter(markdownFileContent);
115
- const { content, contentTitle } = parseMarkdownContentTitle(contentWithoutFrontMatter, options);
116
- const excerpt = createExcerpt(content);
117
- return {
118
- frontMatter,
119
- content,
120
- contentTitle,
121
- excerpt,
122
- };
123
- }
124
- catch (e) {
125
- console.error(chalk_1.default.red(`Error while parsing Markdown frontmatter.
126
- This can happen if you use special characters in frontmatter values (try using double quotes around that value).`));
127
- throw e;
128
- }
129
- }
130
- exports.parseMarkdownString = parseMarkdownString;
131
- async function parseMarkdownFile(source, options) {
132
- const markdownString = await fs_extra_1.default.readFile(source, 'utf-8');
133
- try {
134
- return parseMarkdownString(markdownString, options);
135
- }
136
- catch (e) {
137
- throw new Error(`Error while parsing Markdown file ${source}: "${e.message}".`);
138
- }
139
- }
140
- exports.parseMarkdownFile = parseMarkdownFile;
@@ -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,4 +0,0 @@
1
- {
2
- "id1": "message 1 en",
3
- "id2": "message 2 en"
4
- }
@@ -1,5 +0,0 @@
1
- {
2
- "id1": "message 1 fr_FR",
3
- "id2": "message 2 fr_FR",
4
- "id3": "message 3 fr_FR"
5
- }
@@ -1,4 +0,0 @@
1
- {
2
- "id1": "message 1 fr",
3
- "id2": "message 2 fr"
4
- }
@@ -1,8 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`getFolderContainingFile throw if no folder contain such file 1`] = `
4
- "File \\"index.test.ts\\" does not exist in any of these folders:
5
- - /abcdef
6
- - /gehij
7
- - /klmn]"
8
- `;
@@ -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
- });
@@ -1,87 +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 {getFilePathForRoutePath} from '../getFilePathForRoutePath';
9
- import {posixPath} from '../posixPath';
10
-
11
- describe('getFilePathForRoutePath trailingSlash=undefined', () => {
12
- test('works for /', () => {
13
- expect(posixPath(getFilePathForRoutePath('/', undefined))).toEqual(
14
- '/index.html',
15
- );
16
- });
17
-
18
- test('works for /somePath', () => {
19
- expect(posixPath(getFilePathForRoutePath('/somePath', undefined))).toEqual(
20
- '/somePath/index.html',
21
- );
22
- });
23
-
24
- test('works for /somePath/', () => {
25
- expect(posixPath(getFilePathForRoutePath('/somePath/', undefined))).toEqual(
26
- '/somePath/index.html',
27
- );
28
- });
29
-
30
- test('works for /somePath/xyz.html', () => {
31
- expect(
32
- posixPath(getFilePathForRoutePath('/somePath/xyz.html', undefined)),
33
- ).toEqual('/somePath/xyz.html');
34
- });
35
- });
36
-
37
- describe('getFilePathForRoutePath trailingSlash=true', () => {
38
- test('works for /', () => {
39
- expect(posixPath(getFilePathForRoutePath('/', true))).toEqual(
40
- '/index.html',
41
- );
42
- });
43
-
44
- test('works for /somePath', () => {
45
- expect(posixPath(getFilePathForRoutePath('/somePath', true))).toEqual(
46
- '/somePath/index.html',
47
- );
48
- });
49
-
50
- test('works for /somePath/', () => {
51
- expect(posixPath(getFilePathForRoutePath('/somePath/', true))).toEqual(
52
- '/somePath/index.html',
53
- );
54
- });
55
-
56
- test('works for /somePath/xyz.html', () => {
57
- expect(
58
- posixPath(getFilePathForRoutePath('/somePath/xyz.html', true)),
59
- ).toEqual('/somePath/xyz.html');
60
- });
61
- });
62
-
63
- describe('getFilePathForRoutePath trailingSlash=false', () => {
64
- test('works for /', () => {
65
- expect(posixPath(getFilePathForRoutePath('/', false))).toEqual(
66
- '/index.html',
67
- );
68
- });
69
-
70
- test('works for /somePath', () => {
71
- expect(posixPath(getFilePathForRoutePath('/somePath', false))).toEqual(
72
- '/somePath.html',
73
- );
74
- });
75
-
76
- test('works for /somePath/', () => {
77
- expect(posixPath(getFilePathForRoutePath('/somePath/', false))).toEqual(
78
- '/somePath/index.html',
79
- );
80
- });
81
-
82
- test('works for /somePath/xyz.html', () => {
83
- expect(
84
- posixPath(getFilePathForRoutePath('/somePath/xyz.html', false)),
85
- ).toEqual('/somePath/xyz.html');
86
- });
87
- });