Fix line break support in Mermaid graph definition (#932)

This commit is contained in:
Jonatan Kłosko 2022-01-25 16:19:33 +01:00 committed by GitHub
parent 13b9fe0f94
commit 2acefde9f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions

View file

@ -17,6 +17,7 @@ import { removePosition } from "unist-util-remove-position";
import { highlight } from "./live_editor/monaco";
import { renderMermaid } from "./markdown/mermaid";
import { escapeHtml } from "../lib/utils";
/**
* Renders markdown content in the given container.
@ -163,7 +164,7 @@ function remarkPrepareMermaid(options) {
visit(ast, "code", (node, index, parent) => {
if (node.lang === "mermaid") {
node.type = "html";
node.value = `<div class="mermaid">${node.value}</div>`;
node.value = `<div class="mermaid">${escapeHtml(node.value)}</div>`;
}
});
};

View file

@ -34,13 +34,13 @@ const maybeInjectFontAwesome = (value) => {
export function renderMermaid(value) {
return importMermaid().then((mermaid) => {
try {
// Inject font-awesome when fa: prefix is used
maybeInjectFontAwesome(value);
// Inject font-awesome when fa: prefix is used
maybeInjectFontAwesome(value);
try {
return mermaid.render(getId(), value);
} catch (e) {
return `<pre><code>${e.message}</code></pre>`;
return `<div class="error-box whitespace-pre-wrap"><span class="font-semibold">Mermaid</span>\n${e.message}</div>`;
}
});
}

View file

@ -144,3 +144,18 @@ export function cancelEvent(event) {
// Stop event propagation (e.g. so it doesn't reach the editor).
event.stopPropagation();
}
const htmlEscapes = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39",
};
/**
* Transforms the given string to a HTML-safe value.
*/
export function escapeHtml(string) {
return (string || "").replace(/[&<>"']/g, (char) => htmlEscapes[char]);
}