@docusaurus/utils 2.0.0-beta.8bda3b2db → 2.0.0-beta.9

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 (42) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/codeTranslationsUtils.js +2 -2
  3. package/lib/globUtils.d.ts +11 -0
  4. package/lib/globUtils.js +47 -0
  5. package/lib/{docuHash.d.ts → hashUtils.d.ts} +2 -0
  6. package/lib/{docuHash.js → hashUtils.js} +15 -5
  7. package/lib/index.d.ts +5 -4
  8. package/lib/index.js +38 -88
  9. package/lib/markdownLinks.js +19 -6
  10. package/lib/markdownParser.js +20 -12
  11. package/lib/mdxUtils.d.ts +16 -0
  12. package/lib/mdxUtils.js +30 -0
  13. package/lib/{getFilePathForRoutePath.d.ts → normalizeUrl.d.ts} +1 -1
  14. package/lib/normalizeUrl.js +66 -0
  15. package/lib/pathUtils.d.ts +0 -1
  16. package/lib/pathUtils.js +1 -6
  17. package/lib/tags.d.ts +18 -0
  18. package/lib/tags.js +72 -0
  19. package/package.json +17 -6
  20. package/src/__tests__/globUtils.test.ts +109 -0
  21. package/src/__tests__/{docuHash.test.ts → hashUtils.test.ts} +22 -1
  22. package/src/__tests__/index.test.ts +0 -108
  23. package/src/__tests__/markdownParser.test.ts +13 -0
  24. package/src/__tests__/mdxUtils.test.ts +133 -0
  25. package/src/__tests__/normalizeUrl.test.ts +117 -0
  26. package/src/__tests__/pathUtils.test.ts +5 -22
  27. package/src/__tests__/tags.test.ts +183 -0
  28. package/src/dependencies.d.ts +12 -0
  29. package/src/globUtils.ts +63 -0
  30. package/src/{docuHash.ts → hashUtils.ts} +10 -1
  31. package/src/index.ts +14 -83
  32. package/src/markdownLinks.ts +19 -8
  33. package/src/markdownParser.ts +24 -17
  34. package/src/mdxUtils.ts +32 -0
  35. package/src/normalizeUrl.ts +80 -0
  36. package/src/pathUtils.ts +0 -6
  37. package/src/tags.ts +100 -0
  38. package/src/types.d.ts +10 -0
  39. package/yarn-error.log +17862 -0
  40. package/lib/getFilePathForRoutePath.js +0 -40
  41. package/src/__tests__/getFilePathForRoutePath.test.ts +0 -87
  42. package/src/getFilePathForRoutePath.ts +0 -43
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
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,10 @@
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
+ declare module 'resolve-pathname' {
9
+ export default function resolvePathname(to: string, from?: string): string;
10
+ }