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

Documentation should also teach registering anchors in plugin code #9986

Open
6 of 7 tasks
danielspofford opened this issue Mar 26, 2024 · 6 comments
Open
6 of 7 tasks
Labels
documentation The issue is related to the documentation of Docusaurus

Comments

@danielspofford
Copy link

danielspofford commented Mar 26, 2024

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

Description

  • I am currently getting warnings from onBrokenLinks/onBrokenAnchors. This is what I am ultimately trying to fix.
  • Docusaurus doesn't know about them because they link to content that is generated at build time by another plugin that is incapable of using official Link/etc components that track this automatically.
  • Docusaurus docs push me in the direction of https://docusaurus.io/docs/docusaurus-core#useBrokenLinks to handle a scenarios like this.
  • Problem is I cannot seem to useBrokenLinks in a plugin. Hopefully I am just doing something wrong, but I am not even sure if I am supposed to be able to use this functionality in a plugin given "Docusarus Client API"; does that mean that functionality cannot be used in plugins?

In any case: is it possible to useBrokenLinks in a plugin? If yes, how/what am I doing wrong? If no, is it possible to solve my problem with a plugin (inform docusaurus of routes that are valid so that it doesn't warn/throw on them with onBrokenLinks/onBrokenAnchors).

Reproducible demo

https://codesandbox.io/p/devbox/jovial-benji-f4599g

Steps to reproduce

  1. Add a no-op plugin that tries to import useBrokenLinks like the codesandbox has at src/plugins/foo/index.js.
  2. Add the plugins: ['src/plugins/foo'] to docusuarus config.

If using the codesandbox link I provide - the above steps are already done (and are the only changes to the sandbox). The project as-is in there will fail to build.

Expected behavior

I hoped to use useBrokenLinks in a plugin to inform docusaurus about anchors/links that it should consider valid to stop receiving false onBrokenLinks/onBrokenAnchors warnings.

Actual behavior

Docusaurus fails to build - it cannot resolve the import of useBrokenLinks in a plugin.

  [cause]: Error: Docusaurus could not load module at path "myproject/src/src/plugins/foo/index.js"
  Cause: Cannot find module '@docusaurus/useBrokenLinks'

Long form: https://gist.github.com/danielspofford/17993f7c77319d3639d52a8fd9f28fc3.

Your environment

Self-service

  • I'd be willing to fix this bug myself.
@danielspofford danielspofford added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Mar 26, 2024
@Josh-Cena
Copy link
Collaborator

Hi! You cannot use useBrokenLinks in a plugin file. Plugins are run in Node. You can only use it in a theme component. I think we should explain better how to register anchors directly in the plugin.

@Josh-Cena Josh-Cena added documentation The issue is related to the documentation of Docusaurus and removed bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Mar 27, 2024
@Josh-Cena Josh-Cena changed the title collecting broken anchors/links in plugins fails with: Cause: Cannot find module '@docusaurus/useBrokenLinks' Documentation should also teach registering anchors in plugin code Mar 27, 2024
@danielspofford
Copy link
Author

Appreciate your confirmation @Josh-Cena - I figured as much after continuing my efforts.

I think we should explain better how to register anchors directly in the plugin.

If this is possible can you please point me at anything (docs/source examples/whatever) that would shine some light on how I do that? Exactly what I'm after.

@Josh-Cena
Copy link
Collaborator

I somewhat suspect that it's impossible, since right now only the rendering code reports back valid anchors. I'll let @slorber answer though; if it's really not achievable, maybe we should have an API. It sounds useful for plugins that create external HTML pages that don't use Docusaurus themes.

@danielspofford
Copy link
Author

I suspect that as well.

It sounds useful for plugins that create external HTML pages that don't use Docusaurus themes.

Yeah that is my exact use case. Was trying to work around rohit-gohri/redocusaurus#321.

The problem for me is that:

  • In a plugin:
    • I have access to the external HTML page's content (fs.reads in postBuild), and I can get anchors I would like to collect from it, but I cannot tell docusaurus about those anchors as discussed in this issue.
  • In a theme component:
    • I can tell docusaurus about anchors (via useBrokenLinks), but I do not have access to the external HTML page's content, so I cannot get anchors I would like to collect from it.

Just to see if it was possible, I tried to: generate the anchors in the plugin, stick them in global data, and then access them from a theme component so that I could useBrokenLinks. But there is an order of operations problem:

  • In order for my plugin to generate the anchors, the external HTML must already exist.
  • External HTML will only exist from my plugin's POV at time of postBuild.
  • setGlobalData is only available at time of contentLoaded, not in postBuild, so I cannot pass the anchors through because by the time I get them, I cannot setGlobalData.

Regardless of the order of operations problem, it feels wrong to me to serialize and pump that data around in that manner. Seems like allowing for that in plugins like you say would be very nice for this use case.

@slorber
Copy link
Collaborator

slorber commented Apr 5, 2024

@danielspofford it would greatly help me if you provided a minimal repro that reports broken anchors that you would like to solve. This way, I could see how to fix that repro so that those anchors do not get reported anymore.

Your repro just shows an attempt to import a React hook in Node.js code, something that is illegal). I can't act on it, and fix the problem, because the problem is not showcased in the first place. Show me the problem and I'll help fix it. Seeing a runnable sandbox of your setup that actually reports wrong broken anchors is more helpful than explaining things in text form.

@danielspofford
Copy link
Author

@slorber here is a minimal repro that reports broken anchors that I would like to solve.

https://codesandbox.io/p/devbox/distracted-cache-7xxfh5

example output image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation The issue is related to the documentation of Docusaurus
Projects
None yet
Development

No branches or pull requests

4 participants
@slorber @danielspofford @Josh-Cena and others