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(content-docs): add custom props front matter #6619

Merged
merged 5 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,19 @@ describe('validateDocFrontMatter sidebar_position', () => {
});
});

describe('validateDocFrontMatter sidebar_custom_props', () => {
testField({
prefix: 'sidebar_custom_props',
validFrontMatters: [
{sidebar_custom_props: {}},
{sidebar_custom_props: {prop: 'custom', number: 1, boolean: true}},
],
invalidFrontMatters: [
[{sidebar_custom_props: ''}, 'must be of type object'],
],
});
});

describe('validateDocFrontMatter custom_edit_url', () => {
testField({
prefix: 'custom_edit_url',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
sidebar_label: Joi.string(),
sidebar_position: Joi.number(),
sidebar_class_name: Joi.string(),
sidebar_custom_props: Joi.object().unknown(),
displayed_sidebar: Joi.string().allow(null),
tags: FrontMatterTagsSchema,
pagination_label: Joi.string(),
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Available document ids are:
label: sidebarLabel || item.label || title,
href: permalink,
className: item.className,
customProps: item.customProps,
customProps:
item.customProps ?? docMetadata.frontMatter.sidebar_custom_props,
docId: docMetadata.unversionedId,
};
};
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type DocFrontMatter = {
sidebar_label?: string;
sidebar_position?: number;
sidebar_class_name?: string;
sidebar_custom_props?: Record<string, unknown>;
displayed_sidebar?: string | null;
pagination_label?: string;
custom_edit_url?: string | null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"label": "Custom Props"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
sidebar_custom_props:
prop: custom
number: 1
boolean: true
---

# Doc with Custom Props

This doc has custom props.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Doc Without Custom Props

This doc doesn't have custom props.
22 changes: 22 additions & 0 deletions website/_dogfooding/_docs tests/tests/custom-props/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Custom Props

```mdx-code-block
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';

export const DocPropsList = ({items}) => (
<table>
<tr>
<th>Doc Page</th>
<th>Custom Props</th>
</tr>
{items.map((item, index) => (
<tr>
<td>{item.label}</td>
<td>{JSON.stringify(item.customProps)}</td>
</tr>
))}
</table>
);

<DocPropsList items={useCurrentSidebarCategory().items}/>
```
4 changes: 2 additions & 2 deletions website/docs/guides/docs/sidebar/autogenerated.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ function isCategoryIndex({fileName, directories}) {

## Autogenerated sidebar metadata {#autogenerated-sidebar-metadata}

For hand-written sidebar definitions, you would provide metadata to sidebar items through `sidebars.js`; for autogenerated, Docusaurus would read them from the item's respective file. In addition, you may want to adjust the relative position of each item, because, by default, items within a sidebar slice will be generated in **alphabetical order** (using files and folders names).
For handwritten sidebar definitions, you would provide metadata to sidebar items through `sidebars.js`; for autogenerated, Docusaurus would read them from the item's respective file. In addition, you may want to adjust the relative position of each item because, by default, items within a sidebar slice will be generated in **alphabetical order** (using file and folder names).

**For docs**: use additional front matter. The `label` and `className` attributes now become `sidebar_label` and `sidebar_class_name`, while there's an additional `sidebar_position` front matter.
**For docs**: use additional front matter. The `position`, `label`, and `className` attributes are declared using `sidebar_position`, `sidebar_label`, and `sidebar_class_name` front matter, respectively. (Custom props can also be supplied via `sidebar_custom_props` front matter.)
TheCatLady marked this conversation as resolved.
Show resolved Hide resolved

```md title="docs/tutorials/tutorial-easy.md"
---
Expand Down
4 changes: 2 additions & 2 deletions website/docs/guides/docs/sidebar/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ toc_max_heading_level: 4
slug: /sidebar/items
---

# Sidebar items

```mdx-code-block
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
```

# Sidebar items

We have introduced three types of item types in the example in the previous section: `doc`, `category`, and `link`, whose usages are fairly intuitive. We will formally introduce their APIs. There's also a fourth type: `autogenerated`, which we will explain in detail later.

- **[Doc](#sidebar-item-doc)**: link to a doc page, associating it with the sidebar
Expand Down