Created
October 30, 2019 18:53
-
-
Save m0n01d/5c0577c5a7c334af5a82d68e5a14bc87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
viewSnippet language title snippet = | |
div [ class "relative my-8" ] | |
[ p [] [ text title ] | |
, Html.node "vertol-snippet" | |
[ Attributes.attribute "snippet" snippet ] | |
[ pre | |
[] | |
[ code | |
[ class <| "language-" ++ language | |
] | |
[] | |
] | |
] | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Prism from 'prismjs'; | |
import 'prismjs/plugins/normalize-whitespace/prism-normalize-whitespace.js'; | |
Prism.plugins.NormalizeWhitespace.setDefaults({ | |
'remove-trailing': true, | |
'remove-indent': true, | |
'left-trim': true, | |
'right-trim': true, | |
'break-lines': 80, | |
}); | |
export default customElements.define( | |
'vertol-snippet', | |
class extends HTMLElement { | |
static get observedAttributes() { | |
return ['snippet']; | |
} | |
constructor() { | |
super(); | |
this._render = this._render.bind(this); | |
} | |
connectedCallback() { | |
this.$el = this.querySelector('[class*="language"]'); | |
const txt = this.attributes.snippet.textContent; | |
this._render(txt); | |
} | |
attributeChangedCallback(name, oldval, newval) { | |
if (name == 'snippet') { | |
this.$el && this._render(newval); | |
} | |
} | |
_render(txt) { | |
this.$el.innerText = txt.replace(/\n/gi, '\n'); | |
Prism.highlightElement(this.$el); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment