mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-01-01 12:41:43 +08:00
Fix line break support in Mermaid graph definition (#932)
This commit is contained in:
parent
13b9fe0f94
commit
2acefde9f7
3 changed files with 21 additions and 5 deletions
|
@ -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>`;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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>`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -144,3 +144,18 @@ export function cancelEvent(event) {
|
|||
// Stop event propagation (e.g. so it doesn't reach the editor).
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
const htmlEscapes = {
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
"'": "'",
|
||||
};
|
||||
|
||||
/**
|
||||
* Transforms the given string to a HTML-safe value.
|
||||
*/
|
||||
export function escapeHtml(string) {
|
||||
return (string || "").replace(/[&<>"']/g, (char) => htmlEscapes[char]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue