mirror of
https://github.com/livebook-dev/livebook.git
synced 2024-11-18 05:44:12 +08:00
9fed524ed5
* Extend LiveMarkdown format to support Elixir snippets in Markdown cells * Highlight Markdown code blocks using Monaco editor API * Use livebook metadata for forcing markdown as well
67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
import marked from "marked";
|
|
import morphdom from "morphdom";
|
|
import DOMPurify from "dompurify";
|
|
import monaco from "./live_editor/monaco";
|
|
|
|
// Reuse Monaco highlighter for Markdown code blocks
|
|
marked.setOptions({
|
|
highlight: (code, lang, callback) => {
|
|
monaco.editor
|
|
.colorize(code, lang)
|
|
.then((result) => {
|
|
// `colorize` always adds additional newline, so we remove it
|
|
result = result.replace(/<br\/>$/, "");
|
|
callback(null, result);
|
|
})
|
|
.catch((error) => {
|
|
callback(error, null);
|
|
});
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Renders markdown content in the given container.
|
|
*/
|
|
class Markdown {
|
|
constructor(container, content) {
|
|
this.container = container;
|
|
this.content = content;
|
|
|
|
this.__render();
|
|
}
|
|
|
|
setContent(content) {
|
|
this.content = content;
|
|
this.__render();
|
|
}
|
|
|
|
__render() {
|
|
this.__getHtml().then((html) => {
|
|
// Wrap the HTML in another element, so that we
|
|
// can use morphdom's childrenOnly option.
|
|
const wrappedHtml = `<div>${html}</div>`;
|
|
|
|
morphdom(this.container, wrappedHtml, { childrenOnly: true });
|
|
});
|
|
}
|
|
|
|
__getHtml() {
|
|
return new Promise((resolve, reject) => {
|
|
marked(this.content, (error, html) => {
|
|
const sanitizedHtml = DOMPurify.sanitize(html);
|
|
|
|
if (sanitizedHtml) {
|
|
resolve(sanitizedHtml);
|
|
} else {
|
|
resolve(`
|
|
<div class="text-gray-300">
|
|
Empty markdown cell
|
|
</div>
|
|
`);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
export default Markdown;
|