Skip to content

Instantly share code, notes, and snippets.

@dfreedm
Forked from robdodson/mark-down-live.html
Last active December 24, 2015 14:29
Show Gist options
  • Save dfreedm/6813470 to your computer and use it in GitHub Desktop.
Save dfreedm/6813470 to your computer and use it in GitHub Desktop.
<polymer-element name="mark-down-editor">
<template>
<style>
#markup, #markdown {
width: 50%;
float: left;
}
#editor {
width: 80%;
height: 250px;
}
</style>
<div id="markup">
<textarea id="editor" on-keyup="render" value="{{ text }}"></textarea>
</div>
<div id="markdown"></div>
</template>
<script>
(function() {
var mo = new MutationObserver(function(mutations) {
mutations.forEach(function(mut) {
// the mutation target will be the <mark-down> element
mut.target.render();
});
});
function observe(element) {
mo.observe(element, {childList: true});
}
Polymer("mark-down-editor", {
created: function() {
observe(this);
this.text = this.trim(this.innerHTML);
this.render();
},
render: function() {
var parsed = markdown.toHTML(this.text);
this.$.markdown.innerHTML = parsed;
},
// Thanks Ryan Seddon! (https://github.com/ryanseddon/markdown-component)
// Remove all leading/trailing whitespace so it's not confused as
// a code block
trim: function(str) {
var cleaned = str.split('\n').map(function(x) {
return x.trim();
}).join('\n');
return cleaned;
}
});
})();
</script>
</polymer-element>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment