Skip to content

Allow a single docs folder to be consumed by 2 docs plugin instances (use-case: normal vs embed/iframe docs) #7480

Open

Description

@alexandernst

Have you read the Contributing Guidelines on issues?

Prerequisites

Description

If I add the docs plugin to the list of plugins (multi-instance) and I point it to the same folder that the preset is already pointing to (docs by default), the build process stops working and I see the following errors:

SyntaxError: /code/src/......../index.mdx: Identifier 'React' has already been declared. (46:184)

I believe the bug might be caused by the way each mdx file is being processed. Because a mdx file is (basically) a JSX file, import React from 'react'; is being prepended at the top of each file. But since I have two instances of the docs plugin pointing to the same folder, the import statement is being prepended twice (hence the error).

I made a minimal repro codesandbox.

Reproducible demo

https://codesandbox.io/s/trusting-rosalind-sc4s15

Steps to reproduce

  1. Open the link
  2. Open a new tty and type npm run build
  3. The build will fail.
  4. Comment the plugins section of the docusaurus.config.js
  5. Run npm run build again.
  6. The build will succeed.

Expected behavior

I should be able to point multiple instances of the docs plugin to the same folder.

Actual behavior

The build crashes.

Your environment

  • Public source code: N/A
  • Public site URL: N/A
  • Docusaurus version used: 2.0.0-beta20
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): N/A
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): N/A

Self-service

  • I'd be willing to fix this bug myself.

Activity

  1. added
    bug
    status: needs triage
    on May 24, 2022
  2. Josh-Cena commented on May 24, 2022

    @Josh-Cena
    Collaborator

    Interesting. I get why it fails, but I don't understand what you are trying to achieve—what's the use-case? We do expect plugins to work in isolation and their folder paths do not overlap.

  3. added
    domain: content plugin
    and removed
    status: needs triage
    domain: content plugin
    on May 24, 2022
  4. alexandernst commented on May 24, 2022

    @alexandernst
    ContributorAuthor

    I'd like to be able to render the same documentation with different layouts on different URLs. Eg. foo.com/docs/intro should render the navbar, the sidebar, the docs itself, etc..., while foo.com/embedded/intro should render just the intro document, without any other elements (no navbar, no sidebar, no header, no footer, etc...). My use-case is being able to embed a particular document inside an iframe.

  5. Josh-Cena commented on May 24, 2022

    @Josh-Cena
    Collaborator

    Ah, I see. I don't really think you need multiple plugins to do this, but I don't have a better solution on top of my head right now. Because you have the same set of data and metadata, it should be doable within the same plugin, just passing it to two themes.

  6. changed the title [-]Multi-inistance of docs plugin pointing to same folder causes errors[/-] [+]Allow same set of docs plugin content to be rendered by multiple themes[/+] on May 24, 2022
  7. added
    proposal
    and removed
    bug
    on May 24, 2022
  8. alexandernst commented on May 24, 2022

    @alexandernst
    ContributorAuthor

    How could I do that?

  9. Josh-Cena commented on May 24, 2022

    @Josh-Cena
    Collaborator

    I don't really know. I don't think it's possible with today's setup, which is why I'm still leaving it open :) But it's definitely legitimate and we'll figure out a way to do it. Sorry if you think it's going to be a quick little bug fix—it's probably more complicated than we have thought.

    @slorber will be back tomorrow, and maybe he can have some opinions to offer as well.

  10. alexandernst commented on May 27, 2022

    @alexandernst
    ContributorAuthor

    @slorber Hi! Did you have a chance to look into this proposal?

  11. slorber commented on May 27, 2022

    @slorber
    Collaborator

    No, I am reviewing existing PRs for now

  12. 6 remaining items

  13. slorber commented on Jul 13, 2022

    @slorber
    Collaborator

    No problem, we'll continue investigating.

    I also have the use-case for a single folder being consumed by 2 plugins myself, as I'd like to render MDX blog posts in 2 variants:

  14. dustinlacewell commented on Aug 14, 2022

    @dustinlacewell

    This is driving me nuts. :( Sorry.

  15. slorber commented on Aug 16, 2022

    @slorber
    Collaborator

    This is driving me nuts. :( Sorry.

    not sure what you mean here 🤷‍♂️ if you are blocked by this issue please describe what's your use-case and what prevents you to achieve it. Having multiple users reporting their pain helps us choose how we want to solve it

  16. slorber commented on Mar 2, 2023

    @slorber
    Collaborator

    For the embedding use-case, would it be useful if we allowed applying a global CSS class directly from the iframe query string?

    For example:

    <iframe src="https://docusaurus.io/docs/intro?docusaurus-html-class=my-custom-class"></iframe>

    This could allow you to easily plug custom CSS rules depending on the class you inject. For example, you could decide to hide the Docusaurus navbar if the class is present:

    html.my-custom-class nav.navbar {
      display: none;
    }

    The class would be applied to <html> as soon as possible, with static inlined JavaScript (similar to what we do with light/dark theme), so there wouldn't be any flash of unstyled content.

    The idea was initially discussed here: #8672 (comment) where we also allow define the Docusaurus theme from a querystring variable (PR: #8708)

  17. alexandernst commented on Mar 2, 2023

    @alexandernst
    ContributorAuthor

    I think that this is a good idea! Maybe docusaurus could append that custom docusaurus-html-class query tag to the links that are rendered, so if the user clicks on a link, the frame will load the content with the same styling?

  18. slorber commented on Mar 2, 2023

    @slorber
    Collaborator

    Thanks @alexandernst , that makes sense that the class stays present as the user navigates the embedded document, so that the layout of that element does not switch to the regular layout.

    Actually, I'm not sure it would be that easy to add a class name to <html> because of the way react-helmet works: it tends to override classes that are added imperatively afaik. We'll have to test this a bit.

    It's possible that allowing users to provide a data-attribute is a safer choice. Once data attributes are set, they should never be overridden. Even if the URL does not contain it anymore (on link click), if we set <html data-embedded="true"/> it should stay there forever (as long as you navigate with SPA/Docusaurus <Link> component).

    <iframe src="https://docusaurus.io/docs/intro?docusaurus-data-embedded=true"></iframe>
    [data-embedded='true'] nav.navbar {
      display: none;
    }

    Let's collect a bit more feedback to see what others think.

  19. jbguerraz commented on Mar 28, 2023

    @jbguerraz

    Hello mates 👋 Hello @slorber

    We would also love embedding the documentation within a (react) app.

    Let's say we have a (react) low-code app and some documentation for full platform documentation available on a static website docs.platform.domain
    When we're (using the low-code app) for instance editing some entity, we'd love to have an help button that would then display the documentation for the current entity type in the current window, without loading an iframe but rendering a component like so <Docusaurus route="/docs/entity/someentityttye">

    That would really be cool! Maybe then we could add some properties to, for instance, show or hide the frame (header, footer, nav, ...)

    We'd be ready to invest time and contribute such feature if somehow it makes sens and if it somehow align with the current project architecture :-)

  20. slorber commented on Apr 6, 2023

    @slorber
    Collaborator

    @jbguerraz

    without loading an iframe but rendering a component like so

    I don't understand wha you mean here. If it's not an iframe, how can you reference a pathname. You are either rendering a local React component or inside an iframe, I don't see how to do otherwise


    That would really be cool! Maybe then we could add some properties to, for instance, show or hide the frame (header, footer, nav, ...)

    In #8672 I suggested that we could inject extra classes from querystring params, so that you can decide to show/hide things conditionally.

    <iframe src="https://docs.platform.domain/docs/entity?docusaurus-html-class=embedded-iframe`>
    

    Would this help?

    Note we don't implement the behavior for .embedded-iframe, but we allow you to implement it without duplicating the page for embedding.

  21. dvdokkum commented on May 30, 2023

    @dvdokkum

    Just want to chime in and say injectable classes via querystring param would be a big help for us. We maintain a full docs site, but also want to embed certain docs directly in our main application. Being able to drop the navbar element would make the execution much cleaner.

  22. slorber commented on May 31, 2023

    @slorber
    Collaborator

    @dvdokkum I implemented it, it should be available in canary today or in v3 alpha/beta/rc/stable a bit later

    I only implemented the support for data-attribute injection because with classes it was causing issues on React hydration: React Helmet is erasing the classes that we add inline when it takes over. I think it should be good enough for styling purpose and data-attribute CSS selectors.

    #9028

    image

    In the future it's possible that we design our own official "iframe/embedded mode" but for now we give you the flexibility to implement it yourself. Having your early adopter feedback on this can actually help design the official iframe mode, so please report if it works fine for you.

  23. dvdokkum commented on May 31, 2023

    @dvdokkum

    Awesome, thank you! I'll give it a whirl later today.

  24. dvdokkum commented on Jun 1, 2023

    @dvdokkum

    Yeah, works great, thanks! One thing I am noticing though (probably unrelated?) is that in the Canary release the copy button is no longer appearing on code blocks. Confirmed it wasn't anything I added related this feature, when I pop my version back to @latest the copy buttons come back.

  25. slorber commented on Jun 8, 2023

    @slorber
    Collaborator

    in the Canary release the copy button is no longer appearing on code blocks.

    🤷‍♂️ I still see the copy button. Are you sure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions