Files: d2da1e4a240525d0319f9bedceffc570047b41bc / index.js
1049 bytesRaw
1 | var toMarkdown = require('to-markdown') |
2 | |
3 | // Get just the inner HTML, not the outer HTML |
4 | toMarkdown.outer = function (node, content) { |
5 | return content |
6 | } |
7 | |
8 | function debounce(fn, wait) { |
9 | var timeout |
10 | return function() { |
11 | clearTimeout(timeout) |
12 | timeout = setTimeout(fn, wait) |
13 | } |
14 | } |
15 | |
16 | var value |
17 | var inputEl = document.getElementById('input') |
18 | var outputEl = document.getElementById('output') |
19 | var outputText = outputEl.appendChild(document.createTextNode('')) |
20 | var opt = { |
21 | gfm: true, |
22 | converters: [{ |
23 | filter: 'li', |
24 | replacement: function (content, node) { |
25 | content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ') |
26 | var parent = node.parentNode |
27 | var index = Array.prototype.indexOf.call(parent.children, node) + 1 |
28 | |
29 | var prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '- ' |
30 | return prefix + content |
31 | } |
32 | }] |
33 | } |
34 | |
35 | inputEl.onkeydown = inputEl.onkeyup = debounce(function() { |
36 | var newVal = inputEl.innerHTML |
37 | if (value == newVal) return |
38 | value = newVal |
39 | outputText.nodeValue = toMarkdown(value, opt) |
40 | }) |
41 |
Built with git-ssb-web