@docusaurus/utils 2.0.0-beta.0f144213d → 2.0.0-beta.11

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 (50) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/constants.d.ts +19 -0
  3. package/lib/constants.js +26 -0
  4. package/lib/globUtils.d.ts +11 -0
  5. package/lib/globUtils.js +47 -0
  6. package/lib/hashUtils.js +4 -4
  7. package/lib/index.d.ts +10 -6
  8. package/lib/index.js +39 -92
  9. package/lib/markdownLinks.js +19 -6
  10. package/lib/markdownParser.js +14 -6
  11. package/lib/mdxUtils.d.ts +16 -0
  12. package/lib/mdxUtils.js +30 -0
  13. package/lib/normalizeUrl.d.ts +7 -0
  14. package/lib/normalizeUrl.js +66 -0
  15. package/lib/pathUtils.js +3 -5
  16. package/lib/slugger.d.ts +13 -0
  17. package/lib/slugger.js +18 -0
  18. package/lib/tags.d.ts +18 -0
  19. package/lib/tags.js +72 -0
  20. package/lib/webpackUtils.d.ts +29 -0
  21. package/lib/webpackUtils.js +109 -0
  22. package/package.json +23 -7
  23. package/src/__tests__/globUtils.test.ts +109 -0
  24. package/src/__tests__/index.test.ts +6 -110
  25. package/src/__tests__/markdownParser.test.ts +15 -2
  26. package/src/__tests__/mdxUtils.test.ts +133 -0
  27. package/src/__tests__/normalizeUrl.test.ts +117 -0
  28. package/src/__tests__/pathUtils.test.ts +4 -2
  29. package/src/__tests__/slugger.test.ts +27 -0
  30. package/src/__tests__/tags.test.ts +183 -0
  31. package/src/__tests__/webpackUtils.test.ts +33 -0
  32. package/src/constants.ts +38 -0
  33. package/src/deps.d.ts +14 -0
  34. package/src/globUtils.ts +63 -0
  35. package/src/index.ts +21 -91
  36. package/src/markdownLinks.ts +19 -8
  37. package/src/markdownParser.ts +18 -11
  38. package/src/mdxUtils.ts +32 -0
  39. package/src/normalizeUrl.ts +80 -0
  40. package/src/pathUtils.ts +2 -3
  41. package/src/slugger.ts +24 -0
  42. package/src/tags.ts +100 -0
  43. package/src/webpackUtils.ts +144 -0
  44. package/lib/codeTranslationsUtils.d.ts +0 -11
  45. package/lib/codeTranslationsUtils.js +0 -50
  46. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  47. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  48. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  49. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  50. package/src/codeTranslationsUtils.ts +0 -56
@@ -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,56 +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 path from 'path';
9
- import fs from 'fs-extra';
10
-
11
- // Return an ordered list of locales we should try
12
- export function codeTranslationLocalesToTry(locale: string): string[] {
13
- // @ts-expect-error: TODO until available in TS, see https://github.com/microsoft/TypeScript/issues/37326
14
- const intlLocale = Intl.Locale ? new Intl.Locale(locale) : undefined;
15
- if (!intlLocale) {
16
- return [locale];
17
- }
18
- // if locale is just a simple language like "pt", we want to fallback to pt-BR (not pt-PT!)
19
- // see https://github.com/facebook/docusaurus/pull/4536#issuecomment-810088783
20
- if (intlLocale.language === locale) {
21
- const maximizedLocale = intlLocale.maximize(); // pt-Latn-BR`
22
- // ["pt","pt-BR"]
23
- return [locale, `${maximizedLocale.language}-${maximizedLocale.region}`];
24
- }
25
- // if locale is like "pt-BR", we want to fallback to "pt"
26
- else {
27
- return [locale, intlLocale.language];
28
- }
29
- }
30
-
31
- // Useful to implement getDefaultCodeTranslationMessages() in themes
32
- export async function readDefaultCodeTranslationMessages({
33
- dirPath,
34
- locale,
35
- }: {
36
- dirPath: string;
37
- locale: string;
38
- }): Promise<Record<string, string>> {
39
- const localesToTry = codeTranslationLocalesToTry(locale);
40
-
41
- // Return the content of the first file that match
42
- // fr_FR.json => fr.json => nothing
43
- // eslint-disable-next-line no-restricted-syntax
44
- for (const fileName of localesToTry) {
45
- const filePath = path.resolve(dirPath, `${fileName}.json`);
46
-
47
- // eslint-disable-next-line no-await-in-loop
48
- if (await fs.pathExists(filePath)) {
49
- // eslint-disable-next-line no-await-in-loop
50
- const fileContent = await fs.readFile(filePath, 'utf8');
51
- return JSON.parse(fileContent);
52
- }
53
- }
54
-
55
- return {};
56
- }