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

Embed mermaid JS Code as base64 data-stream #43

Open
frehberg opened this issue Jun 27, 2023 · 2 comments
Open

Embed mermaid JS Code as base64 data-stream #43

frehberg opened this issue Jun 27, 2023 · 2 comments

Comments

@frehberg
Copy link
Collaborator

Imrprovement:

Currently the Mermaid JS package is downloaded using URLs in Script-tags. These may point to local files or remote web-locations.
Modern browsers prevent loading scripts form local files (CORS), so file based Rust-Docs are not able to render the mermaid diagrams.

Downloading mermaid from remote web-soources may be time consuming, rendering of diagrams may be postponed by multiple seconds.

Instead of refering to external locations the mermaid JS might be embedded as base64 string the following way

<script type="text/javascript" src="data:text/javascript;base64,BASE64_STRING"></script>

The mermaid package v10.2.3 is shipped as single file of byte size 2.9MB. The estimated base64 will increase siize by 30%, around 3.8MB

The aquamarine macro might embed the mermaid JS package.

As the Rust-Docs process is creating an single Html file per Rust "mod", each of these file would require the mermaid code to be embedded.

Sadly, the Rust macro is not "told" the source file and line or mod-depth of the current Span/TokenStream. Therefor it is not possible to tell if a macro has been invoked within this Rust "mod" before. So each macro would perform the same

It might be an idea to define two macros, first one to embed the package into a Rust Doc Html file (per Rust "mod"), second macro might just rendert the diagram.

For example:

#[cfg_attr(doc, aquamarine::aquamarine, embed)]

PROS:

  • Faster loading, eliminate additional download of mermaid package
  • No CORS, so browsers would render mermaid diagrams also when using file based Rust-Docs

CONS:

  • Increasing each Html Rust Doc file by 3MB per diagram
  • Increasing total Rust-Doc byte size by N x 3MB (where N is the number of diagrams within a crate)
@mersinvald
Copy link
Owner

mersinvald commented Jul 6, 2023

Cons seem to outweight the pros for me, especially considering that the browser caching already solves the re-fetching problem well.

That needs to be tested, but I suspect that even with once-per-mod embedding, second and the following reloads of the page will be slower compared to the browser-cached mermaid.js.

@Qix-
Copy link

Qix- commented Sep 12, 2024

Modern browsers prevent loading scripts form local files (CORS), so file based Rust-Docs are not able to render the mermaid diagrams.

This isn't right. Only from file:// URIs when the existing page's scheme is not itself file://. Otherwise local documentation / asset loading wouldn't work. Relative paths with <script> still work just the same, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants