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

Fix line break support in Mermaid graph definition #932

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/js/cell/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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>`;
}
});
};
Expand Down
8 changes: 4 additions & 4 deletions assets/js/cell/markdown/mermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>`;
}
});
}
15 changes: 15 additions & 0 deletions assets/js/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}