Skip to content

Commit

Permalink
#35: porting to mermaid v10.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
frehberg committed Mar 11, 2023
1 parent 6d6f49a commit 9dc4a62
Showing 1 changed file with 81 additions and 4 deletions.
85 changes: 81 additions & 4 deletions src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,91 @@ const MERMAID_INIT_SCRIPT: &str = r#"
elem.innerHTML =
`<div> <mark>
&#9888; Cannot render diagram! Failed to import module from local file and remote location also!
Either access rustdocs vie HTTP/S or enable local file access in browsers Safari/Firefox, or
start Chrome with flag '--allow-file-access-from-files'.
Otherwise access the rustdoc pages using a
<a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/set_up_a_local_testing_server"> local web server</a>.
Either access the rustdocs via HTTP/S using a
<a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/set_up_a_local_testing_server">
local web server
</a>, for example:
<br>
python3 -m http.server --directory target/doc/, or enable local file access in your
Safari/Firefox/Chrome browser, for example
starting Chrome with flag '--allow-file-access-from-files'.
</mark></div> `;
}
}
// If rustdoc is read from file directly, the import of mermaid module
// from file will fail. In this case falling back to remote location.
// If neither succeeds, the mermaid markdown is replaced by notice to
// enable file acecss in browser.
try {
var rootPath = document
.getElementById(rustdocVarsId)
.attributes[dataRootPathAttr]
.value;
const {
default: mermaid,
} = await import(rootPath + mermaidModuleFile);
initializeMermaid(mermaid);
} catch (e) {
try {
const {
default: mermaid,
} = await import(fallbackRemoteUrl);
initializeMermaid(mermaid);
} catch (e) {
failedToLoadWarnings();
}
}
</script>
<div class="mermaid">
graph LR
s([Source]) --> a[[aquamarine]]
r[[rustdoc]] --> f([Docs w/ Mermaid!])
subgraph rustc[Rust Compiler]
a -. inject mermaid.js .-> r
end
</div>
<p>The diagram is going to be located in place of the code snippet</p>
<script type="module">
const mermaidModuleFile = "static.files.mermaid/mermaid.esm.min.mjsx";
const fallbackRemoteUrl = "https://unpkg.com/[email protected]/dist/mermaid.esm.min.mjsx";
const rustdocVarsId= "rustdoc-vars";
const dataRootPathAttr = "data-root-path";
function initializeMermaid(mermaid) {
var amrn_mermaid_theme =
window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'default';
mermaid.initialize({
'startOnLoad':'true',
'theme': amrn_mermaid_theme,
'logLevel': 3 });
mermaid.run();
}
function failedToLoadWarnings() {
for(var elem of document.getElementsByClassName("mermaid")) {
elem.innerHTML =
`<div> <mark>
&#9888; Cannot render diagram! Failed to import module from local file and remote location also!
Either access the rustdocs via HTTP/S using a
<a href="https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/set_up_a_local_testing_server">
local web server
</a>, for example:
<br>
python3 -m http.server --directory target/doc/, or enable local file access in your
Safari/Firefox/Chrome browser, for example
starting Chrome with flag '--allow-file-access-from-files'.
</mark></div> `;
}
}
// If rustdoc is read from file directly, the import of mermaid module
// from file will fail. In this case falling back to remote location.
// If neither succeeds, the mermaid markdown is replaced by notice to
Expand Down

0 comments on commit 9dc4a62

Please sign in to comment.