@docusaurus/theme-common 2.0.0-beta.18 → 2.0.0-beta.19

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 (128) hide show
  1. package/Details.d.ts +14 -0
  2. package/lib/components/Collapsible/index.js +2 -2
  3. package/lib/components/Collapsible/index.js.map +1 -1
  4. package/lib/components/Details/index.d.ts.map +1 -1
  5. package/lib/components/Details/index.js +3 -2
  6. package/lib/components/Details/index.js.map +1 -1
  7. package/lib/components/Details/styles.module.css +4 -0
  8. package/lib/contexts/announcementBar.js +1 -1
  9. package/lib/contexts/colorMode.d.ts.map +1 -1
  10. package/lib/contexts/colorMode.js +34 -16
  11. package/lib/contexts/colorMode.js.map +1 -1
  12. package/lib/contexts/docsPreferredVersion.d.ts +5 -2
  13. package/lib/contexts/docsPreferredVersion.d.ts.map +1 -1
  14. package/lib/contexts/docsPreferredVersion.js +5 -2
  15. package/lib/contexts/docsPreferredVersion.js.map +1 -1
  16. package/lib/contexts/docsSidebar.d.ts +9 -3
  17. package/lib/contexts/docsSidebar.d.ts.map +1 -1
  18. package/lib/contexts/docsSidebar.js +7 -6
  19. package/lib/contexts/docsSidebar.js.map +1 -1
  20. package/lib/contexts/navbarMobileSidebar.js +5 -5
  21. package/lib/contexts/navbarMobileSidebar.js.map +1 -1
  22. package/lib/contexts/{navbarSecondaryMenu.d.ts → navbarSecondaryMenu/content.d.ts} +13 -14
  23. package/lib/contexts/navbarSecondaryMenu/content.d.ts.map +1 -0
  24. package/lib/contexts/navbarSecondaryMenu/content.js +56 -0
  25. package/lib/contexts/navbarSecondaryMenu/content.js.map +1 -0
  26. package/lib/contexts/navbarSecondaryMenu/display.d.ts +24 -0
  27. package/lib/contexts/navbarSecondaryMenu/display.d.ts.map +1 -0
  28. package/lib/contexts/navbarSecondaryMenu/display.js +62 -0
  29. package/lib/contexts/navbarSecondaryMenu/display.js.map +1 -0
  30. package/lib/contexts/tabGroupChoice.d.ts.map +1 -1
  31. package/lib/contexts/tabGroupChoice.js.map +1 -1
  32. package/lib/hooks/useBackToTopButton.d.ts +27 -0
  33. package/lib/hooks/useBackToTopButton.d.ts.map +1 -0
  34. package/lib/hooks/useBackToTopButton.js +50 -0
  35. package/lib/hooks/useBackToTopButton.js.map +1 -0
  36. package/lib/hooks/useCodeWordWrap.d.ts +14 -0
  37. package/lib/hooks/useCodeWordWrap.d.ts.map +1 -0
  38. package/lib/hooks/useCodeWordWrap.js +41 -0
  39. package/lib/hooks/useCodeWordWrap.js.map +1 -0
  40. package/lib/hooks/useHideableNavbar.d.ts.map +1 -1
  41. package/lib/hooks/useHideableNavbar.js +1 -2
  42. package/lib/hooks/useHideableNavbar.js.map +1 -1
  43. package/lib/hooks/usePrismTheme.d.ts +2 -2
  44. package/lib/hooks/usePrismTheme.d.ts.map +1 -1
  45. package/lib/hooks/usePrismTheme.js +1 -2
  46. package/lib/hooks/usePrismTheme.js.map +1 -1
  47. package/lib/hooks/useSkipToContent.d.ts +25 -0
  48. package/lib/hooks/useSkipToContent.d.ts.map +1 -0
  49. package/lib/hooks/useSkipToContent.js +35 -0
  50. package/lib/hooks/useSkipToContent.js.map +1 -0
  51. package/lib/hooks/useTOCHighlight.js +3 -3
  52. package/lib/hooks/useTOCHighlight.js.map +1 -1
  53. package/lib/index.d.ts +9 -6
  54. package/lib/index.d.ts.map +1 -1
  55. package/lib/index.js +8 -5
  56. package/lib/index.js.map +1 -1
  57. package/lib/utils/codeBlockUtils.d.ts +42 -12
  58. package/lib/utils/codeBlockUtils.d.ts.map +1 -1
  59. package/lib/utils/codeBlockUtils.js +89 -58
  60. package/lib/utils/codeBlockUtils.js.map +1 -1
  61. package/lib/utils/docsUtils.d.ts +58 -0
  62. package/lib/utils/docsUtils.d.ts.map +1 -1
  63. package/lib/utils/docsUtils.js +109 -10
  64. package/lib/utils/docsUtils.js.map +1 -1
  65. package/lib/utils/metadataUtils.d.ts +2 -2
  66. package/lib/utils/metadataUtils.d.ts.map +1 -1
  67. package/lib/utils/navbarUtils.d.ts +2 -2
  68. package/lib/utils/navbarUtils.d.ts.map +1 -1
  69. package/lib/utils/navbarUtils.js +7 -5
  70. package/lib/utils/navbarUtils.js.map +1 -1
  71. package/lib/utils/routesUtils.d.ts +4 -4
  72. package/lib/utils/routesUtils.d.ts.map +1 -1
  73. package/lib/utils/routesUtils.js.map +1 -1
  74. package/lib/utils/scrollUtils.d.ts +24 -0
  75. package/lib/utils/scrollUtils.d.ts.map +1 -1
  76. package/lib/utils/scrollUtils.js +52 -0
  77. package/lib/utils/scrollUtils.js.map +1 -1
  78. package/lib/utils/searchUtils.d.ts.map +1 -1
  79. package/lib/utils/searchUtils.js +2 -0
  80. package/lib/utils/searchUtils.js.map +1 -1
  81. package/lib/utils/storageUtils.d.ts +2 -2
  82. package/lib/utils/storageUtils.d.ts.map +1 -1
  83. package/lib/utils/tagsUtils.d.ts +3 -7
  84. package/lib/utils/tagsUtils.d.ts.map +1 -1
  85. package/lib/utils/tagsUtils.js +2 -2
  86. package/lib/utils/tagsUtils.js.map +1 -1
  87. package/lib/utils/tocUtils.d.ts +2 -2
  88. package/lib/utils/tocUtils.d.ts.map +1 -1
  89. package/lib/utils/tocUtils.js +4 -4
  90. package/lib/utils/tocUtils.js.map +1 -1
  91. package/lib/utils/useThemeConfig.d.ts +7 -4
  92. package/lib/utils/useThemeConfig.d.ts.map +1 -1
  93. package/lib/utils/useThemeConfig.js.map +1 -1
  94. package/package.json +18 -10
  95. package/src/components/Collapsible/index.tsx +2 -2
  96. package/src/components/Details/index.tsx +4 -2
  97. package/src/components/Details/styles.module.css +4 -0
  98. package/src/contexts/announcementBar.tsx +1 -1
  99. package/src/contexts/colorMode.tsx +38 -15
  100. package/src/contexts/docsPreferredVersion.tsx +5 -2
  101. package/src/contexts/docsSidebar.tsx +17 -9
  102. package/src/contexts/navbarMobileSidebar.tsx +5 -5
  103. package/src/contexts/navbarSecondaryMenu/content.tsx +110 -0
  104. package/src/contexts/navbarSecondaryMenu/display.tsx +102 -0
  105. package/src/contexts/tabGroupChoice.tsx +6 -3
  106. package/src/hooks/useBackToTopButton.ts +73 -0
  107. package/src/hooks/useCodeWordWrap.ts +56 -0
  108. package/src/hooks/useHideableNavbar.ts +1 -3
  109. package/src/hooks/usePrismTheme.ts +3 -3
  110. package/src/hooks/useSkipToContent.ts +58 -0
  111. package/src/hooks/useTOCHighlight.ts +3 -3
  112. package/src/index.ts +12 -5
  113. package/src/utils/codeBlockUtils.ts +150 -66
  114. package/src/utils/docsUtils.tsx +163 -9
  115. package/src/utils/metadataUtils.tsx +2 -2
  116. package/src/utils/navbarUtils.tsx +11 -6
  117. package/src/utils/routesUtils.ts +7 -7
  118. package/src/utils/scrollUtils.tsx +74 -0
  119. package/src/utils/searchUtils.ts +2 -0
  120. package/src/utils/storageUtils.ts +2 -2
  121. package/src/utils/tagsUtils.ts +4 -9
  122. package/src/utils/tocUtils.ts +5 -5
  123. package/src/utils/useThemeConfig.ts +7 -4
  124. package/yarn-error.log +20199 -0
  125. package/lib/contexts/navbarSecondaryMenu.d.ts.map +0 -1
  126. package/lib/contexts/navbarSecondaryMenu.js +0 -93
  127. package/lib/contexts/navbarSecondaryMenu.js.map +0 -1
  128. package/src/contexts/navbarSecondaryMenu.tsx +0 -170
@@ -6,64 +6,89 @@
6
6
  */
7
7
 
8
8
  import rangeParser from 'parse-numeric-range';
9
+ import type {PrismTheme} from 'prism-react-renderer';
10
+ import type {CSSProperties} from 'react';
9
11
 
10
12
  const codeBlockTitleRegex = /title=(?<quote>["'])(?<title>.*?)\1/;
11
- const highlightLinesRangeRegex = /\{(?<range>[\d,-]+)\}/;
13
+ const metastringLinesRangeRegex = /\{(?<range>[\d,-]+)\}/;
12
14
 
13
15
  // Supported types of highlight comments
14
16
  const commentPatterns = {
15
17
  js: {start: '\\/\\/', end: ''},
16
18
  jsBlock: {start: '\\/\\*', end: '\\*\\/'},
17
19
  jsx: {start: '\\{\\s*\\/\\*', end: '\\*\\/\\s*\\}'},
18
- python: {start: '#', end: ''},
20
+ bash: {start: '#', end: ''},
19
21
  html: {start: '<!--', end: '-->'},
20
22
  };
21
23
 
22
24
  type CommentType = keyof typeof commentPatterns;
23
25
 
24
- const magicCommentDirectives = [
25
- 'highlight-next-line',
26
- 'highlight-start',
27
- 'highlight-end',
28
- ];
26
+ export type MagicCommentConfig = {
27
+ className: string;
28
+ line?: string;
29
+ block?: {start: string; end: string};
30
+ };
29
31
 
30
- function getCommentPattern(languages: CommentType[]) {
31
- // to be more reliable, the opening and closing comment must match
32
+ function getCommentPattern(
33
+ languages: CommentType[],
34
+ magicCommentDirectives: MagicCommentConfig[],
35
+ ) {
36
+ // To be more reliable, the opening and closing comment must match
32
37
  const commentPattern = languages
33
38
  .map((lang) => {
34
39
  const {start, end} = commentPatterns[lang];
35
- return `(?:${start}\\s*(${magicCommentDirectives.join('|')})\\s*${end})`;
40
+ return `(?:${start}\\s*(${magicCommentDirectives
41
+ .flatMap((d) => [d.line, d.block?.start, d.block?.end].filter(Boolean))
42
+ .join('|')})\\s*${end})`;
36
43
  })
37
44
  .join('|');
38
- // white space is allowed, but otherwise it should be on it's own line
45
+ // White space is allowed, but otherwise it should be on it's own line
39
46
  return new RegExp(`^\\s*(?:${commentPattern})\\s*$`);
40
47
  }
41
48
 
42
49
  /**
43
50
  * Select comment styles based on language
44
51
  */
45
- function getAllMagicCommentDirectiveStyles(lang: string) {
52
+ function getAllMagicCommentDirectiveStyles(
53
+ lang: string,
54
+ magicCommentDirectives: MagicCommentConfig[],
55
+ ) {
46
56
  switch (lang) {
47
57
  case 'js':
48
58
  case 'javascript':
49
59
  case 'ts':
50
60
  case 'typescript':
51
- return getCommentPattern(['js', 'jsBlock']);
61
+ return getCommentPattern(['js', 'jsBlock'], magicCommentDirectives);
52
62
 
53
63
  case 'jsx':
54
64
  case 'tsx':
55
- return getCommentPattern(['js', 'jsBlock', 'jsx']);
65
+ return getCommentPattern(
66
+ ['js', 'jsBlock', 'jsx'],
67
+ magicCommentDirectives,
68
+ );
56
69
 
57
70
  case 'html':
58
- return getCommentPattern(['js', 'jsBlock', 'html']);
71
+ return getCommentPattern(
72
+ ['js', 'jsBlock', 'html'],
73
+ magicCommentDirectives,
74
+ );
59
75
 
60
76
  case 'python':
61
77
  case 'py':
62
- return getCommentPattern(['python']);
78
+ case 'bash':
79
+ return getCommentPattern(['bash'], magicCommentDirectives);
80
+
81
+ case 'markdown':
82
+ case 'md':
83
+ // Text uses HTML, front matter uses bash
84
+ return getCommentPattern(['html', 'jsx', 'bash'], magicCommentDirectives);
63
85
 
64
86
  default:
65
- // all comment types
66
- return getCommentPattern(Object.keys(commentPatterns) as CommentType[]);
87
+ // All comment types
88
+ return getCommentPattern(
89
+ Object.keys(commentPatterns) as CommentType[],
90
+ magicCommentDirectives,
91
+ );
67
92
  }
68
93
  }
69
94
 
@@ -71,6 +96,10 @@ export function parseCodeBlockTitle(metastring?: string): string {
71
96
  return metastring?.match(codeBlockTitleRegex)?.groups!.title ?? '';
72
97
  }
73
98
 
99
+ export function containsLineNumbers(metastring?: string): boolean {
100
+ return metastring?.includes('showLineNumbers') || false;
101
+ }
102
+
74
103
  /**
75
104
  * Gets the language name from the class name (set by MDX).
76
105
  * e.g. `"language-javascript"` => `"javascript"`.
@@ -87,79 +116,134 @@ export function parseLanguage(className: string): string | undefined {
87
116
  * Parses the code content, strips away any magic comments, and returns the
88
117
  * clean content and the highlighted lines marked by the comments or metastring.
89
118
  *
90
- * If the metastring contains highlight range, the `content` will be returned
91
- * as-is without any parsing.
119
+ * If the metastring contains a range, the `content` will be returned as-is
120
+ * without any parsing. The returned `lineClassNames` will be a map from that
121
+ * number range to the first magic comment config entry (which _should_ be for
122
+ * line highlight directives.)
92
123
  *
93
124
  * @param content The raw code with magic comments. Trailing newline will be
94
125
  * trimmed upfront.
95
- * @param metastring The full metastring, as received from MDX. Highlight range
96
- * declared here starts at 1.
97
- * @param language Language of the code block, used to determine which kinds of
98
- * magic comment styles to enable.
126
+ * @param options Options for parsing behavior.
99
127
  */
100
128
  export function parseLines(
101
129
  content: string,
102
- metastring?: string,
103
- language?: string,
130
+ options: {
131
+ /**
132
+ * The full metastring, as received from MDX. Line ranges declared here
133
+ * start at 1.
134
+ */
135
+ metastring: string | undefined;
136
+ /**
137
+ * Language of the code block, used to determine which kinds of magic
138
+ * comment styles to enable.
139
+ */
140
+ language: string | undefined;
141
+ /**
142
+ * Magic comment types that we should try to parse. Each entry would
143
+ * correspond to one class name to apply to each line.
144
+ */
145
+ magicComments: MagicCommentConfig[];
146
+ },
104
147
  ): {
105
148
  /**
106
- * The highlighted lines, 0-indexed. e.g. `[0, 1, 4]` means the 1st, 2nd, and
107
- * 5th lines are highlighted.
149
+ * The highlighted lines, 0-indexed. e.g. `{ 0: ["highlight", "sample"] }`
150
+ * means the 1st line should have `highlight` and `sample` as class names.
108
151
  */
109
- highlightLines: number[];
152
+ lineClassNames: {[lineIndex: number]: string[]};
110
153
  /**
111
- * The clean code without any magic comments (only if highlight range isn't
112
- * present in the metastring).
154
+ * If there's number range declared in the metastring, the code block is
155
+ * returned as-is (no parsing); otherwise, this is the clean code with all
156
+ * magic comments stripped away.
113
157
  */
114
158
  code: string;
115
159
  } {
116
160
  let code = content.replace(/\n$/, '');
161
+ const {language, magicComments, metastring} = options;
117
162
  // Highlighted lines specified in props: don't parse the content
118
- if (metastring && highlightLinesRangeRegex.test(metastring)) {
119
- const highlightLinesRange = metastring.match(highlightLinesRangeRegex)!
120
- .groups!.range!;
121
- const highlightLines = rangeParser(highlightLinesRange)
163
+ if (metastring && metastringLinesRangeRegex.test(metastring)) {
164
+ const linesRange = metastring.match(metastringLinesRangeRegex)!.groups!
165
+ .range!;
166
+ if (magicComments.length === 0) {
167
+ throw new Error(
168
+ `A highlight range has been given in code block's metastring (\`\`\` ${metastring}), but no magic comment config is available. Docusaurus applies the first magic comment entry's className for metastring ranges.`,
169
+ );
170
+ }
171
+ const metastringRangeClassName = magicComments[0]!.className;
172
+ const lines = rangeParser(linesRange)
122
173
  .filter((n) => n > 0)
123
- .map((n) => n - 1);
124
- return {highlightLines, code};
174
+ .map((n) => [n - 1, [metastringRangeClassName]]);
175
+ return {lineClassNames: Object.fromEntries(lines), code};
125
176
  }
126
177
  if (language === undefined) {
127
- return {highlightLines: [], code};
178
+ return {lineClassNames: {}, code};
128
179
  }
129
- const directiveRegex = getAllMagicCommentDirectiveStyles(language);
130
- // go through line by line
180
+ const directiveRegex = getAllMagicCommentDirectiveStyles(
181
+ language,
182
+ magicComments,
183
+ );
184
+ // Go through line by line
131
185
  const lines = code.split('\n');
132
- let highlightBlockStart: number;
133
- let highlightRange = '';
134
- // loop through lines
186
+ const blocks = Object.fromEntries(
187
+ magicComments.map((d) => [d.className, {start: 0, range: ''}]),
188
+ );
189
+ const lineToClassName: {[comment: string]: string} = Object.fromEntries(
190
+ magicComments
191
+ .filter((d) => d.line)
192
+ .map(({className, line}) => [line, className]),
193
+ );
194
+ const blockStartToClassName: {[comment: string]: string} = Object.fromEntries(
195
+ magicComments
196
+ .filter((d) => d.block)
197
+ .map(({className, block}) => [block!.start, className]),
198
+ );
199
+ const blockEndToClassName: {[comment: string]: string} = Object.fromEntries(
200
+ magicComments
201
+ .filter((d) => d.block)
202
+ .map(({className, block}) => [block!.end, className]),
203
+ );
135
204
  for (let lineNumber = 0; lineNumber < lines.length; ) {
136
205
  const line = lines[lineNumber]!;
137
206
  const match = line.match(directiveRegex);
138
- if (match !== null) {
139
- const directive = match.slice(1).find((item) => item !== undefined);
140
- switch (directive) {
141
- case 'highlight-next-line':
142
- highlightRange += `${lineNumber},`;
143
- break;
144
-
145
- case 'highlight-start':
146
- highlightBlockStart = lineNumber;
147
- break;
148
-
149
- case 'highlight-end':
150
- highlightRange += `${highlightBlockStart!}-${lineNumber - 1},`;
151
- break;
152
-
153
- default:
154
- break;
155
- }
156
- lines.splice(lineNumber, 1);
157
- } else {
158
- // lines without directives are unchanged
207
+ if (!match) {
208
+ // Lines without directives are unchanged
159
209
  lineNumber += 1;
210
+ continue;
160
211
  }
212
+ const directive = match.slice(1).find((item) => item !== undefined)!;
213
+ if (lineToClassName[directive]) {
214
+ blocks[lineToClassName[directive]!]!.range += `${lineNumber},`;
215
+ } else if (blockStartToClassName[directive]) {
216
+ blocks[blockStartToClassName[directive]!]!.start = lineNumber;
217
+ } else if (blockEndToClassName[directive]) {
218
+ blocks[blockEndToClassName[directive]!]!.range += `${
219
+ blocks[blockEndToClassName[directive]!]!.start
220
+ }-${lineNumber - 1},`;
221
+ }
222
+ lines.splice(lineNumber, 1);
161
223
  }
162
- const highlightLines = rangeParser(highlightRange);
163
224
  code = lines.join('\n');
164
- return {highlightLines, code};
225
+ const lineClassNames: {[lineIndex: number]: string[]} = {};
226
+ Object.entries(blocks).forEach(([className, {range}]) => {
227
+ rangeParser(range).forEach((l) => {
228
+ lineClassNames[l] ??= [];
229
+ lineClassNames[l]!.push(className);
230
+ });
231
+ });
232
+ return {lineClassNames, code};
233
+ }
234
+
235
+ export function getPrismCssVariables(prismTheme: PrismTheme): CSSProperties {
236
+ const mapping: {[name: keyof PrismTheme['plain']]: string} = {
237
+ color: '--prism-color',
238
+ backgroundColor: '--prism-background-color',
239
+ };
240
+
241
+ const properties: {[key: string]: string} = {};
242
+ Object.entries(prismTheme.plain).forEach(([key, value]) => {
243
+ const varName = mapping[key];
244
+ if (varName && typeof value === 'string') {
245
+ properties[varName] = value;
246
+ }
247
+ });
248
+ return properties;
165
249
  }
@@ -5,9 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
+ import {useMemo} from 'react';
8
9
  import {
9
10
  useAllDocsData,
10
11
  useActivePlugin,
12
+ useActiveDocContext,
13
+ useLatestVersion,
14
+ type GlobalVersion,
15
+ type GlobalSidebar,
16
+ type GlobalDoc,
11
17
  } from '@docusaurus/plugin-content-docs/client';
12
18
  import type {
13
19
  PropSidebar,
@@ -16,10 +22,14 @@ import type {
16
22
  PropVersionDoc,
17
23
  PropSidebarBreadcrumbsItem,
18
24
  } from '@docusaurus/plugin-content-docs';
25
+ import type {Props as DocPageProps} from '@theme/DocPage';
26
+ import {useDocsPreferredVersion} from '../contexts/docsPreferredVersion';
19
27
  import {useDocsVersion} from '../contexts/docsVersion';
20
28
  import {useDocsSidebar} from '../contexts/docsSidebar';
29
+ import {uniq} from './jsUtils';
21
30
  import {isSamePath} from './routesUtils';
22
- import {useLocation} from '@docusaurus/router';
31
+ import {matchPath, useLocation} from '@docusaurus/router';
32
+ import renderRoutes from '@docusaurus/renderRoutes';
23
33
 
24
34
  // TODO not ideal, see also "useDocs"
25
35
  export const isDocsPluginEnabled: boolean = !!useAllDocsData;
@@ -84,13 +94,8 @@ export function findFirstCategoryLink(
84
94
  if (categoryLink) {
85
95
  return categoryLink;
86
96
  }
87
- } else if (subItem.type === 'html') {
88
- // skip
89
- } else {
90
- throw new Error(
91
- `Unexpected category item type for ${JSON.stringify(subItem)}`,
92
- );
93
97
  }
98
+ // Could be "html" items
94
99
  }
95
100
  return undefined;
96
101
  }
@@ -105,7 +110,7 @@ export function useCurrentSidebarCategory(): PropSidebarItemCategory {
105
110
  if (!sidebar) {
106
111
  throw new Error('Unexpected: cant find current sidebar in context');
107
112
  }
108
- const category = findSidebarCategory(sidebar, (item) =>
113
+ const category = findSidebarCategory(sidebar.items, (item) =>
109
114
  isSamePath(item.href, pathname),
110
115
  );
111
116
  if (!category) {
@@ -174,7 +179,156 @@ export function useSidebarBreadcrumbs(): PropSidebarBreadcrumbsItem[] | null {
174
179
  return false;
175
180
  }
176
181
 
177
- extract(sidebar);
182
+ extract(sidebar.items);
178
183
 
179
184
  return breadcrumbs.reverse();
180
185
  }
186
+
187
+ /**
188
+ * "Version candidates" are mostly useful for the layout components, which must
189
+ * be able to work on all pages. For example, if a user has `{ type: "doc",
190
+ * docId: "intro" }` as a navbar item, which version does that refer to? We
191
+ * believe that it could refer to at most three version candidates:
192
+ *
193
+ * 1. The **active version**, the one that the user is currently browsing. See
194
+ * {@link useActiveDocContext}.
195
+ * 2. The **preferred version**, the one that the user last visited. See
196
+ * {@link useDocsPreferredVersion}.
197
+ * 3. The **latest version**, the "default". See {@link useLatestVersion}.
198
+ *
199
+ * @param docsPluginId The plugin ID to get versions from.
200
+ * @returns An array of 1~3 versions with priorities defined above, guaranteed
201
+ * to be unique and non-sparse. Will be memoized, hence stable for deps array.
202
+ */
203
+ export function useDocsVersionCandidates(
204
+ docsPluginId?: string,
205
+ ): [GlobalVersion, ...GlobalVersion[]] {
206
+ const {activeVersion} = useActiveDocContext(docsPluginId);
207
+ const {preferredVersion} = useDocsPreferredVersion(docsPluginId);
208
+ const latestVersion = useLatestVersion(docsPluginId);
209
+ return useMemo(
210
+ () =>
211
+ uniq(
212
+ [activeVersion, preferredVersion, latestVersion].filter(Boolean),
213
+ ) as [GlobalVersion, ...GlobalVersion[]],
214
+ [activeVersion, preferredVersion, latestVersion],
215
+ );
216
+ }
217
+
218
+ /**
219
+ * The layout components, like navbar items, must be able to work on all pages,
220
+ * even on non-doc ones where there's no version context, so a sidebar ID could
221
+ * be ambiguous. This hook would always return a sidebar to be linked to. See
222
+ * also {@link useDocsVersionCandidates} for how this selection is done.
223
+ *
224
+ * @throws This hook throws if a sidebar with said ID is not found.
225
+ */
226
+ export function useLayoutDocsSidebar(
227
+ sidebarId: string,
228
+ docsPluginId?: string,
229
+ ): GlobalSidebar {
230
+ const versions = useDocsVersionCandidates(docsPluginId);
231
+ return useMemo(() => {
232
+ const allSidebars = versions.flatMap((version) =>
233
+ version.sidebars ? Object.entries(version.sidebars) : [],
234
+ );
235
+ const sidebarEntry = allSidebars.find(
236
+ (sidebar) => sidebar[0] === sidebarId,
237
+ );
238
+ if (!sidebarEntry) {
239
+ throw new Error(
240
+ `Can't find any sidebar with id "${sidebarId}" in version${
241
+ versions.length > 1 ? 's' : ''
242
+ } ${versions.map((version) => version.name).join(', ')}".
243
+ Available sidebar ids are:
244
+ - ${Object.keys(allSidebars).join('\n- ')}`,
245
+ );
246
+ }
247
+ return sidebarEntry[1];
248
+ }, [sidebarId, versions]);
249
+ }
250
+
251
+ /**
252
+ * The layout components, like navbar items, must be able to work on all pages,
253
+ * even on non-doc ones where there's no version context, so a doc ID could be
254
+ * ambiguous. This hook would always return a doc to be linked to. See also
255
+ * {@link useDocsVersionCandidates} for how this selection is done.
256
+ *
257
+ * @throws This hook throws if a doc with said ID is not found.
258
+ */
259
+ export function useLayoutDoc(
260
+ docId: string,
261
+ docsPluginId?: string,
262
+ ): GlobalDoc | null {
263
+ const versions = useDocsVersionCandidates(docsPluginId);
264
+ return useMemo(() => {
265
+ const allDocs = versions.flatMap((version) => version.docs);
266
+ const doc = allDocs.find((versionDoc) => versionDoc.id === docId);
267
+ if (!doc) {
268
+ const isDraft = versions
269
+ .flatMap((version) => version.draftIds)
270
+ .includes(docId);
271
+ // Drafts should be silently filtered instead of throwing
272
+ if (isDraft) {
273
+ return null;
274
+ }
275
+ throw new Error(
276
+ `DocNavbarItem: couldn't find any doc with id "${docId}" in version${
277
+ versions.length > 1 ? 's' : ''
278
+ } ${versions.map((version) => version.name).join(', ')}".
279
+ Available doc ids are:
280
+ - ${uniq(allDocs.map((versionDoc) => versionDoc.id)).join('\n- ')}`,
281
+ );
282
+ }
283
+ return doc;
284
+ }, [docId, versions]);
285
+ }
286
+
287
+ // TODO later read version/route directly from context
288
+ /**
289
+ * The docs plugin creates nested routes, with the top-level route providing the
290
+ * version metadata, and the subroutes creating individual doc pages. This hook
291
+ * will match the current location against all known sub-routes.
292
+ *
293
+ * @param props The props received by `@theme/DocPage`
294
+ * @returns The data of the relevant document at the current location, or `null`
295
+ * if no document associated with the current location can be found.
296
+ */
297
+ export function useDocRouteMetadata({
298
+ route,
299
+ versionMetadata,
300
+ }: DocPageProps): null | {
301
+ /** The element that should be rendered at the current location. */
302
+ docElement: JSX.Element;
303
+ /**
304
+ * The name of the sidebar associated with the current doc. `sidebarName` and
305
+ * `sidebarItems` correspond to the value of {@link useDocsSidebar}.
306
+ */
307
+ sidebarName: string | undefined;
308
+ /** The items of the sidebar associated with the current doc. */
309
+ sidebarItems: PropSidebar | undefined;
310
+ } {
311
+ const location = useLocation();
312
+ const docRoutes = route.routes!;
313
+ const currentDocRoute = docRoutes.find((docRoute) =>
314
+ matchPath(location.pathname, docRoute),
315
+ );
316
+ if (!currentDocRoute) {
317
+ return null;
318
+ }
319
+
320
+ // For now, the sidebarName is added as route config: not ideal!
321
+ const sidebarName = currentDocRoute.sidebar;
322
+
323
+ const sidebarItems = sidebarName
324
+ ? versionMetadata.docsSidebars[sidebarName]
325
+ : undefined;
326
+
327
+ const docElement = renderRoutes(docRoutes, {versionMetadata});
328
+
329
+ return {
330
+ docElement,
331
+ sidebarName,
332
+ sidebarItems,
333
+ };
334
+ }
@@ -12,13 +12,13 @@ import useRouteContext from '@docusaurus/useRouteContext';
12
12
  import {useBaseUrlUtils} from '@docusaurus/useBaseUrl';
13
13
  import {useTitleFormatter} from './generalUtils';
14
14
 
15
- interface PageMetadataProps {
15
+ type PageMetadataProps = {
16
16
  readonly title?: string;
17
17
  readonly description?: string;
18
18
  readonly keywords?: readonly string[] | string;
19
19
  readonly image?: string;
20
20
  readonly children?: ReactNode;
21
- }
21
+ };
22
22
 
23
23
  /**
24
24
  * Helper component to manipulate page metadata and override site defaults.
@@ -7,7 +7,8 @@
7
7
 
8
8
  import React, {type ReactNode} from 'react';
9
9
  import {NavbarMobileSidebarProvider} from '../contexts/navbarMobileSidebar';
10
- import {NavbarSecondaryMenuProvider} from '../contexts/navbarSecondaryMenu';
10
+ import {NavbarSecondaryMenuContentProvider} from '../contexts/navbarSecondaryMenu/content';
11
+ import {NavbarSecondaryMenuDisplayProvider} from '../contexts/navbarSecondaryMenu/display';
11
12
 
12
13
  const DefaultNavItemPosition = 'right';
13
14
 
@@ -28,13 +29,17 @@ export function splitNavbarItems<T extends {position?: 'left' | 'right'}>(
28
29
  }
29
30
 
30
31
  /**
31
- * Composes the `NavbarMobileSidebarProvider` and `NavbarSecondaryMenuProvider`.
32
- * Because the latter depends on the former, they can't be re-ordered.
32
+ * Composes multiple navbar state providers that are mutually dependent and
33
+ * hence can't be re-ordered.
33
34
  */
34
35
  export function NavbarProvider({children}: {children: ReactNode}): JSX.Element {
35
36
  return (
36
- <NavbarMobileSidebarProvider>
37
- <NavbarSecondaryMenuProvider>{children}</NavbarSecondaryMenuProvider>
38
- </NavbarMobileSidebarProvider>
37
+ <NavbarSecondaryMenuContentProvider>
38
+ <NavbarMobileSidebarProvider>
39
+ <NavbarSecondaryMenuDisplayProvider>
40
+ {children}
41
+ </NavbarSecondaryMenuDisplayProvider>
42
+ </NavbarMobileSidebarProvider>
43
+ </NavbarSecondaryMenuContentProvider>
39
44
  );
40
45
  }
@@ -8,7 +8,7 @@
8
8
  import {useMemo} from 'react';
9
9
  import generatedRoutes from '@generated/routes';
10
10
  import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
11
- import type {Route} from '@docusaurus/types';
11
+ import type {RouteConfig} from 'react-router-config';
12
12
 
13
13
  /**
14
14
  * Compare the 2 paths, case insensitive and ignoring trailing slash
@@ -34,18 +34,18 @@ export function findHomePageRoute({
34
34
  baseUrl,
35
35
  routes: initialRoutes,
36
36
  }: {
37
- routes: Route[];
37
+ routes: RouteConfig[];
38
38
  baseUrl: string;
39
- }): Route | undefined {
40
- function isHomePageRoute(route: Route): boolean {
39
+ }): RouteConfig | undefined {
40
+ function isHomePageRoute(route: RouteConfig): boolean {
41
41
  return route.path === baseUrl && route.exact === true;
42
42
  }
43
43
 
44
- function isHomeParentRoute(route: Route): boolean {
44
+ function isHomeParentRoute(route: RouteConfig): boolean {
45
45
  return route.path === baseUrl && !route.exact;
46
46
  }
47
47
 
48
- function doFindHomePageRoute(routes: Route[]): Route | undefined {
48
+ function doFindHomePageRoute(routes: RouteConfig[]): RouteConfig | undefined {
49
49
  if (routes.length === 0) {
50
50
  return undefined;
51
51
  }
@@ -66,7 +66,7 @@ export function findHomePageRoute({
66
66
  * Fetches the route that points to "/". Use this instead of the naive "/",
67
67
  * because the homepage may not exist.
68
68
  */
69
- export function useHomePageRoute(): Route | undefined {
69
+ export function useHomePageRoute(): RouteConfig | undefined {
70
70
  const {baseUrl} = useDocusaurusContext().siteConfig;
71
71
  return useMemo(
72
72
  () => findHomePageRoute({routes: generatedRoutes, baseUrl}),