@docusaurus/theme-common 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.
Files changed (60) hide show
  1. package/lib/.tsbuildinfo +1 -1
  2. package/lib/components/Collapsible/index.js +5 -13
  3. package/lib/components/Details/index.js +3 -3
  4. package/lib/components/Details/styles.module.css +8 -9
  5. package/lib/index.d.ts +11 -1
  6. package/lib/index.js +8 -0
  7. package/lib/utils/ThemeClassNames.d.ts +36 -12
  8. package/lib/utils/ThemeClassNames.js +36 -3
  9. package/lib/utils/announcementBarUtils.d.ts +1 -1
  10. package/lib/utils/announcementBarUtils.js +6 -6
  11. package/lib/utils/docsPreferredVersion/DocsPreferredVersionProvider.js +1 -1
  12. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.d.ts +5 -3
  13. package/lib/utils/docsPreferredVersion/useDocsPreferredVersion.js +1 -2
  14. package/lib/utils/generalUtils.js +2 -2
  15. package/lib/utils/historyUtils.d.ts +11 -0
  16. package/lib/utils/historyUtils.js +42 -0
  17. package/lib/utils/jsUtils.d.ts +13 -0
  18. package/lib/utils/jsUtils.js +16 -0
  19. package/lib/utils/mobileSecondaryMenu.js +1 -0
  20. package/lib/utils/reactUtils.d.ts +9 -0
  21. package/lib/utils/reactUtils.js +26 -0
  22. package/lib/utils/regexpUtils.d.ts +10 -0
  23. package/lib/utils/regexpUtils.js +16 -0
  24. package/lib/utils/scrollUtils.d.ts +52 -0
  25. package/lib/utils/scrollUtils.js +137 -0
  26. package/lib/utils/tagsUtils.d.ts +18 -0
  27. package/lib/utils/tagsUtils.js +33 -0
  28. package/lib/utils/tocUtils.d.ts +15 -0
  29. package/lib/utils/tocUtils.js +36 -0
  30. package/lib/utils/useLocationChange.js +5 -9
  31. package/lib/utils/usePrevious.js +3 -2
  32. package/lib/utils/useTOCHighlight.d.ts +14 -0
  33. package/lib/utils/useTOCHighlight.js +124 -0
  34. package/lib/utils/useThemeConfig.d.ts +13 -2
  35. package/package.json +12 -10
  36. package/src/components/Collapsible/index.tsx +6 -18
  37. package/src/components/Details/index.tsx +3 -3
  38. package/src/components/Details/styles.module.css +8 -9
  39. package/src/index.ts +27 -0
  40. package/src/types.d.ts +0 -2
  41. package/src/utils/ThemeClassNames.ts +42 -4
  42. package/src/utils/__tests__/tagUtils.test.ts +66 -0
  43. package/src/utils/__tests__/tocUtils.test.ts +197 -0
  44. package/src/utils/announcementBarUtils.tsx +7 -7
  45. package/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +4 -4
  46. package/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +13 -14
  47. package/src/utils/generalUtils.ts +2 -2
  48. package/src/utils/historyUtils.ts +50 -0
  49. package/src/utils/jsUtils.ts +23 -0
  50. package/src/utils/mobileSecondaryMenu.tsx +2 -1
  51. package/src/utils/reactUtils.tsx +34 -0
  52. package/src/utils/regexpUtils.ts +23 -0
  53. package/src/utils/scrollUtils.tsx +238 -0
  54. package/src/utils/storageUtils.ts +1 -1
  55. package/src/utils/tagsUtils.ts +48 -0
  56. package/src/utils/tocUtils.ts +54 -0
  57. package/src/utils/useLocationChange.ts +6 -10
  58. package/src/utils/usePrevious.ts +3 -2
  59. package/src/utils/useTOCHighlight.ts +179 -0
  60. package/src/utils/useThemeConfig.ts +17 -2
@@ -0,0 +1,14 @@
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
+ export declare type TOCHighlightConfig = {
8
+ linkClassName: string;
9
+ linkActiveClassName: string;
10
+ minHeadingLevel: number;
11
+ maxHeadingLevel: number;
12
+ };
13
+ declare function useTOCHighlight(config: TOCHighlightConfig | undefined): void;
14
+ export default useTOCHighlight;
@@ -0,0 +1,124 @@
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
+ import { useEffect, useRef } from 'react';
8
+ import { useThemeConfig } from './useThemeConfig';
9
+ /*
10
+ TODO make the hardcoded theme-classic classnames configurable
11
+ (or add them to ThemeClassNames?)
12
+ */
13
+ // If the anchor has no height and is just a "marker" in the dom; we'll use the parent (normally the link text) rect boundaries instead
14
+ function getVisibleBoundingClientRect(element) {
15
+ const rect = element.getBoundingClientRect();
16
+ const hasNoHeight = rect.top === rect.bottom;
17
+ if (hasNoHeight) {
18
+ return getVisibleBoundingClientRect(element.parentNode);
19
+ }
20
+ return rect;
21
+ }
22
+ // Considering we divide viewport into 2 zones of each 50vh
23
+ // This returns true if an element is in the first zone (ie, appear in viewport, near the top)
24
+ function isInViewportTopHalf(boundingRect) {
25
+ return boundingRect.top > 0 && boundingRect.bottom < window.innerHeight / 2;
26
+ }
27
+ function getAnchors({ minHeadingLevel, maxHeadingLevel, }) {
28
+ const selectors = [];
29
+ for (let i = minHeadingLevel; i <= maxHeadingLevel; i += 1) {
30
+ selectors.push(`h${i}.anchor`);
31
+ }
32
+ return Array.from(document.querySelectorAll(selectors.join()));
33
+ }
34
+ function getActiveAnchor(anchors, { anchorTopOffset, }) {
35
+ var _a;
36
+ // Naming is hard
37
+ // The "nextVisibleAnchor" is the first anchor that appear under the viewport top boundary
38
+ // Note: it does not mean this anchor is visible yet, but if user continues scrolling down, it will be the first to become visible
39
+ const nextVisibleAnchor = anchors.find((anchor) => {
40
+ const boundingRect = getVisibleBoundingClientRect(anchor);
41
+ return boundingRect.top >= anchorTopOffset;
42
+ });
43
+ if (nextVisibleAnchor) {
44
+ const boundingRect = getVisibleBoundingClientRect(nextVisibleAnchor);
45
+ // If anchor is in the top half of the viewport: it is the one we consider "active"
46
+ // (unless it's too close to the top and and soon to be scrolled outside viewport)
47
+ if (isInViewportTopHalf(boundingRect)) {
48
+ return nextVisibleAnchor;
49
+ }
50
+ // If anchor is in the bottom half of the viewport, or under the viewport, we consider the active anchor is the previous one
51
+ // This is because the main text appearing in the user screen mostly belong to the previous anchor
52
+ else {
53
+ // Returns null for the first anchor, see https://github.com/facebook/docusaurus/issues/5318
54
+ return (_a = anchors[anchors.indexOf(nextVisibleAnchor) - 1]) !== null && _a !== void 0 ? _a : null;
55
+ }
56
+ }
57
+ // no anchor under viewport top? (ie we are at the bottom of the page)
58
+ // => highlight the last anchor found
59
+ else {
60
+ return anchors[anchors.length - 1];
61
+ }
62
+ }
63
+ function getLinkAnchorValue(link) {
64
+ return decodeURIComponent(link.href.substring(link.href.indexOf('#') + 1));
65
+ }
66
+ function getLinks(linkClassName) {
67
+ return Array.from(document.getElementsByClassName(linkClassName));
68
+ }
69
+ function getNavbarHeight() {
70
+ // Not ideal to obtain actual height this way
71
+ // Using TS ! (not ?) because otherwise a bad selector would be un-noticed
72
+ return document.querySelector('.navbar').clientHeight;
73
+ }
74
+ function useAnchorTopOffsetRef() {
75
+ const anchorTopOffsetRef = useRef(0);
76
+ const { navbar: { hideOnScroll }, } = useThemeConfig();
77
+ useEffect(() => {
78
+ anchorTopOffsetRef.current = hideOnScroll ? 0 : getNavbarHeight();
79
+ }, [hideOnScroll]);
80
+ return anchorTopOffsetRef;
81
+ }
82
+ function useTOCHighlight(config) {
83
+ const lastActiveLinkRef = useRef(undefined);
84
+ const anchorTopOffsetRef = useAnchorTopOffsetRef();
85
+ useEffect(() => {
86
+ if (!config) {
87
+ // no-op, highlighting is disabled
88
+ return () => { };
89
+ }
90
+ const { linkClassName, linkActiveClassName, minHeadingLevel, maxHeadingLevel, } = config;
91
+ function updateLinkActiveClass(link, active) {
92
+ var _a;
93
+ if (active) {
94
+ if (lastActiveLinkRef.current && lastActiveLinkRef.current !== link) {
95
+ (_a = lastActiveLinkRef.current) === null || _a === void 0 ? void 0 : _a.classList.remove(linkActiveClassName);
96
+ }
97
+ link.classList.add(linkActiveClassName);
98
+ lastActiveLinkRef.current = link;
99
+ }
100
+ else {
101
+ link.classList.remove(linkActiveClassName);
102
+ }
103
+ }
104
+ function updateActiveLink() {
105
+ const links = getLinks(linkClassName);
106
+ const anchors = getAnchors({ minHeadingLevel, maxHeadingLevel });
107
+ const activeAnchor = getActiveAnchor(anchors, {
108
+ anchorTopOffset: anchorTopOffsetRef.current,
109
+ });
110
+ const activeLink = links.find((link) => activeAnchor && activeAnchor.id === getLinkAnchorValue(link));
111
+ links.forEach((link) => {
112
+ updateLinkActiveClass(link, link === activeLink);
113
+ });
114
+ }
115
+ document.addEventListener('scroll', updateActiveLink);
116
+ document.addEventListener('resize', updateActiveLink);
117
+ updateActiveLink();
118
+ return () => {
119
+ document.removeEventListener('scroll', updateActiveLink);
120
+ document.removeEventListener('resize', updateActiveLink);
121
+ };
122
+ }, [config, anchorTopOffsetRef]);
123
+ }
124
+ export default useTOCHighlight;
@@ -1,15 +1,18 @@
1
1
  import { PrismTheme } from 'prism-react-renderer';
2
2
  import { CSSProperties } from 'react';
3
+ import { DeepPartial } from 'utility-types';
3
4
  export declare type DocsVersionPersistence = 'localStorage' | 'none';
4
5
  export declare type NavbarItem = {
5
6
  type?: string | undefined;
6
7
  items?: NavbarItem[];
7
8
  label?: string;
8
9
  position?: 'left' | 'right';
9
- };
10
+ } & Record<string, unknown>;
10
11
  export declare type NavbarLogo = {
11
12
  src: string;
12
13
  srcDark?: string;
14
+ width?: string | number;
15
+ height?: string | number;
13
16
  href?: string;
14
17
  target?: string;
15
18
  alt?: string;
@@ -62,11 +65,17 @@ export declare type Footer = {
62
65
  alt?: string;
63
66
  src?: string;
64
67
  srcDark?: string;
68
+ width?: string | number;
69
+ height?: string | number;
65
70
  href?: string;
66
71
  };
67
72
  copyright?: string;
68
73
  links: FooterLinks[];
69
74
  };
75
+ export declare type TableOfContents = {
76
+ minHeadingLevel: number;
77
+ maxHeadingLevel: number;
78
+ };
70
79
  export declare type ThemeConfig = {
71
80
  docs: {
72
81
  versionPersistence: DocsVersionPersistence;
@@ -77,8 +86,10 @@ export declare type ThemeConfig = {
77
86
  prism: PrismConfig;
78
87
  footer?: Footer;
79
88
  hideableSidebar: boolean;
80
- image: string;
89
+ image?: string;
81
90
  metadatas: Array<Record<string, string>>;
82
91
  sidebarCollapsible: boolean;
92
+ tableOfContents: TableOfContents;
83
93
  };
94
+ export declare type UserThemeConfig = DeepPartial<ThemeConfig>;
84
95
  export declare function useThemeConfig(): ThemeConfig;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/theme-common",
3
- "version": "2.0.0-beta.8e9b829d9",
3
+ "version": "2.0.0-beta.9",
4
4
  "description": "Common code for Docusaurus themes.",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -18,17 +18,19 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@docusaurus/core": "2.0.0-beta.8e9b829d9",
22
- "@docusaurus/plugin-content-blog": "2.0.0-beta.8e9b829d9",
23
- "@docusaurus/plugin-content-docs": "2.0.0-beta.8e9b829d9",
24
- "@docusaurus/plugin-content-pages": "2.0.0-beta.8e9b829d9",
25
- "@docusaurus/types": "2.0.0-beta.8e9b829d9",
21
+ "@docusaurus/core": "2.0.0-beta.9",
22
+ "@docusaurus/plugin-content-blog": "2.0.0-beta.9",
23
+ "@docusaurus/plugin-content-docs": "2.0.0-beta.9",
24
+ "@docusaurus/plugin-content-pages": "2.0.0-beta.9",
25
+ "@docusaurus/types": "2.0.0-beta.9",
26
26
  "clsx": "^1.1.1",
27
27
  "fs-extra": "^10.0.0",
28
- "tslib": "^2.1.0"
28
+ "tslib": "^2.3.1",
29
+ "utility-types": "^3.10.0"
29
30
  },
30
31
  "devDependencies": {
31
- "@docusaurus/module-type-aliases": "2.0.0-beta.8e9b829d9"
32
+ "@docusaurus/module-type-aliases": "2.0.0-beta.9",
33
+ "lodash": "^4.17.20"
32
34
  },
33
35
  "peerDependencies": {
34
36
  "prism-react-renderer": "^1.2.1",
@@ -36,7 +38,7 @@
36
38
  "react-dom": "^16.8.4 || ^17.0.0"
37
39
  },
38
40
  "engines": {
39
- "node": ">=12.13.0"
41
+ "node": ">=14"
40
42
  },
41
- "gitHead": "cb79fda03ac12835068842a7e0a86f610974eae2"
43
+ "gitHead": "8a491fc29ad002f90e97f5b5fe4178ac8fa0c4d7"
42
44
  }
@@ -120,7 +120,7 @@ function useCollapseAnimation({
120
120
 
121
121
  el.style.willChange = 'height';
122
122
 
123
- function startAnimation(): () => void {
123
+ function startAnimation() {
124
124
  const animationFrame = requestAnimationFrame(() => {
125
125
  // When collapsing
126
126
  if (collapsed) {
@@ -182,35 +182,23 @@ function CollapsibleBase({
182
182
  disableSSRStyle,
183
183
  }: CollapsibleBaseProps) {
184
184
  // any because TS is a pain for HTML element refs, see https://twitter.com/sebastienlorber/status/1412784677795110914
185
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
185
186
  const collapsibleRef = useRef<any>(null);
186
187
 
187
188
  useCollapseAnimation({collapsibleRef, collapsed, animation});
188
189
 
189
190
  return (
190
191
  <As
191
- // @ts-expect-error: see https://twitter.com/sebastienlorber/status/1412784677795110914
192
+ // @ts-expect-error: the "too complicated type" is produced from "CollapsibleElementType" being a huge union
192
193
  ref={collapsibleRef}
193
194
  style={disableSSRStyle ? undefined : getSSRStyle(collapsed)}
194
- onTransitionEnd={(e) => {
195
+ onTransitionEnd={(e: React.TransitionEvent) => {
195
196
  if (e.propertyName !== 'height') {
196
197
  return;
197
198
  }
198
199
 
199
- const el = collapsibleRef.current!;
200
- const currentCollapsibleElementHeight = el.style.height;
201
-
202
- if (
203
- !collapsed &&
204
- parseInt(currentCollapsibleElementHeight, 10) === el.scrollHeight
205
- ) {
206
- applyCollapsedStyle(el, false);
207
- onCollapseTransitionEnd?.(false);
208
- }
209
-
210
- if (currentCollapsibleElementHeight === CollapsedStyles.height) {
211
- applyCollapsedStyle(el, true);
212
- onCollapseTransitionEnd?.(true);
213
- }
200
+ applyCollapsedStyle(collapsibleRef.current!, collapsed);
201
+ onCollapseTransitionEnd?.(collapsed);
214
202
  }}
215
203
  className={className}>
216
204
  {children}
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import React, {ComponentProps, ReactElement, useRef, useState} from 'react';
9
- import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
9
+ import useIsBrowser from '@docusaurus/useIsBrowser';
10
10
  import clsx from 'clsx';
11
11
  import {useCollapsible, Collapsible} from '../Collapsible';
12
12
  import styles from './styles.module.css';
@@ -30,7 +30,7 @@ export type DetailsProps = {
30
30
  } & ComponentProps<'details'>;
31
31
 
32
32
  const Details = ({summary, children, ...props}: DetailsProps): JSX.Element => {
33
- const {isClient} = useDocusaurusContext();
33
+ const isBrowser = useIsBrowser();
34
34
  const detailsRef = useRef<HTMLDetailsElement>(null);
35
35
 
36
36
  const {collapsed, setCollapsed} = useCollapsible({
@@ -48,7 +48,7 @@ const Details = ({summary, children, ...props}: DetailsProps): JSX.Element => {
48
48
  data-collapsed={collapsed}
49
49
  className={clsx(
50
50
  styles.details,
51
- {[styles.isClient]: isClient},
51
+ {[styles.isBrowser]: isBrowser},
52
52
  props.className,
53
53
  )}
54
54
  onMouseDown={(e) => {
@@ -18,9 +18,10 @@ CSS variables, meant to be overriden by final theme
18
18
  position: relative;
19
19
  cursor: pointer;
20
20
  list-style: none;
21
- margin-left: 1.8rem;
21
+ padding-left: 1rem;
22
22
  }
23
23
 
24
+ /* TODO: deprecation, need to remove this after Safari will support `::marker` */
24
25
  .details > summary::-webkit-details-marker {
25
26
  display: none;
26
27
  }
@@ -28,15 +29,13 @@ CSS variables, meant to be overriden by final theme
28
29
  .details > summary:before {
29
30
  position: absolute;
30
31
  top: 0.45rem;
31
- left: -1.2rem;
32
+ left: 0;
32
33
 
33
34
  /* CSS-only Arrow */
34
35
  content: '';
35
- width: 0;
36
- height: 0;
37
- border-top: var(--docusaurus-details-summary-arrow-size) solid transparent;
38
- border-bottom: var(--docusaurus-details-summary-arrow-size) solid transparent;
39
- border-left: var(--docusaurus-details-summary-arrow-size) solid
36
+ border-width: var(--docusaurus-details-summary-arrow-size);
37
+ border-style: solid;
38
+ border-color: transparent transparent transparent
40
39
  var(--docusaurus-details-decoration-color);
41
40
 
42
41
  /* Arrow rotation anim */
@@ -46,9 +45,9 @@ CSS variables, meant to be overriden by final theme
46
45
  }
47
46
 
48
47
  /* When JS disabled/failed to load: we use the open property for arrow animation: */
49
- .details[open]:not(.isClient) > summary:before,
48
+ .details[open]:not(.isBrowser) > summary:before,
50
49
  /* When JS works: we use the data-attribute for arrow animation */
51
- .details[data-collapsed='false'].isClient > summary:before {
50
+ .details[data-collapsed='false'].isBrowser > summary:before {
52
51
  transform: rotate(90deg);
53
52
  }
54
53
 
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ export {useThemeConfig} from './utils/useThemeConfig';
9
9
 
10
10
  export type {
11
11
  ThemeConfig,
12
+ UserThemeConfig,
12
13
  Navbar,
13
14
  NavbarItem,
14
15
  NavbarLogo,
@@ -58,6 +59,8 @@ export {
58
59
  useDocsPreferredVersionByPluginId,
59
60
  } from './utils/docsPreferredVersion/useDocsPreferredVersion';
60
61
 
62
+ export {duplicates} from './utils/jsUtils';
63
+
61
64
  export {DocsPreferredVersionContextProvider} from './utils/docsPreferredVersion/DocsPreferredVersionProvider';
62
65
 
63
66
  export {ThemeClassNames} from './utils/ThemeClassNames';
@@ -68,3 +71,27 @@ export {
68
71
  } from './utils/announcementBarUtils';
69
72
 
70
73
  export {useLocalPathname} from './utils/useLocalPathname';
74
+
75
+ export {translateTagsPageTitle, listTagsByLetters} from './utils/tagsUtils';
76
+ export type {TagLetterEntry} from './utils/tagsUtils';
77
+
78
+ export {useHistoryPopHandler} from './utils/historyUtils';
79
+
80
+ export {default as useTOCHighlight} from './utils/useTOCHighlight';
81
+ export type {TOCHighlightConfig} from './utils/useTOCHighlight';
82
+
83
+ export {useTOCFilter} from './utils/tocUtils';
84
+
85
+ export {
86
+ ScrollControllerProvider,
87
+ useScrollController,
88
+ useScrollPosition,
89
+ useScrollPositionBlocker,
90
+ } from './utils/scrollUtils';
91
+
92
+ export {
93
+ useIsomorphicLayoutEffect,
94
+ useDynamicCallback,
95
+ } from './utils/reactUtils';
96
+
97
+ export {isRegexpStringMatch} from './utils/regexpUtils';
package/src/types.d.ts CHANGED
@@ -5,8 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- /* eslint-disable import/no-duplicates */
9
- /* eslint-disable spaced-comment */
10
8
  /// <reference types="@docusaurus/module-type-aliases" />
11
9
  /// <reference types="@docusaurus/plugin-content-blog" />
12
10
  /// <reference types="@docusaurus/plugin-content-docs" />
@@ -6,19 +6,57 @@
6
6
  */
7
7
 
8
8
  // These class names are used to style page layouts in Docusaurus
9
+ // Those are meant to be targeted by user-provided custom CSS selectors
10
+ // /!\ Please do not modify the classnames! This is a breaking change, and annoying for users!
9
11
  export const ThemeClassNames = {
10
12
  page: {
11
13
  blogListPage: 'blog-list-page',
12
14
  blogPostPage: 'blog-post-page',
13
15
  blogTagsListPage: 'blog-tags-list-page',
14
- blogTagsPostPage: 'blog-tags-post-page',
15
- docPage: 'doc-page',
16
+ blogTagPostListPage: 'blog-tags-post-list-page',
17
+
18
+ docsDocPage: 'docs-doc-page',
19
+ docsTagsListPage: 'docs-tags-list-page', // List of tags
20
+ docsTagDocListPage: 'docs-tags-doc-list-page', // Docs for a tag
21
+
16
22
  mdxPage: 'mdx-page',
17
23
  },
18
24
  wrapper: {
19
25
  main: 'main-wrapper',
20
26
  blogPages: 'blog-wrapper',
21
- docPages: 'docs-wrapper',
27
+ docsPages: 'docs-wrapper',
22
28
  mdxPages: 'mdx-wrapper',
23
29
  },
24
- };
30
+
31
+ // /!\ Please keep the naming convention consistent!
32
+ // Something like: "theme-{blog,doc,version,page}?-<suffix>"
33
+ common: {
34
+ editThisPage: 'theme-edit-this-page',
35
+ lastUpdated: 'theme-last-updated',
36
+ backToTopButton: 'theme-back-to-top-button',
37
+ },
38
+ layout: {
39
+ // TODO add other stable classNames here
40
+ },
41
+ docs: {
42
+ docVersionBanner: 'theme-doc-version-banner',
43
+ docVersionBadge: 'theme-doc-version-badge',
44
+ docMarkdown: 'theme-doc-markdown',
45
+ docTocMobile: 'theme-doc-toc-mobile',
46
+ docTocDesktop: 'theme-doc-toc-desktop',
47
+ docFooter: 'theme-doc-footer',
48
+ docFooterTagsRow: 'theme-doc-footer-tags-row',
49
+ docFooterEditMetaRow: 'theme-doc-footer-edit-meta-row',
50
+ docSidebarMenu: 'theme-doc-sidebar-menu',
51
+ docSidebarItemCategory: 'theme-doc-sidebar-item-category',
52
+ docSidebarItemLink: 'theme-doc-sidebar-item-link',
53
+ docSidebarItemCategoryLevel: (level: number) =>
54
+ `theme-doc-sidebar-item-category-level-${level}` as const,
55
+ docSidebarItemLinkLevel: (level: number) =>
56
+ `theme-doc-sidebar-item-link-level-${level}` as const,
57
+ // TODO add other stable classNames here
58
+ },
59
+ blog: {
60
+ // TODO add other stable classNames here
61
+ },
62
+ } as const;
@@ -0,0 +1,66 @@
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 {shuffle} from 'lodash';
9
+ import {listTagsByLetters} from '../tagsUtils';
10
+
11
+ describe('listTagsByLetters', () => {
12
+ type Param = Parameters<typeof listTagsByLetters>[0];
13
+ type Tag = Param[number];
14
+ type Result = ReturnType<typeof listTagsByLetters>;
15
+
16
+ test('Should create letters list', () => {
17
+ const tag1: Tag = {
18
+ name: 'tag1',
19
+ permalink: '/tag1',
20
+ count: 1,
21
+ };
22
+ const tag2: Tag = {
23
+ name: 'Tag2',
24
+ permalink: '/tag2',
25
+ count: 11,
26
+ };
27
+ const tagzxy: Tag = {
28
+ name: 'zxy',
29
+ permalink: '/zxy',
30
+ count: 987,
31
+ };
32
+ const tagAbc: Tag = {
33
+ name: 'Abc',
34
+ permalink: '/abc',
35
+ count: 123,
36
+ };
37
+ const tagdef: Tag = {
38
+ name: 'def',
39
+ permalink: '/def',
40
+ count: 1,
41
+ };
42
+ const tagaaa: Tag = {
43
+ name: 'aaa',
44
+ permalink: '/aaa',
45
+ count: 10,
46
+ };
47
+
48
+ const expectedResult: Result = [
49
+ {letter: 'A', tags: [tagaaa, tagAbc]},
50
+ {letter: 'D', tags: [tagdef]},
51
+ {letter: 'T', tags: [tag1, tag2]},
52
+ {letter: 'Z', tags: [tagzxy]},
53
+ ];
54
+
55
+ // Input order shouldn't matter, output is always consistently sorted
56
+ expect(
57
+ listTagsByLetters([tag1, tag2, tagzxy, tagAbc, tagdef, tagaaa]),
58
+ ).toEqual(expectedResult);
59
+ expect(
60
+ listTagsByLetters([tagzxy, tagdef, tagaaa, tag2, tagAbc, tag1]),
61
+ ).toEqual(expectedResult);
62
+ expect(
63
+ listTagsByLetters(shuffle([tagzxy, tagdef, tagaaa, tag2, tagAbc, tag1])),
64
+ ).toEqual(expectedResult);
65
+ });
66
+ });