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

fix: change subcategory format #1026

Merged
merged 5 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
45 changes: 32 additions & 13 deletions docs/guides-navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,46 @@ You should provide `directory/id` instead of `id` in `sidebars.json`.
}
```

### Adding Sub Categories
### Adding Subcategories

It is possibile to add sub categories to a sidebar. Instead of passing an array to the category like the previous examples you can pass an object where
the keys will be the sub category name. You can then pass an array of document ids to the sub category.
It is possible to add subcategories to a sidebar. Instead of using IDs as the contents of the category array like the previous examples, you can pass an object where the keys will be the subcategory name and the value an array of IDs for that subcategory.

```js
{
"examples-sidebar" : {
"My Example Category" : {
"My Example Sub Category" : [
"my-examples",
...
],
"My Next Sub Category" : [
"some-other-examples"
]
"docs": {
"My Example Category": [
"examples",
{
"My Example Subcategory": [
"my-examples",
...
]
},
{
"My Next Subcategory": [
"some-other-examples"
]
},
"even-more-examples",
...
},
],
...
}
}

/*
The above will generate:

- My Example Category
- examples
- My Example Subcategory
- my-examples
...
- My Next Subcategory
- some-other-examples
- even-more-examples
...
*/
```

### Adding New Sidebars
Expand Down
8 changes: 6 additions & 2 deletions v1/lib/core/BlogSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ class BlogSidebar extends React.Component {

const contents = [
{
name: blogSidebarTitle,
links: MetadataBlog.slice(0, blogSidebarCount),
type: 'CATEGORY',
title: blogSidebarTitle,
children: MetadataBlog.slice(0, blogSidebarCount).map(item => ({
type: 'LINK',
item,
})),
},
];
const title = this.props.current && this.props.current.title;
Expand Down
1 change: 0 additions & 1 deletion v1/lib/core/DocsLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class DocsLayout extends React.Component {
const title =
idx(i18n, ['localized-strings', 'docs', id, 'title']) || defaultTitle;
const hasOnPageNav = this.props.config.onPageNav === 'separate';

const previousTitle =
idx(i18n, ['localized-strings', metadata.previous_id]) ||
idx(i18n, ['localized-strings', 'previous']) ||
Expand Down
7 changes: 4 additions & 3 deletions v1/lib/core/DocsSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ if (fs.existsSync(`../server/languages.js`)) {

class DocsSidebar extends React.Component {
render() {
const sidebar = this.props.metadata.sidebar;
const {category, sidebar} = this.props.metadata;
const docsCategories = readCategories(sidebar, Metadata, languages);
const categoryName = this.props.metadata.category;
if (!categoryName) {

if (!category) {
return null;
}

return (
<Container className="docsNavContainer" id="docsNav" wrapper={false}>
<SideNav
Expand Down
70 changes: 37 additions & 33 deletions v1/lib/core/nav/SideNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,44 +61,48 @@ class SideNav extends React.Component {
return null;
}

renderCategory(category) {
return (
<div className="navGroup" key={category.name}>
<h3 className="navGroupCategoryTitle">
{this.getLocalizedCategoryString(category.name)}
</h3>
<ul>
{category.links.map(this.renderItemLink, this)}
{category.sub_categories &&
category.sub_categories.map(this.renderSubCategory, this)}
</ul>
</div>
);
}

renderSubCategory(subCategory) {
return (
<div className="navGroup subNavGroup" key={subCategory.name}>
<h4 className="navGroupSubCategoryTitle">
{this.getLocalizedCategoryString(subCategory.name)}
</h4>
<ul>{subCategory.links.map(this.renderItemLink, this)}</ul>
</div>
);
}

renderItemLink(link) {
renderCategory = categoryItem => (
<div className="navGroup" key={categoryItem.title}>
<h3 className="navGroupCategoryTitle">
{this.getLocalizedCategoryString(categoryItem.title)}
</h3>
<ul>
{categoryItem.children.map(item => {
switch (item.type) {
case 'LINK':
return this.renderItemLink(item);
case 'SUBCATEGORY':
return this.renderSubcategory(item);
default:
return null;
}
})}
</ul>
</div>
);

renderSubcategory = subcategoryItem => (
<div className="navGroup subNavGroup" key={subcategoryItem.title}>
<h4 className="navGroupSubcategoryTitle">
{this.getLocalizedCategoryString(subcategoryItem.title)}
</h4>
<ul>{subcategoryItem.children.map(this.renderItemLink)}</ul>
</div>
);

renderItemLink = linkItem => {
const linkMetadata = linkItem.item;
const itemClasses = classNames('navListItem', {
navListItemActive: link.id === this.props.current.id,
navListItemActive: linkMetadata.id === this.props.current.id,
});
return (
<li className={itemClasses} key={link.id}>
<a className="navItem" href={this.getLink(link)}>
{this.getLocalizedString(link)}
<li className={itemClasses} key={linkMetadata.id}>
<a className="navItem" href={this.getLink(linkMetadata)}>
{this.getLocalizedString(linkMetadata)}
</a>
</li>
);
}
};

render() {
return (
Expand All @@ -122,7 +126,7 @@ class SideNav extends React.Component {
)}
</div>
<div className="navGroups">
{this.props.contents.map(this.renderCategory, this)}
{this.props.contents.map(this.renderCategory)}
</div>
</section>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
next_id: 'doc2',
next: 'en-doc2',
next_title: 'Document 2',
sub_category: 'Sub Cat 1',
subcategory: 'Sub Cat 1',
sort: 1,
},
'en-doc2': {
Expand All @@ -28,7 +28,7 @@ module.exports = {
previous_id: 'doc1',
previous: 'en-doc1',
previous_title: 'Document 1',
sub_category: 'Sub Cat 1',
subcategory: 'Sub Cat 1',
sort: 2,
},
'en-doc3': {
Expand All @@ -44,7 +44,7 @@ module.exports = {
previous_id: 'doc2',
previous: 'en-doc2',
previous_title: 'Document 2',
sub_category: 'Sub Cat 2',
subcategory: 'Sub Cat 2',
sort: 3,
},
'en-doc4': {
Expand Down
23 changes: 16 additions & 7 deletions v1/lib/server/__tests__/__fixtures__/sidebar-subcategories.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
module.exports = {
docs: {
'First Category': {
'Sub Cat One': ['doc2', 'doc1'],
'Sub Cat Two': ['doc3', 'doc5'],
},
'Second Category': {
Hello: ['doc4'],
},
'First Category': ['doc1', 'doc2'],
'Second Category': [
'doc3',
{
'First Subcategory': ['doc4'],
},
'doc5',
],
'Third Category': [
{
'Second Subcategory': ['doc6'],
},
{
'Third Subcategory': ['doc7'],
},
],
},
};
Loading