var toMarkdown = require('to-markdown') function debounce(fn, wait) { var timeout return function() { clearTimeout(timeout) timeout = setTimeout(fn, wait) } } var value var inputEl = document.getElementById('input') var outputEl = document.getElementById('output') var outputText = outputEl.appendChild(document.createTextNode('')) var opt = { gfm: true, converters: [{ filter: ['span', 'div', 'small', 'font'], replacement: function (content) { return content } }, { filter: 'li', replacement: function (content, node) { content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ') var parent = node.parentNode var index = Array.prototype.indexOf.call(parent.children, node) + 1 var prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '- ' return prefix + content } }] } inputEl.onkeydown = inputEl.onkeyup = debounce(function() { var newVal = inputEl.innerHTML if (value == newVal) return value = newVal outputText.nodeValue = toMarkdown(value, opt) })