Skip to content

Instantly share code, notes, and snippets.

@atlefren
Last active December 31, 2015 05:59
Show Gist options
  • Select an option

  • Save atlefren/7944892 to your computer and use it in GitHub Desktop.

Select an option

Save atlefren/7944892 to your computer and use it in GitHub Desktop.
<html>
<head>
<title></title>
<style>
div.col50 {
width: 50%;
float: left;
}
textarea {
width: 95%;
height: 95%;
}
</style>
</head>
<body>
<div class="col50">
<textarea id="input"></textarea>
</div>
<div id="preview" class="col50"></div>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="markdown.min.js"></script> <!-- https://github.com/evilstreak/markdown-js/releases -->
<script type="text/javascript">
(function () {
"use strict";
var tabLength = 4;
function previewMarkdown() {
var markdown_text = $("#input").val();
$("#preview").html(markdown.toHTML(markdown_text));
}
previewMarkdown();
$("#input").keyup(previewMarkdown);
$("textarea").keydown(function(e) {
if(e.keyCode === 9) { // tab was pressed
// get caret position/selection
var start = this.selectionStart;
var end = this.selectionEnd;
var $this = $(this);
var value = $this.val();
// set textarea value to: text before caret + tab + text after caret
$this.val(value.substring(0, start) + Array(tabLength + 1).join(" ") + value.substring(end));
// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + tabLength;
// prevent the focus lose
e.preventDefault();
}
});
}());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment