@docusaurus/utils 2.0.0-beta.1 → 2.0.0-beta.10
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/lib/.tsbuildinfo +1 -1
- package/lib/constants.d.ts +19 -0
- package/lib/constants.js +26 -0
- package/lib/globUtils.d.ts +11 -0
- package/lib/globUtils.js +47 -0
- package/lib/{docuHash.d.ts → hashUtils.d.ts} +2 -0
- package/lib/{docuHash.js → hashUtils.js} +15 -5
- package/lib/index.d.ts +11 -9
- package/lib/index.js +44 -98
- package/lib/markdownLinks.js +19 -6
- package/lib/markdownParser.js +20 -12
- package/lib/mdxUtils.d.ts +16 -0
- package/lib/mdxUtils.js +30 -0
- package/lib/{getFilePathForRoutePath.d.ts → normalizeUrl.d.ts} +1 -1
- package/lib/normalizeUrl.js +66 -0
- package/lib/pathUtils.d.ts +0 -1
- package/lib/pathUtils.js +4 -11
- package/lib/slugger.d.ts +13 -0
- package/lib/slugger.js +18 -0
- package/lib/tags.d.ts +18 -0
- package/lib/tags.js +72 -0
- package/lib/webpackUtils.d.ts +29 -0
- package/lib/webpackUtils.js +109 -0
- package/package.json +23 -7
- package/src/__tests__/globUtils.test.ts +109 -0
- package/src/__tests__/{docuHash.test.ts → hashUtils.test.ts} +22 -1
- package/src/__tests__/index.test.ts +6 -110
- package/src/__tests__/markdownParser.test.ts +15 -2
- package/src/__tests__/mdxUtils.test.ts +133 -0
- package/src/__tests__/normalizeUrl.test.ts +117 -0
- package/src/__tests__/pathUtils.test.ts +5 -22
- package/src/__tests__/slugger.test.ts +27 -0
- package/src/__tests__/tags.test.ts +183 -0
- package/src/__tests__/webpackUtils.test.ts +33 -0
- package/src/constants.ts +38 -0
- package/src/deps.d.ts +14 -0
- package/src/globUtils.ts +63 -0
- package/src/{docuHash.ts → hashUtils.ts} +10 -1
- package/src/index.ts +23 -96
- package/src/markdownLinks.ts +19 -8
- package/src/markdownParser.ts +24 -17
- package/src/mdxUtils.ts +32 -0
- package/src/normalizeUrl.ts +80 -0
- package/src/pathUtils.ts +2 -9
- package/src/slugger.ts +24 -0
- package/src/tags.ts +100 -0
- package/src/webpackUtils.ts +144 -0
- package/lib/codeTranslationsUtils.d.ts +0 -11
- package/lib/codeTranslationsUtils.js +0 -50
- package/lib/getFilePathForRoutePath.js +0 -40
- 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__/codeTranslationsUtils.test.ts +0 -112
- package/src/__tests__/getFilePathForRoutePath.test.ts +0 -87
- package/src/codeTranslationsUtils.ts +0 -56
- package/src/getFilePathForRoutePath.ts +0 -43
|
@@ -0,0 +1,144 @@
|
|
|
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 type {RuleSetRule} from 'webpack';
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import {posixPath} from './posixPath';
|
|
11
|
+
import {
|
|
12
|
+
WEBPACK_URL_LOADER_LIMIT,
|
|
13
|
+
OUTPUT_STATIC_ASSETS_DIR_NAME,
|
|
14
|
+
} from './constants';
|
|
15
|
+
|
|
16
|
+
type AssetFolder = 'images' | 'files' | 'fonts' | 'medias';
|
|
17
|
+
|
|
18
|
+
type FileLoaderUtils = {
|
|
19
|
+
loaders: {
|
|
20
|
+
file: (options: {folder: AssetFolder}) => RuleSetRule;
|
|
21
|
+
url: (options: {folder: AssetFolder}) => RuleSetRule;
|
|
22
|
+
inlineMarkdownImageFileLoader: string;
|
|
23
|
+
inlineMarkdownLinkFileLoader: string;
|
|
24
|
+
};
|
|
25
|
+
rules: {
|
|
26
|
+
images: () => RuleSetRule;
|
|
27
|
+
fonts: () => RuleSetRule;
|
|
28
|
+
media: () => RuleSetRule;
|
|
29
|
+
svg: () => RuleSetRule;
|
|
30
|
+
otherAssets: () => RuleSetRule;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Inspired by https://github.com/gatsbyjs/gatsby/blob/8e6e021014da310b9cc7d02e58c9b3efe938c665/packages/gatsby/src/utils/webpack-utils.ts#L447
|
|
35
|
+
export function getFileLoaderUtils(): FileLoaderUtils {
|
|
36
|
+
// files/images < urlLoaderLimit will be inlined as base64 strings directly in the html
|
|
37
|
+
const urlLoaderLimit = WEBPACK_URL_LOADER_LIMIT;
|
|
38
|
+
|
|
39
|
+
// defines the path/pattern of the assets handled by webpack
|
|
40
|
+
const fileLoaderFileName = (folder: AssetFolder) =>
|
|
41
|
+
`${OUTPUT_STATIC_ASSETS_DIR_NAME}/${folder}/[name]-[hash].[ext]`;
|
|
42
|
+
|
|
43
|
+
const loaders: FileLoaderUtils['loaders'] = {
|
|
44
|
+
file: (options: {folder: AssetFolder}) => ({
|
|
45
|
+
loader: require.resolve(`file-loader`),
|
|
46
|
+
options: {
|
|
47
|
+
name: fileLoaderFileName(options.folder),
|
|
48
|
+
},
|
|
49
|
+
}),
|
|
50
|
+
url: (options: {folder: AssetFolder}) => ({
|
|
51
|
+
loader: require.resolve('url-loader'),
|
|
52
|
+
options: {
|
|
53
|
+
limit: urlLoaderLimit,
|
|
54
|
+
name: fileLoaderFileName(options.folder),
|
|
55
|
+
fallback: require.resolve('file-loader'),
|
|
56
|
+
},
|
|
57
|
+
}),
|
|
58
|
+
|
|
59
|
+
// TODO find a better solution to avoid conflicts with the ideal-image plugin
|
|
60
|
+
// TODO this may require a little breaking change for ideal-image users?
|
|
61
|
+
// Maybe with the ideal image plugin, all md images should be "ideal"?
|
|
62
|
+
// This is used to force url-loader+file-loader on markdown images
|
|
63
|
+
// https://webpack.js.org/concepts/loaders/#inline
|
|
64
|
+
inlineMarkdownImageFileLoader: `!${posixPath(
|
|
65
|
+
require.resolve('url-loader'),
|
|
66
|
+
)}?limit=${urlLoaderLimit}&name=${fileLoaderFileName(
|
|
67
|
+
'images',
|
|
68
|
+
)}&fallback=${posixPath(require.resolve('file-loader'))}!`,
|
|
69
|
+
inlineMarkdownLinkFileLoader: `!${posixPath(
|
|
70
|
+
require.resolve('file-loader'),
|
|
71
|
+
)}?name=${fileLoaderFileName('files')}!`,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const rules: FileLoaderUtils['rules'] = {
|
|
75
|
+
/**
|
|
76
|
+
* Loads image assets, inlines images via a data URI if they are below
|
|
77
|
+
* the size threshold
|
|
78
|
+
*/
|
|
79
|
+
images: () => ({
|
|
80
|
+
use: [loaders.url({folder: 'images'})],
|
|
81
|
+
test: /\.(ico|jpg|jpeg|png|gif|webp)(\?.*)?$/,
|
|
82
|
+
}),
|
|
83
|
+
|
|
84
|
+
fonts: () => ({
|
|
85
|
+
use: [loaders.url({folder: 'fonts'})],
|
|
86
|
+
test: /\.(woff|woff2|eot|ttf|otf)$/,
|
|
87
|
+
}),
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Loads audio and video and inlines them via a data URI if they are below
|
|
91
|
+
* the size threshold
|
|
92
|
+
*/
|
|
93
|
+
media: () => ({
|
|
94
|
+
use: [loaders.url({folder: 'medias'})],
|
|
95
|
+
test: /\.(mp4|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/,
|
|
96
|
+
}),
|
|
97
|
+
|
|
98
|
+
svg: () => ({
|
|
99
|
+
test: /\.svg?$/,
|
|
100
|
+
oneOf: [
|
|
101
|
+
{
|
|
102
|
+
use: [
|
|
103
|
+
{
|
|
104
|
+
loader: require.resolve('@svgr/webpack'),
|
|
105
|
+
options: {
|
|
106
|
+
prettier: false,
|
|
107
|
+
svgo: true,
|
|
108
|
+
svgoConfig: {
|
|
109
|
+
plugins: [
|
|
110
|
+
{
|
|
111
|
+
name: 'preset-default',
|
|
112
|
+
params: {
|
|
113
|
+
overrides: {
|
|
114
|
+
removeViewBox: false,
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
titleProp: true,
|
|
121
|
+
ref: ![path],
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
// We don't want to use SVGR loader for non-React source code
|
|
126
|
+
// ie we don't want to use SVGR for CSS files...
|
|
127
|
+
issuer: {
|
|
128
|
+
and: [/\.(ts|tsx|js|jsx|md|mdx)$/],
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
use: [loaders.url({folder: 'images'})],
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
}),
|
|
136
|
+
|
|
137
|
+
otherAssets: () => ({
|
|
138
|
+
use: [loaders.file({folder: 'files'})],
|
|
139
|
+
test: /\.(pdf|doc|docx|xls|xlsx|zip|rar)$/,
|
|
140
|
+
}),
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return {loaders, rules};
|
|
144
|
+
}
|
|
@@ -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,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,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,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
|
-
});
|
|
@@ -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
|
-
}
|
|
@@ -1,43 +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
|
-
|
|
10
|
-
/*
|
|
11
|
-
export function getFilePathForRoutePath(routePath: string): string {
|
|
12
|
-
const fileName = path.basename(routePath);
|
|
13
|
-
const filePath = path.dirname(routePath);
|
|
14
|
-
return path.join(filePath, `${fileName}/index.html`);
|
|
15
|
-
}
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
// Almost exact copy of the behavior we implemented in our Docusaurus fork of the webpack static gen plugin
|
|
19
|
-
// See https://github.com/slorber/static-site-generator-webpack-plugin/blob/master/index.js#L167
|
|
20
|
-
export function getFilePathForRoutePath(
|
|
21
|
-
routePath: string,
|
|
22
|
-
trailingSlash: boolean | undefined,
|
|
23
|
-
): string {
|
|
24
|
-
// const outputFileName = routePath.replace(/^(\/|\\)/, ''); // Remove leading slashes for webpack-dev-server
|
|
25
|
-
|
|
26
|
-
// Paths ending with .html are left untouched
|
|
27
|
-
if (/\.(html?)$/i.test(routePath)) {
|
|
28
|
-
return routePath;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Legacy retro-compatible behavior
|
|
32
|
-
if (typeof trailingSlash === 'undefined') {
|
|
33
|
-
return path.join(routePath, 'index.html');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// New behavior: we can say if we prefer file/folder output
|
|
37
|
-
// Useful resource: https://github.com/slorber/trailing-slash-guide
|
|
38
|
-
if (routePath === '' || routePath.endsWith('/') || trailingSlash) {
|
|
39
|
-
return path.join(routePath, 'index.html');
|
|
40
|
-
} else {
|
|
41
|
-
return `${routePath}.html`;
|
|
42
|
-
}
|
|
43
|
-
}
|