Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): useDocusaurusContext().siteMetadata #3058

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ declare module '@generated/docusaurus.config' {
export default config;
}

declare module '@generated/site-metadata' {
const siteMetadata: any;
export default siteMetadata;
}

declare module '@generated/registry' {
const registry: {
readonly [key: string]: [() => Promise<any>, string, string];
Expand All @@ -37,29 +42,6 @@ declare module '@generated/routesChunkNames' {
export default routesChunkNames;
}

declare module '@generated/site-metadata' {
/**
* - `type: 'package'`, plugin is in a different package.
* - `type: 'project'`, plugin is in the same docusaurus project.
* - `type: 'local'`, none of plugin's ancestor directory contains any package.json.
* - `type: 'synthetic'`, docusaurus generated internal plugin.
*/
export type PluginVersionInformation =
| {readonly type: 'package'; readonly version?: string}
| {readonly type: 'project'}
| {readonly type: 'local'}
| {readonly type: 'synthetic'};

export type DocusaurusSiteMetadata = {
readonly docusaurusVersion: string;
readonly siteVersion?: string;
readonly pluginVersions: Record<string, PluginVersionInformation>;
};

const siteMetadata: DocusaurusSiteMetadata;
export default siteMetadata;
}

declare module '@theme/*';

declare module '@theme-original/*';
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-debug/src/theme/Debug/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import Layout from '@theme/Layout';

import registry from '@generated/registry';
import routes from '@generated/routes';
import siteMetadata from '@generated/site-metadata';

import styles from './styles.module.css';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

function Debug() {
const {siteMetadata} = useDocusaurusContext();
return (
<Layout permalink="__docusaurus/debug" title="Debug">
<main className={styles.Container}>
Expand Down
23 changes: 21 additions & 2 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,28 @@ export interface DocusaurusConfig {
)[];
}

/**
* - `type: 'package'`, plugin is in a different package.
* - `type: 'project'`, plugin is in the same docusaurus project.
* - `type: 'local'`, none of plugin's ancestor directory contains any package.json.
* - `type: 'synthetic'`, docusaurus generated internal plugin.
*/
export type DocusaurusPluginVersionInformation =
| {readonly type: 'package'; readonly version?: string}
| {readonly type: 'project'}
| {readonly type: 'local'}
| {readonly type: 'synthetic'};

export interface DocusaurusSiteMetadata {
readonly docusaurusVersion: string;
readonly siteVersion?: string;
readonly pluginVersions: Record<string, DocusaurusPluginVersionInformation>;
}

export interface DocusaurusContext {
siteConfig?: DocusaurusConfig;
isClient?: boolean;
siteConfig: DocusaurusConfig;
siteMetadata: DocusaurusSiteMetadata;
isClient: boolean;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the ? here, we shouldn't use the hook and retrieve a partial context, we should fail fast instead.


export interface Preset {
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus/src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React, {useEffect, useState} from 'react';

import routes from '@generated/routes';
import siteConfig from '@generated/docusaurus.config';
import siteMetadata from '@generated/site-metadata';
import renderRoutes from './exports/renderRoutes';
import DocusaurusContext from './exports/context';
import PendingNavigation from './PendingNavigation';
Expand All @@ -23,7 +24,7 @@ function App(): JSX.Element {
}, []);

return (
<DocusaurusContext.Provider value={{siteConfig, isClient}}>
<DocusaurusContext.Provider value={{siteConfig, siteMetadata, isClient}}>
<PendingNavigation routes={routes}>
{renderRoutes(routes)}
</PendingNavigation>
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/client/exports/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
import React from 'react';
import {DocusaurusContext} from '@docusaurus/types';

export default React.createContext<DocusaurusContext>({});
export default React.createContext<DocusaurusContext | null>(null);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import context from './context';
import {DocusaurusContext} from '@docusaurus/types';

function useDocusaurusContext(): DocusaurusContext {
return useContext(context);
const docusaurusContext = useContext(context);
if (docusaurusContext === null) {
// should not happen normally
throw new Error('Docusaurus context not provided');
}
return docusaurusContext;
}

export default useDocusaurusContext;
2 changes: 1 addition & 1 deletion packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import {generate} from '@docusaurus/utils';
import {DocusaurusSiteMetadata} from '@generated/site-metadata';
import path, {join} from 'path';
import {
BUILD_DIR_NAME,
Expand All @@ -22,6 +21,7 @@ import loadRoutes from './routes';
import loadThemeAlias from './themes';
import {
DocusaurusConfig,
DocusaurusSiteMetadata,
LoadContext,
PluginConfig,
Props,
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/server/plugins/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
Plugin,
PluginConfig,
ValidationSchema,
DocusaurusPluginVersionInformation,
} from '@docusaurus/types';
import {PluginVersionInformation} from '@generated/site-metadata';
import {CONFIG_FILE_NAME} from '../../constants';
import {getPluginVersion} from '../versions';

Expand All @@ -40,7 +40,7 @@ function validateAndStrip<T>(schema: ValidationSchema<T>, options: Partial<T>) {
}

export type PluginWithVersionInformation = Plugin<unknown> & {
readonly version: PluginVersionInformation;
readonly version: DocusaurusPluginVersionInformation;
};

export default function initPlugins({
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus/src/server/versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {PluginVersionInformation} from '@generated/site-metadata';
import {DocusaurusPluginVersionInformation} from '@docusaurus/types';
import {existsSync, lstatSync} from 'fs-extra';
import {dirname, join} from 'path';

Expand All @@ -23,7 +23,7 @@ export function getPackageJsonVersion(
export function getPluginVersion(
pluginPath: string,
siteDir: string,
): PluginVersionInformation {
): DocusaurusPluginVersionInformation {
let potentialPluginPackageJsonDirectory = dirname(pluginPath);
while (potentialPluginPackageJsonDirectory !== '/') {
const packageJsonPath = join(
Expand Down
3 changes: 1 addition & 2 deletions website/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

const Hello = () => {
// highlight-start
const context = useDocusaurusContext();
const {siteConfig = {}} = context;
const {siteConfig} = useDocusaurusContext();
// highlight-end
const {title, tagline} = siteConfig;

Expand Down
34 changes: 25 additions & 9 deletions website/docs/docusaurus-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,42 @@ function MyComponent() {

### `useDocusaurusContext`

React hook to access Docusaurus Context. Context contains `siteConfig` object from [docusaurus.config.js](docusaurus.config.js.md).
React hook to access Docusaurus Context. Context contains `siteConfig` object from [docusaurus.config.js](docusaurus.config.js.md), and some additional site metadata.

```ts
type DocusaurusPluginVersionInformation =
| {readonly type: 'package'; readonly version?: string}
| {readonly type: 'project'}
| {readonly type: 'local'}
| {readonly type: 'synthetic'};

interface DocusaurusSiteMetadata {
readonly docusaurusVersion: string;
readonly siteVersion?: string;
readonly pluginVersions: Record<string, DocusaurusPluginVersionInformation>;
}

interface DocusaurusContext {
siteConfig?: DocusaurusConfig;
siteConfig: DocusaurusConfig;
siteMetadata: DocusaurusSiteMetadata;
}
```

Usage example:

```jsx {2,5}
```jsx {5,8,9}
import React from 'react';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

const Test = () => {
const context = useDocusaurusContext();
const {siteConfig = {}} = context;
const {title} = siteConfig;

return <h1>{title}</h1>;
const MyComponent = () => {
const {siteConfig, siteMetadata} = useDocusaurusContext();
return (
<div>
<h1>{siteConfig.title}</h1>
<div>{siteMetadata.siteVersion}</div>
<div>{siteMetadata.docusaurusVersion}</div>
</div>
);
};
```

Expand Down