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): allow infinitely nested sidebar #1812

Merged
merged 4 commits into from
Oct 7, 2019
Merged

Conversation

endiliey
Copy link
Contributor

@endiliey endiliey commented Oct 7, 2019

Motivation

continuation from #1811

Expect more PR on docs plugin probably this week as I try to make docs metadata more efficient later on.
fix #827 #868 #1182

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

Try with docu v2 site

// sidebars.js
module.exports = {
  docs: [
    {
      type: 'category',
      label: 'level 1',
      items: [
        'introduction',
        {
          type: 'category',
          label: 'level 2',
          items: [
            'contributing',
            {
              type: 'category',
              label: 'level 3',
              items: [
                'design-principles',
                {
                  type: 'category',
                  label: 'level 4',
                  items: [
                    'installation',
                    {
                      type: 'category',
                      label: 'deeper more more',
                      items: ['sidebar'],
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    },
  ],
};

image

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@endiliey endiliey requested a review from wgao19 October 7, 2019 15:08
@endiliey endiliey requested a review from yangshun as a code owner October 7, 2019 15:08
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 7, 2019
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Oct 7, 2019

Deploy preview for docusaurus-2 ready!

Built with commit e056ac2

https://deploy-preview-1812--docusaurus-2.netlify.com

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Oct 7, 2019

Deploy preview for docusaurus-preview ready!

Built with commit e056ac2

https://deploy-preview-1812--docusaurus-preview.netlify.com

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

Awesome work! I made some small nitpicky changes. Do we want to consider removing support for the object format and force an array format? Also, we can allow a simpler format for the most common use cases. If a 2nd index is present and it's an array, that item is a category and the values in the 2nd array are children of that category. This definitely makes us have more work to do but is less verbose and resembles the plugins/preset config. WDYT?

module.exports = {
  docs: [
    [
      'item', [
        'inner-foo',
        'inner-bar',
      ],
    ],
  ]
}

This would be a breaking change but it's one that's worth making IMO.

@yangshun
Copy link
Contributor

yangshun commented Oct 7, 2019

Merging first, we can talk about new API in future.

@yangshun yangshun merged commit 95f0552 into master Oct 7, 2019
@yangshun yangshun deleted the endiliey/deep-sidebar branch October 7, 2019 18:58
@endiliey
Copy link
Contributor Author

endiliey commented Oct 8, 2019

I personally dont mind but from another perspective I think its not a good idea to force array format as it is only a syntatic sugar. There has been many breaking changes already :( Note that there are versioned sidebar as well. Migration will be even more painful (eg: for RN). If we can have less breaking changes thats better

const old = {
  docs: [
    {
      type: 'category',
      label: 'level 1',
      items: [
        'introduction',
        'contributing',
      ]
    },
    {
      type: 'category',
      label: 'Guides',
      items: [
        'blog',
      ]
    },
  ]
};

const new = {
  docs: {
    'level 1': [
      'introduction',
      'contributing'
    ],
    'Guides': ['blog']
  }
}

Both old and new is equivalent. In fact, old (v1 syntax) is transformed to new through

/**
* Converts sidebars object to mapping to arrays of sidebar item objects
*/
function normalizeSidebar(sidebars: SidebarRaw): Sidebar {
return Object.entries(sidebars).reduce(
(acc: Sidebar, [sidebarId, sidebar]) => {
let normalizedSidebar: SidebarItemCategoryRaw[];
if (!Array.isArray(sidebar)) {
// convert sidebar to a more generic structure
normalizedSidebar = Object.entries(sidebar).map(([label, items]) => ({
type: 'category',
label,
items,
}));
} else {
normalizedSidebar = sidebar;
}
acc[sidebarId] = normalizedSidebar.map(item => normalizeCategory(item));
return acc;
},
{},
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow linking to hrefs for Sidebars
4 participants