@docusaurus/utils 2.0.0-beta.1 → 2.0.0-beta.12

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 (57) 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/{docuHash.d.ts → hashUtils.d.ts} +2 -0
  7. package/lib/{docuHash.js → hashUtils.js} +15 -5
  8. package/lib/index.d.ts +11 -9
  9. package/lib/index.js +44 -98
  10. package/lib/markdownLinks.js +19 -6
  11. package/lib/markdownParser.js +20 -12
  12. package/lib/mdxUtils.d.ts +16 -0
  13. package/lib/mdxUtils.js +30 -0
  14. package/lib/{getFilePathForRoutePath.d.ts → normalizeUrl.d.ts} +1 -1
  15. package/lib/normalizeUrl.js +66 -0
  16. package/lib/pathUtils.d.ts +0 -1
  17. package/lib/pathUtils.js +4 -11
  18. package/lib/slugger.d.ts +13 -0
  19. package/lib/slugger.js +18 -0
  20. package/lib/tags.d.ts +18 -0
  21. package/lib/tags.js +72 -0
  22. package/lib/webpackUtils.d.ts +29 -0
  23. package/lib/webpackUtils.js +109 -0
  24. package/package.json +23 -7
  25. package/src/__tests__/globUtils.test.ts +109 -0
  26. package/src/__tests__/{docuHash.test.ts → hashUtils.test.ts} +22 -1
  27. package/src/__tests__/index.test.ts +6 -110
  28. package/src/__tests__/markdownParser.test.ts +15 -2
  29. package/src/__tests__/mdxUtils.test.ts +133 -0
  30. package/src/__tests__/normalizeUrl.test.ts +117 -0
  31. package/src/__tests__/pathUtils.test.ts +5 -22
  32. package/src/__tests__/slugger.test.ts +27 -0
  33. package/src/__tests__/tags.test.ts +183 -0
  34. package/src/__tests__/webpackUtils.test.ts +33 -0
  35. package/src/constants.ts +38 -0
  36. package/src/deps.d.ts +14 -0
  37. package/src/globUtils.ts +63 -0
  38. package/src/{docuHash.ts → hashUtils.ts} +10 -1
  39. package/src/index.ts +23 -96
  40. package/src/markdownLinks.ts +19 -8
  41. package/src/markdownParser.ts +24 -17
  42. package/src/mdxUtils.ts +32 -0
  43. package/src/normalizeUrl.ts +80 -0
  44. package/src/pathUtils.ts +2 -9
  45. package/src/slugger.ts +24 -0
  46. package/src/tags.ts +100 -0
  47. package/src/webpackUtils.ts +144 -0
  48. package/lib/codeTranslationsUtils.d.ts +0 -11
  49. package/lib/codeTranslationsUtils.js +0 -50
  50. package/lib/getFilePathForRoutePath.js +0 -40
  51. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  52. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  53. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  54. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  55. package/src/__tests__/getFilePathForRoutePath.test.ts +0 -87
  56. package/src/codeTranslationsUtils.ts +0 -56
  57. package/src/getFilePathForRoutePath.ts +0 -43
package/src/index.ts CHANGED
@@ -18,22 +18,32 @@ import {
18
18
  TranslationFile,
19
19
  } from '@docusaurus/types';
20
20
 
21
- // @ts-expect-error: no typedefs :s
22
21
  import resolvePathnameUnsafe from 'resolve-pathname';
23
22
 
24
23
  import {posixPath as posixPathImport} from './posixPath';
25
- import {simpleHash} from './pathUtils';
26
- import {docuHash} from './docuHash';
24
+ import {simpleHash, docuHash} from './hashUtils';
25
+ import {normalizeUrl} from './normalizeUrl';
26
+ import {DEFAULT_PLUGIN_ID} from './constants';
27
+
28
+ export * from './constants';
29
+ export * from './mdxUtils';
30
+ export * from './normalizeUrl';
31
+ export * from './tags';
27
32
 
28
33
  export const posixPath = posixPathImport;
29
34
 
30
- export * from './getFilePathForRoutePath';
31
- export * from './codeTranslationsUtils';
32
35
  export * from './markdownParser';
33
36
  export * from './markdownLinks';
34
37
  export * from './escapePath';
35
- export * from './docuHash';
36
- export {simpleHash} from './pathUtils';
38
+ export * from './slugger';
39
+ export {md5Hash, simpleHash, docuHash} from './hashUtils';
40
+ export {
41
+ Globby,
42
+ GlobExcludeDefault,
43
+ createMatcher,
44
+ createAbsoluteFilePathMatcher,
45
+ } from './globUtils';
46
+ export * from './webpackUtils';
37
47
 
38
48
  const fileHash = new Map();
39
49
  export async function generate(
@@ -187,80 +197,6 @@ export function getSubFolder(file: string, refDir: string): string | null {
187
197
  return match && match[1];
188
198
  }
189
199
 
190
- export function normalizeUrl(rawUrls: string[]): string {
191
- const urls = [...rawUrls];
192
- const resultArray = [];
193
-
194
- let hasStartingSlash = false;
195
- let hasEndingSlash = false;
196
-
197
- // If the first part is a plain protocol, we combine it with the next part.
198
- if (urls[0].match(/^[^/:]+:\/*$/) && urls.length > 1) {
199
- const first = urls.shift();
200
- urls[0] = first + urls[0];
201
- }
202
-
203
- // There must be two or three slashes in the file protocol,
204
- // two slashes in anything else.
205
- const replacement = urls[0].match(/^file:\/\/\//) ? '$1:///' : '$1://';
206
- urls[0] = urls[0].replace(/^([^/:]+):\/*/, replacement);
207
-
208
- // eslint-disable-next-line
209
- for (let i = 0; i < urls.length; i++) {
210
- let component = urls[i];
211
-
212
- if (typeof component !== 'string') {
213
- throw new TypeError(`Url must be a string. Received ${typeof component}`);
214
- }
215
-
216
- if (component === '') {
217
- if (i === urls.length - 1 && hasEndingSlash) {
218
- resultArray.push('/');
219
- }
220
- // eslint-disable-next-line
221
- continue;
222
- }
223
-
224
- if (component !== '/') {
225
- if (i > 0) {
226
- // Removing the starting slashes for each component but the first.
227
- component = component.replace(
228
- /^[/]+/,
229
- // Special case where the first element of rawUrls is empty ["", "/hello"] => /hello
230
- component[0] === '/' && !hasStartingSlash ? '/' : '',
231
- );
232
- }
233
-
234
- hasEndingSlash = component[component.length - 1] === '/';
235
- // Removing the ending slashes for each component but the last.
236
- // For the last component we will combine multiple slashes to a single one.
237
- component = component.replace(/[/]+$/, i < urls.length - 1 ? '' : '/');
238
- }
239
-
240
- hasStartingSlash = true;
241
- resultArray.push(component);
242
- }
243
-
244
- let str = resultArray.join('/');
245
- // Each input component is now separated by a single slash
246
- // except the possible first plain protocol part.
247
-
248
- // Remove trailing slash before parameters or hash.
249
- str = str.replace(/\/(\?|&|#[^!])/g, '$1');
250
-
251
- // Replace ? in parameters with &.
252
- const parts = str.split('?');
253
- str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
254
-
255
- // Dedupe forward slashes in the entire path, avoiding protocol slashes.
256
- str = str.replace(/([^:]\/)\/+/g, '$1');
257
-
258
- // Dedupe forward slashes at the beginning of the path.
259
- str = str.replace(/^\/+/g, '/');
260
-
261
- return str;
262
- }
263
-
264
200
  /**
265
201
  * Alias filepath relative to site directory, very useful so that we
266
202
  * don't expose user's site structure.
@@ -326,7 +262,7 @@ export function removePrefix(str: string, prefix: string): string {
326
262
  return str.startsWith(prefix) ? str.slice(prefix.length) : str;
327
263
  }
328
264
 
329
- export function getElementsAround<T extends unknown>(
265
+ export function getElementsAround<T>(
330
266
  array: T[],
331
267
  aroundIndex: number,
332
268
  ): {
@@ -349,7 +285,7 @@ export function getPluginI18nPath({
349
285
  siteDir,
350
286
  locale,
351
287
  pluginName,
352
- pluginId = 'default', // TODO duplicated constant
288
+ pluginId = DEFAULT_PLUGIN_ID,
353
289
  subPaths = [],
354
290
  }: {
355
291
  siteDir: string;
@@ -365,22 +301,18 @@ export function getPluginI18nPath({
365
301
  locale,
366
302
  // Make it convenient to use for single-instance
367
303
  // ie: return "docs", not "docs-default" nor "docs/default"
368
- `${pluginName}${
369
- // TODO duplicate constant :(
370
- pluginId === 'default' ? '' : `-${pluginId}`
371
- }`,
304
+ `${pluginName}${pluginId === DEFAULT_PLUGIN_ID ? '' : `-${pluginId}`}`,
372
305
  ...subPaths,
373
306
  );
374
307
  }
375
308
 
376
- export async function mapAsyncSequencial<T extends unknown, R extends unknown>(
309
+ export async function mapAsyncSequencial<T, R>(
377
310
  array: T[],
378
311
  action: (t: T) => Promise<R>,
379
312
  ): Promise<R[]> {
380
313
  const results: R[] = [];
381
314
  // eslint-disable-next-line no-restricted-syntax
382
315
  for (const t of array) {
383
- // eslint-disable-next-line no-await-in-loop
384
316
  const result = await action(t);
385
317
  results.push(result);
386
318
  }
@@ -393,7 +325,6 @@ export async function findAsyncSequential<T>(
393
325
  ): Promise<T | undefined> {
394
326
  // eslint-disable-next-line no-restricted-syntax
395
327
  for (const t of array) {
396
- // eslint-disable-next-line no-await-in-loop
397
328
  if (await predicate(t)) {
398
329
  return t;
399
330
  }
@@ -458,9 +389,7 @@ export function reportMessage(
458
389
  export function mergeTranslations(
459
390
  contents: TranslationFileContent[],
460
391
  ): TranslationFileContent {
461
- return contents.reduce((acc, content) => {
462
- return {...acc, ...content};
463
- }, {});
392
+ return contents.reduce((acc, content) => ({...acc, ...content}), {});
464
393
  }
465
394
 
466
395
  export function getSwizzledComponent(
@@ -494,9 +423,7 @@ export function updateTranslationFileMessages(
494
423
 
495
424
  // Input: ## Some heading {#some-heading}
496
425
  // Output: {text: "## Some heading", id: "some-heading"}
497
- export function parseMarkdownHeadingId(
498
- heading: string,
499
- ): {
426
+ export function parseMarkdownHeadingId(heading: string): {
500
427
  text: string;
501
428
  id?: string;
502
429
  } {
@@ -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,21 +33,29 @@ 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, '')
38
47
  // Remove Title headers
39
- .replace(/^\#\s*([^#]*)\s*\#?/gm, '')
48
+ .replace(/^#\s*([^#]*)\s*#?/gm, '')
40
49
  // Remove Markdown + ATX-style headers
41
- .replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1')
50
+ .replace(/^#{1,6}\s*([^#]*)\s*(#{1,6})?/gm, '$1')
42
51
  // Remove emphasis and strikethroughs.
43
- .replace(/([\*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
52
+ .replace(/([*_~]{1,3})(\S.*?\S{0,1})\1/g, '$2')
44
53
  // Remove images.
45
- .replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
54
+ .replace(/!\[(.*?)\][[(].*?[\])]/g, '$1')
46
55
  // Remove footnotes.
47
- .replace(/\[\^.+?\](\: .*?$)?/g, '')
56
+ .replace(/\[\^.+?\](: .*?$)?/g, '')
48
57
  // Remove inline links.
49
- .replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
58
+ .replace(/\[(.*?)\][[(].*?[\])]/g, '$1')
50
59
  // Remove inline code.
51
60
  .replace(/`(.+?)`/g, '$1')
52
61
  // Remove blockquotes.
@@ -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
@@ -7,8 +7,6 @@
7
7
 
8
8
  // Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
9
9
 
10
- import {createHash} from 'crypto';
11
-
12
10
  // MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
13
11
  const MAX_PATH_SEGMENT_CHARS = 255;
14
12
  const MAX_PATH_SEGMENT_BYTES = 255;
@@ -18,11 +16,10 @@ const SPACE_FOR_APPENDING = 10;
18
16
  const isMacOs = process.platform === `darwin`;
19
17
  const isWindows = process.platform === `win32`;
20
18
 
21
- export const isNameTooLong = (str: string): boolean => {
22
- return isMacOs || isWindows
19
+ export const isNameTooLong = (str: string): boolean =>
20
+ isMacOs || isWindows
23
21
  ? str.length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_CHARS // MacOS (APFS) and Windows (NTFS) filename length limit (255 chars)
24
22
  : Buffer.from(str).length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_BYTES; // Other (255 bytes)
25
- };
26
23
 
27
24
  export const shortName = (str: string): string => {
28
25
  if (isMacOs || isWindows) {
@@ -42,7 +39,3 @@ export const shortName = (str: string): string => {
42
39
  )
43
40
  .toString();
44
41
  };
45
-
46
- export function simpleHash(str: string, length: number): string {
47
- return createHash('md5').update(str).digest('hex').substr(0, length);
48
- }
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
+ }