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

Extend existing doc plugin? #144

Closed
slorber opened this issue Feb 17, 2021 · 9 comments · Fixed by #979
Closed

Extend existing doc plugin? #144

slorber opened this issue Feb 17, 2021 · 9 comments · Fixed by #979
Labels
bug Something isn't working enhancement New feature or request

Comments

@slorber
Copy link

slorber commented Feb 17, 2021

Hi and thanks for working on this interesting plugin.

Don't hesitate to submit it to our community docs so users can more easily find it.

One suggestion I could make is, instead of generating the md docs with the CLI, maybe you could extend the docs plugin so that it creates the md files just before reading the md files?

You may find this interesting to try:
facebook/docusaurus#4138 (comment)

Not sure it would be more convenient than a cli though, but can be worth giving it a try

@edno
Copy link
Member

edno commented Feb 20, 2021

Hi @slorber

Thank you for the feedback.

I forgot to submit after releasing 1.0.0. I will do it this weekend ;-)

I did think about having the build at runtime, but I faced some issue, and it was not important for our internal project.
But thanks for the pointer. I will definitively look into that direction, also rewrite the plugin in TS and with a proper theme for displaying the pages (currently it is more of a smart hack rather than a plugin).
However, I think that the plugin should offer both options: cli and runtime, both have different use cases:

  • with the cli you can have documentation as a code in you repo
  • with the runtime you can keep the schema as source of truth, and avoid generated files in your repo
  • any many other combos are possible...

@edno edno added the enhancement New feature or request label Feb 20, 2021
@slorber
Copy link
Author

slorber commented Feb 22, 2021

thanks for the PR

sure, all this is just a suggestion :) having cli + runtime make sense

@edno edno added this to the 2.0.0 milestone Jun 24, 2021
@edno edno removed this from the 2.0.0 milestone Mar 20, 2022
@edno edno added this to the 2.0.0 milestone Apr 19, 2023
@edno edno modified the milestones: 2.0.0, 1.21.0 Aug 30, 2023
@edno edno closed this as completed in #979 Aug 30, 2023
@edno edno reopened this Aug 31, 2023
@edno edno linked a pull request Sep 9, 2023 that will close this issue
5 tasks
@edno
Copy link
Member

edno commented Sep 9, 2023

@slorber - I am tagging you in this thread since you created it, but if you'd rather have this discussion under the Docusaurus repo, then just ask and I'll report it there.

I restarted, without success, the work on plugin extension following the docs for plugin lifecycle and the following discussion on the Docusaurus repo:

Here a simplified version of the implementation (full code here and in PR #991)

import * as PluginContentDocs from "@docusaurus/plugin-content-docs";

const pluginContentDocs = plugin.default ? plugin.default : plugin;

export default async function pluginGraphQLMarkdown(context, options) {
  const docsPluginInstance = await pluginContentDocs(context, options);

  const loadContent = async () => {
    // resolve configuration
    const config = await buildConfig(options);
    // generate MDX files from Schema
    await generateDocFromSchema(config);
    // Handover to @docusaurus/plugin-content-docs 
    return await docsPluginInstance.loadContent();
  };

  return {
    ...docsPluginInstance,
    name: "docusaurus-graphql-doc-generator",
    loadContent,
  };
}

The MDX files are correctly generated by generateDocFromSchema, but when contentLoaded is called I end up with the following error

[ERROR] MDX loader can't read MDX metadata file "/docusaurus2/.docusaurus/docusaurus-plugin-content-docs/schema_tweets/site-docs-groups-md-fd7.json". Maybe the isMDXPartial option function was not provided?
[ERROR] Error: ENOENT: no such file or directory, open '/docusaurus2/.docusaurus/docusaurus-plugin-content-docs/schema_tweets/site-docs-groups-md-fd7.json'

This let me assume that docsPluginInstance.loadContent() is not processing files previously created by my plugin 🤔

@slorber
Copy link
Author

slorber commented Sep 14, 2023

Hmmm I don't know, what are the files it generates, their content and path?

Maybe try to create a repro by hardcoding these files locally instead of generating them, so that I can inspect it?

@edno
Copy link
Member

edno commented Sep 30, 2023

@slorber - Apologies for the late feedback.

I managed to create a simple repo reproducing the issue: https://github.com/edno/graphql-markdown-docusaurus-plugin-debug

@edno edno removed this from the 1.21.0 milestone Sep 30, 2023
@edno
Copy link
Member

edno commented Oct 6, 2023

@slorber - I think I found a possible root cause.

If one wants to "extend" docusaurus-plugin-content-docs by intercepting the plugin lifecyle, it will break because the data is not generated under the temp folder docusaurus-plugin-content-docs. However, it is not possible to change the data location because of:

  1. Docusaurus creates a temp path for every plugin declared in docusaurus.config.js. However, the path is defined using the plugin name as declared in the config and not the plugin name as exported by the plugin. So, far I assume this is exactly how it should be (but undocumented). Subfolders with instance id are created below the plugin temp path.
  2. Plugin docusaurus-plugin-content-docs has a hardcoded path containing docusaurus-plugin-content-docs (https://github.com/facebook/docusaurus/blob/56410aa94665699f75cb889f477ccdffb98f2b21/packages/docusaurus-plugin-content-docs/src/index.ts#L77).

A possible solution would be to be able to override the path in 2. Ideally, this would be done by adding a pluginDataDirRoot to the Docusaurus context defined in 1. This would allow a plugin to be aware of the temp data path without calculating it, and avoid adding "extension" parameters.

If this solution makes sense to you, then I will investigate how to implement it and push a PR for Docusaurus.

@edno edno added the bug Something isn't working label Oct 6, 2023
@slorber
Copy link
Author

slorber commented Oct 9, 2023

Hey, to be honest I have a hard time understanding your issue 🤪

If one wants to "extend" docusaurus-plugin-content-docs by intercepting the plugin lifecyle, it will break because the data is not generated under the temp folder docusaurus-plugin-content-docs.

Which data, what is the temp folder path exactly, and what does it mean to "break"?

  1. Docusaurus creates a temp path for every plugin declared in docusaurus.config.js. However, the path is defined using the plugin name as declared in the config and not the plugin name as exported by the plugin.

Can you give me a concrete example, what is this temp folder path exactly for the docs plugin?

VS the one you use for your inherited plugin?

And how is it a problem?

Where is the code that is supposed to create this behavior? Because I don't see what you mean here. Afaik we only create .docusaurus/docusaurus-plugin-content-docs/id and that's all (if .docusaurus is what you mean by "temp folder")

  1. Plugin docusaurus-plugin-content-docs has a hardcoded path.

Yes, and if you want to emit json bundle files in the same path you can hardcode this as well in your inherited plugin no?

  const pluginDataDirRoot = path.join(
    generatedFilesDir,
    'docusaurus-plugin-content-docs',
  );
  const dataDir = path.join(pluginDataDirRoot, pluginId);

A possible solution would be to be able to override the path in 2. Ideally, this would be done by adding a pluginDataDirRoot to the Docusaurus context defined in 1. This would allow a plugin to be aware of the temp data path without calculating it, and avoid adding "extension" parameters.

How would that pluginDataDirRoot in context be populated?
To be able to read plugin.name you must first call the plugin function which receives that context, so you kind of have a circular dependency here.


All this is not super fresh in my mind sorry 😅 maybe we should first figure out what are the blockers exactly, on a super simple use case (like just generating one doc file) before trying to even make this plugin work entirely

@edno
Copy link
Member

edno commented Oct 9, 2023

All this is not super fresh in my mind sorry 😅 maybe we should first figure out what are the blockers exactly, on a super simple use case (like just generating one doc file) before trying to even make this plugin work entirely

@slorber - Maybe the repo https://github.com/edno/graphql-markdown-docusaurus-plugin-debug will be more explanatory than my previous comment.

@edno
Copy link
Member

edno commented Jan 14, 2024

Closing the issue as there's no straight forward solution without changing Docusaurus plugin handling (lifecycle hooks).

@edno edno closed this as completed Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
2 participants