livebook/assets/js/highlight/index.js
Jonatan Kłosko 2ff327e742
Implement cells bin (#414)
* Implement cells bin

* Temporarily keep cells source

* Send complete bin entries from session to clients when a cell gets removed

* Polish styles

* Hydrate bin entries on section deletion as well
2021-06-30 17:48:27 +02:00

40 lines
777 B
JavaScript

import { getAttributeOrThrow } from "../lib/attribute";
import { highlight } from "../cell/live_editor/monaco";
/**
* A hook used to highlight source code in the root element.
*
* Configuration:
*
* * `data-language` - language of the source code
*/
const Highlight = {
mounted() {
this.props = getProps(this);
highlightIn(this.el, this.props.language);
},
updated() {
this.props = getProps(this);
highlightIn(this.el, this.props.language);
},
};
function getProps(hook) {
return {
language: getAttributeOrThrow(hook.el, "data-language"),
};
}
function highlightIn(element, language) {
const code = element.innerText;
highlight(code, language).then((html) => {
element.innerHTML = html;
});
}
export default Highlight;