@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.
Files changed (88) hide show
  1. package/README.md +1 -1
  2. package/lib/constants.d.ts +20 -0
  3. package/lib/constants.d.ts.map +1 -0
  4. package/lib/constants.js +27 -0
  5. package/lib/constants.js.map +1 -0
  6. package/lib/dataFileUtils.d.ts +24 -0
  7. package/lib/dataFileUtils.d.ts.map +1 -0
  8. package/lib/dataFileUtils.js +65 -0
  9. package/lib/dataFileUtils.js.map +1 -0
  10. package/lib/gitUtils.d.ts +17 -0
  11. package/lib/gitUtils.d.ts.map +1 -0
  12. package/lib/gitUtils.js +63 -0
  13. package/lib/gitUtils.js.map +1 -0
  14. package/lib/globUtils.d.ts +12 -0
  15. package/lib/globUtils.d.ts.map +1 -0
  16. package/lib/globUtils.js +48 -0
  17. package/lib/globUtils.js.map +1 -0
  18. package/lib/hashUtils.d.ts +16 -0
  19. package/lib/hashUtils.d.ts.map +1 -0
  20. package/lib/hashUtils.js +41 -0
  21. package/lib/hashUtils.js.map +1 -0
  22. package/lib/index.d.ts +30 -52
  23. package/lib/index.d.ts.map +1 -0
  24. package/lib/index.js +126 -247
  25. package/lib/index.js.map +1 -0
  26. package/lib/markdownLinks.d.ts +1 -0
  27. package/lib/markdownLinks.d.ts.map +1 -0
  28. package/lib/markdownLinks.js +36 -11
  29. package/lib/markdownLinks.js.map +1 -0
  30. package/lib/markdownParser.d.ts +7 -3
  31. package/lib/markdownParser.d.ts.map +1 -0
  32. package/lib/markdownParser.js +77 -48
  33. package/lib/markdownParser.js.map +1 -0
  34. package/lib/pathUtils.d.ts +51 -0
  35. package/lib/pathUtils.d.ts.map +1 -0
  36. package/lib/pathUtils.js +106 -0
  37. package/lib/pathUtils.js.map +1 -0
  38. package/lib/slugger.d.ts +14 -0
  39. package/lib/slugger.d.ts.map +1 -0
  40. package/lib/slugger.js +19 -0
  41. package/lib/slugger.js.map +1 -0
  42. package/lib/tags.d.ts +27 -0
  43. package/lib/tags.d.ts.map +1 -0
  44. package/lib/tags.js +77 -0
  45. package/lib/tags.js.map +1 -0
  46. package/lib/urlUtils.d.ts +9 -0
  47. package/lib/urlUtils.d.ts.map +1 -0
  48. package/lib/urlUtils.js +81 -0
  49. package/lib/urlUtils.js.map +1 -0
  50. package/lib/webpackUtils.d.ts +30 -0
  51. package/lib/webpackUtils.d.ts.map +1 -0
  52. package/lib/webpackUtils.js +112 -0
  53. package/lib/webpackUtils.js.map +1 -0
  54. package/package.json +20 -10
  55. package/src/constants.ts +38 -0
  56. package/src/dataFileUtils.ts +90 -0
  57. package/src/deps.d.ts +10 -0
  58. package/src/gitUtils.ts +93 -0
  59. package/src/globUtils.ts +64 -0
  60. package/src/hashUtils.ts +37 -0
  61. package/src/index.ts +135 -294
  62. package/src/markdownLinks.ts +35 -13
  63. package/src/markdownParser.ts +86 -62
  64. package/src/pathUtils.ts +115 -0
  65. package/src/slugger.ts +24 -0
  66. package/src/tags.ts +105 -0
  67. package/src/urlUtils.ts +96 -0
  68. package/src/webpackUtils.ts +146 -0
  69. package/lib/.tsbuildinfo +0 -3972
  70. package/lib/codeTranslationsUtils.d.ts +0 -11
  71. package/lib/codeTranslationsUtils.js +0 -50
  72. package/lib/escapePath.d.ts +0 -17
  73. package/lib/escapePath.js +0 -25
  74. package/lib/posixPath.d.ts +0 -14
  75. package/lib/posixPath.js +0 -28
  76. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  77. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  78. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  79. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
  80. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  81. package/src/__tests__/escapePath.test.ts +0 -25
  82. package/src/__tests__/index.test.ts +0 -681
  83. package/src/__tests__/markdownParser.test.ts +0 -772
  84. package/src/__tests__/posixPath.test.ts +0 -25
  85. package/src/codeTranslationsUtils.ts +0 -56
  86. package/src/escapePath.ts +0 -23
  87. package/src/posixPath.ts +0 -27
  88. 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;
@@ -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,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
- "relativeFilePath=[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
- });