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

Expose frontmatter in plugin-content-docs client hooks #9263

Closed
1 of 2 tasks
imp-dance opened this issue Aug 26, 2023 · 1 comment
Closed
1 of 2 tasks

Expose frontmatter in plugin-content-docs client hooks #9263

imp-dance opened this issue Aug 26, 2023 · 1 comment
Labels
closed: duplicate This issue or pull request already exists in another issue or pull request feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.

Comments

@imp-dance
Copy link

Have you read the Contributing Guidelines on issues?

Description

Currently, the only properties available are

  • id
  • path
  • sidebar

...when accessing the data through one of Docusaurus' client hooks (ex: useGlobalData or useDocusaurusContext). It would be great if we could get some more metadata in these objects. Some properties that could be included could be:

  • frontmatter - A record that contains frontmatter data for the relevant document
  • markdown - A string that contains the markdown content

I realise that exposing the markdown could be a challenge, especially considering the fact that pages can be written in MDX. Maybe that wouldn't be necessary for my specific use case.

Has this been requested on Canny?

No response

Motivation

I want to create a "internal link preview" component that when hovered, shows a popover that contains a small preview of the document that the user is linking to.

I'm currently able to "find" the document, but I'm not getting enough relevant information to render a preview of the document. If I receive the frontmatter, atleast I can show a title and description.

API design

Currently, docusaurus-plugin-content-docs exposes this data to the client hooks:

type PluginContentDocsData = Record<string, {
   breadcrumbs: boolean;
   path: string;
   versions: Array<{
      docs: Array<{
         id: string;
         path: string;
         sidebar: string;
      }>;
      // ...rest
   }>;
}>;

But if the frontmatter is available, it would be great to also get that in the doc-object:

type PluginContentDocsData = Record<string, {
   breadcrumbs: boolean;
   path: string;
   versions: Array<{
      docs: Array<{
         id: string;
         path: string;
         sidebar: string;
         frontmatter: Record<string, string | string[]>;
      }>;
      // ...rest
   }>;
}>;

Have you tried building it?

Yes I've tried looking into every client API available to see if it was possible to render a document preview from a React component, but alas it seems to not be possible given the current API of docusaurus-plugin-content-docs (since it doesn't expose enough information about the documents).

Self-service

  • I'd be willing to contribute this feature to Docusaurus myself.
@imp-dance imp-dance added feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. status: needs triage This issue has not been triaged by maintainers labels Aug 26, 2023
@slorber
Copy link
Collaborator

slorber commented Aug 31, 2023

Hi

I understand your use-case, but unfortunately the global data is kept minimal for a good reason. If we add lots of content there, then loading any of your site's page would be much more heavy in terms of JS/JSON data to load. Even if a page does not use that data, it will still have to be loaded because everything ends up in the same file.

In #6923 I propose to introduce this so that you can register yourself extra global data, but keep in mind that data should rather be kept minimal in any case:

global_custom_props: 
  a: b
  c: d
  e: ["f"]

In the future, React Server Components (track #9089) are going to provide good flexibility to implement your hover preview use case, without the need for global data. It's possible that we'll even deprecate the global data API in favor of RSCs.

@slorber slorber closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2023
@slorber slorber added closed: duplicate This issue or pull request already exists in another issue or pull request and removed status: needs triage This issue has not been triaged by maintainers labels Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: duplicate This issue or pull request already exists in another issue or pull request feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.
Projects
None yet
Development

No branches or pull requests

2 participants