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 1 commit
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}/>
```