livebook/assets/js/cell/markdown.js
Jonatan Kłosko 77675ad61e
UI polishing (#43)
* Tiny UI improvements

* Add icon for entering insert mode on a markdown cell

* Highlight selected section

* Improve contenteditable elements

* Highlight notebook/section name while editing
2021-02-18 15:11:24 +01:00

46 lines
961 B
JavaScript

import marked from "marked";
import morphdom from "morphdom";
import DOMPurify from "dompurify";
/**
* 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() {
const html = this.__getHtml();
// 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() {
const html = marked(this.content);
const sanitizedHtml = DOMPurify.sanitize(html);
if (sanitizedHtml) {
return sanitizedHtml;
} else {
return `
<div class="text-gray-300">
Empty markdown cell
</div>
`;
}
}
}
export default Markdown;