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

Ability to link navbar to a given sidebar #6117

Closed
1 of 2 tasks
slorber opened this issue Dec 16, 2021 · 5 comments · Fixed by #6139
Closed
1 of 2 tasks

Ability to link navbar to a given sidebar #6117

slorber opened this issue Dec 16, 2021 · 5 comments · Fixed by #6139
Labels
feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. good first issue If you are just getting started with Docusaurus, this issue should be a good place to begin.

Comments

@slorber
Copy link
Collaborator

slorber commented Dec 16, 2021

Have you read the Contributing Guidelines on issues?

Motivation

Sometimes we just want to link to the first doc of a given sidebar, without having to specify a given doc ID.

This can be useful to implement tabs navigation, like on our site, RN, Jest...

          {
            type: 'doc',
            docId: 'introduction',
            label: 'Docs',
          },
          {
            type: 'doc',
            docId: 'cli',
            label: 'API',
          },

This could become more natural with:

          {
            type: 'docSidebar',
            id: 'docs',
            label: 'Docs',
          },
          {
            type: 'doc',
            id: 'api',
            label: 'API',
          },

Another use-case mentioned is to use the docs plugin for a podcast: https://thediffpodcast.com/docs/episode-10

image

This is not convenient because on each new episode it requires editing the config navbar items to link to that latest episode.


This shouldn't be too hard to implement so if any external contributor is interested, please let me know ;)

Self-service

  • I'd be willing to do some initial work on this proposal myself.
@slorber slorber added good first issue If you are just getting started with Docusaurus, this issue should be a good place to begin. proposal This issue is a proposal, usually non-trivial change labels Dec 16, 2021
@lmpham1
Copy link
Contributor

lmpham1 commented Dec 16, 2021

I'd like to give this issue a try!

@Josh-Cena Josh-Cena added feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. and removed proposal This issue is a proposal, usually non-trivial change labels Dec 17, 2021
@slorber
Copy link
Collaborator Author

slorber commented Dec 17, 2021

This may be more complicated than I thought.

The theme layout does not have access to all docs-related data (on purpose), but it can only read docs global data (like other docs-related navbar items).

I don't think there's enough data in this global data structure to be able to find out what the link should be.

Also, if a sidebar starts with a category, and that category has a generated-index (example: https://docusaurus.io/docs/category/getting-started), we should probably link to that index instead of the first doc.

Cf the podcast site above. If the "Episodes" category was a generated-index (it is not atm), the Episodes navbar link should probably link to this page instead of linking to the latest episode-10.

image

The new global data structure should probably look like this:

type GlobalSidebarLink = { label: string; path: string };

type GlobalSidebar = {
  link?: GlobalSidebarLink;
  // ... we may add other things here later
};

// already existing type
export type GlobalVersion = {
  // ...
  sidebars: Record<string, GlobalSidebar>;
};

Also, can we infer the navbar label from the link label so that the navbar label is optional? 🤷‍♂️ not sure if it's very useful but maybe in some cases?

@lmpham1
Copy link
Contributor

lmpham1 commented Dec 18, 2021

The new global data structure should probably look like this:

@slorber ah so I assume after that we need to add a toGlobalSidebar function to convert the sidebar object in a LoadedVersion object into a GlobalSidebar object, and stick that object into the GlobalVersion object. Then in the Navbar Item module in theme, we can fetch the GlobalVersion -> get GlobalSidebar from it, which contains the link to the first doc, which we can use to link it to that navbar item.

Is this the approach you have in mind?

@Josh-Cena
Copy link
Collaborator

@lmpham1 Sounds about right. You can mimic how the toGlobalDoc function is implemented, and register the sidebars as global data. It would be a mapping from sidebar IDs to the first doc's URL.

@slorber
Copy link
Collaborator Author

slorber commented Dec 20, 2021

Yes 👍

It would be a mapping from sidebar IDs to the first doc's URL.

For future proofness, I'd rather have an object with a link attribute, also the label

type GlobalSidebarLink = { label: string; path: string };

type GlobalSidebar = {
  link?: GlobalSidebarLink;
  // ... we may add other things here later
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. good first issue If you are just getting started with Docusaurus, this issue should be a good place to begin.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants