@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
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {resolve} from 'url';
8
+ import path from 'path';
9
9
  import {aliasedSitePath} from './index';
10
10
 
11
11
  export type ContentPaths = {
@@ -63,16 +63,27 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
63
63
  // Replace it to correct html link.
64
64
  const mdLink = mdMatch[1];
65
65
 
66
- const aliasedSource = (source: string) =>
67
- aliasedSitePath(source, siteDir);
66
+ const sourcesToTry = [
67
+ path.resolve(path.dirname(filePath), decodeURIComponent(mdLink)),
68
+ `${contentPathLocalized}/${decodeURIComponent(mdLink)}`,
69
+ `${contentPath}/${decodeURIComponent(mdLink)}`,
70
+ ];
68
71
 
69
- const permalink: string | undefined =
70
- sourceToPermalink[aliasedSource(resolve(filePath, mdLink))] ||
71
- sourceToPermalink[aliasedSource(`${contentPathLocalized}/${mdLink}`)] ||
72
- sourceToPermalink[aliasedSource(`${contentPath}/${mdLink}`)];
72
+ const aliasedSourceMatch = sourcesToTry
73
+ .map((source) => aliasedSitePath(source, siteDir))
74
+ .find((source) => sourceToPermalink[source]);
75
+
76
+ const permalink: string | undefined = aliasedSourceMatch
77
+ ? sourceToPermalink[aliasedSourceMatch]
78
+ : undefined;
73
79
 
74
80
  if (permalink) {
75
- modifiedLine = modifiedLine.replace(mdLink, permalink);
81
+ // MDX won't be happy if the permalink contains a space, we need to convert it to %20
82
+ const encodedPermalink = permalink
83
+ .split('/')
84
+ .map((part) => part.replace(/\s/g, '%20'))
85
+ .join('/');
86
+ modifiedLine = modifiedLine.replace(mdLink, encodedPermalink);
76
87
  } else {
77
88
  const brokenMarkdownLink: BrokenMarkdownLink<T> = {
78
89
  contentPaths,
@@ -18,6 +18,7 @@ export function createExcerpt(fileString: string): string | undefined {
18
18
  // Remove Markdown alternate title
19
19
  .replace(/^[^\n]*\n[=]+/g, '')
20
20
  .split('\n');
21
+ let inCode = false;
21
22
 
22
23
  /* eslint-disable no-continue */
23
24
  // eslint-disable-next-line no-restricted-syntax
@@ -32,6 +33,14 @@ export function createExcerpt(fileString: string): string | undefined {
32
33
  continue;
33
34
  }
34
35
 
36
+ // Skip code block line.
37
+ if (fileLine.trim().startsWith('```')) {
38
+ inCode = !inCode;
39
+ continue;
40
+ } else if (inCode) {
41
+ continue;
42
+ }
43
+
35
44
  const cleanedLine = fileLine
36
45
  // Remove HTML tags.
37
46
  .replace(/<[^>]*>/g, '')
@@ -67,9 +76,7 @@ export function createExcerpt(fileString: string): string | undefined {
67
76
  return undefined;
68
77
  }
69
78
 
70
- export function parseFrontMatter(
71
- markdownFileContent: string,
72
- ): {
79
+ export function parseFrontMatter(markdownFileContent: string): {
73
80
  frontMatter: Record<string, unknown>;
74
81
  content: string;
75
82
  } {
@@ -98,10 +105,11 @@ export function parseMarkdownContentTitle(
98
105
 
99
106
  const content = contentUntrimmed.trim();
100
107
 
101
- const IMPORT_STATEMENT = /import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/
102
- .source;
103
- const REGULAR_TITLE = /(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
104
- .source;
108
+ const IMPORT_STATEMENT =
109
+ /import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/.source;
110
+ const REGULAR_TITLE =
111
+ /(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
112
+ .source;
105
113
  const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
106
114
 
107
115
  const regularTitleMatch = new RegExp(
@@ -141,9 +149,8 @@ export function parseMarkdownString(
141
149
  options?: {removeContentTitle?: boolean},
142
150
  ): ParsedMarkdown {
143
151
  try {
144
- const {frontMatter, content: contentWithoutFrontMatter} = parseFrontMatter(
145
- markdownFileContent,
146
- );
152
+ const {frontMatter, content: contentWithoutFrontMatter} =
153
+ parseFrontMatter(markdownFileContent);
147
154
 
148
155
  const {content, contentTitle} = parseMarkdownContentTitle(
149
156
  contentWithoutFrontMatter,
@@ -176,7 +183,7 @@ export async function parseMarkdownFile(
176
183
  return parseMarkdownString(markdownString, options);
177
184
  } catch (e) {
178
185
  throw new Error(
179
- `Error while parsing Markdown file ${source}: "${e.message}".`,
186
+ `Error while parsing Markdown file ${source}: "${(e as Error).message}".`,
180
187
  );
181
188
  }
182
189
  }
@@ -0,0 +1,32 @@
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 React from 'react';
9
+ import ReactDOMServer from 'react-dom/server';
10
+ import MDX from '@mdx-js/runtime';
11
+ import removeImports from 'remark-mdx-remove-imports';
12
+ import removeExports from 'remark-mdx-remove-exports';
13
+
14
+ /**
15
+ * Transform mdx text to plain html text
16
+ * Initially created to convert MDX blog posts to HTML for the RSS feed
17
+ * without import/export nodes
18
+ *
19
+ * TODO not ideal implementation, won't work well with MDX elements!
20
+ * TODO theme+global site config should be able to declare MDX comps in scope for rendering the RSS feeds
21
+ * see also https://github.com/facebook/docusaurus/issues/4625
22
+ */
23
+ export function mdxToHtml(
24
+ mdxStr: string,
25
+ // TODO allow providing components/scope here, see https://github.com/mdx-js/mdx/tree/v1.6.13/packages/runtime
26
+ ): string {
27
+ return ReactDOMServer.renderToString(
28
+ React.createElement(MDX, {remarkPlugins: [removeImports, removeExports]}, [
29
+ mdxStr,
30
+ ]),
31
+ );
32
+ }
@@ -0,0 +1,80 @@
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
+ export function normalizeUrl(rawUrls: string[]): string {
9
+ const urls = [...rawUrls];
10
+ const resultArray = [];
11
+
12
+ let hasStartingSlash = false;
13
+ let hasEndingSlash = false;
14
+
15
+ // If the first part is a plain protocol, we combine it with the next part.
16
+ if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
17
+ const first = urls.shift();
18
+ urls[0] = first + urls[0];
19
+ }
20
+
21
+ // There must be two or three slashes in the file protocol,
22
+ // two slashes in anything else.
23
+ const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
24
+ urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
25
+
26
+ // eslint-disable-next-line
27
+ for (let i = 0; i < urls.length; i++) {
28
+ let component = urls[i];
29
+
30
+ if (typeof component !== 'string') {
31
+ throw new TypeError(`Url must be a string. Received ${typeof component}`);
32
+ }
33
+
34
+ if (component === '') {
35
+ if (i === urls.length - 1 && hasEndingSlash) {
36
+ resultArray.push('/');
37
+ }
38
+ // eslint-disable-next-line
39
+ continue;
40
+ }
41
+
42
+ if (component !== '/') {
43
+ if (i > 0) {
44
+ // Removing the starting slashes for each component but the first.
45
+ component = component.replace(
46
+ /^[/]+/,
47
+ // Special case where the first element of rawUrls is empty ["", "/hello"] => /hello
48
+ component[0] === '/' && !hasStartingSlash ? '/' : '',
49
+ );
50
+ }
51
+
52
+ hasEndingSlash = component[component.length - 1] === '/';
53
+ // Removing the ending slashes for each component but the last.
54
+ // For the last component we will combine multiple slashes to a single one.
55
+ component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
56
+ }
57
+
58
+ hasStartingSlash = true;
59
+ resultArray.push(component);
60
+ }
61
+
62
+ let str = resultArray.join('/');
63
+ // Each input component is now separated by a single slash
64
+ // except the possible first plain protocol part.
65
+
66
+ // Remove trailing slash before parameters or hash.
67
+ str = str.replace(/\/(\?|&|#[^!])/g, '$1');
68
+
69
+ // Replace ? in parameters with &.
70
+ const parts = str.split('?');
71
+ str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
72
+
73
+ // Dedupe forward slashes in the entire path, avoiding protocol slashes.
74
+ str = str.replace(/([^:]\/)\/+/g, '$1');
75
+
76
+ // Dedupe forward slashes at the beginning of the path.
77
+ str = str.replace(/^\/+/g, '/');
78
+
79
+ return str;
80
+ }
package/src/pathUtils.ts CHANGED
@@ -16,11 +16,10 @@ const SPACE_FOR_APPENDING = 10;
16
16
  const isMacOs = process.platform === `darwin`;
17
17
  const isWindows = process.platform === `win32`;
18
18
 
19
- export const isNameTooLong = (str: string): boolean => {
20
- return isMacOs || isWindows
19
+ export const isNameTooLong = (str: string): boolean =>
20
+ isMacOs || isWindows
21
21
  ? str.length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_CHARS // MacOS (APFS) and Windows (NTFS) filename length limit (255 chars)
22
22
  : Buffer.from(str).length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_BYTES; // Other (255 bytes)
23
- };
24
23
 
25
24
  export const shortName = (str: string): string => {
26
25
  if (isMacOs || isWindows) {
package/src/slugger.ts ADDED
@@ -0,0 +1,24 @@
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 GithubSlugger from 'github-slugger';
9
+
10
+ // We create our own abstraction on top of the lib:
11
+ // - unify usage everywhere in the codebase
12
+ // - ability to add extra options
13
+ export type SluggerOptions = {maintainCase?: boolean};
14
+
15
+ export type Slugger = {
16
+ slug: (value: string, options?: SluggerOptions) => string;
17
+ };
18
+
19
+ export function createSlugger(): Slugger {
20
+ const githubSlugger = new GithubSlugger();
21
+ return {
22
+ slug: (value, options) => githubSlugger.slug(value, options?.maintainCase),
23
+ };
24
+ }
package/src/tags.ts ADDED
@@ -0,0 +1,100 @@
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 {kebabCase, uniq, uniqBy} from 'lodash';
9
+ import {normalizeUrl} from './normalizeUrl';
10
+
11
+ export type Tag = {
12
+ label: string;
13
+ permalink: string;
14
+ };
15
+
16
+ export type FrontMatterTag = string | Tag;
17
+
18
+ export function normalizeFrontMatterTag(
19
+ tagsPath: string,
20
+ frontMatterTag: FrontMatterTag,
21
+ ): Tag {
22
+ function toTagObject(tagString: string): Tag {
23
+ return {
24
+ label: tagString,
25
+ permalink: kebabCase(tagString),
26
+ };
27
+ }
28
+
29
+ // TODO maybe make ensure the permalink is valid url path?
30
+ function normalizeTagPermalink(permalink: string): string {
31
+ // note: we always apply tagsPath on purpose
32
+ // for versioned docs, v1/doc.md and v2/doc.md tags with custom permalinks don't lead to the same created page
33
+ // tagsPath is different for each doc version
34
+ return normalizeUrl([tagsPath, permalink]);
35
+ }
36
+
37
+ const tag: Tag =
38
+ typeof frontMatterTag === 'string'
39
+ ? toTagObject(frontMatterTag)
40
+ : frontMatterTag;
41
+
42
+ return {
43
+ label: tag.label,
44
+ permalink: normalizeTagPermalink(tag.permalink),
45
+ };
46
+ }
47
+
48
+ export function normalizeFrontMatterTags(
49
+ tagsPath: string,
50
+ frontMatterTags: FrontMatterTag[] | undefined,
51
+ ): Tag[] {
52
+ const tags =
53
+ frontMatterTags?.map((tag) => normalizeFrontMatterTag(tagsPath, tag)) ?? [];
54
+
55
+ return uniqBy(tags, (tag) => tag.permalink);
56
+ }
57
+
58
+ export type TaggedItemGroup<Item> = {
59
+ tag: Tag;
60
+ items: Item[];
61
+ };
62
+
63
+ // Permits to group docs/blogPosts by tag (provided by FrontMatter)
64
+ // Note: groups are indexed by permalink, because routes must be unique in the end
65
+ // Labels may vary on 2 md files but they are normalized.
66
+ // Docs with label='some label' and label='some-label' should end-up in the same group/page in the end
67
+ // We can't create 2 routes /some-label because one would override the other
68
+ export function groupTaggedItems<Item>(
69
+ items: Item[],
70
+ getItemTags: (item: Item) => Tag[],
71
+ ): Record<string, TaggedItemGroup<Item>> {
72
+ const result: Record<string, TaggedItemGroup<Item>> = {};
73
+
74
+ function handleItemTag(item: Item, tag: Tag) {
75
+ // Init missing tag groups
76
+ // TODO: it's not really clear what should be the behavior if 2 items have the same tag but the permalink is different for each
77
+ // For now, the first tag found wins
78
+ result[tag.permalink] = result[tag.permalink] ?? {
79
+ tag,
80
+ items: [],
81
+ };
82
+
83
+ // Add item to group
84
+ result[tag.permalink].items.push(item);
85
+ }
86
+
87
+ items.forEach((item) => {
88
+ getItemTags(item).forEach((tag) => {
89
+ handleItemTag(item, tag);
90
+ });
91
+ });
92
+
93
+ // If user add twice the same tag to a md doc (weird but possible),
94
+ // we don't want the item to appear twice in the list...
95
+ Object.values(result).forEach((group) => {
96
+ group.items = uniq(group.items);
97
+ });
98
+
99
+ return result;
100
+ }
@@ -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,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
- }