@docusaurus/utils 2.0.0-beta.fbdeefcac → 2.0.0-rc.1

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 +73 -0
  2. package/lib/constants.d.ts.map +1 -0
  3. package/lib/constants.js +78 -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 +63 -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 +53 -0
  26. package/lib/i18nUtils.d.ts.map +1 -0
  27. package/lib/i18nUtils.js +70 -0
  28. package/lib/i18nUtils.js.map +1 -0
  29. package/lib/index.d.ts +16 -75
  30. package/lib/index.d.ts.map +1 -0
  31. package/lib/index.js +91 -395
  32. package/lib/index.js.map +1 -0
  33. package/lib/jsUtils.d.ts +28 -0
  34. package/lib/jsUtils.d.ts.map +1 -0
  35. package/lib/jsUtils.js +57 -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 +57 -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/shellUtils.d.ts +8 -0
  50. package/lib/shellUtils.d.ts.map +1 -0
  51. package/lib/shellUtils.js +21 -0
  52. package/lib/shellUtils.js.map +1 -0
  53. package/lib/slugger.d.ts +24 -0
  54. package/lib/slugger.d.ts.map +1 -0
  55. package/lib/slugger.js +23 -0
  56. package/lib/slugger.js.map +1 -0
  57. package/lib/tags.d.ts +59 -0
  58. package/lib/tags.d.ts.map +1 -0
  59. package/lib/tags.js +91 -0
  60. package/lib/tags.js.map +1 -0
  61. package/lib/urlUtils.d.ts +66 -0
  62. package/lib/urlUtils.d.ts.map +1 -0
  63. package/lib/urlUtils.js +207 -0
  64. package/lib/urlUtils.js.map +1 -0
  65. package/lib/webpackUtils.d.ts +35 -0
  66. package/lib/webpackUtils.d.ts.map +1 -0
  67. package/lib/webpackUtils.js +115 -0
  68. package/lib/webpackUtils.js.map +1 -0
  69. package/package.json +28 -10
  70. package/src/constants.ts +98 -0
  71. package/src/dataFileUtils.ts +122 -0
  72. package/src/deps.d.ts +10 -0
  73. package/src/emitUtils.ts +99 -0
  74. package/src/gitUtils.ts +146 -0
  75. package/src/globUtils.ts +85 -0
  76. package/src/hashUtils.ts +9 -8
  77. package/src/i18nUtils.ts +114 -0
  78. package/src/index.ts +97 -502
  79. package/src/jsUtils.ts +59 -0
  80. package/src/markdownLinks.ts +101 -30
  81. package/src/markdownUtils.ts +357 -0
  82. package/src/pathUtils.ts +93 -12
  83. package/src/shellUtils.ts +18 -0
  84. package/src/slugger.ts +36 -0
  85. package/src/tags.ts +130 -0
  86. package/src/urlUtils.ts +234 -0
  87. package/src/webpackUtils.ts +153 -0
  88. package/lib/.tsbuildinfo +0 -1
  89. package/lib/codeTranslationsUtils.d.ts +0 -11
  90. package/lib/codeTranslationsUtils.js +0 -50
  91. package/lib/escapePath.d.ts +0 -17
  92. package/lib/escapePath.js +0 -25
  93. package/lib/markdownParser.d.ts +0 -30
  94. package/lib/markdownParser.js +0 -140
  95. package/lib/posixPath.d.ts +0 -14
  96. package/lib/posixPath.js +0 -28
  97. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  98. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  99. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  100. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
  101. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  102. package/src/__tests__/escapePath.test.ts +0 -25
  103. package/src/__tests__/hashUtils.test.ts +0 -51
  104. package/src/__tests__/index.test.ts +0 -631
  105. package/src/__tests__/markdownParser.test.ts +0 -817
  106. package/src/__tests__/pathUtils.test.ts +0 -63
  107. package/src/__tests__/posixPath.test.ts +0 -25
  108. package/src/codeTranslationsUtils.ts +0 -56
  109. package/src/escapePath.ts +0 -23
  110. package/src/markdownParser.ts +0 -182
  111. package/src/posixPath.ts +0 -27
  112. package/tsconfig.json +0 -9
@@ -1,63 +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 {isNameTooLong, shortName} from '../pathUtils';
9
-
10
- describe('pathUtils', () => {
11
- test('isNameTooLong', () => {
12
- const asserts: Record<string, boolean> = {
13
- '': false,
14
- 'foo-bar-096': false,
15
- 'foo-bar-1df': false,
16
- 'endi-lie-9fa': false,
17
- 'endi-lie-fd3': false,
18
- 'yangshun-tay-48d': false,
19
- 'yangshun-tay-f3b': false,
20
- 'foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-d46': true,
21
- 'foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-test-1-test-2-787': true,
22
- };
23
- Object.keys(asserts).forEach((path) => {
24
- expect(isNameTooLong(path)).toBe(asserts[path]);
25
- });
26
- });
27
-
28
- describe('shortName', () => {
29
- test('works', () => {
30
- const asserts: Record<string, string> = {
31
- '': '',
32
- 'foo-bar': 'foo-bar',
33
- 'endi-lie': 'endi-lie',
34
- 'yangshun-tay': 'yangshun-tay',
35
- 'foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar':
36
- 'foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-',
37
- 'foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-test-1-test-2':
38
- 'foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-test-1-test-',
39
- };
40
- Object.keys(asserts).forEach((file) => {
41
- expect(shortName(file)).toBe(asserts[file]);
42
- });
43
- });
44
-
45
- // Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
46
-
47
- const SHORT_PATH = `/short/path/without/trailing/slash`;
48
- const VERY_LONG_PATH = `/${`x`.repeat(256)}/`;
49
- const VERY_LONG_PATH_NON_LATIN = `/${`あ`.repeat(255)}/`;
50
-
51
- it(`Truncates long paths correctly`, () => {
52
- const truncatedPathLatin = shortName(VERY_LONG_PATH);
53
- const truncatedPathNonLatin = shortName(VERY_LONG_PATH_NON_LATIN);
54
- expect(truncatedPathLatin.length).toBeLessThanOrEqual(255);
55
- expect(truncatedPathNonLatin.length).toBeLessThanOrEqual(255);
56
- });
57
-
58
- it(`Does not truncate short paths`, () => {
59
- const truncatedPath = shortName(SHORT_PATH);
60
- expect(truncatedPath).toEqual(SHORT_PATH);
61
- });
62
- });
63
- });
@@ -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 {posixPath} from '../posixPath';
9
-
10
- describe('posixPath', () => {
11
- test('posixPath 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(posixPath(file)).toBe(asserts[file]);
23
- });
24
- });
25
- });
@@ -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
- }
package/src/escapePath.ts DELETED
@@ -1,23 +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
- /**
9
- * When you have a path like C:\X\Y
10
- * It is not safe to use directly when generating code
11
- * For example, this would fail due to unescaped \: `<img src={require('${filePath}')} />`
12
- * But this would work: `<img src={require('${escapePath(filePath)}')} />`
13
- *
14
- * Workaround for issue in posixPath, maybe we won't need it anymore soon?
15
- * https://github.com/facebook/docusaurus/issues/4730#issuecomment-833530370
16
- * https://github.com/sindresorhus/slash/pull/16#issuecomment-833528479
17
- */
18
- export function escapePath(str: string): string {
19
- const escaped = JSON.stringify(str);
20
-
21
- // Remove the " around the json string;
22
- return escaped.substring(1, escaped.length - 1);
23
- }
@@ -1,182 +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 chalk from 'chalk';
9
- import fs from 'fs-extra';
10
- import matter from 'gray-matter';
11
-
12
- // Hacky way of stripping out import statements from the excerpt
13
- // TODO: Find a better way to do so, possibly by compiling the Markdown content,
14
- // stripping out HTML tags and obtaining the first line.
15
- export function createExcerpt(fileString: string): string | undefined {
16
- const fileLines = fileString
17
- .trimLeft()
18
- // Remove Markdown alternate title
19
- .replace(/^[^\n]*\n[=]+/g, '')
20
- .split('\n');
21
-
22
- /* eslint-disable no-continue */
23
- // eslint-disable-next-line no-restricted-syntax
24
- for (const fileLine of fileLines) {
25
- // Skip empty line.
26
- if (!fileLine.trim()) {
27
- continue;
28
- }
29
-
30
- // Skip import/export declaration.
31
- if (/^\s*?import\s.*(from.*)?;?|export\s.*{.*};?/.test(fileLine)) {
32
- continue;
33
- }
34
-
35
- const cleanedLine = fileLine
36
- // Remove HTML tags.
37
- .replace(/<[^>]*>/g, '')
38
- // Remove Title headers
39
- .replace(/^#\s*([^#]*)\s*#?/gm, '')
40
- // Remove Markdown + ATX-style headers
41
- .replace(/^#{1,6}\s*([^#]*)\s*(#{1,6})?/gm, '$1')
42
- // Remove emphasis and strikethroughs.
43
- .replace(/([*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
44
- // Remove images.
45
- .replace(/!\[(.*?)\][[(].*?[\])]/g, '$1')
46
- // Remove footnotes.
47
- .replace(/\[\^.+?\](: .*?$)?/g, '')
48
- // Remove inline links.
49
- .replace(/\[(.*?)\][[(].*?[\])]/g, '$1')
50
- // Remove inline code.
51
- .replace(/`(.+?)`/g, '$1')
52
- // Remove blockquotes.
53
- .replace(/^\s{0,3}>\s?/g, '')
54
- // Remove admonition definition.
55
- .replace(/(:{3}.*)/, '')
56
- // Remove Emoji names within colons include preceding whitespace.
57
- .replace(/\s?(:(::|[^:\n])+:)/g, '')
58
- // Remove custom Markdown heading id.
59
- .replace(/{#*[\w-]+}/, '')
60
- .trim();
61
-
62
- if (cleanedLine) {
63
- return cleanedLine;
64
- }
65
- }
66
-
67
- return undefined;
68
- }
69
-
70
- export function parseFrontMatter(
71
- markdownFileContent: string,
72
- ): {
73
- frontMatter: Record<string, unknown>;
74
- content: string;
75
- } {
76
- const {data, content} = matter(markdownFileContent);
77
- return {
78
- frontMatter: data ?? {},
79
- content: content?.trim() ?? '',
80
- };
81
- }
82
-
83
- // Try to convert markdown heading as text
84
- // Does not need to be perfect, it is only used as a fallback when frontMatter.title is not provided
85
- // For now, we just unwrap possible inline code blocks (# `config.js`)
86
- function toTextContentTitle(contentTitle: string): string {
87
- if (contentTitle.startsWith('`') && contentTitle.endsWith('`')) {
88
- return contentTitle.substring(1, contentTitle.length - 1);
89
- }
90
- return contentTitle;
91
- }
92
-
93
- export function parseMarkdownContentTitle(
94
- contentUntrimmed: string,
95
- options?: {removeContentTitle?: boolean},
96
- ): {content: string; contentTitle: string | undefined} {
97
- const removeContentTitleOption = options?.removeContentTitle ?? false;
98
-
99
- const content = contentUntrimmed.trim();
100
-
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;
105
- const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
106
-
107
- const regularTitleMatch = new RegExp(
108
- `^(?:${IMPORT_STATEMENT})*?${REGULAR_TITLE}`,
109
- 'g',
110
- ).exec(content);
111
- const alternateTitleMatch = new RegExp(
112
- `^(?:${IMPORT_STATEMENT})*?${ALTERNATE_TITLE}`,
113
- 'g',
114
- ).exec(content);
115
-
116
- const titleMatch = regularTitleMatch ?? alternateTitleMatch;
117
- const {pattern, title} = titleMatch?.groups ?? {};
118
-
119
- if (!pattern || !title) {
120
- return {content, contentTitle: undefined};
121
- } else {
122
- const newContent = removeContentTitleOption
123
- ? content.replace(pattern, '')
124
- : content;
125
- return {
126
- content: newContent.trim(),
127
- contentTitle: toTextContentTitle(title.trim()).trim(),
128
- };
129
- }
130
- }
131
-
132
- type ParsedMarkdown = {
133
- frontMatter: Record<string, unknown>;
134
- content: string;
135
- contentTitle: string | undefined;
136
- excerpt: string | undefined;
137
- };
138
-
139
- export function parseMarkdownString(
140
- markdownFileContent: string,
141
- options?: {removeContentTitle?: boolean},
142
- ): ParsedMarkdown {
143
- try {
144
- const {frontMatter, content: contentWithoutFrontMatter} = parseFrontMatter(
145
- markdownFileContent,
146
- );
147
-
148
- const {content, contentTitle} = parseMarkdownContentTitle(
149
- contentWithoutFrontMatter,
150
- options,
151
- );
152
-
153
- const excerpt = createExcerpt(content);
154
-
155
- return {
156
- frontMatter,
157
- content,
158
- contentTitle,
159
- excerpt,
160
- };
161
- } catch (e) {
162
- console.error(
163
- chalk.red(`Error while parsing Markdown frontmatter.
164
- This can happen if you use special characters in frontmatter values (try using double quotes around that value).`),
165
- );
166
- throw e;
167
- }
168
- }
169
-
170
- export async function parseMarkdownFile(
171
- source: string,
172
- options?: {removeContentTitle?: boolean},
173
- ): Promise<ParsedMarkdown> {
174
- const markdownString = await fs.readFile(source, 'utf-8');
175
- try {
176
- return parseMarkdownString(markdownString, options);
177
- } catch (e) {
178
- throw new Error(
179
- `Error while parsing Markdown file ${source}: "${e.message}".`,
180
- );
181
- }
182
- }
package/src/posixPath.ts DELETED
@@ -1,27 +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
- /**
9
- * Convert Windows backslash paths to posix style paths.
10
- * E.g: endi\\lie -> endi/lie
11
- *
12
- * Looks like this code was originally copied from https://github.com/sindresorhus/slash/blob/main/index.js
13
- *
14
- */
15
- export function posixPath(str: string): string {
16
- const isExtendedLengthPath = /^\\\\\?\\/.test(str);
17
-
18
- // TODO not sure why we need this
19
- // See https://github.com/sindresorhus/slash/pull/16#issuecomment-833528479
20
- // See https://github.com/facebook/docusaurus/issues/4730#issuecomment-833530370
21
- const hasNonAscii = /[^\u0000-\u0080]+/.test(str); // eslint-disable-line
22
-
23
- if (isExtendedLengthPath || hasNonAscii) {
24
- return str;
25
- }
26
- return str.replace(/\\/g, '/');
27
- }
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "incremental": true,
5
- "tsBuildInfoFile": "./lib/.tsbuildinfo",
6
- "rootDir": "src",
7
- "outDir": "lib"
8
- }
9
- }