Adds font-awesome when is required (mermaid). (#913)

* Adds font-awesome when is required (mermaid).

* applied suggestions

* format with prettier vscode plugin
This commit is contained in:
Roberto Estrada 2022-01-22 12:36:00 -06:00 committed by GitHub
parent b42d28f7a8
commit cdb17e8573
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,8 @@ let getId = () => `mermaid-graph-${idCount++}`;
let mermaidInitialized = false;
const fontAwesomeVersion = "5.15.4";
function importMermaid() {
return import(
/* webpackChunkName: "mermaid" */
@ -16,9 +18,26 @@ function importMermaid() {
});
}
const maybeInjectFontAwesome = (value) => {
const fontAwesomeUrl = `https://cdnjs.cloudflare.com/ajax/libs/font-awesome/${fontAwesomeVersion}/css/all.min.css`;
if (
value.includes("fa:") &&
!document.querySelector(`link[href="${fontAwesomeUrl}"]`)
) {
const link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = fontAwesomeUrl;
document.head.appendChild(link);
}
};
export function renderMermaid(value) {
return importMermaid().then((mermaid) => {
try {
// Inject font-awesome when fa: prefix is used
maybeInjectFontAwesome(value);
return mermaid.render(getId(), value);
} catch (e) {
return `<pre><code>${e.message}</code></pre>`;