@docusaurus/utils 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17

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 (104) hide show
  1. package/lib/constants.d.ts +20 -0
  2. package/lib/constants.d.ts.map +1 -0
  3. package/lib/constants.js +26 -0
  4. package/lib/constants.js.map +1 -0
  5. package/lib/dataFileUtils.d.ts +24 -0
  6. package/lib/dataFileUtils.d.ts.map +1 -0
  7. package/lib/dataFileUtils.js +65 -0
  8. package/lib/dataFileUtils.js.map +1 -0
  9. package/lib/emitUtils.d.ts +23 -0
  10. package/lib/emitUtils.d.ts.map +1 -0
  11. package/lib/emitUtils.js +90 -0
  12. package/lib/emitUtils.js.map +1 -0
  13. package/lib/gitUtils.d.ts +17 -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 +12 -0
  18. package/lib/globUtils.d.ts.map +1 -0
  19. package/lib/globUtils.js +48 -0
  20. package/lib/globUtils.js.map +1 -0
  21. package/lib/hashUtils.d.ts +1 -0
  22. package/lib/hashUtils.d.ts.map +1 -0
  23. package/lib/hashUtils.js +7 -5
  24. package/lib/hashUtils.js.map +1 -0
  25. package/lib/i18nUtils.d.ts +17 -0
  26. package/lib/i18nUtils.d.ts.map +1 -0
  27. package/lib/i18nUtils.js +40 -0
  28. package/lib/i18nUtils.js.map +1 -0
  29. package/lib/index.d.ts +15 -75
  30. package/lib/index.d.ts.map +1 -0
  31. package/lib/index.js +84 -395
  32. package/lib/index.js.map +1 -0
  33. package/lib/jsUtils.d.ts +17 -0
  34. package/lib/jsUtils.d.ts.map +1 -0
  35. package/lib/jsUtils.js +72 -0
  36. package/lib/jsUtils.js.map +1 -0
  37. package/lib/markdownLinks.d.ts +1 -0
  38. package/lib/markdownLinks.d.ts.map +1 -0
  39. package/lib/markdownLinks.js +36 -11
  40. package/lib/markdownLinks.js.map +1 -0
  41. package/lib/markdownParser.d.ts +5 -3
  42. package/lib/markdownParser.d.ts.map +1 -0
  43. package/lib/markdownParser.js +72 -52
  44. package/lib/markdownParser.js.map +1 -0
  45. package/lib/pathUtils.d.ts +44 -0
  46. package/lib/pathUtils.d.ts.map +1 -0
  47. package/lib/pathUtils.js +88 -10
  48. package/lib/pathUtils.js.map +1 -0
  49. package/lib/slugger.d.ts +14 -0
  50. package/lib/slugger.d.ts.map +1 -0
  51. package/lib/slugger.js +19 -0
  52. package/lib/slugger.js.map +1 -0
  53. package/lib/tags.d.ts +27 -0
  54. package/lib/tags.d.ts.map +1 -0
  55. package/lib/tags.js +76 -0
  56. package/lib/tags.js.map +1 -0
  57. package/lib/urlUtils.d.ts +20 -0
  58. package/lib/urlUtils.d.ts.map +1 -0
  59. package/lib/urlUtils.js +136 -0
  60. package/lib/urlUtils.js.map +1 -0
  61. package/lib/webpackUtils.d.ts +30 -0
  62. package/lib/webpackUtils.d.ts.map +1 -0
  63. package/lib/webpackUtils.js +112 -0
  64. package/lib/webpackUtils.js.map +1 -0
  65. package/package.json +20 -10
  66. package/src/constants.ts +38 -0
  67. package/src/dataFileUtils.ts +90 -0
  68. package/src/deps.d.ts +10 -0
  69. package/src/emitUtils.ts +113 -0
  70. package/src/gitUtils.ts +93 -0
  71. package/src/globUtils.ts +64 -0
  72. package/src/hashUtils.ts +3 -3
  73. package/src/i18nUtils.ts +58 -0
  74. package/src/index.ts +87 -502
  75. package/src/jsUtils.ts +88 -0
  76. package/src/markdownLinks.ts +35 -13
  77. package/src/markdownParser.ts +76 -57
  78. package/src/pathUtils.ts +87 -8
  79. package/src/slugger.ts +24 -0
  80. package/src/tags.ts +105 -0
  81. package/src/urlUtils.ts +149 -0
  82. package/src/webpackUtils.ts +146 -0
  83. package/lib/.tsbuildinfo +0 -1
  84. package/lib/codeTranslationsUtils.d.ts +0 -11
  85. package/lib/codeTranslationsUtils.js +0 -50
  86. package/lib/escapePath.d.ts +0 -17
  87. package/lib/escapePath.js +0 -25
  88. package/lib/posixPath.d.ts +0 -14
  89. package/lib/posixPath.js +0 -28
  90. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  91. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  92. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  93. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
  94. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  95. package/src/__tests__/escapePath.test.ts +0 -25
  96. package/src/__tests__/hashUtils.test.ts +0 -51
  97. package/src/__tests__/index.test.ts +0 -631
  98. package/src/__tests__/markdownParser.test.ts +0 -817
  99. package/src/__tests__/pathUtils.test.ts +0 -63
  100. package/src/__tests__/posixPath.test.ts +0 -25
  101. package/src/codeTranslationsUtils.ts +0 -56
  102. package/src/escapePath.ts +0 -23
  103. package/src/posixPath.ts +0 -27
  104. package/tsconfig.json +0 -9
@@ -5,8 +5,8 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import {resolve} from 'url';
9
- import {aliasedSitePath} from './index';
8
+ import path from 'path';
9
+ import {aliasedSitePath} from './pathUtils';
10
10
 
11
11
  export type ContentPaths = {
12
12
  contentPath: string;
@@ -45,9 +45,17 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
45
45
 
46
46
  // Replace internal markdown linking (except in fenced blocks).
47
47
  let fencedBlock = false;
48
+ let lastCodeFence = '';
48
49
  const lines = fileString.split('\n').map((line) => {
49
50
  if (line.trim().startsWith('```')) {
50
- fencedBlock = !fencedBlock;
51
+ if (!fencedBlock) {
52
+ fencedBlock = true;
53
+ [lastCodeFence] = line.trim().match(/^`+/)!;
54
+ // If we are in a ````-fenced block, all ``` would be plain text instead
55
+ // of fences
56
+ } else if (line.trim().match(/^`+/)![0].length >= lastCodeFence.length) {
57
+ fencedBlock = false;
58
+ }
51
59
  }
52
60
  if (fencedBlock) {
53
61
  return line;
@@ -55,24 +63,38 @@ export function replaceMarkdownLinks<T extends ContentPaths>({
55
63
 
56
64
  let modifiedLine = line;
57
65
  // Replace inline-style links or reference-style links e.g:
58
- // This is [Document 1](doc1.md) -> we replace this doc1.md with correct link
66
+ // This is [Document 1](doc1.md) -> we replace this doc1.md with correct
67
+ // ink
59
68
  // [doc1]: doc1.md -> we replace this doc1.md with correct link
60
- const mdRegex = /(?:(?:\]\()|(?:\]:\s?))(?!https)([^'")\]\s>]+\.mdx?)/g;
69
+ const mdRegex =
70
+ /(?:(?:\]\()|(?:\]:\s*))(?!https?:\/\/|@site\/)(?<filename>[^'")\]\s>]+\.mdx?)/g;
61
71
  let mdMatch = mdRegex.exec(modifiedLine);
62
72
  while (mdMatch !== null) {
63
73
  // Replace it to correct html link.
64
- const mdLink = mdMatch[1];
74
+ const mdLink = mdMatch.groups!.filename;
75
+
76
+ const sourcesToTry = [
77
+ path.resolve(path.dirname(filePath), decodeURIComponent(mdLink)),
78
+ `${contentPathLocalized}/${decodeURIComponent(mdLink)}`,
79
+ `${contentPath}/${decodeURIComponent(mdLink)}`,
80
+ ];
65
81
 
66
- const aliasedSource = (source: string) =>
67
- aliasedSitePath(source, siteDir);
82
+ const aliasedSourceMatch = sourcesToTry
83
+ .map((source) => aliasedSitePath(source, siteDir))
84
+ .find((source) => sourceToPermalink[source]);
68
85
 
69
- const permalink: string | undefined =
70
- sourceToPermalink[aliasedSource(resolve(filePath, mdLink))] ||
71
- sourceToPermalink[aliasedSource(`${contentPathLocalized}/${mdLink}`)] ||
72
- sourceToPermalink[aliasedSource(`${contentPath}/${mdLink}`)];
86
+ const permalink: string | undefined = aliasedSourceMatch
87
+ ? sourceToPermalink[aliasedSourceMatch]
88
+ : undefined;
73
89
 
74
90
  if (permalink) {
75
- modifiedLine = modifiedLine.replace(mdLink, permalink);
91
+ // MDX won't be happy if the permalink contains a space, we need to
92
+ // convert it to %20
93
+ const encodedPermalink = permalink
94
+ .split('/')
95
+ .map((part) => part.replace(/\s/g, '%20'))
96
+ .join('/');
97
+ modifiedLine = modifiedLine.replace(mdLink, encodedPermalink);
76
98
  } else {
77
99
  const brokenMarkdownLink: BrokenMarkdownLink<T> = {
78
100
  contentPaths,
@@ -5,10 +5,26 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import chalk from 'chalk';
9
- import fs from 'fs-extra';
8
+ import logger from '@docusaurus/logger';
10
9
  import matter from 'gray-matter';
11
10
 
11
+ // Input: ## Some heading {#some-heading}
12
+ // Output: {text: "## Some heading", id: "some-heading"}
13
+ export function parseMarkdownHeadingId(heading: string): {
14
+ text: string;
15
+ id?: string;
16
+ } {
17
+ const customHeadingIdRegex = /^(?<text>.*?)\s*\{#(?<id>[\w-]+)\}$/;
18
+ const matches = customHeadingIdRegex.exec(heading);
19
+ if (matches) {
20
+ return {
21
+ text: matches.groups!.text,
22
+ id: matches.groups!.id,
23
+ };
24
+ }
25
+ return {text: heading, id: undefined};
26
+ }
27
+
12
28
  // Hacky way of stripping out import statements from the excerpt
13
29
  // TODO: Find a better way to do so, possibly by compiling the Markdown content,
14
30
  // stripping out HTML tags and obtaining the first line.
@@ -18,9 +34,10 @@ export function createExcerpt(fileString: string): string | undefined {
18
34
  // Remove Markdown alternate title
19
35
  .replace(/^[^\n]*\n[=]+/g, '')
20
36
  .split('\n');
37
+ let inCode = false;
38
+ let lastCodeFence = '';
21
39
 
22
40
  /* eslint-disable no-continue */
23
- // eslint-disable-next-line no-restricted-syntax
24
41
  for (const fileLine of fileLines) {
25
42
  // Skip empty line.
26
43
  if (!fileLine.trim()) {
@@ -28,7 +45,24 @@ export function createExcerpt(fileString: string): string | undefined {
28
45
  }
29
46
 
30
47
  // Skip import/export declaration.
31
- if (/^\s*?import\s.*(from.*)?;?|export\s.*{.*};?/.test(fileLine)) {
48
+ if (/^(?:import|export)\s.*/.test(fileLine)) {
49
+ continue;
50
+ }
51
+
52
+ // Skip code block line.
53
+ if (fileLine.trim().startsWith('```')) {
54
+ if (!inCode) {
55
+ inCode = true;
56
+ [lastCodeFence] = fileLine.trim().match(/^`+/)!;
57
+ // If we are in a ````-fenced block, all ``` would be plain text instead
58
+ // of fences
59
+ } else if (
60
+ fileLine.trim().match(/^`+/)![0].length >= lastCodeFence.length
61
+ ) {
62
+ inCode = false;
63
+ }
64
+ continue;
65
+ } else if (inCode) {
32
66
  continue;
33
67
  }
34
68
 
@@ -36,25 +70,27 @@ export function createExcerpt(fileString: string): string | undefined {
36
70
  // Remove HTML tags.
37
71
  .replace(/<[^>]*>/g, '')
38
72
  // Remove Title headers
39
- .replace(/^#\s*([^#]*)\s*#?/gm, '')
73
+ .replace(/^#\s*[^#]*\s*#?/gm, '')
40
74
  // 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')
75
+ .replace(/^#{1,6}\s*(?<text>[^#]*)\s*(?:#{1,6})?/gm, '$1')
76
+ // Remove emphasis.
77
+ .replace(/(?<opening>[*_]{1,3})(?<text>.*?)\1/g, '$2')
78
+ // Remove strikethroughs.
79
+ .replace(/~~(?<text>\S.*\S)~~/g, '$1')
44
80
  // Remove images.
45
- .replace(/!\[(.*?)\][[(].*?[\])]/g, '$1')
81
+ .replace(/!\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
46
82
  // Remove footnotes.
47
- .replace(/\[\^.+?\](: .*?$)?/g, '')
83
+ .replace(/\[\^.+?\](?:: .*?$)?/g, '')
48
84
  // Remove inline links.
49
- .replace(/\[(.*?)\][[(].*?[\])]/g, '$1')
85
+ .replace(/\[(?<alt>.*?)\][[(].*?[\])]/g, '$1')
50
86
  // Remove inline code.
51
- .replace(/`(.+?)`/g, '$1')
87
+ .replace(/`(?<text>.+?)`/g, '$1')
52
88
  // Remove blockquotes.
53
89
  .replace(/^\s{0,3}>\s?/g, '')
54
90
  // Remove admonition definition.
55
- .replace(/(:{3}.*)/, '')
91
+ .replace(/:::.*/, '')
56
92
  // Remove Emoji names within colons include preceding whitespace.
57
- .replace(/\s?(:(::|[^:\n])+:)/g, '')
93
+ .replace(/\s?:(?:::|[^:\n])+:/g, '')
58
94
  // Remove custom Markdown heading id.
59
95
  .replace(/{#*[\w-]+}/, '')
60
96
  .trim();
@@ -67,22 +103,22 @@ export function createExcerpt(fileString: string): string | undefined {
67
103
  return undefined;
68
104
  }
69
105
 
70
- export function parseFrontMatter(
71
- markdownFileContent: string,
72
- ): {
106
+ export function parseFrontMatter(markdownFileContent: string): {
73
107
  frontMatter: Record<string, unknown>;
74
108
  content: string;
75
109
  } {
76
110
  const {data, content} = matter(markdownFileContent);
77
111
  return {
78
- frontMatter: data ?? {},
79
- content: content?.trim() ?? '',
112
+ frontMatter: data,
113
+ content: content.trim(),
80
114
  };
81
115
  }
82
116
 
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`)
117
+ /**
118
+ * Try to convert markdown heading to text. Does not need to be perfect, it is
119
+ * only used as a fallback when frontMatter.title is not provided. For now, we
120
+ * just unwrap possible inline code blocks (# `config.js`)
121
+ */
86
122
  function toTextContentTitle(contentTitle: string): string {
87
123
  if (contentTitle.startsWith('`') && contentTitle.endsWith('`')) {
88
124
  return contentTitle.substring(1, contentTitle.length - 1);
@@ -98,10 +134,11 @@ export function parseMarkdownContentTitle(
98
134
 
99
135
  const content = contentUntrimmed.trim();
100
136
 
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;
137
+ const IMPORT_STATEMENT =
138
+ /import\s+(?:[\w*{}\s\n,]+from\s+)?["'\s][@\w/_.-]+["'\s];?|\n/.source;
139
+ const REGULAR_TITLE =
140
+ /(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>(?:{#*[\w-]+})|#)?\n*?)/
141
+ .source;
105
142
  const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
106
143
 
107
144
  const regularTitleMatch = new RegExp(
@@ -118,15 +155,14 @@ export function parseMarkdownContentTitle(
118
155
 
119
156
  if (!pattern || !title) {
120
157
  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
158
  }
159
+ const newContent = removeContentTitleOption
160
+ ? content.replace(pattern, '')
161
+ : content;
162
+ return {
163
+ content: newContent.trim(),
164
+ contentTitle: toTextContentTitle(title.trim()).trim(),
165
+ };
130
166
  }
131
167
 
132
168
  type ParsedMarkdown = {
@@ -141,9 +177,8 @@ export function parseMarkdownString(
141
177
  options?: {removeContentTitle?: boolean},
142
178
  ): ParsedMarkdown {
143
179
  try {
144
- const {frontMatter, content: contentWithoutFrontMatter} = parseFrontMatter(
145
- markdownFileContent,
146
- );
180
+ const {frontMatter, content: contentWithoutFrontMatter} =
181
+ parseFrontMatter(markdownFileContent);
147
182
 
148
183
  const {content, contentTitle} = parseMarkdownContentTitle(
149
184
  contentWithoutFrontMatter,
@@ -158,25 +193,9 @@ export function parseMarkdownString(
158
193
  contentTitle,
159
194
  excerpt,
160
195
  };
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
- );
196
+ } catch (err) {
197
+ logger.error(`Error while parsing Markdown front matter.
198
+ This can happen if you use special characters in front matter values (try using double quotes around that value).`);
199
+ throw err;
181
200
  }
182
201
  }
package/src/pathUtils.ts CHANGED
@@ -5,25 +5,27 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- // Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
8
+ import path from 'path';
9
9
 
10
- // MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
10
+ // Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
11
+ // MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars,
12
+ // Others = 255 bytes
11
13
  const MAX_PATH_SEGMENT_CHARS = 255;
12
14
  const MAX_PATH_SEGMENT_BYTES = 255;
13
15
  // Space for appending things to the string like file extensions and so on
14
16
  const SPACE_FOR_APPENDING = 10;
15
17
 
16
- const isMacOs = process.platform === `darwin`;
17
- const isWindows = process.platform === `win32`;
18
+ const isMacOs = () => process.platform === 'darwin';
19
+ const isWindows = () => process.platform === 'win32';
18
20
 
19
- export const isNameTooLong = (str: string): boolean => {
20
- return isMacOs || isWindows
21
+ export const isNameTooLong = (str: string): boolean =>
22
+ // Not entirely correct: we can't assume FS from OS. But good enough?
23
+ isMacOs() || isWindows()
21
24
  ? str.length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_CHARS // MacOS (APFS) and Windows (NTFS) filename length limit (255 chars)
22
25
  : Buffer.from(str).length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_BYTES; // Other (255 bytes)
23
- };
24
26
 
25
27
  export const shortName = (str: string): string => {
26
- if (isMacOs || isWindows) {
28
+ if (isMacOs() || isWindows()) {
27
29
  const overflowingChars = str.length - MAX_PATH_SEGMENT_CHARS;
28
30
  return str.slice(
29
31
  0,
@@ -40,3 +42,80 @@ export const shortName = (str: string): string => {
40
42
  )
41
43
  .toString();
42
44
  };
45
+
46
+ /**
47
+ * Convert Windows backslash paths to posix style paths.
48
+ * E.g: endi\lie -> endi/lie
49
+ *
50
+ * Returns original path if the posix counterpart is not valid Windows path.
51
+ * This makes the legacy code that uses posixPath safe; but also makes it less
52
+ * useful when you actually want a path with forward slashes (e.g. for URL)
53
+ *
54
+ * Adopted from https://github.com/sindresorhus/slash/blob/main/index.js
55
+ */
56
+ export function posixPath(str: string): string {
57
+ const isExtendedLengthPath = /^\\\\\?\\/.test(str);
58
+
59
+ // Forward slashes are only valid Windows paths when they don't contain non-
60
+ // ascii characters.
61
+ // eslint-disable-next-line no-control-regex
62
+ const hasNonAscii = /[^\u0000-\u0080]+/.test(str);
63
+
64
+ if (isExtendedLengthPath || hasNonAscii) {
65
+ return str;
66
+ }
67
+ return str.replace(/\\/g, '/');
68
+ }
69
+
70
+ /**
71
+ * When you want to display a path in a message/warning/error, it's more
72
+ * convenient to:
73
+ *
74
+ * - make it relative to `cwd()`
75
+ * - convert to posix (ie not using windows \ path separator)
76
+ *
77
+ * This way, Jest tests can run more reliably on any computer/CI on both
78
+ * Unix/Windows
79
+ * For Windows users this is not perfect (as they see / instead of \) but it's
80
+ * probably good enough
81
+ */
82
+ export function toMessageRelativeFilePath(filePath: string): string {
83
+ return posixPath(path.relative(process.cwd(), filePath));
84
+ }
85
+
86
+ /**
87
+ * Alias filepath relative to site directory, very useful so that we
88
+ * don't expose user's site structure.
89
+ * Example: some/path/to/website/docs/foo.md -> @site/docs/foo.md
90
+ */
91
+ export function aliasedSitePath(filePath: string, siteDir: string): string {
92
+ const relativePath = posixPath(path.relative(siteDir, filePath));
93
+ // Cannot use path.join() as it resolves '../' and removes
94
+ // the '@site'. Let webpack loader resolve it.
95
+ return `@site/${relativePath}`;
96
+ }
97
+
98
+ /**
99
+ * When you have a path like C:\X\Y
100
+ * It is not safe to use directly when generating code
101
+ * For example, this would fail due to unescaped \:
102
+ * `<img src={require('${filePath}')} />`
103
+ * But this would work: `<img src={require('${escapePath(filePath)}')} />`
104
+ *
105
+ * posixPath can't be used in all cases, because forward slashes are only valid
106
+ * Windows paths when they don't contain non-ascii characters, and posixPath
107
+ * doesn't escape those that fail to be converted.
108
+ */
109
+ export function escapePath(str: string): string {
110
+ const escaped = JSON.stringify(str);
111
+
112
+ // Remove the " around the json string;
113
+ return escaped.substring(1, escaped.length - 1);
114
+ }
115
+
116
+ export function addTrailingPathSeparator(str: string): string {
117
+ return str.endsWith(path.sep)
118
+ ? str
119
+ : // If this is Windows, we need to change the forward slash to backward
120
+ `${str.replace(/\/$/, '')}${path.sep}`;
121
+ }
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,105 @@
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 _ from 'lodash';
9
+ import {normalizeUrl} from './urlUtils';
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. For versioned docs, v1/doc.md
32
+ // and v2/doc.md tags with custom permalinks don't lead to the same created
33
+ // page. 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 = frontMatterTags.map((tag) =>
53
+ normalizeFrontMatterTag(tagsPath, tag),
54
+ );
55
+
56
+ return _.uniqBy(tags, (tag) => tag.permalink);
57
+ }
58
+
59
+ export type TaggedItemGroup<Item> = {
60
+ tag: Tag;
61
+ items: Item[];
62
+ };
63
+
64
+ /**
65
+ * Permits to group docs/blogPosts by tag (provided by front matter)
66
+ * Note: groups are indexed by permalink, because routes must be unique in the
67
+ * end. Labels may vary on 2 md files but they are normalized. Docs with
68
+ * label='some label' and label='some-label' should end-up in the same
69
+ * group/page in the end. We can't create 2 routes /some-label because one would
70
+ * override the other
71
+ */
72
+ export function groupTaggedItems<Item>(
73
+ items: Item[],
74
+ getItemTags: (item: Item) => Tag[],
75
+ ): Record<string, TaggedItemGroup<Item>> {
76
+ const result: Record<string, TaggedItemGroup<Item>> = {};
77
+
78
+ function handleItemTag(item: Item, tag: Tag) {
79
+ // Init missing tag groups
80
+ // TODO: it's not really clear what should be the behavior if 2 items have
81
+ // the same tag but the permalink is different for each
82
+ // For now, the first tag found wins
83
+ result[tag.permalink] = result[tag.permalink] ?? {
84
+ tag,
85
+ items: [],
86
+ };
87
+
88
+ // Add item to group
89
+ result[tag.permalink].items.push(item);
90
+ }
91
+
92
+ items.forEach((item) => {
93
+ getItemTags(item).forEach((tag) => {
94
+ handleItemTag(item, tag);
95
+ });
96
+ });
97
+
98
+ // If user add twice the same tag to a md doc (weird but possible),
99
+ // we don't want the item to appear twice in the list...
100
+ Object.values(result).forEach((group) => {
101
+ group.items = _.uniq(group.items);
102
+ });
103
+
104
+ return result;
105
+ }
@@ -0,0 +1,149 @@
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 {removeSuffix} from './jsUtils';
9
+ import resolvePathnameUnsafe from 'resolve-pathname';
10
+
11
+ export function normalizeUrl(rawUrls: string[]): string {
12
+ const urls = [...rawUrls];
13
+ const resultArray = [];
14
+
15
+ let hasStartingSlash = false;
16
+ let hasEndingSlash = false;
17
+
18
+ // If the first part is a plain protocol, we combine it with the next part.
19
+ if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
20
+ const first = urls.shift();
21
+ if (first!.startsWith('file:') && urls[0].startsWith('/')) {
22
+ // Force a double slash here, else we lose the information that the next
23
+ // segment is an absolute path
24
+ urls[0] = `${first}//${urls[0]}`;
25
+ } else {
26
+ urls[0] = first + urls[0];
27
+ }
28
+ }
29
+
30
+ // There must be two or three slashes in the file protocol,
31
+ // two slashes in anything else.
32
+ const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
33
+ urls[0] = urls[0].replace(/^(?<protocol>[^/:]+):\/*/, replacement);
34
+
35
+ for (let i = 0; i < urls.length; i += 1) {
36
+ let component = urls[i];
37
+
38
+ if (typeof component !== 'string') {
39
+ throw new TypeError(`Url must be a string. Received ${typeof component}`);
40
+ }
41
+
42
+ if (component === '') {
43
+ if (i === urls.length - 1 && hasEndingSlash) {
44
+ resultArray.push('/');
45
+ }
46
+ // eslint-disable-next-line no-continue
47
+ continue;
48
+ }
49
+
50
+ if (component !== '/') {
51
+ if (i > 0) {
52
+ // Removing the starting slashes for each component but the first.
53
+ component = component.replace(
54
+ /^[/]+/,
55
+ // Special case where the first element of rawUrls is empty
56
+ // ["", "/hello"] => /hello
57
+ component[0] === '/' && !hasStartingSlash ? '/' : '',
58
+ );
59
+ }
60
+
61
+ hasEndingSlash = component[component.length - 1] === '/';
62
+ // Removing the ending slashes for each component but the last. For the
63
+ // last component we will combine multiple slashes to a single one.
64
+ component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
65
+ }
66
+
67
+ hasStartingSlash = true;
68
+ resultArray.push(component);
69
+ }
70
+
71
+ let str = resultArray.join('/');
72
+ // Each input component is now separated by a single slash
73
+ // except the possible first plain protocol part.
74
+
75
+ // Remove trailing slash before parameters or hash.
76
+ str = str.replace(/\/(?<search>\?|&|#[^!])/g, '$1');
77
+
78
+ // Replace ? in parameters with &.
79
+ const parts = str.split('?');
80
+ str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
81
+
82
+ // Dedupe forward slashes in the entire path, avoiding protocol slashes.
83
+ str = str.replace(/(?<textBefore>[^:/]\/)\/+/g, '$1');
84
+
85
+ // Dedupe forward slashes at the beginning of the path.
86
+ str = str.replace(/^\/+/g, '/');
87
+
88
+ return str;
89
+ }
90
+
91
+ export function getEditUrl(
92
+ fileRelativePath: string,
93
+ editUrl?: string,
94
+ ): string | undefined {
95
+ return editUrl
96
+ ? // Don't use posixPath for this: we need to force a forward slash path
97
+ normalizeUrl([editUrl, fileRelativePath.replace(/\\/g, '/')])
98
+ : undefined;
99
+ }
100
+
101
+ /**
102
+ * Convert filepath to url path.
103
+ * Example: 'index.md' -> '/', 'foo/bar.js' -> '/foo/bar',
104
+ */
105
+ export function fileToPath(file: string): string {
106
+ const indexRE = /(?<dirname>^|.*\/)index\.(?:mdx?|jsx?|tsx?)$/i;
107
+ const extRE = /\.(?:mdx?|jsx?|tsx?)$/;
108
+
109
+ if (indexRE.test(file)) {
110
+ return file.replace(indexRE, '/$1');
111
+ }
112
+ return `/${file.replace(extRE, '').replace(/\\/g, '/')}`;
113
+ }
114
+
115
+ export function encodePath(userPath: string): string {
116
+ return userPath
117
+ .split('/')
118
+ .map((item) => encodeURIComponent(item))
119
+ .join('/');
120
+ }
121
+
122
+ export function isValidPathname(str: string): boolean {
123
+ if (!str.startsWith('/')) {
124
+ return false;
125
+ }
126
+ try {
127
+ // weird, but is there a better way?
128
+ const parsedPathname = new URL(str, 'https://domain.com').pathname;
129
+ return parsedPathname === str || parsedPathname === encodeURI(str);
130
+ } catch {
131
+ return false;
132
+ }
133
+ }
134
+
135
+ // resolve pathname and fail fast if resolution fails
136
+ export function resolvePathname(to: string, from?: string): string {
137
+ return resolvePathnameUnsafe(to, from);
138
+ }
139
+ export function addLeadingSlash(str: string): string {
140
+ return str.startsWith('/') ? str : `/${str}`;
141
+ }
142
+
143
+ // TODO deduplicate: also present in @docusaurus/utils-common
144
+ export function addTrailingSlash(str: string): string {
145
+ return str.endsWith('/') ? str : `${str}/`;
146
+ }
147
+ export function removeTrailingSlash(str: string): string {
148
+ return removeSuffix(str, '/');
149
+ }