@docusaurus/core 0.0.0-4731 → 0.0.0-4734

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.
package/bin/beforeCli.mjs CHANGED
@@ -72,8 +72,7 @@ export default async function beforeCli() {
72
72
  * @param {import('update-notifier').UpdateInfo} update
73
73
  */
74
74
  function ignoreUpdate(update) {
75
- const isCanaryRelease =
76
- update && update.current && update.current.startsWith('0.0.0');
75
+ const isCanaryRelease = update?.current?.startsWith('0.0.0');
77
76
  return isCanaryRelease;
78
77
  }
79
78
 
@@ -10,12 +10,20 @@ import Loading from '@theme/Loading';
10
10
  import routesChunkNames from '@generated/routesChunkNames';
11
11
  import registry from '@generated/registry';
12
12
  import flat from '../flat';
13
+ import { RouteContextProvider } from '../routeContext';
13
14
  export default function ComponentCreator(path, hash) {
14
15
  // 404 page
15
16
  if (path === '*') {
16
17
  return Loadable({
17
18
  loading: Loading,
18
- loader: () => import('@theme/NotFound'),
19
+ loader: async () => {
20
+ const NotFound = (await import('@theme/NotFound')).default;
21
+ return (props) => (
22
+ // Is there a better API for this?
23
+ <RouteContextProvider value={{ plugin: { name: 'native', id: 'default' } }}>
24
+ <NotFound {...props}/>
25
+ </RouteContextProvider>);
26
+ },
19
27
  });
20
28
  }
21
29
  const chunkNamesKey = `${path}-${hash}`;
@@ -43,8 +51,7 @@ export default function ComponentCreator(path, hash) {
43
51
  if (chunkRegistry) {
44
52
  // eslint-disable-next-line prefer-destructuring
45
53
  optsLoader[key] = chunkRegistry[0];
46
- optsModules.push(chunkRegistry[1]);
47
- optsWebpack.push(chunkRegistry[2]);
54
+ optsModules.push(chunkRegistry[1], chunkRegistry[2]);
48
55
  }
49
56
  });
50
57
  return Loadable.Map({
@@ -63,7 +70,7 @@ export default function ComponentCreator(path, hash) {
63
70
  });
64
71
  val[keyPath[keyPath.length - 1]] = loaded[key].default;
65
72
  const nonDefaultKeys = Object.keys(loaded[key]).filter((k) => k !== 'default');
66
- if (nonDefaultKeys && nonDefaultKeys.length) {
73
+ if (nonDefaultKeys?.length) {
67
74
  nonDefaultKeys.forEach((nonDefaultKey) => {
68
75
  val[keyPath[keyPath.length - 1]][nonDefaultKey] =
69
76
  loaded[key][nonDefaultKey];
@@ -72,7 +79,14 @@ export default function ComponentCreator(path, hash) {
72
79
  });
73
80
  const Component = loadedModules.component;
74
81
  delete loadedModules.component;
75
- return <Component {...loadedModules} {...props}/>;
82
+ /* eslint-disable no-underscore-dangle */
83
+ const routeContextModule = loadedModules.__routeContextModule;
84
+ delete loadedModules.__routeContextModule;
85
+ /* eslint-enable no-underscore-dangle */
86
+ // Is there any way to put this RouteContextProvider upper in the tree?
87
+ return (<RouteContextProvider value={routeContextModule}>
88
+ <Component {...loadedModules} {...props}/>;
89
+ </RouteContextProvider>);
76
90
  },
77
91
  });
78
92
  }
@@ -0,0 +1,8 @@
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 type { PluginRouteContext } from '@docusaurus/types';
8
+ export default function useRouteContext(): PluginRouteContext;
@@ -0,0 +1,15 @@
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 React from 'react';
8
+ import { Context } from '../routeContext';
9
+ export default function useRouteContext() {
10
+ const context = React.useContext(Context);
11
+ if (!context) {
12
+ throw new Error('Unexpected: no Docusaurus parent/current route context found');
13
+ }
14
+ return context;
15
+ }
@@ -0,0 +1,13 @@
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 React, { type ReactNode } from 'react';
8
+ import type { PluginRouteContext } from '@docusaurus/types';
9
+ export declare const Context: React.Context<PluginRouteContext | null>;
10
+ export declare function RouteContextProvider({ children, value, }: {
11
+ children: ReactNode;
12
+ value: PluginRouteContext | null;
13
+ }): JSX.Element;
@@ -0,0 +1,31 @@
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 React, { useMemo } from 'react';
8
+ export const Context = React.createContext(null);
9
+ function mergeContexts({ parent, value, }) {
10
+ if (!parent) {
11
+ if (!value) {
12
+ throw new Error('Unexpected: no Docusaurus parent/current route context found');
13
+ }
14
+ else if (!('plugin' in value)) {
15
+ throw new Error('Unexpected: Docusaurus parent route context has no plugin attribute');
16
+ }
17
+ return value;
18
+ }
19
+ // TODO deep merge this
20
+ const data = { ...parent.data, ...value?.data };
21
+ return {
22
+ // nested routes are not supposed to override plugin attribute
23
+ plugin: parent.plugin,
24
+ data,
25
+ };
26
+ }
27
+ export function RouteContextProvider({ children, value, }) {
28
+ const parent = React.useContext(Context);
29
+ const mergedValue = useMemo(() => mergeContexts({ parent, value }), [parent, value]);
30
+ return <Context.Provider value={mergedValue}>{children}</Context.Provider>;
31
+ }
@@ -31,8 +31,7 @@ This component is safe to swizzle and was designed for this purpose.
31
31
  The swizzled component is retro-compatible with minor version upgrades.
32
32
  `,
33
33
  ],
34
- });
35
- table.push({
34
+ }, {
36
35
  [tableStatusLabel('unsafe')]: [
37
36
  logger_1.default.code('--danger'),
38
37
  `
@@ -44,8 +43,7 @@ ${logger_1.default.green('Tip')}: your customization can't be done in a ${tableS
44
43
  Report it here: https://github.com/facebook/docusaurus/discussions/5468
45
44
  `,
46
45
  ],
47
- });
48
- table.push({
46
+ }, {
49
47
  [tableStatusLabel('forbidden')]: [
50
48
  '',
51
49
  `
@@ -69,8 +67,7 @@ Allows rendering other components before/after the original theme component.
69
67
  ${logger_1.default.green('Tip')}: prefer ${logger_1.default.code('--wrap')} whenever possible to reduce the amount of code to maintain.
70
68
  `,
71
69
  ],
72
- });
73
- table.push({
70
+ }, {
74
71
  [logger_1.default.bold('Eject')]: [
75
72
  logger_1.default.code('--eject'),
76
73
  `
@@ -122,8 +122,7 @@ async function filterExistingFileLinks({ baseUrl, outDir, allCollectedLinks, })
122
122
  // -> /outDir/javadoc/index.html
123
123
  const filePathsToTry = [baseFilePath];
124
124
  if (!path_1.default.extname(baseFilePath)) {
125
- filePathsToTry.push(`${baseFilePath}.html`);
126
- filePathsToTry.push(path_1.default.join(baseFilePath, 'index.html'));
125
+ filePathsToTry.push(`${baseFilePath}.html`, path_1.default.join(baseFilePath, 'index.html'));
127
126
  }
128
127
  for (const file of filePathsToTry) {
129
128
  if (await isExistingFile(file)) {
@@ -184,7 +184,7 @@ function createMDXFallbackPlugin({ siteDir, siteConfig, }) {
184
184
  options: {
185
185
  staticDirs: siteConfig.staticDirectories.map((dir) => path_1.default.resolve(siteDir, dir)),
186
186
  siteDir,
187
- isMDXPartial: (_filename) => true,
187
+ isMDXPartial: () => true,
188
188
  isMDXPartialFrontMatterWarningDisabled: true,
189
189
  remarkPlugins: [remark_admonitions_1.default],
190
190
  },
@@ -222,8 +222,7 @@ next build. You can clear all build artifacts (including this folder) with the
222
222
  */
223
223
  export default ${JSON.stringify(siteConfig, null, 2)};
224
224
  `);
225
- plugins.push(createBootstrapPlugin({ siteDir, siteConfig }));
226
- plugins.push(createMDXFallbackPlugin({ siteDir, siteConfig }));
225
+ plugins.push(createBootstrapPlugin({ siteDir, siteConfig }), createMDXFallbackPlugin({ siteDir, siteConfig }));
227
226
  // Load client modules.
228
227
  const clientModules = (0, client_modules_1.default)(plugins);
229
228
  const genClientModules = (0, utils_1.generate)(generatedFilesDir, 'client-modules.js', `export default [
@@ -96,19 +96,32 @@ async function loadPlugins({ pluginConfigs, context, }) {
96
96
  // plugins data files are namespaced by pluginName/pluginId
97
97
  const dataDirRoot = path_1.default.join(context.generatedFilesDir, plugin.name);
98
98
  const dataDir = path_1.default.join(dataDirRoot, pluginId);
99
+ const createData = async (name, data) => {
100
+ const modulePath = path_1.default.join(dataDir, name);
101
+ await fs_extra_1.default.ensureDir(path_1.default.dirname(modulePath));
102
+ await (0, utils_1.generate)(dataDir, name, data);
103
+ return modulePath;
104
+ };
105
+ // TODO this would be better to do all that in the codegen phase
106
+ // TODO handle context for nested routes
107
+ const pluginRouteContext = {
108
+ plugin: { name: plugin.name, id: pluginId },
109
+ data: undefined, // TODO allow plugins to provide context data
110
+ };
111
+ const pluginRouteContextModulePath = await createData(`${(0, utils_1.docuHash)('pluginRouteContextModule')}.json`, JSON.stringify(pluginRouteContext, null, 2));
99
112
  const addRoute = (initialRouteConfig) => {
100
113
  // Trailing slash behavior is handled in a generic way for all plugins
101
114
  const finalRouteConfig = (0, applyRouteTrailingSlash_1.default)(initialRouteConfig, {
102
115
  trailingSlash: context.siteConfig.trailingSlash,
103
116
  baseUrl: context.siteConfig.baseUrl,
104
117
  });
105
- pluginsRouteConfigs.push(finalRouteConfig);
106
- };
107
- const createData = async (name, data) => {
108
- const modulePath = path_1.default.join(dataDir, name);
109
- await fs_extra_1.default.ensureDir(path_1.default.dirname(modulePath));
110
- await (0, utils_1.generate)(dataDir, name, data);
111
- return modulePath;
118
+ pluginsRouteConfigs.push({
119
+ ...finalRouteConfig,
120
+ modules: {
121
+ ...finalRouteConfig.modules,
122
+ __routeContextModule: pluginRouteContextModulePath,
123
+ },
124
+ });
112
125
  };
113
126
  // the plugins global data are namespaced to avoid data conflicts:
114
127
  // - by plugin name
@@ -203,9 +203,8 @@ ${sourceWarningPart(path.node)}`);
203
203
  .filter((children) => !(children.isJSXText() &&
204
204
  children.node.value.replace('\n', '').trim() === ''))
205
205
  .pop();
206
- const isJSXText = singleChildren && singleChildren.isJSXText();
207
- const isJSXExpressionContainer = singleChildren &&
208
- singleChildren.isJSXExpressionContainer() &&
206
+ const isJSXText = singleChildren?.isJSXText();
207
+ const isJSXExpressionContainer = singleChildren?.isJSXExpressionContainer() &&
209
208
  singleChildren.get('expression').evaluate().confident;
210
209
  if (isJSXText || isJSXExpressionContainer) {
211
210
  message = isJSXText
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@docusaurus/core",
3
3
  "description": "Easy to Maintain Open Source Documentation Websites",
4
- "version": "0.0.0-4731",
4
+ "version": "0.0.0-4734",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -41,13 +41,13 @@
41
41
  "@babel/runtime": "^7.17.7",
42
42
  "@babel/runtime-corejs3": "^7.17.7",
43
43
  "@babel/traverse": "^7.17.3",
44
- "@docusaurus/cssnano-preset": "0.0.0-4731",
45
- "@docusaurus/logger": "0.0.0-4731",
46
- "@docusaurus/mdx-loader": "0.0.0-4731",
44
+ "@docusaurus/cssnano-preset": "0.0.0-4734",
45
+ "@docusaurus/logger": "0.0.0-4734",
46
+ "@docusaurus/mdx-loader": "0.0.0-4734",
47
47
  "@docusaurus/react-loadable": "5.5.2",
48
- "@docusaurus/utils": "0.0.0-4731",
49
- "@docusaurus/utils-common": "0.0.0-4731",
50
- "@docusaurus/utils-validation": "0.0.0-4731",
48
+ "@docusaurus/utils": "0.0.0-4734",
49
+ "@docusaurus/utils-common": "0.0.0-4734",
50
+ "@docusaurus/utils-validation": "0.0.0-4734",
51
51
  "@slorber/static-site-generator-webpack-plugin": "^4.0.1",
52
52
  "@svgr/webpack": "^6.2.1",
53
53
  "autoprefixer": "^10.4.2",
@@ -106,8 +106,8 @@
106
106
  "webpackbar": "^5.0.2"
107
107
  },
108
108
  "devDependencies": {
109
- "@docusaurus/module-type-aliases": "0.0.0-4731",
110
- "@docusaurus/types": "0.0.0-4731",
109
+ "@docusaurus/module-type-aliases": "0.0.0-4734",
110
+ "@docusaurus/types": "0.0.0-4734",
111
111
  "@types/detect-port": "^1.3.2",
112
112
  "@types/nprogress": "^0.2.0",
113
113
  "@types/react-dom": "^17.0.13",
@@ -128,5 +128,5 @@
128
128
  "engines": {
129
129
  "node": ">=14"
130
130
  },
131
- "gitHead": "f3787c5cd81d0d934005814e3038d897f5ba58aa"
131
+ "gitHead": "0ca49e19e1c96a7d4a0f2bb72ccfca4b7a3eb9bb"
132
132
  }