@docusaurus/utils 2.0.0-beta.8e9b829d9 → 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.
@@ -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/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
+ }