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

Local Mermaid library not found #90

Closed
frank-lenormand opened this issue Sep 19, 2023 · 11 comments
Closed

Local Mermaid library not found #90

frank-lenormand opened this issue Sep 19, 2023 · 11 comments
Labels
bug Something isn't working fixed The issue is fixed

Comments

@frank-lenormand
Copy link

Hi,

This is a follow-up to #84.

My configuration is as follows:

plugins:
  - mermaid2:
       javascript: js/mermaid.min.js

The library is located in /docs/js.

The documentation that makes use of Mermaid is located in /docs/subdir.

At runtime, I can see in the console that runs the development server:

WARNING -  [14:11:46] "GET /site/subdir/js/mermaid.min.js HTTP/1.1" code 404

Diagrams are still rendered, however, because the plugin fetches the library from unpkg.com.

Thanks!

@fralau
Copy link
Owner

fralau commented Sep 19, 2023

🤔 There should be a logical explanation.

Hints:

  1. What does the HTML say in the page served by your development server, when calling the mermaid javascript library? (that will tell us, more or less what the plugin did).
  2. Is the file is really there and is the extension correct? (min.js)?

@frank-lenormand
Copy link
Author

When I browse to /site/subdir:

  1. The injected tag is:

    <script src="js/mermaid.min.js"></script>

  2. Since the current page is located in a sub-directory, there is no such site/subdir/js/mermaid.min.js file (it is present at site/js/mermaid.min.js though)

It seems that the path to the Mermaid library injected into the HTML should be prepended with BASE_URL (or whatever is valid in MkDocs).

@fralau
Copy link
Owner

fralau commented Sep 19, 2023

Perhaps I am missing something. What is the hierarchy of your mkdocs project?

@frank-lenormand
Copy link
Author

Here’s the general hierarchy:

docs
├── subdir
│   └── diagrams.md
├── index.md
└── js
    └── mermaid.min.js

The page generated for diagrams.md tries to load js/mermaid.min.js, which doesn’t exist.

@frank-lenormand
Copy link
Author

frank-lenormand commented Sep 20, 2023

Here is a change I made locally that allows the diagrams to render:

diff --git a/mermaid2/plugin.py b/mermaid2/plugin.py
index 8bbfe31..27a9673 100755
--- a/mermaid2/plugin.py
+++ b/mermaid2/plugin.py
@@ -276,7 +276,10 @@ class MarkdownMermaidPlugin(BasePlugin):
                 else:
                     # <script src="...">
                     # generally for self.mermaid_major_version < 10:
-                    new_tag['src'] = self.javascript
+                    if self.javascript.startswith("http"):
+                        new_tag['src'] = self.javascript
+                    else:
+                        new_tag['src'] = os.path.relpath(self.javascript, os.path.dirname(page.url))
                     # it's necessary to close and reopen the tag:
                     soup.body.append(new_tag)
                     new_tag = soup.new_tag("script")

@fralau
Copy link
Owner

fralau commented Sep 20, 2023

🤔 When doing the build, the idea is that the javascript file must be copied in the subdirectory of the site directory, together with all other (html) files.

There is a test site under test/local_lib. I retested it on my site and it worked... for the first page only. Indeed that would not work for subbpages.

We do have a problem; I would have to see how MkDocs solved it with extra_javascript.

What result does it give you for new_tag['src'] = os.path.relpath(self.javascript, os.path.dirname(page.url)) ?

@frank-lenormand
Copy link
Author

I tested with test/local_lib and got the same results.

My patch generates a relative link that fetches the root directory of the site.

In my case, it generates script tags that look like so:

<script src="../js/mermaid.min.js"></script>

It should work for arbitrarily nested subdirectories (i.e. add as many ../ as necessary).

Ideally it should probably use an absolute path, but I haven’t found how to do that rapidly enough for a PoC:

<script src="/mysite/js/mermaid.min.js"></script>

@frank-lenormand
Copy link
Author

Any progress on this?

Maybe my workaround could be shipped in a new version, until a more robust solution is developed?

@fralau fralau added bug Something isn't working planned labels Sep 25, 2023
fralau pushed a commit that referenced this issue Sep 26, 2023
  - Fixed error of incorrect call to javascript library
    (the relative path was not being adapted)
@fralau fralau added fixed The issue is fixed and removed planned labels Sep 26, 2023
@fralau
Copy link
Owner

fralau commented Sep 26, 2023

Fixed in 1.1.1 (github); I took inspiration from the solution you proposed.

Let me know if it works for you?

@frank-lenormand
Copy link
Author

Looks good!

I also tried with a doubly-nested document, the script tag points at the correct file.

Feel free to close this issue once v1.1.1 has officially tagged/released!

@fralau
Copy link
Owner

fralau commented Sep 26, 2023

Released on Pypi, as v1.1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed The issue is fixed
Projects
None yet
Development

No branches or pull requests

2 participants